Compare commits

...

13 Commits

Author SHA1 Message Date
D2A62006
3cc67f0343 Add tests for nextTreeData functionality 2025-12-08 17:49:18 +01:00
D2A62006
ea36ddeec2 Additional unitTests 2025-12-07 21:27:04 +01:00
D2A62006
492a101160 Fix nextTreeData 2025-12-07 21:26:44 +01:00
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
D2A62006
70c0f9bcaf Merge branch 'dev_kob_stack' into bintree_dev 2025-12-07 19:22:21 +01:00
D2A62006
06168693b7 Implement treeSize() and update clearTree() 2025-12-07 19:08:02 +01:00
D2A62006
7652d3ea7d Create bintreeTest.c file 2025-12-07 18:47:50 +01:00
D2A62006
9f15c0c01f implement clearTree() and fix addToTree() 2025-12-07 18:47:33 +01:00
D2A62006
2d79193d6c implement *addToTree() 2025-12-07 17:34:08 +01:00
=
64a16f06d7 added more unit tests for test_stack.c 2025-12-04 19:30:33 +01:00
=
204f2a2526 Added complete stack functionality 2025-12-04 18:56:18 +01:00
9 changed files with 559 additions and 14 deletions

2
.gitignore vendored
View File

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

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.associations": {
"stdio.h": "c",
"stdlib.h": "c"
}
}

View File

@ -13,6 +13,54 @@
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{
if(root == NULL){
//Allocate Memory for Node
TreeNode *newNode = calloc(1, sizeof(TreeNode));
if(newNode == NULL){
return NULL; //Memory allocation failed
}
//Allocate Memory for data
newNode->data = malloc(dataSize);
if(newNode->data == NULL){
free(newNode); //Free unused Memory
return NULL;; //Memory allocation failed
}
memcpy(newNode->data, data, dataSize); //Copy Data
newNode->left = NULL;
newNode->right = NULL;
if(isDuplicate != NULL){
*isDuplicate = 0;
}
return newNode;
}
int cmp = compareFct(root->data, data);
if(cmp == 0){
//Duplicate
if(isDuplicate != NULL){
//Ignore duplicate
*isDuplicate = 1;
return root;
} else{
//Accept duplicate
root->right = addToTree(root->right, data, dataSize, compareFct, NULL);
}
}
else if (cmp > 0)
{
//Data is smaller -> left
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
}else{
//Data is bigger -> right
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
}
return root;
}
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
@ -20,17 +68,52 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
// push the top node and push all its left nodes.
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;
}
if(stack == NULL){
return 0;
}
TreeNode *node = (TreeNode *)top(stack);
stack = pop(stack);
current = node->right;
return node->data;
}
// Releases all memory resources (including data copies).
void clearTree(TreeNode *root)
{
if(root == NULL){
return;
}
clearTree(root->left);
clearTree(root->right);
free(root->data);
free(root);
}
// Returns the number of entries in the tree given by root.
unsigned int treeSize(const TreeNode *root)
{
if(root == NULL){
return 0;
}
return 1 + treeSize(root->left) + treeSize(root->right);
}

330
bintreeTest.c Normal file
View File

