#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); } static void testNextTreeDataInorderTraversal(){ TreeNode *root = NULL; int values[] = {5,3,8,1,4}; int expectedInorder[] = {1,3,4,5,8}; for(int i=0;i<5;i++) { root = addToTree(root,&values[i],sizeof(int),compareInt,NULL); //Tree befüllen } void *data = nextTreeData(root); TEST_ASSERT_NOT_NULL(data); for(int i=0;i<5;i++){ TEST_ASSERT_NOT_NULL(data); TEST_ASSERT_EQUAL_INT(expectedInorder[i],*(int*)data); data = nextTreeData(NULL); // Nächstes Element } TEST_ASSERT_NULL(data); // Nach letzten Element muss NULL kommen } static void testClearTree() { TreeNode *root = NULL; int values[] = {10,5,15}; int i; for(i=0;i<3;i++) { root = addToTree(root,&values[i],sizeof(int),compareInt,NULL); } TEST_ASSERT_NOT_NULL(root); clearTree(root); root = NULL; TEST_ASSERT_NULL(root); } static void testTreeSizeEmpty() { TreeNode *root = NULL; TEST_ASSERT_EQUAL_INT(0,treeSize(root)); } static void testTreeSizeSingleNode() { TreeNode *root = NULL; int value = 42; root = addToTree(root,&value,sizeof(int),compareInt,NULL); TEST_ASSERT_EQUAL_INT(1,treeSize(root)); clearTree(root); } static void testTreeSizeMultipleNodes() { TreeNode *root = NULL; int values[] = {10,5,15,3,7,12,18}; unsigned int expectedSize = sizeof(values) / sizeof(values[0]); //Auf groeße von data achten for (int i=0;i