#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; }