@ -0,0 +1,330 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity.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){
//Use if needed
}
void tearDown(void){
//Use if needed
}
//addToTree()
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);
}
void test_addToTree_multipleElements(void){
TreeNode *tree = NULL;
int values[] = {20, 30, 40, 50, 60, 70, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
TEST_ASSERT_NOT_NULL(tree);
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->data);
TEST_ASSERT_EQUAL_INT(30, *(int *)tree->right->data);
TEST_ASSERT_EQUAL_INT(40, *(int *)tree->right->right->data);
TEST_ASSERT_EQUAL_INT(50, *(int *)tree->right->right->right->data);
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->right->right->right->data);
}
void test_addToTree_multipleElementsOptimised(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
TEST_ASSERT_NOT_NULL(tree);
TEST_ASSERT_EQUAL_INT(50, *(int *)tree->data);
TEST_ASSERT_EQUAL_INT(30, *(int *)tree->left->data);
TEST_ASSERT_EQUAL_INT(70, *(int *)tree->right->data);
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->left->left->data);
TEST_ASSERT_EQUAL_INT(40, *(int *)tree->left->right->data);
}
void test_addToTree_withDuplicatesAccept(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 20, 60, 60};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
TEST_ASSERT_NOT_NULL(tree);
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->left->left->data);
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->left->left->right->data);
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->left->data);
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->left->right->data);
}
void test_addToTree_withoutDuplicatesAccept(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 20, 60, 60};
int isDuplicate = 0;
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, &isDuplicate);
if(i == 4 || i == 6){
TEST_ASSERT_EQUAL_INT(1, isDuplicate);
} else{
TEST_ASSERT_EQUAL_INT(0, isDuplicate);
}
}
TEST_ASSERT_NOT_NULL(tree);
TEST_ASSERT_EQUAL_INT(20, *(int *)tree->left->left->data);
TEST_ASSERT_EQUAL_INT(60, *(int *)tree->right->left->data);
TEST_ASSERT_EQUAL_INT(5, treeSize(tree));
}
//treeSize()
void test_treeSize_emptyTree(void){
TEST_ASSERT_EQUAL_UINT(0, treeSize(NULL));
}
void test_treeSize_singleNode(void){
TreeNode *tree = NULL;
int value = 42;
tree = addToTree(tree, &value, sizeof(int), compareInts, NULL);
TEST_ASSERT_NOT_NULL(tree);
TEST_ASSERT_EQUAL_UINT(1, treeSize(tree));
}
void test_treeSize_multipleNodes(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
TEST_ASSERT_NOT_NULL(tree);
TEST_ASSERT_EQUAL_UINT(7, treeSize(tree));
}
//nextTreeData()
void test_nextTreeData_emptyTree(void){
TEST_ASSERT_NULL(nextTreeData(NULL));
}
void test_nextTreeData_singleElement(void){
TreeNode *tree = NULL;
int value = 42;
tree = addToTree(tree, &value, sizeof(int), compareInts, NULL);
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(value, *result);
TEST_ASSERT_NULL(nextTreeData(NULL));
clearTree(tree);
}
void test_nextTreeData_multipleElements(void){
TreeNode *tree = NULL;
int values[] = {20, 30, 40, 50, 60, 70, 80};
int expected[] = {20, 30, 40, 50, 60, 70, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 7; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
TEST_ASSERT_NULL(nextTreeData(NULL));
clearTree(tree);
}
void test_nextTreeData_multipleElements_optimized(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
int expected[] = {20, 30, 40, 50, 60, 70, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 7; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
TEST_ASSERT_NULL(nextTreeData(NULL));
clearTree(tree);
}
void test_nextTreeData_withDuplicates(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 20, 60, 60};
int expected[] = {20, 20, 30, 50, 60, 60, 70};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 7; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
TEST_ASSERT_NULL(nextTreeData(NULL));
}
void test_nextTreeData_restart(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
int expected[] = {20, 30, 40, 50, 60, 70, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
int *result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 4; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
result = (int *)nextTreeData(tree);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[0], *result);
for(int i = 1; i < 7; i++){
result = (int *)nextTreeData(NULL);
TEST_ASSERT_NOT_NULL(result);
TEST_ASSERT_EQUAL_INT(expected[i], *result);
}
}
//clearTree()
void test_clearTree_emptyTree(void){
clearTree(NULL);
TEST_ASSERT_TRUE(1);
}
void test_clearTree_singleElement(void){
int value = 42;
TreeNode *tree = NULL;
tree = addToTree(tree, &value, sizeof(int), compareInts, NULL);
clearTree(tree);
TEST_ASSERT_TRUE(1);
}
void test_clearTree_multipleElements(void){
TreeNode *tree = NULL;
int values[] = {50, 30, 70, 20, 40, 60, 80};
for(int i = 0; i < 7; i++){
tree = addToTree(tree, &values[i], sizeof(int), compareInts, NULL);
}
clearTree(tree);
TEST_ASSERT_TRUE(1);
}
int main(){
UNITY_BEGIN();
printf("\n============================\nBintree tests\n============================\n");
//addToTree()
RUN_TEST(test_addToTree_singleElement);
RUN_TEST(test_addToTree_multipleElements);
RUN_TEST(test_addToTree_multipleElementsOptimised);
RUN_TEST(test_addToTree_withDuplicatesAccept);
RUN_TEST(test_addToTree_withoutDuplicatesAccept);
//treeSize()
RUN_TEST(test_treeSize_emptyTree);
RUN_TEST(test_treeSize_singleNode);
RUN_TEST(test_treeSize_multipleNodes);
//nextTreeData()
RUN_TEST(test_nextTreeData_emptyTree);
RUN_TEST(test_nextTreeData_singleElement);
RUN_TEST(test_nextTreeData_multipleElements);
RUN_TEST(test_nextTreeData_multipleElements_optimized);
RUN_TEST(test_nextTreeData_withDuplicates);
RUN_TEST(test_nextTreeData_restart);
//clearTree()
RUN_TEST(test_clearTree_emptyTree);
RUN_TEST(test_clearTree_singleElement);
RUN_TEST(test_clearTree_multipleElements);
return UNITY_END();
}

View File

@ -37,6 +37,10 @@ doble_initial:
# --------------------------
# Unit Tests
# --------------------------
stackTests: stack.o test_stack.c $(unityfolder)/unity.c
$(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:
echo "needs to be implemented"
@ -45,7 +49,7 @@ unitTests:
# --------------------------
clean:
ifeq ($(OS),Windows_NT)
del /f *.o doble
del /f *.o doble doble_initial bintreeTests stackTests
else
rm -f *.o doble
rm -f *.o doble doble_initial bintreeTests stackTests
endif

View File

@ -14,7 +14,7 @@
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers.
unsigned int *createNumbers(unsigned int len)
unsigned int* createNumbers(unsigned int len)
{
}

37
stack.c
View File

@ -8,26 +8,53 @@
* `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
StackNode* push(StackNode* stack, void* data)
{
//pointer onto new struct
StackNode* newNode = malloc(sizeof(StackNode));
newNode->data = data;
//*stack := first node of the stack; is now the second to first node
newNode->nextNode = stack;
return newNode;
}
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode *pop(StackNode *stack)
StackNode* pop(StackNode* stack)
{
//getting the second to first node
StackNode* tmp = stack->nextNode;
// data should be freed by the caller
// free(stack->data);
// stack->data = NULL;
free(stack);
stack = NULL;
return tmp;
}
// Returns the data of the top element.
void *top(StackNode *stack)
void* top(StackNode* stack)
{
//No stack --> No data-pointer
if (stack == NULL) return NULL;
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
void clearStack(StackNode* stack)
{
if (stack == NULL) return;
if (stack->nextNode != NULL) clearStack(stack->nextNode);
// date should be freed by the caller.
// free(stack->data);
// stack->data = NULL;
free(stack);
// technically not needed (dangling pointer)
stack = NULL;
}

11
stack.h
View File

@ -7,17 +7,20 @@ The latest element is taken from the stack. */
#include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen
typedef struct StackNode {
void* data;
struct StackNode* nextNode;
} StackNode;
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);
StackNode* push(StackNode *stack, void *data);
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
// freed by caller.)
StackNode *pop(StackNode *stack);
StackNode* pop(StackNode *stack);
// Returns the data of the top element.
void *top(StackNode *stack);
void* top(StackNode *stack);
// Clears stack and releases all memory.
void clearStack(StackNode *stack);

90
test_stack.c Normal file
View File

@ -0,0 +1,90 @@
#include <stdlib.h>
#include <stdio.h>
#include "unity.h"
#include "stack.h"
void test_topReturnsCorrectValue(void) {
// 1. Arrange
int* value1;
int* value2;
int* outVal1;
int* outVal2;
value1 = malloc(sizeof(int));
value2 = malloc(sizeof(int));
StackNode* startNode = NULL;
// 2. Act
*value1 = 1234;
*value2 = 5678;
startNode = push(startNode, value1);
// new top node is node 2
startNode = push(startNode, value2);
outVal2 = top(startNode);
// node 1 should now be the top node
startNode = pop(startNode);
outVal1 = top(startNode);
// 3. Assert
//Also tests for the functionality of 'push'
TEST_ASSERT_EQUAL_INT32(*value1, *outVal1);
TEST_ASSERT_EQUAL_INT32(*value2, *outVal2);
free(value1);
value1 = NULL;
free(value2);
value2 = NULL;
clearStack(startNode);
}
void test_topReturnsNullOnNotExistingStack(void) {
// 1. Arrange
StackNode* startNode = NULL;
int* data;
// 2. Act
data = top(startNode);
//3. Assert
TEST_ASSERT_NULL(data);
}
void test_popRemovesElements(void) {
// 1. Arrange
StackNode* startNode = NULL;
// 2. Act
// Entering some Elements into Stack (could be added manually for testing)
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
startNode = push(startNode, NULL);
// Now removing created Elements
startNode = pop(startNode);
startNode = pop(startNode);
startNode = pop(startNode);
startNode = pop(startNode);
// 3. Assert
TEST_ASSERT_NULL(startNode);
}
void setUp(void){
}
void tearDown(void){
}
int main(void) {
UNITY_BEGIN();
printf("\n============================\nStack tests\n============================\n");
RUN_TEST(test_topReturnsNullOnNotExistingStack);
RUN_TEST(test_topReturnsCorrectValue);
RUN_TEST(test_popRemovesElements);
return UNITY_END();
}