From 517d363f3e889f0144a22c0f60bbcb9ced494a10 Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Mon, 8 Dec 2025 22:41:49 +0100 Subject: [PATCH] debugging --- bintree.c | 25 ++++++++++++----- bintree.o | Bin 2163 -> 2629 bytes bintreeTests.c | 71 ++++++++++++++++++++++++++++++++++--------------- 3 files changed, 69 insertions(+), 27 deletions(-) diff --git a/bintree.c b/bintree.c index 9aa8baa..3c34c42 100644 --- a/bintree.c +++ b/bintree.c @@ -1,3 +1,4 @@ +#include #include #include "stack.h" #include "bintree.h" @@ -39,7 +40,8 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc newNode->data = calloc(1, dataSize); newNode->left = NULL; newNode->right = NULL; - memcpy(newNode->data, data, dataSize); + memcpy(&newNode->data, data, dataSize); + // printf("node created\n"); return addToTreeRec(root, newNode, compareFct, isDuplicate); @@ -48,22 +50,33 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate) { + // printf("entered addRec\n"); + //printf("curdat = %d\n", currentNode->data); + //printf("newdat = %d\n", newNode->data); if (currentNode == NULL) { - isDuplicate = 0; + printf("currentNode == NULL\n"); + if (isDuplicate != NULL) + { + *isDuplicate = 0; + } + return newNode; } - else if (compareFct(newNode->data, currentNode->data) < 0) + else if (compareFct(currentNode->data, newNode->data) < 0) { + printf("compareFct(currentNode->data, newNode->data) < 0\n"); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); } - else if (compareFct(newNode->data, currentNode->data) > 0) + else if (compareFct(currentNode->data, newNode->data) > 0) { + printf("compareFct(currentNode->data, newNode->data) > 0\n"); currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate); } - else + else if (compareFct(currentNode->data, newNode->data) == 0) { //duplicate + printf("duplicate\n"); if (isDuplicate == NULL) { currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate); @@ -73,7 +86,7 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType *isDuplicate = 1; } } - + printf("passed everything\n"); return currentNode; } diff --git a/bintree.o b/bintree.o index e8fcbd71dca67cd0a6bd0ac632196b14cdff8de3..f472e9eb2c68e7dd89c2c522cba49d3946327894 100644 GIT binary patch delta 1011 zcmaizTWb?h5XWb8X`VrM%6moHc^19R1*kO8|BN8QBvn9Vk^AGXzd?8j|I_?Qk0Lpf~GWy>ym;j&jQ`{lAA zzL^vIv=8!xo%$22=16csvgaa^Odwc8FrJP_PJ{4lJdyd4;Ua&_qw!&?`jN HcBu9T@KMXR delta 546 zcmX>q@>zg4rHzdN1eAc3^h929!4FIf3@nU55mvA`1M5W7Y^E>F6Bnsz1+XwM7(s<4 zfU*t@1`hfr5M?kj0La+DGFghTjFDk-A7iMVpvQ4n4Unuyx2wdi0;N_ccL z_;kA}cyu0g{C}bKK&AfV4@`2Vlm7qz@6jD9;L+*C@v;@KjyfHnF+ign1SUH$iSfB3 zbPBw-o&1tXl}Y6PWC3RJ&1THOjFT_0NK9VB;t@FqMjW zm=yzJZ9d5s&R7p}1S8NYpa>AC0Wru)2oYnT2*^PQ5nG@L$XN&xFQ5p>aS)M`qSVyj z%&Jt76A@}cfNDSvg^1{tq*j0&#xQw42PZ4Y@vM{Aa?G8a$H~LUHo2P9dh!X*S^%$I BXK4Tc diff --git a/bintreeTests.c b/bintreeTests.c index 0ab00e7..6aa1c01 100644 --- a/bintreeTests.c +++ b/bintreeTests.c @@ -5,14 +5,24 @@ #define MAX_TEST_NAME_LEN 10 +void setUp(void) { + // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden +} + +void tearDown(void) { + // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden +} + + static int compareIntEntries(const void *arg1, const void *arg2) { + // printf("in comp\n"); const int *entry1 = (const int *)arg1; const int *entry2 = (const int *)arg2; + // printf("const set\n"); + int result = entry2 - entry1; - int result = *entry2 - *entry1; - - + // printf("exit comp\n"); return result; } @@ -34,43 +44,62 @@ void test_addToTreeExpandsTreeCorrectly(void) testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL); + printf("add1passed\n"); testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL); + printf("add2passed\n"); testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL); + printf("add3passed\n"); testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL); + printf("add4passed\n"); testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL); + printf("add5passed\n"); testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL); + printf("add6passed\n"); testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL); + printf("add7passed\n"); // Checking the Tree without Doubles TEST_ASSERT_NOT_NULL(testRoot); - TEST_ASSERT_EQUAL(score1, testRoot); + TEST_ASSERT_EQUAL_UINT16(score1, testRoot->data); + printf("node1passed\n"); + TEST_ASSERT_NOT_NULL(testRoot->left); - TEST_ASSERT_EQUAL(score2, testRoot->left); + TEST_ASSERT_EQUAL_UINT16(score2, testRoot->left->data); + printf("node2passed\n"); + TEST_ASSERT_NOT_NULL(testRoot->right); - TEST_ASSERT_EQUAL(score3, testRoot->right); - TEST_ASSERT_NOT_NULL(testRoot->left->left); - TEST_ASSERT_EQUAL(score4, testRoot->left->left); - TEST_ASSERT_NOT_NULL(testRoot->left->right); - TEST_ASSERT_EQUAL(score5, testRoot->left->right); - TEST_ASSERT_NOT_NULL(testRoot->right->left); - TEST_ASSERT_EQUAL(score6, testRoot->right->left); - TEST_ASSERT_NOT_NULL(testRoot->right->right); - TEST_ASSERT_EQUAL(score7, testRoot->right->right); + TEST_ASSERT_EQUAL_UINT16(score3, testRoot->right->data); + printf("node3passed\n"); + TEST_ASSERT_NOT_NULL(testRoot->left->left); + TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->data); + printf("node4passed\n"); + + TEST_ASSERT_NOT_NULL(testRoot->left->right); + TEST_ASSERT_EQUAL_UINT16(score5, testRoot->left->right->data); + printf("node5passed\n"); + /* + TEST_ASSERT_NOT_NULL(testRoot->right->left); + TEST_ASSERT_EQUAL_UINT16(score6, testRoot->right->left->data); + printf("node6passed\n"); + /* + TEST_ASSERT_NOT_NULL(testRoot->right->right); + TEST_ASSERT_EQUAL_UINT16(score7, testRoot->right->right->data); + printf("node7passed\n"); + /* // Adding Double testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL); - TEST_ASSERT_NOT_NULL(testRoot->left->left->left); - TEST_ASSERT_EQUAL(score4, testRoot->left->left->left); + TEST_ASSERT_EQUAL_UINT16(score4, testRoot->left->left->left); // Trying to add Double while Doubles not Permitted testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, testIsDouble); - TEST_ASSERT_NULL(testRoot->right->right->left); - TEST_ASSERT_EQUAL(1, testIsDouble); - + TEST_ASSERT_EQUAL_UINT16(1, testIsDouble); + clearTree(testRoot); + // */ } @@ -172,8 +201,8 @@ int main() printf("\n============================\nBinary Tree tests\n============================\n"); RUN_TEST(test_addToTreeExpandsTreeCorrectly); // RUN_TEST(test_nextTreeDataReturnsNextDataCorrectly); - RUN_TEST(test_clearTreeworksLikeExpected); - RUN_TEST(test_treeSizeWorkingLikeExpected); + // RUN_TEST(test_clearTreeworksLikeExpected); + // RUN_TEST(test_treeSizeWorkingLikeExpected); return UNITY_END(); }