more stack tests and fix in stack
This commit is contained in:
parent
91f5c52077
commit
f66f6179ce
6
stack.c
6
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);
|
||||
}
|
||||
}
|
||||
17
test_stack.c
17
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();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user