more stack tests and fix in stack

This commit is contained in:
Simon Wiesend 2025-12-11 20:18:13 +01:00
parent 91f5c52077
commit f66f6179ce
Signed by untrusted user: wiesendsi102436
GPG Key ID: C18A833054142CF0
2 changed files with 21 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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();
}