// // Created by Johannes on 15.12.2025. // #include #include #include #include "unity.h" #include "bintree.h" #include "stack.h" /* -------------------------------------------------- * Pflichtfunktionen für Unity * -------------------------------------------------- */ void setUp(void) {} void tearDown(void) {} /* -------------------------------------------------- * Mini-Testframework (RUNTEST) * -------------------------------------------------- */ static int tests_run = 0; static int tests_failed = 0; #define RUNTEST(test) do { \ tests_run++; \ printf("Running %-35s ... ", #test); \ if (test()) { \ printf("OK\n"); \ } else { \ printf("FAIL\n"); \ tests_failed++; \ } \ } while (0) #define ASSERT_TRUE(expr) do { \ if (!(expr)) return false; \ } while (0) /* -------------------------------------------------- * Hilfsfunktionen * -------------------------------------------------- */ static int compareInt(const void *a, const void *b) { int x = *(const int *)a; int y = *(const int *)b; return (x > y) - (x < y); } /* -------------------------------------------------- * Tests * -------------------------------------------------- */ // Test: Einfügen eines einzelnen Knotens bool test_addToTree_single(void) { TreeNode *tree = NULL; int value = 10; int isDup = -1; tree = addToTree(tree, &value, sizeof(int), compareInt, &isDup); ASSERT_TRUE(tree != NULL); ASSERT_TRUE(*(int *)tree->data == 10); ASSERT_TRUE(isDup == 0); clearTree(tree); return true; } // Test: Duplikate erkennen bool test_addToTree_duplicate(void) { TreeNode *tree = NULL; int value = 5; int isDup; tree = addToTree(tree, &value, sizeof(int), compareInt, &isDup); tree = addToTree(tree, &value, sizeof(int), compareInt, &isDup); ASSERT_TRUE(isDup == 1); ASSERT_TRUE(treeSize(tree) == 1); clearTree(tree); return true; } // Test: Baumgröße korrekt zählen bool test_treeSize(void) { TreeNode *tree = NULL; int values[] = {5, 3, 7, 2, 4}; for (int i = 0; i < 5; i++) tree = addToTree(tree, &values[i], sizeof(int), compareInt, NULL); ASSERT_TRUE(treeSize(tree) == 5); clearTree(tree); return true; } // Test: Inorder-Traversierung (nextTreeData) bool test_nextTreeData_inorder(void) { TreeNode *tree = NULL; int values[] = {5, 3, 7, 2, 4}; for (int i = 0; i < 5; i++) tree = addToTree(tree, &values[i], sizeof(int), compareInt, NULL); int expected[] = {2, 3, 4, 5, 7}; int *val = nextTreeData(tree); for (int i = 0; i < 5; i++) { ASSERT_TRUE(val != NULL); ASSERT_TRUE(*val == expected[i]); val = nextTreeData(NULL); } ASSERT_TRUE(val == NULL); clearTree(tree); return true; } // Test: clearTree räumt vollständig auf bool test_clearTree(void) { TreeNode *tree = NULL; int values[] = {1, 2, 3}; for (int i = 0; i < 3; i++) tree = addToTree(tree, &values[i], sizeof(int), compareInt, NULL); clearTree(tree); tree = NULL; ASSERT_TRUE(tree == NULL); return true; } /* -------------------------------------------------- * main * -------------------------------------------------- */ int main(void) { UNITY_BEGIN(); RUNTEST(test_addToTree_single); RUNTEST(test_addToTree_duplicate); RUNTEST(test_treeSize); RUNTEST(test_nextTreeData_inorder); RUNTEST(test_clearTree); printf("\nTests run: %d\n", tests_run); printf("Tests failed: %d\n", tests_failed); UNITY_END(); return tests_failed ? EXIT_FAILURE : EXIT_SUCCESS; }