diff --git a/Start_Windows/bintree.c b/Start_Windows/bintree.c index 5cf82a9..c2493c9 100644 --- a/Start_Windows/bintree.c +++ b/Start_Windows/bintree.c @@ -12,7 +12,45 @@ // 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 (!data){ + return NULL; + } + if (!root){ + TreeNode *new = malloc(sizeof(TreeNode)); + if (!new){ + return NULL; + } + new->data = malloc(dataSize); + if (!new->data){ + free(new); + return NULL; + } + + memcpy(new->data, data, dataSize); + new->left = new->right = NULL; + + if (isDuplicate){ + *isDuplicate = 0; + } + return new; + } + + int cmp = compareFct(data, root->data); + + if (cmp < 0) { + root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate); + } else if (cmp > 0) { + root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate); + } else { + if (isDuplicate == NULL) { + root->left = addToTree(root->left, data, dataSize, compareFct, NULL); + } else { + *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. @@ -20,7 +58,7 @@ 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) { - + } // Releases all memory resources (including data copies). diff --git a/Start_Windows/bintree.o b/Start_Windows/bintree.o new file mode 100644 index 0000000..4a63186 Binary files /dev/null and b/Start_Windows/bintree.o differ diff --git a/Start_Windows/bintreeTests.c b/Start_Windows/bintreeTests.c new file mode 100644 index 0000000..f637281 --- /dev/null +++ b/Start_Windows/bintreeTests.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include "unity.h" +#include "bintree.h" + +int compareInt(const void *arg1, const void *arg2) { + int a = *(const int *)arg1; + int b = *(const int *)arg2; + return a - b; +} +int compareString(const void *arg1, const void *arg2) { + const char *str1 = (const char *)arg1; + const char *str2 = (const char *)arg2; + return strcmp(str1, str2); +} + + + + +static void testAddToTreeNewRoot(){ + TreeNode *root=NULL; + int a = 1; + root = addToTree(root,&a, sizeof(int),compareInt,0); + + TEST_ASSERT_EQUAL_INT(a,*(int*)(root->data)); +} + +static void testAddToTreeToExistingRoot(){ + TreeNode *root=NULL; + int a = 1; + int b = 3; + int c = 4; + int d = 2; + root = addToTree(root,&a, sizeof(int),compareInt,0); + root = addToTree(root,&b, sizeof(int),compareInt,0); + root = addToTree(root,&c, sizeof(int),compareInt,0); + root = addToTree(root,&d, sizeof(int),compareInt,0); + + TEST_ASSERT_EQUAL_INT(a,*(int*)(root->data)); + TEST_ASSERT_EQUAL_INT(b,*(int*)(root->right->data)); + TEST_ASSERT_EQUAL_INT(c,*(int*)(root->right->right->data)); + TEST_ASSERT_EQUAL_INT(d,*(int*)(root->right->left->data)); +} +static void testAddToTreeNoData(){ + TreeNode *root=NULL; + root = addToTree(root,NULL, sizeof(int),compareInt,0); + + TEST_ASSERT_NULL(root); +} + + +void setUp(void){ + +} +void tearDown(void){ + +} + + + +int main(){ + UNITY_BEGIN(); + printf("\n============================\nBintree tests\n============================\n"); + + RUN_TEST(testAddToTreeNewRoot); + RUN_TEST(testAddToTreeToExistingRoot); + RUN_TEST(testAddToTreeNoData); + + return UNITY_END(); +} \ No newline at end of file diff --git a/Start_Windows/makefile b/Start_Windows/makefile index b3d0844..0965468 100644 --- a/Start_Windows/makefile +++ b/Start_Windows/makefile @@ -29,6 +29,10 @@ unitTests: stackTests: stack.o stackTests.c $(unityfolder)/unity.c $(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests stackTests.c stack.o $(unityfolder)/unity.c +bintreeTests: stack.o bintree.o bintreeTests.c $(unityfolder)/unity.c + $(CC) $(CFLAGS) -I$(unityfolder) -o runBintreeTests bintreeTests.c stack.o bintree.o $(unityfolder)/unity.c + + # -------------------------- # Clean # -------------------------- diff --git a/Start_Windows/runBintreeTests.exe b/Start_Windows/runBintreeTests.exe new file mode 100644 index 0000000..bda5ff1 Binary files /dev/null and b/Start_Windows/runBintreeTests.exe differ