From 6671a3aacc6d8f7e3b38ae2d4526ef48bd278b71 Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Thu, 27 Nov 2025 16:29:29 +0100 Subject: [PATCH 1/7] Initial commit for stack --- highscores.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/highscores.txt b/highscores.txt index 4edd5a7..520fe3d 100644 --- a/highscores.txt +++ b/highscores.txt @@ -1 +1,2 @@ +Lukas;4985 player1;3999 From 94e489cfae5dfcf2cae2baadc91337ca6bfefe78 Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Thu, 27 Nov 2025 17:10:02 +0100 Subject: [PATCH 2/7] Finished first draw of stack, next step: writing tests --- stack.c | 34 +++++++++++++++++++++++++++++++++- stack.h | 5 +++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/stack.c b/stack.c index e3a90d4..e4d02d2 100644 --- a/stack.c +++ b/stack.c @@ -10,6 +10,18 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { + if(!(stack)) { // check if stack is empty + stack = malloc(sizeof(StackNode)); + stack->stackData = data; + stack->below = NULL; + return stack; + } + + StackNode* newStack = malloc(sizeof(StackNode)); + newStack->below = stack; + newStack->stackData = data; + stack = newStack; + return stack; } @@ -17,17 +29,37 @@ StackNode *push(StackNode *stack, void *data) // freed by caller.) StackNode *pop(StackNode *stack) { - + if(stack) { + StackNode* temp = stack; + stack = stack->below; + free(temp->data); + temp->stackData = NULL; + free(temp); + temp = NULL; + return stack; + } else { + return NULL; + } } // Returns the data of the top element. void *top(StackNode *stack) { + if(stack) + return stack->stackData; + else + return NULL; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { + if(stack) { + do { + stack = pop(stack); + } while (stack->below); + pop(stack); + } } \ No newline at end of file diff --git a/stack.h b/stack.h index f7d542d..d15a367 100644 --- a/stack.h +++ b/stack.h @@ -9,6 +9,11 @@ The latest element is taken from the stack. */ //TODO: passenden Datentyp als struct anlegen +typedef struct StackNode { + StackNode* below; + void* stackData; +} StackNode; + // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data); From 0bc9263b60298cba0f41ade9e34cbbf3dac34411 Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Wed, 3 Dec 2025 12:07:15 +0100 Subject: [PATCH 3/7] Started working on tests, added config to makefile --- makefile | 4 +++- test_stack.c | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test_stack.c diff --git a/makefile b/makefile index 1f15f75..b94e026 100644 --- a/makefile +++ b/makefile @@ -35,7 +35,9 @@ $(program_obj_filesobj_files): %.o: %.c # -------------------------- # Unit Tests # -------------------------- -unitTests: +stackTests: stack.o test_stack.c $(unityfolder)/unity.c + $(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c + echo "needs to be implemented" # -------------------------- diff --git a/test_stack.c b/test_stack.c new file mode 100644 index 0000000..c4fe248 --- /dev/null +++ b/test_stack.c @@ -0,0 +1,27 @@ +#include "stack.h" +#include "unity.h" + + +void test_firstNodeAddedCorrectly(void) { + StackNode* modelStack; + int modelData = 5; + modelStack->below = NULL; + modelStack->data = &modelData; + + StackNode* testStack = NULL; + int testData = 5; + testStack = push(testStack, &testData); + TEST_ASSERT_EQUAL_INT(*(modelStack->data), *(testStack->data)); + TEST_ASSERT_NULL(testStack->below); +} + + +int main(void) { + + UNITY_BEGIN(); + + printf("\n----------------------------Stack-Tests----------------------------\n"); + RUN_TEST(test_firstNodeAddedCorrectly); + + return UNITY_END(); +} \ No newline at end of file From 036158fc59945abd480b7febd3ee9e4254212a04 Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Wed, 3 Dec 2025 12:34:47 +0100 Subject: [PATCH 4/7] Fixed some things in stack.c, first test working --- stack.c | 13 ++++++------- stack.h | 2 +- test_stack.c | 16 +++++++++++++--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/stack.c b/stack.c index e4d02d2..61c4c01 100644 --- a/stack.c +++ b/stack.c @@ -12,12 +12,16 @@ StackNode *push(StackNode *stack, void *data) { if(!(stack)) { // check if stack is empty stack = malloc(sizeof(StackNode)); + if(!stack) + return NULL; stack->stackData = data; stack->below = NULL; return stack; } StackNode* newStack = malloc(sizeof(StackNode)); + if(!newStack) + return stack; newStack->below = stack; newStack->stackData = data; stack = newStack; @@ -32,7 +36,6 @@ StackNode *pop(StackNode *stack) if(stack) { StackNode* temp = stack; stack = stack->below; - free(temp->data); temp->stackData = NULL; free(temp); temp = NULL; @@ -55,11 +58,7 @@ void *top(StackNode *stack) // Clears stack and releases all memory. void clearStack(StackNode *stack) { - if(stack) { - do { - stack = pop(stack); - } while (stack->below); - pop(stack); - } + while(stack) + stack = pop(stack); } \ No newline at end of file diff --git a/stack.h b/stack.h index d15a367..faab5e5 100644 --- a/stack.h +++ b/stack.h @@ -10,7 +10,7 @@ The latest element is taken from the stack. */ //TODO: passenden Datentyp als struct anlegen typedef struct StackNode { - StackNode* below; + struct StackNode* below; void* stackData; } StackNode; diff --git a/test_stack.c b/test_stack.c index c4fe248..d21177d 100644 --- a/test_stack.c +++ b/test_stack.c @@ -3,18 +3,28 @@ void test_firstNodeAddedCorrectly(void) { - StackNode* modelStack; + printf("\nStarting first test...\n"); + StackNode* modelStack = malloc(sizeof(StackNode)); int modelData = 5; modelStack->below = NULL; - modelStack->data = &modelData; + modelStack->stackData = &modelData; StackNode* testStack = NULL; int testData = 5; testStack = push(testStack, &testData); - TEST_ASSERT_EQUAL_INT(*(modelStack->data), *(testStack->data)); + TEST_ASSERT_EQUAL_INT(*(int*)modelStack->stackData, *(int*)testStack->stackData); TEST_ASSERT_NULL(testStack->below); + clearStack(modelStack); + clearStack(testStack); } +void setUp(void) { + +} + +void tearDown(void) { + +} int main(void) { From c663833b6e6f0c9448a3a5378aefc74e52f12807 Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Wed, 3 Dec 2025 12:41:05 +0100 Subject: [PATCH 5/7] Added second test, not yet running fully --- test_stack.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test_stack.c b/test_stack.c index d21177d..e352d68 100644 --- a/test_stack.c +++ b/test_stack.c @@ -18,6 +18,21 @@ void test_firstNodeAddedCorrectly(void) { clearStack(testStack); } +void test_topReturnsCorrectValues(void) { + printf("Starting second test...\n"); + StackNode* testStack = NULL; + int data1 = 1; + testStack = push(testStack, &data1); + int data2 = 0; + testStack = push(testStack, &data2); + int data3 = 3; + testStack = push(testStack, &data3); + TEST_ASSERT_EQUAL_INT(1, *(int*)testStack->stackData); + TEST_ASSERT_EQUAL_INT(0, *(int*)testStack->stackData); + TEST_ASSERT_EQUAL_INT(3, *(int*)testStack->stackData); + clearStack(testStack); +} + void setUp(void) { } @@ -32,6 +47,7 @@ int main(void) { printf("\n----------------------------Stack-Tests----------------------------\n"); RUN_TEST(test_firstNodeAddedCorrectly); + RUN_TEST(test_topReturnsCorrectValues); return UNITY_END(); } \ No newline at end of file From bf4c41b8977840ed269e02d1f73c72032a4e2079 Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Wed, 3 Dec 2025 13:30:29 +0100 Subject: [PATCH 6/7] Test 2 working now, only one test missing --- test_stack.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test_stack.c b/test_stack.c index e352d68..3b4fc48 100644 --- a/test_stack.c +++ b/test_stack.c @@ -23,13 +23,22 @@ void test_topReturnsCorrectValues(void) { StackNode* testStack = NULL; int data1 = 1; testStack = push(testStack, &data1); + int* returnData1 = (int*) top(testStack); int data2 = 0; testStack = push(testStack, &data2); + int* returnData2 = (int*) top(testStack); int data3 = 3; testStack = push(testStack, &data3); - TEST_ASSERT_EQUAL_INT(1, *(int*)testStack->stackData); - TEST_ASSERT_EQUAL_INT(0, *(int*)testStack->stackData); - TEST_ASSERT_EQUAL_INT(3, *(int*)testStack->stackData); + int* returnData3 = (int*) top(testStack); + + size_t counter = 0; + if(*returnData1 == 1) + counter++; + if(*returnData2 == 0) + counter++; + if(*returnData3 == 3) + counter++; + TEST_ASSERT_EQUAL_INT(3, counter); clearStack(testStack); } From 288385a2200343e38d90e1a25197000cce82ff12 Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Wed, 3 Dec 2025 13:37:44 +0100 Subject: [PATCH 7/7] Third test working now, everything should be ready for merge to main --- test_stack.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test_stack.c b/test_stack.c index 3b4fc48..79d85cc 100644 --- a/test_stack.c +++ b/test_stack.c @@ -42,6 +42,40 @@ void test_topReturnsCorrectValues(void) { clearStack(testStack); } +void test_popRemovesCorrectly(void) { + printf("Starting third test...\n"); + StackNode* testStack = NULL; + int data1 = 1; + testStack = push(testStack, &data1); + int data2 = 0; + testStack = push(testStack, &data2); + int data3 = 3; + testStack = push(testStack, &data3); + int data4 = 9; + testStack = push(testStack, &data4); + + int* returnData1 = (int*) top(testStack); + testStack = pop(testStack); + int* returnData2 = (int*) top(testStack); + testStack = pop(testStack); + int* returnData3 = (int*) top(testStack); + testStack = pop(testStack); + int* returnData4 = (int*) top(testStack); + testStack = pop(testStack); + + size_t counter = 0; + if(*returnData1 == 9) + counter++; + if(*returnData2 == 3) + counter++; + if(*returnData3 == 0) + counter++; + if(*returnData4 == 1) + counter++; + TEST_ASSERT_EQUAL_INT(4, counter); + clearStack(testStack); +} + void setUp(void) { } @@ -57,6 +91,7 @@ int main(void) { printf("\n----------------------------Stack-Tests----------------------------\n"); RUN_TEST(test_firstNodeAddedCorrectly); RUN_TEST(test_topReturnsCorrectValues); + RUN_TEST(test_popRemovesCorrectly); return UNITY_END(); } \ No newline at end of file