Tests fuer den BinaryTree geschrieben, makefile ergaenzt

This commit is contained in:
silvana884 2025-12-10 19:15:09 +01:00
parent bc24ca7b46
commit 2c35bafba9
3 changed files with 110 additions and 13 deletions

View File

@ -15,34 +15,115 @@ void setUp() {}
void tearDown() {}
//Adds a single element to the tree
void add_single_element_to_Tree()
void test_add_single_element_to_Tree()
{
TreeNode *root = NULL;
int value = 5;
int duplicate = -1;
root = addToTree(root, &value, sizeof(int), compare, &duplicate);
TEST_ASSERT_NOT_NULL(root); //uberprueft, ob root dem Tree hinzugefuegt werden konnte
TEST_ASSERT_EQUAL_INT(10, *(int*)root->data); //ueberprueft, ob der Wert fuer data richtig uebernommen wurde
TEST_ASSERT_EQUAL_INT(0, dup); //ueberprueft, ob isDuplicate 0 gesetzt wurde (neue Knoten -> isDuplicate sollte 0 sein)
clearTree(root);
}
//Adds multiplie elements to a tree, just one element should be in the tree twice
void add_multiple_elements_to_Tree()
//Adds multiplie elements to a tree
void test_add_multiple_elements_to_Tree()
{
TreeNode *root = NULL;
int value = [2, 5, 7, 9];
int duplicate = -1;
for(int j = 0; j < 4; ++j)
{
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
}
TEST_ASSERT_EQUAl_INT(4, sizeofTree(root));
clearTree(root);
}
//Adds multiple elements to the tree, but only allows one duplicate
void test_add_multiplie_elements_one_dup()
{
TreeNode *root = NULL;
int value = [1, 3, 1, 4, 5, 6, 7, 5, 9, 10];
int duplicate = -1;
for(int j = 0; j < 10; ++j)
{
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
}
TEST_ASSERT_EQUAl_INT(9, sizeofTree(root));
}
//Detects the size of a tree
void detect_size()
void test_detect_empty_size()
{
TEST_ASSERT_EQUAL_INT(0, treeSize(NULL));
}
//Tests, wether one duplicate is in the tree
void detect_duplicate()
//checks, wether size of tree is correctly determined and wether clearTree() works
void test_detect_size()
{
//fuellt Baum
TreeNode *root = NULL;
int value = [1, 3, 1, 4, 5, 6, 7, 5, 9, 10];
int duplicate = -1;
for(int j = 0; j < 10; ++j)
{
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
}
//ueberprueft, ob Baum korrekt gefuellt wurde
TEST_ASSERT_EQUAl_INT(9, sizeofTree(root));
}
clearTree(root);
root = NULL;
TEST_ASSERT_EQUAL_INT(0, treeSize(root));
//Traverses the three inorder to check wether nextTreeData works
void whole_tree()
void test_inorder()
{
}
//Tests, wether whole tree is deleted from memory
void delete_tree()
TreeNode *root = NULL;
int values[] = {5, 3, 7, 2, 4, 6, 8};
// Einfügen
for (int i = 0; i < 7; i++)
root = addToTree(root, &values[i], sizeof(int), compare, NULL);
// Erwartete Reihenfolge (inorder): 2,3,4,5,6,7,8
int expected[] = {2,3,4,5,6,7,8};
int idx = 0;
void *p = nextTreeData(root); // Iterator starten
while (p != NULL)
{
TEST_ASSERT_EQUAL_INT(expected[idx], *(int*)p);
idx++;
p = nextTreeData(NULL); // Fortsetzen mit NULL
}
TEST_ASSERT_EQUAL(7, idx); //alle Einträge geprüft
clearTree(root);
}
int main()
{
UNITY_BEGIN();
RUN_TEST(test_add_single_element_to_Tree());
RUN_TEST(test_add_multiple_elements_to_Tree());
RUN_TEST(test_add_multiplie_elements_one_dup());
RUN_TEST(test_detect_empty_size());
RUN_TEST(test_detect_size());
RUN_TEST(test_inorder());
return UNITY_END();
}

View File

@ -31,7 +31,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
newNode->left = NULL;
newNode->right = NULL;
if(isDuplicate!= NULL)
if(isDuplicate!= NULL) //wenn Zeiger isDUplicate auf einen Wert zeigt, wird isDuplicate auf 0 gesetzt
{
*isDuplicate = 0;
}

View File

@ -36,7 +36,23 @@ $(program_obj_filesobj_files): %.o: %.c
# Unit Tests
# --------------------------
unitTests:
echo "needs to be implemented"
unity_src = $(unityfolder)/unity.c
unitTests: numbersTest stackTest bintreeTest
#./runNumbersTest
# ./runStackTest
./runBinTreeTest
#numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src) stack.o
# $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest
#stackTest: stack.o stackTest.c $(unity_src)
# $(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTest
bintreeTest: bintree.o binTreeTest.c $(unity_src) stack.o
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBintreeTest
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
# --------------------------
# Clean