added stackTest

This commit is contained in:
Jonas Urban 2025-11-21 12:35:12 +01:00
parent 3fdc249d86
commit 5857955dc0

View File

@ -1,5 +1,4 @@
#include "stack.h" #include "stack.h"
<<<<<<< HEAD
#include "unity/unity.h" #include "unity/unity.h"
#include "unity/unity_internals.h" #include "unity/unity_internals.h"
@ -144,18 +143,97 @@ void test_clearStackFreesMemory(void)
TEST_ASSERT_NULL(stack); TEST_ASSERT_NULL(stack);
} }
*/ */
=======
#include "unity.h"
#include "unity_internals.h"
void setUp(void) { } void test_push_should_add_new_node_at_top(void)
void tearDown(void) { } {
>>>>>>> 0c075ea (stackTest file added) 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() int main()
{ {
UNITY_BEGIN(); UNITY_BEGIN();
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
printf("\n============================\n Stack tests\n============================\n"); printf("\n============================\n Stack tests\n============================\n");
printf("-> Create Nodes (push)\n"); printf("-> Create Nodes (push)\n");
@ -174,5 +252,15 @@ int main()
======= =======
>>>>>>> 0c075ea (stackTest file added) >>>>>>> 0c075ea (stackTest file added)
=======
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);
>>>>>>> 0d561c0 (added stackTest)
return UNITY_END(); return UNITY_END();
} }