#include "unity.h" #include #include #include #include "bintree.h" int compareUnsignedInt(const void *a, const void *b) { unsigned int x = *(unsigned int *)a; unsigned int y = *(unsigned int *)b; if (x < y) return -1; if (x > y) return 1; return 0; } TreeNode *root = NULL; void setUp(void) { root = NULL; // vor jedem Test leeren } void tearDown(void) { clearTree(root); } // Test, ob addToTree Knoten korrekt hinzufügt void test_addToTree_basic(void) { int isDup; unsigned int val = 10; root = addToTree(root, &val, sizeof(val), compareUnsignedInt, &isDup); TEST_ASSERT_NOT_NULL(root); TEST_ASSERT_EQUAL_UINT(10, *(unsigned int *)root->data); TEST_ASSERT_EQUAL_INT(0, isDup); TEST_ASSERT_EQUAL_UINT(1, treeSize(root)); } // Test, dass Duplikate erkannt werden void test_addToTree_duplicate(void) { int isDup; unsigned int val1 = 10, val2 = 10; root = addToTree(root, &val1, sizeof(val1), compareUnsignedInt, &isDup); TEST_ASSERT_EQUAL_INT(0, isDup); root = addToTree(root, &val2, sizeof(val2), compareUnsignedInt, &isDup); TEST_ASSERT_EQUAL_INT(1, isDup); TEST_ASSERT_EQUAL_UINT(1, treeSize(root)); // Duplikate nicht hinzufügen } // Test nextTreeData Traversierung void test_nextTreeData_in_order(void) { unsigned int values[] = {20, 10, 30}; int isDup; for (int i = 0; i < 3; i++) { root = addToTree(root, &values[i], sizeof(values[i]), compareUnsignedInt, &isDup); } unsigned int expected[] = {10, 20, 30}; int idx = 0; void *data; // **Neue Iteration starten** data = nextTreeData(root); while (data != NULL) { TEST_ASSERT_EQUAL_UINT(expected[idx], *(unsigned int *)data); idx++; data = nextTreeData(NULL); // weitere Elemente abrufen } TEST_ASSERT_EQUAL_INT(3, idx); // alle 3 Knoten besucht } // Test clearTree gibt Speicher frei void test_clearTree(void) { unsigned int val = 42; int isDup; root = addToTree(root, &val, sizeof(val), compareUnsignedInt, &isDup); clearTree(root); root = NULL; // clearTree löscht nicht die root-Variable selbst TEST_ASSERT_NULL(root); } // Test treeSize zählt korrekt void test_treeSize(void) { unsigned int vals[] = {10, 20, 5}; int isDup; for (int i = 0; i < 3; i++) { root = addToTree(root, &vals[i], sizeof(vals[i]), compareUnsignedInt, &isDup); } TEST_ASSERT_EQUAL_UINT(3, treeSize(root)); } int main(void) { UNITY_BEGIN(); printf( "\n------------------------binarytree test------------------------\n\n"); RUN_TEST(test_addToTree_basic); RUN_TEST(test_addToTree_duplicate); RUN_TEST(test_nextTreeData_in_order); RUN_TEST(test_clearTree); RUN_TEST(test_treeSize); return UNITY_END(); }