diff --git a/numbers.c b/numbers.c index 39ed471..fb4bf6a 100644 --- a/numbers.c +++ b/numbers.c @@ -42,6 +42,10 @@ unsigned int *createNumbers(unsigned int len) } //TODO: sicherstellen, dass im Array keine doppelten Zahlen vorhanden sind (Max) + + + + position = rand() % len; duplicate_value = array[position]; diff --git a/test_bintree.c b/test_bintree.c new file mode 100644 index 0000000..cb63fd6 --- /dev/null +++ b/test_bintree.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include "bintree.h" + +// Vergleichsfunktion für den Binärbaum (nur für Tests) +static int testCompareFct(const void *argument1, const void *argument2) +{ + unsigned int x = *(const unsigned int *)argument1; + unsigned int y = *(const unsigned int *)argument2; + + if (x > y) return 1; + if (x < y) return -1; + return 0; +} + +// Unit-Test für die Vergleichsfunktion +void test_compareFct(void) +{ + unsigned int a = 3, b = 5, c = 3; + + assert(testCompareFct(&a, &b) < 0); + assert(testCompareFct(&b, &a) > 0); + assert(testCompareFct(&a, &c) == 0); +} + +// Test für Einfügen und Baumgröße +void test_addToTree_and_treeSize(void) +{ + TreeNode *root = NULL; + int isDuplicate; + + int values[] = {5, 3, 7, 2, 4, 6, 8}; + int n = sizeof(values) / sizeof(values[0]); + + for (int i = 0; i < n; i++) + { + root = addToTree(root, &values[i], sizeof(int), + testCompareFct, &isDuplicate); + assert(isDuplicate == 0); + } + + assert(treeSize(root) == 7); + + clearTree(root); +} + +// Test für Duplikaterkennung +void test_duplicates(void) +{ + TreeNode *root = NULL; + int isDuplicate; + + int value = 5; + + root = addToTree(root, &value, sizeof(int), + testCompareFct, &isDuplicate); + assert(isDuplicate == 0); + + root = addToTree(root, &value, sizeof(int), + testCompareFct, &isDuplicate); + assert(isDuplicate == 1); + + clearTree(root); +} + +// Test für Inorder-Traversierung +void test_nextTreeData(void) +{ + TreeNode *root = NULL; + int isDuplicate; + + int values[] = {5, 3, 7, 2, 4, 6, 8}; + int expected[] = {2, 3, 4, 5, 6, 7, 8}; + + for (int i = 0; i < 7; i++) + { + root = addToTree(root, &values[i], sizeof(int), + testCompareFct, &isDuplicate); + } + + for (int i = 0; i < 7; i++) + { + int *data = (int *)nextTreeData(root); + assert(data != NULL); + assert(*data == expected[i]); + } + + assert(nextTreeData(NULL) == NULL); + + clearTree(root); +} + +int main(void) +{ + test_compareFct(); + test_addToTree_and_treeSize(); + test_duplicates(); + test_nextTreeData(); + + printf("Alle Unit-Tests erfolgreich bestanden.\n"); + return 0; +} \ No newline at end of file