From 0d561c017505dbc1bce0c2f68ed82fca55633d57 Mon Sep 17 00:00:00 2001 From: Jonas Date: Fri, 21 Nov 2025 12:35:12 +0100 Subject: [PATCH] added stackTest --- I2_Dobble/stackTest.c | 93 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/I2_Dobble/stackTest.c b/I2_Dobble/stackTest.c index d58dc45..75ef216 100644 --- a/I2_Dobble/stackTest.c +++ b/I2_Dobble/stackTest.c @@ -5,9 +5,102 @@ 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(); } \ No newline at end of file