From 2c10463852c9dcef060a8ab1f5d47ce5e3373c4e Mon Sep 17 00:00:00 2001 From: Timo Hertel Date: Sun, 23 Nov 2025 16:07:25 +0100 Subject: [PATCH 1/5] Implement stack, basic test program & makefile entry for the test --- I2_Dobble/makefile | 10 ++++-- I2_Dobble/stack.c | 33 ++++++++++++++--- I2_Dobble/stack.h | 7 +++- I2_Dobble/test_stack.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 I2_Dobble/test_stack.c diff --git a/I2_Dobble/makefile b/I2_Dobble/makefile index 1f15f75..e855199 100644 --- a/I2_Dobble/makefile +++ b/I2_Dobble/makefile @@ -30,8 +30,14 @@ doble : main.o $(program_obj_files) $(CC) $(FLAGS) $^ -o doble $(program_obj_filesobj_files): %.o: %.c - $(CC) -c $(FLAGS) $^ -o $@ + $(CC) -c $(FLAGS) -g3 -fPIC $^ -o $@ +# -------------------------- +# Stack Tests +# -------------------------- +stackTests : stack.o test_stack.c + $(CC) $(FLAGS) -g3 $^ -o stackTest + # -------------------------- # Unit Tests # -------------------------- @@ -46,4 +52,4 @@ ifeq ($(OS),Windows_NT) del /f *.o doble else rm -f *.o doble -endif \ No newline at end of file +endif diff --git a/I2_Dobble/stack.c b/I2_Dobble/stack.c index e3a90d4..6c3c54b 100644 --- a/I2_Dobble/stack.c +++ b/I2_Dobble/stack.c @@ -10,24 +10,47 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { - + // ,-→ Wenn keine Daten angegeben wird die Liste unverändert zurückgegeben; Ist das sinnvoll? kein Anzeichen, kein Rückgabewert... + if (data != NULL) + { + StackNode *newNode; + newNode = (StackNode *)malloc(sizeof(StackNode)); + newNode->value = data; + + // ,-→ bedeutet Liste war leer - das neue Element hat keinen Nachfolger (next = NULL) + if (stack == NULL) { + newNode->next = NULL; + } else { + newNode->next = stack; + } + stack = newNode; + } + return stack; } // 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) { - + if(stack != NULL) + { + StackNode *nextNode = stack->next; + free(stack); + stack = nextNode; + } + return stack; } // Returns the data of the top element. void *top(StackNode *stack) { - + return stack->value; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { - -} \ No newline at end of file + while (stack != NULL) { + stack = pop(stack); + } +} diff --git a/I2_Dobble/stack.h b/I2_Dobble/stack.h index f7d542d..d1ca13c 100644 --- a/I2_Dobble/stack.h +++ b/I2_Dobble/stack.h @@ -7,7 +7,12 @@ The latest element is taken from the stack. */ #include -//TODO: passenden Datentyp als struct anlegen +//DONE: passenden Datentyp als struct anlegen +typedef struct Node +{ + void *value; + struct Node* next; +} StackNode; // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data); diff --git a/I2_Dobble/test_stack.c b/I2_Dobble/test_stack.c new file mode 100644 index 0000000..60fabd8 --- /dev/null +++ b/I2_Dobble/test_stack.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include "stack.h" + +void inspectStack(StackNode *stack) +{ + if (stack != NULL) + { + printf("Der Stack enthält die folgenden Elemente: %d", *(int*)stack->value); + while (stack->next != NULL) + { + printf(" %d", *(int*)stack->next->value); + stack = stack->next; + } + putchar('\n'); + } + else + { + printf("Der Stack ist leer\n"); + } +} + +int main() +{ + StackNode *stack; + stack = NULL; // initialisierung mit NULL -> leere Liste + + printf("...ein Element wird eingefügt...\n"); + int toBeRemoved = 42; + stack = push(stack, &toBeRemoved); + + inspectStack(stack); + + printf("...das Element wird wieder entfernt...\n"); + stack = pop(stack); + + inspectStack(stack); + + printf("...pop auf leeren Stack...\n"); + stack = pop(stack); + + inspectStack(stack); + putchar('\n'); + + int data[5] = {1, 2, 3, 4, 5}; + + // alle 5 werte der reihe nach auf den Satck legen - 1 unten ... 5 oben + for (int i = 0; i < 5; i++) + { + stack = push(stack, &data[i]); + } + + + //alle Elemente mit Test-Funktion ausgeben + inspectStack(stack); + + // Elemente stück für Stück ausgeben und entfernen + printf("1. Element war: %d\n", *(int*)top(stack)); + stack = pop(stack); + printf("2. Element war: %d\n", *(int*)top(stack)); + stack = pop(stack); + printf("3. Element war: %d\n", *(int*)top(stack)); + stack = pop(stack); + printf("4. Element war: %d\n", *(int*)top(stack)); + stack = pop(stack); + printf("5. Element war: %d\n", *(int*)top(stack)); +} + +/* +int main() +{ + UNITY_BEGIN(); + + printf("============================\n Stack tests\n============================\n"); + RUN_TEST(test_createNodeFailsOnZeroData); + + return UNITY_END(); +} +*/ From 0e7802fb30ca369426b1217e94d4e7cec2c99788 Mon Sep 17 00:00:00 2001 From: Timo Hertel Date: Tue, 2 Dec 2025 14:19:37 +0100 Subject: [PATCH 2/5] =?UTF-8?q?Unit=20Tests=20f=C3=BCr=20den=20Stack=20ers?= =?UTF-8?q?tellt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- I2_Dobble/stack.c | 17 +++-- I2_Dobble/stack.h | 2 +- I2_Dobble/stackTest.c | 167 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 I2_Dobble/stackTest.c diff --git a/I2_Dobble/stack.c b/I2_Dobble/stack.c index 6c3c54b..2bc5825 100644 --- a/I2_Dobble/stack.c +++ b/I2_Dobble/stack.c @@ -1,7 +1,7 @@ #include #include "stack.h" -//TODO: grundlegende Stackfunktionen implementieren: +//DONE: grundlegende Stackfunktionen implementieren: /* * `push`: legt ein Element oben auf den Stack, * `pop`: entfernt das oberste Element, * `top`: liefert das oberste Element zurück, @@ -10,12 +10,12 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { - // ,-→ Wenn keine Daten angegeben wird die Liste unverändert zurückgegeben; Ist das sinnvoll? kein Anzeichen, kein Rückgabewert... + // ,-→ Wenn keine Daten angegeben wird die Liste unverändert zurückgegeben if (data != NULL) { StackNode *newNode; newNode = (StackNode *)malloc(sizeof(StackNode)); - newNode->value = data; + newNode->data = data; // ,-→ bedeutet Liste war leer - das neue Element hat keinen Nachfolger (next = NULL) if (stack == NULL) { @@ -25,11 +25,12 @@ StackNode *push(StackNode *stack, void *data) } stack = newNode; } + return stack; } -// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be -// freed by caller.) +// 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) { if(stack != NULL) @@ -44,7 +45,11 @@ StackNode *pop(StackNode *stack) // Returns the data of the top element. void *top(StackNode *stack) { - return stack->value; + if (stack != NULL) { + return stack->data; + } else { + return NULL; + } } // Clears stack and releases all memory. diff --git a/I2_Dobble/stack.h b/I2_Dobble/stack.h index d1ca13c..641b400 100644 --- a/I2_Dobble/stack.h +++ b/I2_Dobble/stack.h @@ -10,7 +10,7 @@ The latest element is taken from the stack. */ //DONE: passenden Datentyp als struct anlegen typedef struct Node { - void *value; + void *data; struct Node* next; } StackNode; diff --git a/I2_Dobble/stackTest.c b/I2_Dobble/stackTest.c new file mode 100644 index 0000000..76173dc --- /dev/null +++ b/I2_Dobble/stackTest.c @@ -0,0 +1,167 @@ +#include "stack.h" +#include "unity/unity.h" +#include "unity/unity_internals.h" + +/* wird nicht mehr gebraucht + +// Ein Blick in den Stack - listet den Stack-Inhalt der Reihe nach auf +void inspectStack(StackNode *stack) +{ + if (stack != NULL) + { + printf("Der Stack enthält die folgenden Elemente: %d", *(int*)stack->data); + while (stack->next != NULL) + { + printf(" %d", *(int*)stack->next->data); + stack = stack->next; + } + putchar('\n'); + } + else + { + printf("Der Stack ist leer\n"); + } +} +*/ + +// Setup-Funktionen von Unity, die zumindest als Platzhalter definiert sein müssen +void setUp(void) {}; +void tearDown(void) {}; + +void test_createNodeAbortsOnZeroData(void) +{ + void *data = NULL; + StackNode *stack = NULL; + stack = push(stack, data); + TEST_ASSERT_EQUAL_PTR(stack, NULL); + + clearStack(stack); +} + +void test_createdNodeIsFirstInList(void) +{ + int data = 42; + StackNode *stack = NULL; + stack = push(stack, &data); + TEST_ASSERT_NOT_NULL(stack); + TEST_ASSERT_EQUAL_PTR(&data, stack->data); + TEST_ASSERT_NULL(stack->next); + + clearStack(stack); +} + +void test_createNodeCorrectOrder(void) +{ + int data[3] = {0, 1, 2}; + StackNode *stack = NULL; + stack = push(stack, &data[2]); + stack = push(stack, &data[1]); + stack = push(stack, &data[0]); + + TEST_ASSERT_NOT_NULL(stack); + TEST_ASSERT_EQUAL_PTR(&data[0], stack->data); + TEST_ASSERT_EQUAL_PTR(&data[1], stack->next->data); + TEST_ASSERT_EQUAL_PTR(&data[2], stack->next->next->data); + TEST_ASSERT_NULL(stack->next->next->next); + + clearStack(stack); +} + +void test_removeNodeAbortsOnZero(void) +{ + StackNode *stack = NULL; + TEST_ASSERT_NULL(pop(stack)); + + clearStack(stack); +} + +void test_removeOnlyNodeEmptysList(void) +{ + int data = 42; + StackNode *stack = NULL; + stack = pop(push(stack, &data)); + + TEST_ASSERT_NULL(stack); + + clearStack(stack); +} + +void test_removeNodeRemovesFirst(void) +{ + int data[2] = {0, 1}; + StackNode *stack = NULL; + stack = push(stack, &data[1]); + stack = push(stack, &data[0]); + stack = pop(stack); + + TEST_ASSERT_NOT_NULL(stack); + TEST_ASSERT_EQUAL_PTR(&data[1], stack->data); + TEST_ASSERT_NULL(stack->next); + + clearStack(stack); +} + +void test_outputTopFailsOnZero(void) +{ + StackNode *stack = NULL; + + TEST_ASSERT_NULL(top(stack)); + + clearStack(stack); +} + +void test_outputTopCorrect(void) +{ + int data[2] = {0, 1}; + StackNode *stack = NULL; + stack = push(stack, &data[1]); + stack = push(stack, &data[0]); + + TEST_ASSERT_NOT_NULL(stack); + TEST_ASSERT_EQUAL_PTR(top(stack), stack->data); + + clearStack(stack); +} + + +/* Speicher freigabe lässt sich nicht mit Unity testen, am besten Programm mit valgrind ausführen +void test_clearStackFreesMemory(void) +{ + int data[2] = {22, 17}; + StackNode *lowNode = (StackNode *)malloc(sizeof(StackNode)); + lowNode->next = NULL; + lowNode->data = &data[0]; + StackNode *highNode = (StackNode *)malloc(sizeof(StackNode)); + highNode->next = lowNode; + highNode->data = &data[1]; + StackNode *stack = highNode; + + clearStack(stack); + + TEST_ASSERT_NULL(lowNode); + TEST_ASSERT_NULL(highNode); + TEST_ASSERT_NULL(stack); +} +*/ + +int main() +{ + UNITY_BEGIN(); + + printf("\n============================\n Stack tests\n============================\n"); + printf("-> Create Nodes (push)\n"); + RUN_TEST(test_createNodeAbortsOnZeroData); + RUN_TEST(test_createdNodeIsFirstInList); + RUN_TEST(test_createNodeCorrectOrder); + + printf("\n-> Remove Nodes (pop)\n"); + RUN_TEST(test_removeNodeAbortsOnZero); + RUN_TEST(test_removeOnlyNodeEmptysList); + RUN_TEST(test_removeNodeRemovesFirst); + + printf("\n-> Output Top (top)\n"); + RUN_TEST(test_outputTopFailsOnZero); + RUN_TEST(test_outputTopCorrect); + + return UNITY_END(); +} \ No newline at end of file From 7d54dac71acf336e2b4628e92c9ecc2fcd3f2bae Mon Sep 17 00:00:00 2001 From: Timo Hertel Date: Tue, 2 Dec 2025 13:36:28 +0000 Subject: [PATCH 3/5] Dobble makefile in die Spur gebracht --- I2_Dobble/makefile | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/I2_Dobble/makefile b/I2_Dobble/makefile index e855199..918e956 100644 --- a/I2_Dobble/makefile +++ b/I2_Dobble/makefile @@ -1,5 +1,6 @@ CC = gcc -FLAGS = -g -Wall -lm +CFLAGS = -g -Wall +LDFLAGS = -lm ifeq ($(OS),Windows_NT) include makefile_windows.variables @@ -27,29 +28,33 @@ doble_initial: program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o doble : main.o $(program_obj_files) - $(CC) $(FLAGS) $^ -o doble + $(CC) $(FLAGS) $(LDFLAGS) $^ -o doble $(program_obj_filesobj_files): %.o: %.c - $(CC) -c $(FLAGS) -g3 -fPIC $^ -o $@ + $(CC) -c $(FLAGS) $^ -o $@ -# -------------------------- -# Stack Tests -# -------------------------- -stackTests : stack.o test_stack.c - $(CC) $(FLAGS) -g3 $^ -o stackTest - # -------------------------- # Unit Tests # -------------------------- -unitTests: - echo "needs to be implemented" +unitTests: numbersTest + ./runNumbersTest + ./runStackTest + +numbersTest: numbers.o bintree.o numbersTest.o $($(unityfolder)/unity.c) + $(CC) $(FLAGS) $(LDFLAGS) $^ -o runNumbersTest + +stackTest: stack.o stackTest.o $($(unityfolder)/unity.c) + $(CC) $(FLAGS) $(LDFLAGS) $^ -o runStackTest + +%.o: %.c + $(CC) -c $(FLAGS) $< -o $@ # -------------------------- # Clean # -------------------------- clean: ifeq ($(OS),Windows_NT) - del /f *.o doble + del /f *.o doble runNumbersTest else - rm -f *.o doble -endif + rm -f *.o doble runNumbersTest +endif \ No newline at end of file From 8151f21b3817cac6df534c59072be114b9d758ab Mon Sep 17 00:00:00 2001 From: Timo Hertel Date: Tue, 2 Dec 2025 13:42:29 +0000 Subject: [PATCH 4/5] =?UTF-8?q?Target=20clean=20im=20Dobble=20Makefile=20f?= =?UTF-8?q?=C3=BCr=20stackTest=20aktualisiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- I2_Dobble/makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/I2_Dobble/makefile b/I2_Dobble/makefile index 918e956..34b3905 100644 --- a/I2_Dobble/makefile +++ b/I2_Dobble/makefile @@ -54,7 +54,7 @@ stackTest: stack.o stackTest.o $($(unityfolder)/unity.c) # -------------------------- clean: ifeq ($(OS),Windows_NT) - del /f *.o doble runNumbersTest + del /f *.o doble runNumbersTest runStackTest else - rm -f *.o doble runNumbersTest + rm -f *.o doble runNumbersTest runStackTest endif \ No newline at end of file From eb868c356a17daf0465c71233e53c5dfff8b37df Mon Sep 17 00:00:00 2001 From: Timo Hertel Date: Tue, 2 Dec 2025 14:56:06 +0100 Subject: [PATCH 5/5] =?UTF-8?q?Letzte=20Fixes=20am=20Makefile,=20um=20mit?= =?UTF-8?q?=20Main=20mergen=20zu=20k=C3=B6nnen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- I2_Dobble/makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/I2_Dobble/makefile b/I2_Dobble/makefile index 34b3905..b60440c 100644 --- a/I2_Dobble/makefile +++ b/I2_Dobble/makefile @@ -36,14 +36,14 @@ $(program_obj_filesobj_files): %.o: %.c # -------------------------- # Unit Tests # -------------------------- -unitTests: numbersTest +unitTests: numbersTest stackTest ./runNumbersTest ./runStackTest -numbersTest: numbers.o bintree.o numbersTest.o $($(unityfolder)/unity.c) +numbersTest: numbers.o bintree.o numbersTest.o $(unityfolder)/unity.c $(CC) $(FLAGS) $(LDFLAGS) $^ -o runNumbersTest -stackTest: stack.o stackTest.o $($(unityfolder)/unity.c) +stackTest: stack.o stackTest.o $(unityfolder)/unity.c $(CC) $(FLAGS) $(LDFLAGS) $^ -o runStackTest %.o: %.c