2025-12-10 12:00:39 +01:00

101 lines
2.4 KiB
C

#include "unity.h"
#include "stack.h" // Stack-Header-Datei
#include <stdlib.h>
// Test Setup and Teardown Functions
void setUp(void) {
// Setup code (falls notwendig, wie Initialisierungen)
}
void tearDown(void) {
// Cleanup code (falls notwendig)
}
// Test for push operation
void test_push(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Check if the stack is not empty
TEST_ASSERT_NOT_NULL(stack);
// Check if the top element is correct
int *topData = top(stack);
TEST_ASSERT_EQUAL_INT(20, *topData); // The last pushed element should be on top
}
// Test for pop operation
void test_pop(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Pop the top element
stack = pop(stack);
// Check if the top element is now the first pushed element
int *topData = top(stack);
TEST_ASSERT_EQUAL_INT(10, *topData); // After popping, the first element should be on top
// Pop the last element
stack = pop(stack);
// Check if the stack is empty now
TEST_ASSERT_NULL(stack); // Stack should be NULL now
}
// Test for top operation
void test_top(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Check the top element
int *topData = top(stack);
TEST_ASSERT_EQUAL_INT(20, *topData); // The top element should be 20 (last pushed)
// Pop the top element and check the new top
stack = pop(stack);
topData = top(stack);
TEST_ASSERT_EQUAL_INT(10, *topData); // Now the top element should be 10
}
// Test for clearStack operation
void test_clearStack(void) {
StackNode *stack = NULL;
int data1 = 10, data2 = 20;
// Push elements to the stack
stack = push(stack, &data1);
stack = push(stack, &data2);
// Clear the stack
clearStack(stack);
// The stack should be empty now
TEST_ASSERT_NULL(stack); // Stack should be NULL
}
// Run all tests
int main(void) {
UNITY_BEGIN();
// Run the individual test functions
RUN_TEST(test_push);
RUN_TEST(test_pop);
RUN_TEST(test_top);
RUN_TEST(test_clearStack);
return UNITY_END();
}