clear Tree and test now working corectly

This commit is contained in:
Jonas Hofmann 2025-12-09 17:51:11 +01:00
parent 144648886f
commit 8754182f79
4 changed files with 21 additions and 19 deletions

View File

@ -135,18 +135,21 @@ void clearNode(TreeNode *node)
// printf("in clearNode\n"); // printf("in clearNode\n");
// printf("node-> data = %u\n", node->data); // printf("node-> data = %u\n", node->data);
// printf("node-> data = %u\n", _ADDRESSOF(node->data)); // printf("node-> data = %u\n", _ADDRESSOF(node->data));
// printf("data = %p\n", node->data);
free(node->data); free(node->data);
node->data = NULL; node->data = NULL;
// printf("data = %p\n", node->data);
// printf("node-> data = %u\n", &node->data); // printf("node-> data = %u\n", &node->data);
// printf("data freed \n"); // printf("data freed \n");
node->left = NULL; node->left = NULL;
node->right = NULL; node->right = NULL;
// printf("left & right = Null\n"); // printf("left & right = Null\n");
printf("node = %u\n", &node); // printf("node = %p\n", node);
// printf("node = %u\n", _ADDRESSOF(node)); // printf("node = %u\n", _ADDRESSOF(node));
free(node); free(node);
// printf("node = %d\n", node); // printf("node = %d\n", node);
node = NULL; node = NULL;
// printf("node = %p\n", node);
// printf("node = %u\n", &node); // printf("node = %u\n", &node);
// printf("freed node\n"); // printf("freed node\n");
} }

BIN
bintree.o

Binary file not shown.

View File

@ -80,7 +80,7 @@ void test_addToTreeExpandsTreeCorrectly(void)
TEST_ASSERT_EQUAL_UINT16(1, testIsDouble); TEST_ASSERT_EQUAL_UINT16(1, testIsDouble);
// clearTree(testRoot); clearTree(testRoot);
} }
@ -92,6 +92,7 @@ void test_nextTreeDataReturnsNextDataCorrectly(void)
} }
// test if clear Tree frees all node.name and node memory AND sets them to zero // test if clear Tree frees all node.name and node memory AND sets them to zero
// aditionally tests if the memoryspaces have been cleared // aditionally tests if the memoryspaces have been cleared
void test_clearTreeworksLikeExpected(void) void test_clearTreeworksLikeExpected(void)
@ -113,30 +114,28 @@ void test_clearTreeworksLikeExpected(void)
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL); testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
printf("Tree Filled\n"); // printf("Tree Filled\n");
// Save all Adresses // Save all Adresses
TreeNode *node1 = testRoot->left; TreeNode *node2 = testRoot->left->left;
TreeNode *node3 = testRoot->left->right;
TreeNode *node4 = testRoot->right;
TreeNode *node5 = testRoot->right->left;
TreeNode *node6 = testRoot->right->right;
TreeNode *node7 = testRoot->left->left->left;
TreeNode *node2 = testRoot->left->left; // printf("Adresses Saved\n");
TreeNode *node3 = testRoot->left->right;
TreeNode *node4 = testRoot->right;
TreeNode *node5 = testRoot->right->left;
TreeNode *node6 = testRoot->right->right;
TreeNode *node7 = testRoot->left->left->left;
printf("Adresses Saved\n");
clearTree(testRoot);
printf("Tree Cleared\n");
clearTree(testRoot);
// printf("Tree Cleared\n");
// Check if everything has been set to NULL // Check if everything has been set to NULL
TEST_ASSERT_NULL(testRoot->data); TEST_ASSERT_NULL(testRoot->data);
TEST_ASSERT_NULL(testRoot); //TEST_ASSERT_NULL(testRoot);
//TEST_ASSERT_NULL(node1->data); //TEST_ASSERT_NULL(node1->data);
// TEST_ASSERT_NULL(node1); // TEST_ASSERT_NULL(node1);
/*
TEST_ASSERT_NULL(node2->data); TEST_ASSERT_NULL(node2->data);
// TEST_ASSERT_NULL(node2); // TEST_ASSERT_NULL(node2);
@ -199,7 +198,7 @@ int main()
UNITY_BEGIN(); UNITY_BEGIN();
printf("\n============================\nBinary Tree tests\n============================\n"); printf("\n============================\nBinary Tree tests\n============================\n");
// RUN_TEST(test_addToTreeExpandsTreeCorrectly); RUN_TEST(test_addToTreeExpandsTreeCorrectly);
// RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly); // RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly);
RUN_TEST(test_clearTreeworksLikeExpected); RUN_TEST(test_clearTreeworksLikeExpected);
// RUN_TEST(test_treeSizeWorkingLikeExpected); // RUN_TEST(test_treeSizeWorkingLikeExpected);

Binary file not shown.