20 Commits
Author SHA1 Message Date
hofmannjo99187 45b6fab21a testing 2025-12-13 15:04:25 +01:00
hofmannjo99187 d1320cf6b7 changed addToTreeRec so it now sets inDuplicate to 0 if needed 2025-12-12 06:48:37 +01:00
hofmannjo99187 369c019c30 ran stackTest 2025-12-11 08:05:48 +01:00
hofmannjo99187 cccc013b99 ran stackTest 2025-12-11 08:05:37 +01:00
hofmannjo99187 9a79ff0ad4 corrected treeSizeRec cause previously end of non void function could be reached 2025-12-11 08:04:43 +01:00
hofmannjo99187 284cdbdcfb made runbintreeTests.exe 2025-12-10 12:18:49 +01:00
hofmannjo99187 e3501e8550 added runbintreeTests.exe and stack.o 2025-12-09 23:18:36 +01:00
hofmannjo99187 6a9461bf5b all binaryTree functions and Tests working 2025-12-09 23:13:39 +01:00
hofmannjo99187 ef3e0b7891 changed binTreeTest makefile, test_nextTreeDataReturnsNextDataCorrectly() not yet working 2025-12-09 22:41:35 +01:00
hofmannjo99187 82b2fb283f merged branchjens 2025-12-09 22:28:31 +01:00
hofmannjo99187 602220ba21 Merge branch 'branchjens' into branchjonas 2025-12-09 22:27:30 +01:00
hofmannjo99187 2368c9f3b9 added stack to makefile 2025-12-09 22:27:14 +01:00
hofmannjo99187 e4a8afabdd wrote test_nextTreeDataReturnsNextDataCorrectly() 2025-12-09 21:48:43 +01:00
hofmannjo99187 9d08970513 wrote nextTreeData() and nextTreeDataRec() 2025-12-09 21:12:03 +01:00
hofmannjo99187 9ee0f9e281 treeSize and treeSize test now workingW 2025-12-09 18:41:02 +01:00
hofmannjo99187 21aa67b165 removed no longer needed comments 2025-12-09 18:04:58 +01:00
Jens Burger 01bc613189 Kommentare gelöscht 2025-12-09 10:45:10 +01:00
Jens Burger 79a6eb7267 Debugging Unity Aufruf 2025-12-09 10:41:53 +01:00
Jens Burger 53c12ce0ed added makefile command 2025-12-09 10:38:00 +01:00
Jens Burger 8deeb6417e Testfunktion auf Unity umgebaut 2025-12-09 10:31:12 +01:00
9 changed files with 156 additions and 118 deletions
+68 -48
View File
@@ -6,25 +6,26 @@
//TODO: binären Suchbaum implementieren //TODO: binären Suchbaum implementieren
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done /* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv), Done
* `clearTree`: gibt den gesamten Baum frei (rekursiv), Done * `clearTree`: gibt den gesamten Baum frei (rekursiv), Done
* `treeSize`: zählt die Knoten im Baum (rekursiv), Done * `treeSize`: zählt die Knoten im Baum (rekursiv), Done
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */ * `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. Done */
static TreeNode *root = NULL; static StackNode *stackRoot = NULL;
TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate); TreeNode *addToTree (TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate);
void *nextTreeData (TreeNode *root); void *nextTreeData (TreeNode *root);
void clearTree (TreeNode *root); void clearTree (TreeNode *root);
unsigned int treeSize (const TreeNode *root); unsigned int treeSize (const TreeNode *root);
// self declared functions // self declared functions
TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root); TreeNode *addToTreeRec (TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root);
void clearTreeRec (TreeNode *currentNode); void clearTreeRec (TreeNode *currentNode);
void clearNode (TreeNode *node); void clearNode (TreeNode *node);
void treeSizeRec (const TreeNode *currentNode, unsigned int *nodeCount); int treeSizeRec (const TreeNode *currentNode);
void *nextTreeDataRec (TreeNode *node, StackNode *stack);
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates // Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
@@ -52,29 +53,31 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
{ {
if ((currentNode == NULL)) if ((currentNode == NULL))
{ {
// printf("Node == NULL\n");
if ((isDuplicate == NULL) || root) if ((isDuplicate == NULL) || root)
{ {
if (isDuplicate != NULL)
{
*isDuplicate = 0;
}
return newNode; return newNode;
} }
else else
{ {
*isDuplicate = 0;
return currentNode; return currentNode;
} }
} }
else if ((compareFct(currentNode->data, newNode->data) < 0)) else if ((compareFct(currentNode->data, newNode->data) < 0))
{ {
// printf("<0\n");
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
} }
else if ((compareFct(currentNode->data, newNode->data) > 0)) else if ((compareFct(currentNode->data, newNode->data) > 0))
{ {
// printf(">0\n");
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0); currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
} }
else if ((compareFct(currentNode->data, newNode->data) == 0)) else if ((compareFct(currentNode->data, newNode->data) == 0))
{ {
// printf("==0\n");
if (isDuplicate == NULL) if (isDuplicate == NULL)
{ {
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0); currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
@@ -85,7 +88,10 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
} }
} }
if (isDuplicate != NULL)
{
*isDuplicate = 0;
}
return currentNode; return currentNode;
} }
@@ -95,9 +101,40 @@ TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
// Needs stack!! // Needs stack!!
// Stack functions: push(), pop(), top(), clearStack()
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
void *pointerToReturn = NULL;
if (root != NULL)
{
// Add tree to stack so that bigest entry is ontop
stackRoot = nextTreeDataRec(root, stackRoot);
pointerToReturn = stackRoot;
}
else
{
// return current top entry and then pop that top entry
pointerToReturn = top(stackRoot);
stackRoot = pop(stackRoot);
}
return pointerToReturn;
}
void *nextTreeDataRec(TreeNode *tree, StackNode *stack)
{
if (tree != NULL)
{
stack = nextTreeDataRec (tree->left, stack);
stack = push (stack, tree->data);
stack = nextTreeDataRec (tree->right, stack);
}
return stack;
} }
@@ -113,45 +150,22 @@ void clearTreeRec(TreeNode *currentNode)
if (currentNode != NULL) if (currentNode != NULL)
{ {
clearTree(currentNode->left); clearTree(currentNode->left);
clearTree(currentNode->right); clearTree(currentNode->right);
clearNode(currentNode); clearNode(currentNode);
/*
printf("1\n");
free(&currentNode->data);
currentNode->data = NULL;
printf("2\n");
// free(currentNode);
// currentNode = NULL;
printf("3\n");
// */
} }
} }
void clearNode(TreeNode *node) void clearNode(TreeNode *node)
{ {
// printf("in clearNode\n");
// printf("node-> data = %u\n", 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("data freed \n");
node->left = NULL; node->left = NULL;
node->right = NULL; node->right = NULL;
// printf("left & right = Null\n");
// printf("node = %p\n", node);
// printf("node = %u\n", _ADDRESSOF(node));
free(node); free(node);
// printf("node = %d\n", node);
node = NULL; node = NULL;
// printf("node = %p\n", node);
// printf("node = %u\n", &node);
// printf("freed node\n");
} }
@@ -161,19 +175,25 @@ unsigned int treeSize(const TreeNode *root)
unsigned int amountOfNodes = 0; unsigned int amountOfNodes = 0;
treeSizeRec(root, &amountOfNodes); amountOfNodes = treeSizeRec(root);
return amountOfNodes; return amountOfNodes;
} }
void treeSizeRec(const TreeNode *currentNode, unsigned int *nodeCount)
int treeSizeRec(const TreeNode *currentNode)
{ {
int nodeCount = 0;
if (currentNode != NULL) if (currentNode != NULL)
{ {
treeSizeRec(currentNode->left, nodeCount); nodeCount += treeSizeRec(currentNode->left);
*nodeCount++; nodeCount += treeSizeRec(currentNode->right);
treeSizeRec(currentNode->right, nodeCount); return nodeCount + 1;
} }
return nodeCount;
} }
BIN
View File
Binary file not shown.
+38 -24
View File
@@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "unity.h" #include "unity.h"
#include "bintree.h" #include "bintree.h"
#include "stack.h"
#define MAX_TEST_NAME_LEN 10 #define MAX_TEST_NAME_LEN 10
@@ -11,6 +12,7 @@ void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
} }
void tearDown(void) { void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
} }
@@ -88,11 +90,44 @@ void test_addToTreeExpandsTreeCorrectly(void)
// needs Stack!!! // needs Stack!!!
void test_nextTreeDataReturnsNextDataCorrectly(void) void test_nextTreeDataReturnsNextDataCorrectly(void)
{ {
TreeNode *testRoot = NULL;
int score1 = 12;
int score2 = 6;
int score3 = 18;
int score4 = 3;
int score5 = 9;
int score6 = 15;
int score7 = 21;
// Prepare a Tree
testRoot = addToTree(testRoot, &score1, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score2, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score3, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score5, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score6, sizeof(int), compareIntEntries, NULL);
testRoot = addToTree(testRoot, &score7, sizeof(int), compareIntEntries, NULL);
// Create Stack
StackNode *entry = nextTreeData(testRoot);
// check if nextTreeData returns Data correctly
TEST_ASSERT_NOT_NULL(entry);
TEST_ASSERT_EQUAL(score7, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score3, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score6, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score1, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score5, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score2, *(int *)nextTreeData(NULL));
TEST_ASSERT_EQUAL(score4, *(int *)nextTreeData(NULL));
clearTree(testRoot);
} }
// 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)
@@ -114,7 +149,6 @@ 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");
// Save all Adresses // Save all Adresses
TreeNode *node1 = testRoot; TreeNode *node1 = testRoot;
@@ -125,36 +159,17 @@ void test_clearTreeworksLikeExpected(void)
TreeNode *node6 = testRoot->right->left; TreeNode *node6 = testRoot->right->left;
TreeNode *node7 = testRoot->right->right; TreeNode *node7 = testRoot->right->right;
// printf("Adresses Saved\n");
clearTree(testRoot); 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(node1->data); TEST_ASSERT_NULL(node1->data);
//TEST_ASSERT_NULL(testRoot);
TEST_ASSERT_NULL(node1->data); TEST_ASSERT_NULL(node1->data);
// TEST_ASSERT_NULL(node1);
TEST_ASSERT_NULL(node2->data); TEST_ASSERT_NULL(node2->data);
// TEST_ASSERT_NULL(node2);
TEST_ASSERT_NULL(node3->data); TEST_ASSERT_NULL(node3->data);
// TEST_ASSERT_NULL(node3);
TEST_ASSERT_NULL(node4->data); TEST_ASSERT_NULL(node4->data);
// TEST_ASSERT_NULL(node4);
TEST_ASSERT_NULL(node5->data); TEST_ASSERT_NULL(node5->data);
// TEST_ASSERT_NULL(node5);
TEST_ASSERT_NULL(node6->data); TEST_ASSERT_NULL(node6->data);
// TEST_ASSERT_NULL(node6);
TEST_ASSERT_NULL(node7->data); TEST_ASSERT_NULL(node7->data);
// TEST_ASSERT_NULL(node7);
// */
} }
@@ -183,7 +198,6 @@ void test_treeSizeWorkingLikeExpected(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);
testRoot = addToTree(testRoot, &score4, sizeof(int), compareIntEntries, NULL);
testTreeSize = treeSize(testRoot); testTreeSize = treeSize(testRoot);
@@ -200,9 +214,9 @@ int main()
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);
return UNITY_END(); return UNITY_END();
} }
+7 -2
View File
@@ -41,9 +41,14 @@ $(program_obj_filesobj_files): %.o: %.c
bintree: bintree.c bintree: bintree.c
$(CC) $(FLAGS) -c bintree bintree.c $(CC) $(FLAGS) -c bintree bintree.c
bintreeTests: bintree.o bintreeTests.c $(unityfolder)/unity.c bintreeTests: stack.o bintree.o bintreeTests.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -o runbintreeTests bintreeTests.c bintree.o $(unityfolder)/unity.c $(CC) $(FLAGS) -o runbintreeTests bintreeTests.c bintree.o stack.o $(unityfolder)/unity.c
stack: stack.c
$(CC) $(FLAGS) -c stack stack.c
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------
BIN
View File
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+43 -44
View File
@@ -1,24 +1,30 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "unity.h"
#include "stack.h" #include "stack.h"
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
void testeStackBeschreiben() void testeStackBeschreiben()
{ {
printf("=== Test: push() ===\n"); //printf("=== Test: push() ===\n");
StackNode *stack = NULL; StackNode *stack = NULL;
int wert1 = 42; int wert1 = 42;
stack = push(stack, &wert1); stack = push(stack, &wert1);
int *topValue = (int *)(stack->data); int *topValue = (int *)(stack->data);
if(topValue != NULL && *topValue == 42)
{ TEST_ASSERT_NOT_NULL(topValue);
printf("Test 1: Erstes Element erfolgreich gepusht!\n"); TEST_ASSERT_EQUAL_INT(42, *topValue);
}
else
{
printf("Test 1: FEHLGESCHLAGEN!\n");
}
int wert2 = 12; int wert2 = 12;
@@ -27,23 +33,19 @@ void testeStackBeschreiben()
topValue = (int *)(stack->data); topValue = (int *)(stack->data);
int *secondValue = (int *)((stack->next)->data); int *secondValue = (int *)((stack->next)->data);
if(topValue != NULL && *topValue == 12.25 && secondValue != NULL && *secondValue == 42) TEST_ASSERT_NOT_NULL(topValue);
{ TEST_ASSERT_EQUAL_INT(12, *topValue);
printf("Test 2: Zweites Element erfolgreich gepusht!\n"); TEST_ASSERT_NOT_NULL(secondValue);
} TEST_ASSERT_EQUAL_INT(42, *secondValue);
else
{
printf("Test 2: FEHLGESCHLAGEN!\n");
}
printf("=== Ende Test: push() ===\n\n"); //printf("=== Ende Test: push() ===\n\n");
return; return;
} }
void testepop() void testepop()
{ {
printf("=== Test: pop() ===\n"); //printf("=== Test: pop() ===\n");
StackNode *stack = NULL; StackNode *stack = NULL;
int wert1 = 20; int wert1 = 20;
@@ -56,21 +58,15 @@ void testepop()
int *topValue = (int *)(stack->data); int *topValue = (int *)(stack->data);
if(topValue != NULL && *topValue == 20) TEST_ASSERT_NOT_NULL(topValue);
{ TEST_ASSERT_EQUAL_INT(20, *topValue);
printf("Test: Erstes Element erfolgreich gelöscht!\n");
}
else
{
printf("Test 1: FEHLGESCHLAGEN!\n");
}
printf("=== Ende Test: pop() ===\n\n"); //printf("=== Ende Test: pop() ===\n\n");
} }
void testetop() void testetop()
{ {
printf("=== Test: top() ===\n"); //printf("=== Test: top() ===\n");
StackNode *stack = NULL; StackNode *stack = NULL;
int wert1 = 20; int wert1 = 20;
@@ -81,21 +77,15 @@ void testetop()
int *topValue = top(stack); int *topValue = top(stack);
if(topValue != NULL && *topValue == 74) TEST_ASSERT_NOT_NULL(topValue);
{ TEST_ASSERT_EQUAL_INT(74, *topValue);
printf("Test: top() gibt korrektes Element zurück!\n");
}
else
{
printf("Test: FEHLGESCHLAGEN!\n");
}
printf("=== Ende Test: top() ===\n\n"); //printf("=== Ende Test: top() ===\n\n");
} }
void testeclearStack() void testeclearStack()
{ {
printf("=== Test: clearStack() ===\n"); //printf("=== Test: clearStack() ===\n");
StackNode *stack = NULL; StackNode *stack = NULL;
int wert1 = 20; int wert1 = 20;
@@ -106,16 +96,25 @@ void testeclearStack()
clearStack(stack); clearStack(stack);
printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n"); //printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
printf("=== Ende Test: clearStack() ===\n\n"); //printf("=== Ende Test: clearStack() ===\n\n");
} }
int main() int main()
{ {
testeStackBeschreiben(); UNITY_BEGIN();
//printf("...");
RUN_TEST(testeStackBeschreiben);
RUN_TEST(testepop);
RUN_TEST(testetop);
RUN_TEST(testeclearStack);
/*testeStackBeschreiben();
testepop(); testepop();
testetop(); testetop();
testeclearStack(); testeclearStack();*/
return 0; return UNITY_END();
} }