Compare commits

...

8 Commits

Author SHA1 Message Date
7127fe6d3f makefile verändert 2025-12-11 11:55:13 +01:00
44434a8c67 makefile verändert 2025-12-11 11:54:52 +01:00
5ee0f97dc6 test 1 2025-12-11 11:42:46 +01:00
6dd963c40e makefile verändert 2025-12-11 11:40:17 +01:00
3b0a8a24ff weitere test funktionen bearbeitet 2025-12-11 11:25:45 +01:00
8f4ab58000 test_pop überarbeitet 2025-12-11 11:23:31 +01:00
be37b3ff21 Unity Tests für stack.c geschrieben 2025-12-11 11:15:44 +01:00
1c1ba9e471 stack.c funktion und typedef struct in .h geschrieben 2025-12-11 10:42:52 +01:00
6 changed files with 116 additions and 2 deletions

View File

@ -35,8 +35,10 @@ $(program_obj_filesobj_files): %.o: %.c
# --------------------------
# Unit Tests
# --------------------------
unitTests:
echo "needs to be implemented"
unitTestsStack: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) $^ -o $@
# --------------------------
# Clean

30
stack.c
View File

@ -10,24 +10,54 @@
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data)
{
// Neues Stack-Element erstellen
StackNode *newNode = malloc(sizeof(StackNode));
if (!newNode) {
return stack; // oder NULL, je nach Fehlerstrategie
}
newNode->data = data;
newNode->next = stack; // bisheriger Stack wird nach unten geschoben
return newNode; // neuer Kopf des Stacks
}
// 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)
return NULL;
StackNode *newTop = stack->next;
// Daten gehen verloren!
// Caller KANN sie nicht freigeben.
free(stack);
return newTop;
}
// Returns the data of the top element.
void *top(StackNode *stack)
{
if (stack == NULL)
return NULL; // kein Element im Stack
return stack->data;
}
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
StackNode *current = stack;
while (current != NULL)
{
StackNode *next = current->next;
free(current);
current = next;
}
}

View File

@ -8,6 +8,10 @@ The latest element is taken from the stack. */
#include <stdlib.h>
//TODO: passenden Datentyp als struct anlegen
typedef struct Node {
void *data;
struct Node *next;
} StackNode;
// Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data);

BIN
stack.o Normal file

Binary file not shown.

78
test_stack.c Normal file
View File

@ -0,0 +1,78 @@
#include <stdlib.h>
#include "stack.h"
#include "unity.h"
void test_push_and_top(void);
void test_pop(void);
void test_clearStack(void);
void setUp(void) {}
void tearDown(void) {}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_push_and_top);
RUN_TEST(test_pop);
RUN_TEST(test_clearStack);
return UNITY_END();
}
void test_push_and_top(void)
{
StackNode *stack = NULL;
int a = 10;
int b = 20;
int c = 30;
stack = push(stack, &a);
stack = push(stack, &b);
stack = push(stack, &c);
TEST_ASSERT_EQUAL_INT(30, *(int*)top(stack));
clearStack(stack);
TEST_ASSERT_NULL(stack);
}
void test_pop(void)
{
StackNode *stack = NULL;
int x = 111;
int y = 222;
stack = push(stack, &x);
stack = push(stack, &y);
// pop removes y
stack = pop(stack);
TEST_ASSERT_EQUAL_INT(111, *(int*)top(stack));
// pop removes x
stack = pop(stack);
TEST_ASSERT_NULL(stack);
}
void test_clearStack(void)
{
StackNode *stack = NULL;
int x = 5;
int y = 6;
stack = push(stack, &x);
stack = push(stack, &y);
clearStack(stack);
TEST_ASSERT_NULL(stack);
}

BIN
unitTestsStack.exe Normal file

Binary file not shown.