From 599e4d7e75a8201fa585658471cd38edd54103c9 Mon Sep 17 00:00:00 2001 From: Elena Riedmann Date: Tue, 9 Jun 2026 10:18:08 +0200 Subject: [PATCH] test push pop --- highscores.txt | 5 ++ makefile | 10 ++- test_push_pop | Bin 0 -> 16568 bytes test_push_pop.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++++ test_stack.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 475 insertions(+), 2 deletions(-) create mode 100755 test_push_pop create mode 100644 test_push_pop.c create mode 100644 test_stack.c diff --git a/highscores.txt b/highscores.txt index a6a0d3e..c5beca9 100644 --- a/highscores.txt +++ b/highscores.txt @@ -1,2 +1,7 @@ +elena;24999 +player_name;14999 player_name;6999 +elena;4999 player1;3999 +elena;2999 +elena;2999 diff --git a/makefile b/makefile index 7a7d9d2..f8c9d17 100644 --- a/makefile +++ b/makefile @@ -53,12 +53,18 @@ unity.o: test_bintree: stack.o bintree.o unity.o $(CC) $(FLAGS) -Iunity test_bintree.c $^ -o runTest_bintree +# -------------------------- +#Test: test_push_pop +# -------------------------- +test_push_pop: stack.o unity.o + $(CC) $(FLAGS) -Iunity test_push_pop.c $^ -o runTest_push_pop + # -------------------------- # Clean # -------------------------- clean: ifeq ($(OS),Windows_NT) - del /f *.o doble runTest_numbers runTest_bintree + del /f *.o doble runTest_numbers runTest_bintree runTest_push_pop else - rm -f *.o doble runTest_numbers runTest_bintree + rm -f *.o doble runTest_numbers runTest_bintree runTest_push_pop endif \ No newline at end of file diff --git a/test_push_pop b/test_push_pop new file mode 100755 index 0000000000000000000000000000000000000000..03f56b7a4439be3083715db780a114fde9ea6bb9 GIT binary patch literal 16568 zcmeHOeQ;FQb-xnG$QUEx_ya=%4=@g3vqnM?z$liG7O!jpDoCy4iMy<{PtuZ=cC-7I zNYr3b*(r)p1~(yQTslKqQZrNk!F19F*UccnVXXTnI~_*p|%0wqF%q&HbvHN$WODsBhq zl@N<@FZ|_=Mo*ydbN(#DS04C0>j`x0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9o8^kG3q8A?{gu3itIMvnF3l$d@grrj- zN=I=)I#vtfNDYDMBL=`=aP#)sE&iZ?Ykd&k6x4rdYwebXI*{=lNzfl)ZlHd<3O?Al zZBuPyaNFk1JN(VT=Gsk-{-FLIqDDSz&|ffkKZ?k4@u|bdecnLdTjXT5B|98)LP|Fq z0qKZyA7bPrtN1I69D!8Z5_ZV38@io8)F;kuG2E5(VV0i{Qx&oRo8-WJPywXy>xy7F z84K$3I(&K2unkh-NOv%m!~ro*omD|TgQ5J{rQzt4G83{(R8EnD`XD^X-;oqVj-Cx8 zmeypF-iP{)DVyrqsiD(~JLu^WpY^G!(MmnnrVX|Slas2xh^94X~X_I5d;RzN3V zV5ijRg3qz+f1t_X}7b@YVtVQ$hHyiEE34W#`vmPqUoO!0olDEnz_^JDfe{jV&R zWOzRf99Qv#qs+cPKYnDNFYrm1E>*q$e-0YuU;Gb!o}_R-&2W^MuHyCoHh6g9O3y(O zlkW-TJ6o^3-~!)+PMJ$z&LNJiBy0KcYOp*m`X7NoW0xNzbI(8W-L!lkEol@z>Hoil zE;Ui~@0BNC?lOn{ zvv`gr`kclbUG&{h`aUOwN(~#lzys!UpwXj*#3SqPji5>FsOC!P8dH7KV_f`-AU$?X Qr2q3T7>Riv1rMtJ6G?2TiU0rr literal 0 HcmV?d00001 diff --git a/test_push_pop.c b/test_push_pop.c new file mode 100644 index 0000000..dbe14a9 --- /dev/null +++ b/test_push_pop.c @@ -0,0 +1,231 @@ +#include +#include +#include "stack.h" + +/* ============================================================================ + * UNIT TESTS FOR STACK PUSH AND POP OPERATIONS + * + * This test suite covers: + * - push(): Add integer values to the stack + * - pop(): Remove values from the stack + * - top(): Retrieve the top element without removing it + * - clearStack(): Clean up stack memory + * ========================================================================== */ + +// ============================================================================ +// HELPER FUNCTIONS FOR TESTING +// ============================================================================ + +// Create an integer value on the heap +int *createIntValue(int value) +{ + int *ptr = (int *)malloc(sizeof(int)); + if (ptr != NULL) + { + *ptr = value; + } + return ptr; +} + +// Count elements in the stack +int countStackElements(StackNode *stack) +{ + int count = 0; + StackNode *current = stack; + while (current != NULL) + { + count++; + current = current->next; + } + return count; +} + +// ============================================================================ +// TEST FUNCTIONS +// ============================================================================ + +void test_push_single_value(void) +{ + printf("\nTEST 1: Push and retrieve a single integer value\n"); + printf("================================================\n"); + + StackNode *stack = NULL; + + // Push a single value + int *value1 = createIntValue(42); + stack = push(stack, value1); + + // Check if value is on top + int *result = (int *)top(stack); + if (result != NULL && *result == 42) + { + printf("✓ Push and top() successful: value = %d\n", *result); + } + else + { + printf("✗ FAILED: Expected 42, got %d\n", result != NULL ? *result : -1); + } + + // Clean up + stack = pop(stack); + free(value1); + printf("Test 1 completed\n"); +} + +void test_push_multiple_values(void) +{ + printf("\nTEST 2: Push multiple integer values (LIFO principle)\n"); + printf("====================================================\n"); + + StackNode *stack = NULL; + + // Push multiple values + int values[] = {10, 20, 30, 40, 50}; + for (int i = 0; i < 5; i++) + { + int *val = createIntValue(values[i]); + stack = push(stack, val); + printf("Pushed: %d\n", values[i]); + } + + printf("\nStack size: %d elements\n", countStackElements(stack)); + + // Pop and verify LIFO order (should be 50, 40, 30, 20, 10) + printf("\nPopping values (should be in reverse order):\n"); + int expected_order[] = {50, 40, 30, 20, 10}; + int all_correct = 1; + + for (int i = 0; i < 5; i++) + { + int *topVal = (int *)top(stack); + if (topVal != NULL && *topVal == expected_order[i]) + { + printf("✓ Pop %d: value = %d (correct)\n", i + 1, *topVal); + free(topVal); + stack = pop(stack); + } + else + { + printf("✗ Pop %d: Expected %d, got %d\n", i + 1, expected_order[i], topVal != NULL ? *topVal : -1); + all_correct = 0; + if (topVal != NULL) + free(topVal); + stack = pop(stack); + } + } + + if (all_correct) + { + printf("\n✓ LIFO principle verified!\n"); + } + else + { + printf("\n✗ LIFO principle test FAILED!\n"); + } + + printf("Test 2 completed\n"); +} + +void test_push_pop_alternating(void) +{ + printf("\nTEST 3: Alternating push and pop operations\n"); + printf("===========================================\n"); + + StackNode *stack = NULL; + + // Push first value + int *val1 = createIntValue(100); + stack = push(stack, val1); + printf("Pushed: 100\n"); + + // Pop it + int *top1 = (int *)top(stack); + printf("Top: %d\n", *top1); + free(top1); + stack = pop(stack); + printf("Popped: 100\n"); + + // Push multiple new values + int *val2 = createIntValue(200); + int *val3 = createIntValue(300); + stack = push(stack, val2); + printf("Pushed: 200\n"); + stack = push(stack, val3); + printf("Pushed: 300\n"); + + // Pop and verify + int *top2 = (int *)top(stack); + if (*top2 == 300) + { + printf("✓ Top is 300 (correct)\n"); + } + free(top2); + stack = pop(stack); + + int *top3 = (int *)top(stack); + if (*top3 == 200) + { + printf("✓ Top is 200 (correct)\n"); + } + free(top3); + stack = pop(stack); + + clearStack(stack); + printf("Test 3 completed\n"); +} + +void test_empty_stack(void) +{ + printf("\nTEST 4: Empty stack handling\n"); + printf("============================\n"); + + StackNode *stack = NULL; + + printf("Initial stack size: %d\n", countStackElements(stack)); + + if (stack == NULL) + { + printf("✓ Empty stack correctly represented as NULL\n"); + } + + // Push a value + int *val = createIntValue(123); + stack = push(stack, val); + printf("After push: stack size = %d\n", countStackElements(stack)); + + // Pop to make it empty again + stack = pop(stack); + free(val); + + printf("After pop: stack size = %d\n", countStackElements(stack)); + if (stack == NULL) + { + printf("✓ Stack correctly empty after popping last element\n"); + } + + printf("Test 4 completed\n"); +} + +// ============================================================================ +// MAIN TEST RUNNER +// ============================================================================ + +int main(void) +{ + printf("╔════════════════════════════════════════════════════════════╗\n"); + printf("║ STACK PUSH & POP TEST SUITE ║\n"); + printf("║ Testing integer value handling in LIFO stack operations ║\n"); + printf("╚════════════════════════════════════════════════════════════╝\n"); + + // Run all tests + test_push_single_value(); + test_push_multiple_values(); + test_push_pop_alternating(); + test_empty_stack(); + + printf("\n╔════════════════════════════════════════════════════════════╗\n"); + printf("║ ALL TESTS COMPLETED ║\n"); + printf("╚════════════════════════════════════════════════════════════╝\n"); + + return 0; +} diff --git a/test_stack.c b/test_stack.c new file mode 100644 index 0000000..dbe14a9 --- /dev/null +++ b/test_stack.c @@ -0,0 +1,231 @@ +#include +#include +#include "stack.h" + +/* ============================================================================ + * UNIT TESTS FOR STACK PUSH AND POP OPERATIONS + * + * This test suite covers: + * - push(): Add integer values to the stack + * - pop(): Remove values from the stack + * - top(): Retrieve the top element without removing it + * - clearStack(): Clean up stack memory + * ========================================================================== */ + +// ============================================================================ +// HELPER FUNCTIONS FOR TESTING +// ============================================================================ + +// Create an integer value on the heap +int *createIntValue(int value) +{ + int *ptr = (int *)malloc(sizeof(int)); + if (ptr != NULL) + { + *ptr = value; + } + return ptr; +} + +// Count elements in the stack +int countStackElements(StackNode *stack) +{ + int count = 0; + StackNode *current = stack; + while (current != NULL) + { + count++; + current = current->next; + } + return count; +} + +// ============================================================================ +// TEST FUNCTIONS +// ============================================================================ + +void test_push_single_value(void) +{ + printf("\nTEST 1: Push and retrieve a single integer value\n"); + printf("================================================\n"); + + StackNode *stack = NULL; + + // Push a single value + int *value1 = createIntValue(42); + stack = push(stack, value1); + + // Check if value is on top + int *result = (int *)top(stack); + if (result != NULL && *result == 42) + { + printf("✓ Push and top() successful: value = %d\n", *result); + } + else + { + printf("✗ FAILED: Expected 42, got %d\n", result != NULL ? *result : -1); + } + + // Clean up + stack = pop(stack); + free(value1); + printf("Test 1 completed\n"); +} + +void test_push_multiple_values(void) +{ + printf("\nTEST 2: Push multiple integer values (LIFO principle)\n"); + printf("====================================================\n"); + + StackNode *stack = NULL; + + // Push multiple values + int values[] = {10, 20, 30, 40, 50}; + for (int i = 0; i < 5; i++) + { + int *val = createIntValue(values[i]); + stack = push(stack, val); + printf("Pushed: %d\n", values[i]); + } + + printf("\nStack size: %d elements\n", countStackElements(stack)); + + // Pop and verify LIFO order (should be 50, 40, 30, 20, 10) + printf("\nPopping values (should be in reverse order):\n"); + int expected_order[] = {50, 40, 30, 20, 10}; + int all_correct = 1; + + for (int i = 0; i < 5; i++) + { + int *topVal = (int *)top(stack); + if (topVal != NULL && *topVal == expected_order[i]) + { + printf("✓ Pop %d: value = %d (correct)\n", i + 1, *topVal); + free(topVal); + stack = pop(stack); + } + else + { + printf("✗ Pop %d: Expected %d, got %d\n", i + 1, expected_order[i], topVal != NULL ? *topVal : -1); + all_correct = 0; + if (topVal != NULL) + free(topVal); + stack = pop(stack); + } + } + + if (all_correct) + { + printf("\n✓ LIFO principle verified!\n"); + } + else + { + printf("\n✗ LIFO principle test FAILED!\n"); + } + + printf("Test 2 completed\n"); +} + +void test_push_pop_alternating(void) +{ + printf("\nTEST 3: Alternating push and pop operations\n"); + printf("===========================================\n"); + + StackNode *stack = NULL; + + // Push first value + int *val1 = createIntValue(100); + stack = push(stack, val1); + printf("Pushed: 100\n"); + + // Pop it + int *top1 = (int *)top(stack); + printf("Top: %d\n", *top1); + free(top1); + stack = pop(stack); + printf("Popped: 100\n"); + + // Push multiple new values + int *val2 = createIntValue(200); + int *val3 = createIntValue(300); + stack = push(stack, val2); + printf("Pushed: 200\n"); + stack = push(stack, val3); + printf("Pushed: 300\n"); + + // Pop and verify + int *top2 = (int *)top(stack); + if (*top2 == 300) + { + printf("✓ Top is 300 (correct)\n"); + } + free(top2); + stack = pop(stack); + + int *top3 = (int *)top(stack); + if (*top3 == 200) + { + printf("✓ Top is 200 (correct)\n"); + } + free(top3); + stack = pop(stack); + + clearStack(stack); + printf("Test 3 completed\n"); +} + +void test_empty_stack(void) +{ + printf("\nTEST 4: Empty stack handling\n"); + printf("============================\n"); + + StackNode *stack = NULL; + + printf("Initial stack size: %d\n", countStackElements(stack)); + + if (stack == NULL) + { + printf("✓ Empty stack correctly represented as NULL\n"); + } + + // Push a value + int *val = createIntValue(123); + stack = push(stack, val); + printf("After push: stack size = %d\n", countStackElements(stack)); + + // Pop to make it empty again + stack = pop(stack); + free(val); + + printf("After pop: stack size = %d\n", countStackElements(stack)); + if (stack == NULL) + { + printf("✓ Stack correctly empty after popping last element\n"); + } + + printf("Test 4 completed\n"); +} + +// ============================================================================ +// MAIN TEST RUNNER +// ============================================================================ + +int main(void) +{ + printf("╔════════════════════════════════════════════════════════════╗\n"); + printf("║ STACK PUSH & POP TEST SUITE ║\n"); + printf("║ Testing integer value handling in LIFO stack operations ║\n"); + printf("╚════════════════════════════════════════════════════════════╝\n"); + + // Run all tests + test_push_single_value(); + test_push_multiple_values(); + test_push_pop_alternating(); + test_empty_stack(); + + printf("\n╔════════════════════════════════════════════════════════════╗\n"); + printf("║ ALL TESTS COMPLETED ║\n"); + printf("╚════════════════════════════════════════════════════════════╝\n"); + + return 0; +}