Compare commits

..
7 Commits
7 changed files with 439 additions and 17 deletions
+70 -2
View File
@@ -12,7 +12,36 @@
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). // 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) TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
if(root == NULL)
{
TreeNode *newNode = malloc(sizeof(TreeNode));
newNode->data = malloc(dataSize);
memcpy(newNode->data, data, dataSize);
newNode->left = NULL;
newNode->right = NULL;
if(isDuplicate != NULL)
*isDuplicate = 0;
return newNode;
}
else
{
int cmpResult = compareFct(data, root->data);
if(cmpResult < 0)
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
else if(cmpResult > 0)
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
else
{
// Duplicate found
if(isDuplicate != NULL)
*isDuplicate = 1;
}
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. // 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 +49,56 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
static StackNode *stack = NULL;
if (root != NULL)
{
clearStack(stack);
stack = NULL;
TreeNode *current = root;
while (current != NULL)
{
stack = push(stack, current);
current = current->left;
}
}
if (stack == NULL)
return NULL;
TreeNode *node = (TreeNode *)top(stack);
stack = pop(stack);
void *data = node->data;
TreeNode *current = node->right;
while (current != NULL)
{
stack = push(stack, current);
current = current->left;
}
return data;
} }
// Releases all memory resources (including data copies). // Releases all memory resources (including data copies).
void clearTree(TreeNode *root) 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. // Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root) unsigned int treeSize(const TreeNode *root)
{ {
if(root == NULL)
return 0;
return 1 + treeSize(root->left) + treeSize(root->right);
} }
+122
View File
@@ -0,0 +1,122 @@
#include <stdio.h>
#include <stdlib.h>
#include "unity.h"
#include "bintree.h"
/* ---------- Helper ---------- */
int compareInt(const void *a, const void *b)
{
int ia = *(const int *)a;
int ib = *(const int *)b;
return ia - ib;
}
void setUp(void){}
void tearDown(void){}
/* ---------- addToTree ---------- */
void test_addToTree_adds_first_element(void)
{
TreeNode *root = NULL;
int value = 5;
int isDup = -1;
root = addToTree(root, &value, sizeof(int), compareInt, &isDup);
TEST_ASSERT_NOT_NULL(root);
TEST_ASSERT_EQUAL_INT(0, isDup);
TEST_ASSERT_EQUAL_INT(1, treeSize(root));
clearTree(root);
}
void test_addToTree_detects_duplicate(void)
{
TreeNode *root = NULL;
int value = 5;
int isDup = -1;
root = addToTree(root, &value, sizeof(int), compareInt, &isDup);
root = addToTree(root, &value, sizeof(int), compareInt, &isDup);
TEST_ASSERT_EQUAL_INT(1, isDup);
TEST_ASSERT_EQUAL_INT(1, treeSize(root));
clearTree(root);
}
/* ---------- treeSize ---------- */
void test_treeSize_counts_multiple_elements(void)
{
TreeNode *root = NULL;
int values[] = {5, 2, 8, 1, 3};
for (int i = 0; i < 5; i++)
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL);
TEST_ASSERT_EQUAL_UINT(5, treeSize(root));
clearTree(root);
}
/* ---------- nextTreeData ---------- */
void test_nextTreeData_returns_sorted_order(void)
{
TreeNode *root = NULL;
int values[] = {5, 2, 8, 1, 3};
for (int i = 0; i < 5; i++)
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL);
int expected[] = {1, 2, 3, 5, 8};
int *val;
/* erster Aufruf initialisiert den Iterator */
val = (int *)nextTreeData(root);
TEST_ASSERT_NOT_NULL(val);
TEST_ASSERT_EQUAL_INT(expected[0], *val);
/* weitere Aufrufe liefern die nächsten Elemente */
for (int i = 1; i < 5; i++)
{
val = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(val);
TEST_ASSERT_EQUAL_INT(expected[i], *val);
}
/* danach muss NULL kommen */
TEST_ASSERT_NULL(nextTreeData(NULL));
clearTree(root);
}
/* ---------- clearTree ---------- */
void test_clearTree_on_empty_tree_does_not_crash(void)
{
clearTree(NULL);
TEST_PASS();
}
/* ---------- MAIN ---------- */
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_addToTree_adds_first_element);
RUN_TEST(test_addToTree_detects_duplicate);
RUN_TEST(test_treeSize_counts_multiple_elements);
RUN_TEST(test_nextTreeData_returns_sorted_order);
RUN_TEST(test_clearTree_on_empty_tree_does_not_crash);
return UNITY_END();
}
+21 -10
View File
@@ -29,26 +29,37 @@ program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
doble : main.o $(program_obj_files) doble : main.o $(program_obj_files)
$(CC) $(FLAGS) $^ -o doble $(CC) $(FLAGS) $^ -o doble
$(program_obj_filesobj_files): %.o: %.c # --------------------------
$(CC) -c $(FLAGS) $^ -o $@ # Objektdateien kompilieren
# --------------------------
numbers.o: numbers.c %.o: %.c
$(CC) -c $(CFLAGS) numbers.c $(CC) -c $(FLAGS) $< -o $@
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
numbersTests: numbers.o numbersTests.c $(unityfolder)/unity.c numbersTests: numbers.o numbersTests.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c numbers.o $(unityfolder)/unity.c $(CC) $(FLAGS) -I$(unityfolder) \
-o runNumbersTests \
numbersTests.c numbers.o $(unityfolder)/unity.c
stackTests: stack.o stackTests.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) \
-o runStackTests \
stackTests.c stack.o $(unityfolder)/unity.c
bintreeTests: bintree.o stack.o bintreeTests.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) \
-o runBintreeTests \
bintreeTests.c bintree.o stack.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------
clean: clean:
ifeq ($(OS),Windows_NT) ifeq ($(OS),!Windows_NT)
del /f *.o doble *.exe del /f *.o doble *.exe
else else
rm -f *.o doble *runNumbersTests rm -f *.o doble run*Tests*
endif endif
+72
View File
@@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "numbers.h" #include "numbers.h"
#include "unity.h" #include "unity.h"
#include <stdbool.h>
//Initialisierung //Initialisierung
void setUp(void){} void setUp(void){}
@@ -92,6 +93,73 @@ void test_getDuplicated_finds_correct_duplicate(void)
free(arr); free(arr);
} }
// ---------------------------------------------------------------------------
// Hilfsfunktion: prüft ob createNumbers einen Fehlerzustand signalisiert
// Du kannst diese später anpassen (z.B. wenn Fehler-Flags eingebaut werden)
// ---------------------------------------------------------------------------
static bool did_createNumbers_fail(unsigned int* ptr)
{
return (ptr == NULL); // aktuell: NULL bedeutet Fehler
}
// ---------------------------------------------------------------------------
// Test: createNumbers soll bei count < 2 fehlschlagen
// ---------------------------------------------------------------------------
void test_createNumbers_rejects_too_small_counts(void)
{
unsigned int* r1 = createNumbers(0);
unsigned int* r2 = createNumbers(1);
TEST_ASSERT_TRUE(did_createNumbers_fail(r1));
TEST_ASSERT_TRUE(did_createNumbers_fail(r2));
}
// ---------------------------------------------------------------------------
// Test: createNumbers(2) soll funktionieren (Minimum gültiger Input)
// ---------------------------------------------------------------------------
void test_createNumbers_with_two_elements_is_valid(void)
{
unsigned int* arr = createNumbers(2);
TEST_ASSERT_FALSE(did_createNumbers_fail(arr));
TEST_ASSERT_NOT_NULL(arr);
// should contain exactly one duplicate
unsigned int c0 = countOccurrences(arr, 2, arr[0]);
unsigned int c1 = countOccurrences(arr, 2, arr[1]);
TEST_ASSERT_TRUE((c0 == 2) || (c1 == 2));
free(arr);
}
// ---------------------------------------------------------------------------
// Test: getDuplicate() soll Fehler erkennen, wenn arr == NULL
// ---------------------------------------------------------------------------
void test_getDuplicate_handles_null_pointer(void)
{
unsigned int result = getDuplicate(NULL, 10);
// Erwartung: Fehlerzustand → aktuell vermutlich 0 oder UINT_MAX
// Wir prüfen nur, DASS es ein Fehler ist:
TEST_ASSERT_TRUE(result == 0 || result == UINT_MAX);
}
// ---------------------------------------------------------------------------
// Test: getDuplicated) soll Fehler erkennen, wenn count < 2
// ---------------------------------------------------------------------------
void test_getDuplicate_handles_invalid_count(void)
{
unsigned int dummy[1] = { 5 };
unsigned int result = getDuplicate(dummy, 1);
TEST_ASSERT_TRUE(result == 0 || result == UINT_MAX);
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// main() für Unity // main() für Unity
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -102,6 +170,10 @@ int main(void)
RUN_TEST(test_createNumbers_returns_valid_array); RUN_TEST(test_createNumbers_returns_valid_array);
RUN_TEST(test_createNumbers_contains_exactly_one_duplicate); RUN_TEST(test_createNumbers_contains_exactly_one_duplicate);
RUN_TEST(test_getDuplicated_finds_correct_duplicate); RUN_TEST(test_getDuplicated_finds_correct_duplicate);
RUN_TEST(test_createNumbers_rejects_too_small_counts);
RUN_TEST(test_createNumbers_with_two_elements_is_valid);
RUN_TEST(test_getDuplicate_handles_null_pointer);
RUN_TEST(test_getDuplicate_handles_invalid_count);
return UNITY_END(); return UNITY_END();
} }
+19 -5
View File
@@ -10,24 +10,38 @@
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data) StackNode *push(StackNode *stack, void *data)
{ {
StackNode *top = malloc(sizeof(StackNode));
top->next= stack;
top->data= data;
return top;
} }
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) StackNode *pop(StackNode *stack)
{ {
if(stack == NULL)
return NULL;
StackNode *next = stack->next;
free(stack);
return next;
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) void *top(StackNode *stack)
{ {
if(stack == NULL)
return NULL;
return stack->data;
} }
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
while(stack != NULL)
} {
StackNode *next = stack->next;
free(stack);
stack = next;
}
}
+5
View File
@@ -8,6 +8,11 @@ The latest element is taken from the stack. */
#include <stdlib.h> #include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen //TODO: passenden Datentyp als struct anlegen
typedef struct StackNode
{
void *data;
struct StackNode *next;
}StackNode;
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data); StackNode *push(StackNode *stack, void *data);
+130
View File
@@ -0,0 +1,130 @@
/*#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include "unity.h"
//Initialisierung
void setUp(void){}
void tearDown(void){}
void test_push_created_new_stacknode(void)
{
int testdata1 = 111;
StackNode test = {&testdata1,NULL};
int testdata2 = 222;
StackNode *test1 = push(&test,&testdata2);
TEST_ASSERT_NOT_NULL(test1);
TEST_ASSERT_EQUAL_PTR(&testdata2,test1->data);
TEST_ASSERT_EQUAL_PTR(&test,test1->next);
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_push_created_new_stacknode);
return UNITY_END();
}*/
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
#include "unity.h"
void setUp(void){}
void tearDown(void){}
/* ---------------- PUSH ---------------- */
void test_push_created_new_stacknode(void)
{
int testdata1 = 111;
StackNode test = {&testdata1,NULL};
int testdata2 = 222;
StackNode *test1 = push(&test,&testdata2);
TEST_ASSERT_NOT_NULL(test1);
TEST_ASSERT_EQUAL_PTR(&testdata2,test1->data);
TEST_ASSERT_EQUAL_PTR(&test,test1->next);
}
/* ---------------- TOP ---------------- */
void test_top_on_empty_stack_returns_null(void)
{
TEST_ASSERT_NULL(top(NULL));
}
void test_top_returns_top_element(void)
{
int data = 42;
StackNode node = {&data, NULL};
TEST_ASSERT_EQUAL_PTR(&data, top(&node));
}
/* ---------------- POP ---------------- */
void test_pop_on_empty_stack_returns_null(void)
{
TEST_ASSERT_NULL(pop(NULL));
}
void test_pop_removes_top_element(void)
{
int d1 = 1;
int d2 = 2;
StackNode *s = NULL;
s = push(s, &d1);
s = push(s, &d2);
StackNode *newTop = pop(s);
TEST_ASSERT_NOT_NULL(newTop);
TEST_ASSERT_EQUAL_PTR(&d1, newTop->data);
clearStack(newTop); // Speicher aufräumen
}
/* ---------------- CLEAR ---------------- */
void test_clearStack_on_empty_stack_does_not_crash(void)
{
clearStack(NULL);
TEST_PASS(); // Test besteht, wenn kein Crash
}
void test_clearStack_clears_multiple_elements(void)
{
int d1 = 1, d2 = 2, d3 = 3;
StackNode *s = NULL;
s = push(s, &d1);
s = push(s, &d2);
s = push(s, &d3);
clearStack(s);
TEST_PASS(); // kein Crash = bestanden
}
/* ---------------- MAIN ---------------- */
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_push_created_new_stacknode);
RUN_TEST(test_top_on_empty_stack_returns_null);
RUN_TEST(test_top_returns_top_element);
RUN_TEST(test_pop_on_empty_stack_returns_null);
RUN_TEST(test_pop_removes_top_element);
RUN_TEST(test_clearStack_on_empty_stack_does_not_crash);
RUN_TEST(test_clearStack_clears_multiple_elements);
return UNITY_END();
}