stack bugfix
This commit is contained in:
parent
47a158ca14
commit
1b022d5b2e
56
test_stack.c
56
test_stack.c
@ -24,26 +24,22 @@ int main(void)
|
|||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void test_push_and_top(void)
|
void test_push_and_top(void)
|
||||||
{
|
{
|
||||||
StackNode *stack = NULL;
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
int *a = malloc(sizeof(int)); *a = 10;
|
int a = 10;
|
||||||
int *b = malloc(sizeof(int)); *b = 20;
|
int b = 20;
|
||||||
int *c = malloc(sizeof(int)); *c = 30;
|
int c = 30;
|
||||||
|
|
||||||
stack = push(stack, a);
|
stack = push(stack, &a);
|
||||||
stack = push(stack, b);
|
stack = push(stack, &b);
|
||||||
stack = push(stack, c);
|
stack = push(stack, &c);
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_INT(30, *(int*)top(stack));
|
TEST_ASSERT_EQUAL_INT(30, *(int*)top(stack));
|
||||||
|
|
||||||
// Cleanup data manually
|
clearStack(&stack);
|
||||||
void *data;
|
|
||||||
stack = pop(stack, &data); free(data);
|
|
||||||
stack = pop(stack, &data); free(data);
|
|
||||||
stack = pop(stack, &data); free(data);
|
|
||||||
|
|
||||||
TEST_ASSERT_NULL(stack);
|
TEST_ASSERT_NULL(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,22 +47,18 @@ void test_pop(void)
|
|||||||
{
|
{
|
||||||
StackNode *stack = NULL;
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
int *x = malloc(sizeof(int)); *x = 111;
|
int x = 111;
|
||||||
int *y = malloc(sizeof(int)); *y = 222;
|
int y = 222;
|
||||||
|
|
||||||
stack = push(stack, x);
|
stack = push(stack, &x);
|
||||||
stack = push(stack, y);
|
stack = push(stack, &y);
|
||||||
|
|
||||||
void *data;
|
// pop removes y
|
||||||
|
stack = pop(stack);
|
||||||
stack = pop(stack, &data);
|
TEST_ASSERT_EQUAL_INT(111, *(int*)top(stack));
|
||||||
TEST_ASSERT_EQUAL_INT(222, *(int*)data);
|
|
||||||
free(data);
|
|
||||||
|
|
||||||
stack = pop(stack, &data);
|
|
||||||
TEST_ASSERT_EQUAL_INT(111, *(int*)data);
|
|
||||||
free(data);
|
|
||||||
|
|
||||||
|
// pop removes x
|
||||||
|
stack = pop(stack);
|
||||||
TEST_ASSERT_NULL(stack);
|
TEST_ASSERT_NULL(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,17 +66,11 @@ void test_clearStack(void)
|
|||||||
{
|
{
|
||||||
StackNode *stack = NULL;
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
int *p = malloc(sizeof(int)); *p = 7;
|
int x = 5;
|
||||||
int *q = malloc(sizeof(int)); *q = 8;
|
int y = 6;
|
||||||
|
|
||||||
stack = push(stack, p);
|
stack = push(stack, &x);
|
||||||
stack = push(stack, q);
|
stack = push(stack, &y);
|
||||||
|
|
||||||
// Pop to free data first!
|
|
||||||
void *data;
|
|
||||||
|
|
||||||
stack = pop(stack, &data); free(data);
|
|
||||||
stack = pop(stack, &data); free(data);
|
|
||||||
|
|
||||||
clearStack(&stack);
|
clearStack(&stack);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user