Compare commits

...

3 Commits

Author SHA1 Message Date
D2A62006
c79a61e8ee Implement first test 2025-12-07 20:17:27 +01:00
D2A62006
97a11d8ac6 Implement nextTreeData() 2025-12-07 20:17:04 +01:00
D2A62006
a89ed94c97 Update makefile and gitignore 2025-12-07 20:15:02 +01:00
5 changed files with 57 additions and 3 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
doble_initial doble_initial
stackTests
bintreeTests
*.o *.o
*.exe *.exe

View File

@ -68,6 +68,27 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// push the top node and push all its left nodes. // push the top node and push all its left nodes.
void *nextTreeData(TreeNode *root) void *nextTreeData(TreeNode *root)
{ {
static StackNode *stack = NULL;
static TreeNode *current = NULL;
if(root != NULL){
clearStack(stack);
stack = NULL;
current = root;
}
while (current != NULL)
{
stack = push(stack, current);
current = current->left;
}
TreeNode *node = (TreeNode *)top(stack);
stack = pop(stack);
current = node->right;
return node->data;
} }

View File

@ -1,8 +1,22 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "unity.h" #include "unity.h"
#include "bintree.h" #include "bintree.h"
int compareInts(const void *arg1, const void *arg2){
int val1 = *(int *)arg1;
int val2 = *(int *)arg2;
if(val1 < val2) return 1;
if(val1 > val2) return -1;
return 0;
}
int compareStrings(const void *arg1, const void *arg2){
return -strcmp((const char *)arg1, (const char *)arg2);
}
void setUp(void){ void setUp(void){
//Use if needed //Use if needed
@ -11,9 +25,24 @@ void tearDown(void){
//Use if needed //Use if needed
} }
void test_addToTree_singleElement(void){
int value = 42;
TreeNode *tree = NULL;
tree = addToTree(tree, &value, sizeof(int), compareInts, NULL);
TEST_ASSERT_NOT_NULL(tree);
TEST_ASSERT_EQUAL_INT(value, *(int *)tree->data);
TEST_ASSERT_NULL(tree->left);
TEST_ASSERT_NULL(tree->right);
}
int main(){ int main(){
UNITY_BEGIN(); UNITY_BEGIN();
printf("\n============================\nBintree tests\n============================\n"); printf("\n============================\nBintree tests\n============================\n");
RUN_TEST(test_addToTree_singleElement);
return UNITY_END();
} }

View File

@ -39,6 +39,8 @@ doble_initial:
# -------------------------- # --------------------------
stackTests: stack.o test_stack.c $(unityfolder)/unity.c stackTests: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o stackTests test_stack.c stack.o $(unityfolder)/unity.c ${LDFLAGS} $(CC) $(CFLAGS) -I$(unityfolder) -o stackTests test_stack.c stack.o $(unityfolder)/unity.c ${LDFLAGS}
bintreeTests: bintree.o stack.o bintreeTest.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o bintreeTests bintreeTest.c bintree.o stack.o $(unityfolder)/unity.c ${LDFLAGS}
unitTests: unitTests:
echo "needs to be implemented" echo "needs to be implemented"
@ -47,7 +49,7 @@ unitTests:
# -------------------------- # --------------------------
clean: clean:
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
del /f *.o doble del /f *.o doble doble_initial bintreeTests stackTests
else else
rm -f *.o doble rm -f *.o doble doble_initial bintreeTests stackTests
endif endif

Binary file not shown.