Compare commits
8 Commits
d6a439f85a
...
0d561c0175
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d561c0175 | |||
| 2950b10931 | |||
| adc3ebc8be | |||
| 85b5c181a0 | |||
| b0826ec057 | |||
| 12742b46fe | |||
| cd13bbea4e | |||
| 0c075ea18c |
@ -8,11 +8,51 @@
|
||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||
|
||||
static StackNode *iteratorStack = NULL;
|
||||
|
||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
if (root == NULL)
|
||||
{
|
||||
// Neue Node anlegen
|
||||
TreeNode *newNode = malloc(sizeof(TreeNode));
|
||||
if (!newNode) return NULL;
|
||||
|
||||
newNode->data = malloc(dataSize);
|
||||
if(!newNode->data)
|
||||
{
|
||||
free(newNode);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(newNode->data, data, dataSize);
|
||||
|
||||
newNode->left = NULL;
|
||||
newNode->right = NULL;
|
||||
|
||||
if (isDuplicate)
|
||||
*isDuplicate = 0;
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
int cmp = compareFct(data, root->data);
|
||||
|
||||
if (cmp == 0)
|
||||
{
|
||||
if (isDuplicate)
|
||||
*isDuplicate = 1;
|
||||
return root; // Einfügen verhindern
|
||||
}
|
||||
|
||||
if (cmp < 0)
|
||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||
else
|
||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
||||
@ -20,17 +60,58 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
||||
// push the top node and push all its left nodes.
|
||||
void *nextTreeData(TreeNode *root)
|
||||
{
|
||||
// Neues Traversieren?
|
||||
if (root != NULL)
|
||||
{
|
||||
clearStack(iteratorStack);
|
||||
iteratorStack = NULL;
|
||||
|
||||
// Root + alle linken Nachfolger pushen
|
||||
TreeNode *current = root;
|
||||
while (current != NULL)
|
||||
{
|
||||
iteratorStack = push(iteratorStack, current);
|
||||
current = current->left;
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn Stack leer → fertig
|
||||
if (iteratorStack == NULL)
|
||||
return NULL;
|
||||
|
||||
// Top-Node holen
|
||||
TreeNode *node = (TreeNode *)top(iteratorStack);
|
||||
iteratorStack = pop(iteratorStack);
|
||||
|
||||
// rechten Teilbaum + linke Kette pushen
|
||||
TreeNode *right = node->right;
|
||||
while (right != NULL)
|
||||
{
|
||||
iteratorStack = push(iteratorStack, right);
|
||||
right = right->left;
|
||||
}
|
||||
|
||||
return node->data;
|
||||
}
|
||||
|
||||
// Releases all memory resources (including data copies).
|
||||
void clearTree(TreeNode *root)
|
||||
{
|
||||
if (root != NULL)
|
||||
{
|
||||
clearTree(root->left);
|
||||
clearTree(root->right);
|
||||
|
||||
free(root->data);
|
||||
free(root);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the number of entries in the tree given by root.
|
||||
unsigned int treeSize(const TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return 0;
|
||||
|
||||
return 1 + treeSize(root->left) + treeSize(root->right);
|
||||
}
|
||||
119
I2_Dobble/bintreeTest.c
Normal file
119
I2_Dobble/bintreeTest.c
Normal file
@ -0,0 +1,119 @@
|
||||
#include "unity.h"
|
||||
#include "bintree.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static int compare(const void *a, const void *b)
|
||||
{
|
||||
return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b);
|
||||
}
|
||||
|
||||
void setUp(void) { }
|
||||
void tearDown(void) { }
|
||||
|
||||
void test_addToTree_single_element(void)
|
||||
{
|
||||
TreeNode *root = NULL;
|
||||
int value = 10;
|
||||
int dup = -1;
|
||||
|
||||
root = addToTree(root, &value, sizeof(int), compare, &dup);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(root);
|
||||
TEST_ASSERT_EQUAL_INT(10, *(int*)root->data);
|
||||
TEST_ASSERT_EQUAL_INT(0, dup); // neuer Eintrag
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
void test_addToTree_multiple_elements_and_size(void)
|
||||
{
|
||||
TreeNode *root = NULL;
|
||||
int values[] = {5, 3, 7, 1, 4};
|
||||
int dup = -1;
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
root = addToTree(root, &values[i], sizeof(int), compare, &dup);
|
||||
|
||||
TEST_ASSERT_EQUAL_UINT(5, treeSize(root));
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
void test_addToTree_duplicate_detection(void)
|
||||
{
|
||||
TreeNode *root = NULL;
|
||||
int val = 42;
|
||||
int dup;
|
||||
|
||||
root = addToTree(root, &val, sizeof(int), compare, &dup);
|
||||
TEST_ASSERT_EQUAL(0, dup);
|
||||
|
||||
addToTree(root, &val, sizeof(int), compare, &dup);
|
||||
TEST_ASSERT_EQUAL(1, dup);
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
void test_treeSize_empty_tree_detection(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_UINT(0, treeSize(NULL));
|
||||
}
|
||||
|
||||
void test_nextTreeData_returns_inorder(void)
|
||||
{
|
||||
TreeNode *root = NULL;
|
||||
int values[] = {5, 3, 7, 2, 4, 6, 8};
|
||||
|
||||
// Einfügen
|
||||
for (int i = 0; i < 7; i++)
|
||||
root = addToTree(root, &values[i], sizeof(int), compare, NULL);
|
||||
|
||||
/* Erwartete Reihenfolge (inorder): 2,3,4,5,6,7,8 */
|
||||
int expected[] = {2,3,4,5,6,7,8};
|
||||
|
||||
int idx = 0;
|
||||
void *p = nextTreeData(root); // Iterator starten
|
||||
|
||||
while (p != NULL)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_INT(expected[idx], *(int*)p);
|
||||
idx++;
|
||||
p = nextTreeData(NULL); // Fortsetzen mit NULL
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(7, idx); //alle Einträge geprüft
|
||||
|
||||
clearTree(root);
|
||||
}
|
||||
|
||||
void test_treeSize_returns_correct_size()
|
||||
{
|
||||
TreeNode *root = NULL;
|
||||
int values[] = {8, 3, 10, 1, 6, 14};
|
||||
|
||||
// ersten Baum aufbauen
|
||||
for (int i = 0; i < 6; i++)
|
||||
root = addToTree(root, &values[i], sizeof(int), compare, NULL);
|
||||
|
||||
TEST_ASSERT_EQUAL_UINT(6, treeSize(root));
|
||||
|
||||
// Baum löschen
|
||||
clearTree(root);
|
||||
root = NULL;
|
||||
|
||||
TEST_ASSERT_EQUAL_UINT(0, treeSize(root));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(test_addToTree_single_element);
|
||||
RUN_TEST(test_addToTree_multiple_elements_and_size);
|
||||
RUN_TEST(test_addToTree_duplicate_detection);
|
||||
RUN_TEST(test_treeSize_empty_tree_detection);
|
||||
RUN_TEST(test_nextTreeData_returns_inorder);
|
||||
RUN_TEST(test_treeSize_returns_correct_size);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
@ -28,29 +28,38 @@ doble_initial:
|
||||
program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
|
||||
|
||||
doble : main.o $(program_obj_files)
|
||||
$(CC) $(FLAGS) $(LDFLAGS) $^ -o doble
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o doble
|
||||
|
||||
$(program_obj_filesobj_files): %.o: %.c
|
||||
$(CC) -c $(FLAGS) $^ -o $@
|
||||
$(program_obj_files): %.o: %.c
|
||||
$(CC) -c $(CFLAGS) $^ -o $@
|
||||
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
unitTests: numbersTest
|
||||
unity_src = $(unityfolder)/unity.c
|
||||
|
||||
unitTests: numbersTest stackTest bintreeTest
|
||||
./runNumbersTest
|
||||
./runStackTest
|
||||
./runBintreeTest
|
||||
|
||||
numbersTest: numbers.o bintree.o numbersTest.o $($(unityfolder)/unity.c)
|
||||
$(CC) $(FLAGS) $(LDFLAGS) $^ -o $@
|
||||
numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src) stack.o
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest
|
||||
|
||||
stackTest: stack.o stackTest.c $(unity_src)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTest
|
||||
|
||||
bintreeTest: bintree.o bintreeTest.c $(unity_src) stack.o
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBintreeTest
|
||||
%.o: %.c
|
||||
$(CC) -c $(FLAGS) $< -o $@
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
clean:
|
||||
ifeq ($(OS),Windows_NT)
|
||||
del /f *.o doble runNumbersTest doble_initial
|
||||
del /f *.o doble doble_initial runNumbersTest runStackTest runBintreeTest
|
||||
else
|
||||
rm -f *.o doble runNumbersTest doble_initial
|
||||
rm -f *.o doble doble_initial runNumbersTest runStackTest runBintreeTest
|
||||
endif
|
||||
@ -1,3 +1,4 @@
|
||||
#include <stdlib.h>
|
||||
#include "numbers.h"
|
||||
#include "unity.h"
|
||||
#include "unity_internals.h"
|
||||
@ -75,6 +76,9 @@ void test_complete_function_of_numbers(void)
|
||||
free(arr);
|
||||
}
|
||||
|
||||
void setUp(void) { }
|
||||
void tearDown(void) { }
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
@ -10,24 +10,47 @@
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data)
|
||||
{
|
||||
StackNode *newNode = malloc(sizeof(StackNode));
|
||||
if (!newNode)
|
||||
return stack;
|
||||
|
||||
newNode->data = data;
|
||||
newNode->next = stack;
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||
// freed by caller.)
|
||||
StackNode *pop(StackNode *stack)
|
||||
{
|
||||
if(stack == NULL)
|
||||
return NULL;
|
||||
|
||||
StackNode *newTopElement = stack->next;
|
||||
free(stack);
|
||||
|
||||
return newTopElement;
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
if (!stack)
|
||||
return NULL;
|
||||
|
||||
return stack->data;
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack)
|
||||
{
|
||||
StackNode *current = stack;
|
||||
|
||||
while (current)
|
||||
{
|
||||
StackNode *next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
@ -8,6 +8,11 @@ The latest element is taken from the stack. */
|
||||
#include <stdlib.h>
|
||||
|
||||
//TODO: passenden Datentyp als struct anlegen
|
||||
typedef struct stack
|
||||
{
|
||||
void *data;
|
||||
struct stack *next;
|
||||
} StackNode;
|
||||
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
|
||||
106
I2_Dobble/stackTest.c
Normal file
106
I2_Dobble/stackTest.c
Normal file
@ -0,0 +1,106 @@
|
||||
#include "stack.h"
|
||||
#include "unity.h"
|
||||
#include "unity_internals.h"
|
||||
|
||||
void setUp(void) { }
|
||||
void tearDown(void) { }
|
||||
|
||||
void test_push_should_add_new_node_at_top(void)
|
||||
{
|
||||
StackNode *stack = NULL;
|
||||
|
||||
int value = 42;
|
||||
stack = push(stack, &value);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(stack);
|
||||
TEST_ASSERT_EQUAL_PTR(&value, stack->data);
|
||||
TEST_ASSERT_NULL(stack->next);
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
void test_push_multiple_should_chain_nodes_correctly(void)
|
||||
{
|
||||
int a = 1, b = 2;
|
||||
|
||||
StackNode *first = push(NULL, &a);
|
||||
|
||||
StackNode *second = push(first, &b);
|
||||
|
||||
TEST_ASSERT_EQUAL_PTR(&b, second->data);
|
||||
TEST_ASSERT_EQUAL_PTR(first, second->next);
|
||||
TEST_ASSERT_EQUAL_PTR(&a, first->data);
|
||||
|
||||
clearStack(second);
|
||||
}
|
||||
|
||||
void test_top_should_return_null_on_empty_stack(void)
|
||||
{
|
||||
StackNode *stack = NULL;
|
||||
TEST_ASSERT_NULL(top(stack));
|
||||
}
|
||||
|
||||
void test_top_should_return_data_of_existing_node(void)
|
||||
{
|
||||
StackNode node;
|
||||
int x = 99;
|
||||
|
||||
node.data = &x;
|
||||
node.next = NULL;
|
||||
|
||||
TEST_ASSERT_EQUAL_PTR(&x, top(&node));
|
||||
}
|
||||
|
||||
void test_pop_should_return_null_when_stack_empty(void)
|
||||
{
|
||||
StackNode *stack = NULL;
|
||||
TEST_ASSERT_NULL(pop(stack));
|
||||
}
|
||||
|
||||
void test_pop_should_remove_single_element(void)
|
||||
{
|
||||
StackNode *stack = malloc(sizeof(StackNode));
|
||||
int x = 7;
|
||||
|
||||
stack->data = &x;
|
||||
stack->next = NULL;
|
||||
|
||||
StackNode *result = pop(stack);
|
||||
|
||||
TEST_ASSERT_NULL(result); // Kein Element mehr übrig
|
||||
}
|
||||
|
||||
void test_pop_should_remove_top_node_only(void)
|
||||
{
|
||||
// Manuell verkettete Liste aufbauen
|
||||
StackNode *n1 = malloc(sizeof(StackNode));
|
||||
StackNode *n2 = malloc(sizeof(StackNode));
|
||||
int a = 1, b = 2;
|
||||
|
||||
n1->data = &a;
|
||||
n1->next = NULL;
|
||||
|
||||
n2->data = &b;
|
||||
n2->next = n1;
|
||||
|
||||
StackNode *result = pop(n2);
|
||||
|
||||
TEST_ASSERT_EQUAL_PTR(n1, result);
|
||||
|
||||
clearStack(result);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(test_push_should_add_new_node_at_top);
|
||||
RUN_TEST(test_push_multiple_should_chain_nodes_correctly);
|
||||
RUN_TEST(test_top_should_return_null_on_empty_stack);
|
||||
RUN_TEST(test_top_should_return_data_of_existing_node);
|
||||
RUN_TEST(test_pop_should_return_null_when_stack_empty);
|
||||
RUN_TEST(test_pop_should_remove_single_element);
|
||||
RUN_TEST(test_pop_should_remove_top_node_only);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user