From f66f6179ce1fbb9d21dc13723430342ae7d194f7 Mon Sep 17 00:00:00 2001 From: Simon Wiesend Date: Thu, 11 Dec 2025 20:18:13 +0100 Subject: [PATCH] more stack tests and fix in stack --- stack.c | 6 ++++-- test_stack.c | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/stack.c b/stack.c index 21bc819..f3a815a 100644 --- a/stack.c +++ b/stack.c @@ -50,6 +50,8 @@ void *top(StackNode *stack) // Clears stack and releases all memory. void clearStack(StackNode *stack) { - while (pop(stack)) - ; + while (stack) + { + stack = pop(stack); + } } \ No newline at end of file diff --git a/test_stack.c b/test_stack.c index e9843ab..531e499 100644 --- a/test_stack.c +++ b/test_stack.c @@ -15,6 +15,7 @@ void setUp(void) void tearDown(void) { clearStack(stack); + stack = NULL; } void test_push_and_pop(void) @@ -31,17 +32,33 @@ void test_push_and_pop(void) stack = pop(stack); } +// pop and top should return NULL if called with NULL ptr void test_handle_NULL(void) { TEST_ASSERT_NULL(pop(stack)); TEST_ASSERT_NULL(top(stack)); } +void test_top(void) +{ + TEST_ASSERT_NULL(top(stack)); + stack = push(stack, &data1); + TEST_ASSERT_EQUAL_PTR(top(stack), &data1); + TEST_ASSERT_EQUAL_INT(*(int *)top(stack), data1); + stack = push(stack, &data2); + TEST_ASSERT_EQUAL_PTR(top(stack), &data2); + TEST_ASSERT_EQUAL_INT(*(int *)top(stack), data2); + stack = push(stack, &data3); + TEST_ASSERT_EQUAL_PTR(top(stack), &data3); + TEST_ASSERT_EQUAL_INT(*(int *)top(stack), data3); +} + int main(void) { printf("============================\nStack tests\n============================\n"); UNITY_BEGIN(); RUN_TEST(test_push_and_pop); RUN_TEST(test_handle_NULL); + RUN_TEST(test_top); return UNITY_END(); } \ No newline at end of file