106 lines
2.2 KiB
C
106 lines
2.2 KiB
C
#include "stack.h"
|
|
#include "unity.h"
|
|
#include "unity_internals.h"
|
|
|
|
void setUp(void) { }
|
|
void tearDown(void) { }
|
|
|
|
void test_push_should_add_new_node_at_top(void)
|
|
{
|
|
StackNode *stack = NULL;
|
|
|
|
int value = 42;
|
|
stack = push(stack, &value);
|
|
|
|
TEST_ASSERT_NOT_NULL(stack);
|
|
TEST_ASSERT_EQUAL_PTR(&value, stack->data);
|
|
TEST_ASSERT_NULL(stack->next);
|
|
|
|
clearStack(stack);
|
|
}
|
|
|
|
void test_push_multiple_should_chain_nodes_correctly(void)
|
|
{
|
|
int a = 1, b = 2;
|
|
|
|
StackNode *first = push(NULL, &a);
|
|
|
|
StackNode *second = push(first, &b);
|
|
|
|
TEST_ASSERT_EQUAL_PTR(&b, second->data);
|
|
TEST_ASSERT_EQUAL_PTR(first, second->next);
|
|
TEST_ASSERT_EQUAL_PTR(&a, first->data);
|
|
|
|
clearStack(second);
|
|
}
|
|
|
|
void test_top_should_return_null_on_empty_stack(void)
|
|
{
|
|
StackNode *stack = NULL;
|
|
TEST_ASSERT_NULL(top(stack));
|
|
}
|
|
|
|
void test_top_should_return_data_of_existing_node(void)
|
|
{
|
|
StackNode node;
|
|
int x = 99;
|
|
|
|
node.data = &x;
|
|
node.next = NULL;
|
|
|
|
TEST_ASSERT_EQUAL_PTR(&x, top(&node));
|
|
}
|
|
|
|
void test_pop_should_return_null_when_stack_empty(void)
|
|
{
|
|
StackNode *stack = NULL;
|
|
TEST_ASSERT_NULL(pop(stack));
|
|
}
|
|
|
|
void test_pop_should_remove_single_element(void)
|
|
{
|
|
StackNode *stack = malloc(sizeof(StackNode));
|
|
int x = 7;
|
|
|
|
stack->data = &x;
|
|
stack->next = NULL;
|
|
|
|
StackNode *result = pop(stack);
|
|
|
|
TEST_ASSERT_NULL(result); // Kein Element mehr übrig
|
|
}
|
|
|
|
void test_pop_should_remove_top_node_only(void)
|
|
{
|
|
// Manuell verkettete Liste aufbauen
|
|
StackNode *n1 = malloc(sizeof(StackNode));
|
|
StackNode *n2 = malloc(sizeof(StackNode));
|
|
int a = 1, b = 2;
|
|
|
|
n1->data = &a;
|
|
n1->next = NULL;
|
|
|
|
n2->data = &b;
|
|
n2->next = n1;
|
|
|
|
StackNode *result = pop(n2);
|
|
|
|
TEST_ASSERT_EQUAL_PTR(n1, result);
|
|
|
|
clearStack(result);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
UNITY_BEGIN();
|
|
|
|
RUN_TEST(test_push_should_add_new_node_at_top);
|
|
RUN_TEST(test_push_multiple_should_chain_nodes_correctly);
|
|
RUN_TEST(test_top_should_return_null_on_empty_stack);
|
|
RUN_TEST(test_top_should_return_data_of_existing_node);
|
|
RUN_TEST(test_pop_should_return_null_when_stack_empty);
|
|
RUN_TEST(test_pop_should_remove_single_element);
|
|
RUN_TEST(test_pop_should_remove_top_node_only);
|
|
|
|
return UNITY_END();
|
|
} |