#include #include "stack.h" #include "unity/unity.h" static int tests_passed = 0; static int tests_failed = 0; static void report_result(const char *test_name, int passed) { if (passed) { printf("[PASS] %s\n", test_name); tests_passed++; } else { printf("[FAIL] %s\n", test_name); tests_failed++; } } static void test_push_single_element(void) { StackNode *stack = NULL; int value = 10; stack = push(stack, &value); int passed = stack != NULL && top(stack) != NULL && *(int *) top(stack) == 10; //liefert void pointer deswegen int* zum konvertieren und dann * um auf den wert zu derefernzieren der dort liegt TEST_ASSERT_TRUE(passed); report_result("Push single element", passed); clearStack(stack); } static void test_push_multiple_elements(void) { StackNode *stack = NULL; int a = 10, b = 20, c = 30; stack = push(stack, &a); //übergebe adressen weil push void pointer erwartet stack = push(stack, &b); stack = push(stack, &c); int passed = stack != NULL && stack->data == &c && stack->next != NULL && stack->next->data == &b && stack->next->next != NULL && stack->next->next->data == &a; report_result("Push multiple elements", passed); clearStack(stack); } static void test_push_null(void) { StackNode *stack = NULL; stack = push(stack, NULL); report_result("Push NULL value", stack == NULL); clearStack(stack); } static void test_top_empty_stack(void) { StackNode *stack = NULL; report_result("Top on empty stack", top(stack) == NULL); } static void test_pop_single_element(void) { StackNode *stack = NULL; int value = 10; stack = push(stack, &value); stack = pop(stack); report_result("Pop single element", stack == NULL); clearStack(stack); } static void test_pop_multiple_elements(void) { StackNode *stack = NULL; int a = 10, b = 20; stack = push(stack, &a); stack = push(stack, &b); stack = pop(stack); int passed = stack != NULL && top(stack) != NULL && *(int *) top(stack) == 10; stack = pop(stack); passed = passed && stack == NULL; report_result("Pop multiple elements", passed); clearStack(stack); } static void test_pop_empty_stack(void) { StackNode *stack = NULL; stack = pop(stack); report_result("Pop empty stack", stack == NULL); } static void test_clear_stack(void) { StackNode *stack = NULL; int a = 10, b = 20, c = 30; stack = push(stack, &a); stack = push(stack, &b); stack = push(stack, &c); int passed = stack != NULL && stack->next != NULL && stack->next->next != NULL && stack->next->next->next == NULL; clearStack(stack); report_result("Clear stack", passed); } static void test_clear_empty_stack(void) { StackNode *stack = NULL; clearStack(stack); report_result("Clear empty stack", stack == NULL); } void setUp(void) { } void tearDown(void) { } int main(void) { printf("Stack Unit Tests\n"); printf("----------------\n"); test_push_single_element(); test_push_multiple_elements(); test_push_null(); test_top_empty_stack(); test_pop_single_element(); test_pop_multiple_elements(); test_pop_empty_stack(); test_clear_stack(); test_clear_empty_stack(); printf("----------------\n"); printf("Results: %d passed, %d failed\n", tests_passed, tests_failed); return tests_failed == 0 ? 0 : 1; }