197 lines
4.5 KiB
C
197 lines
4.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "stack.h"
|
|
#include "unity.h"
|
|
|
|
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
|
|
}
|
|
|
|
void test_push1(void)
|
|
{
|
|
StackNode *testNode = NULL;
|
|
int data = 1;
|
|
|
|
// Test für leeren Stack
|
|
testNode = push(testNode, &data);
|
|
TEST_ASSERT_NOT_NULL(&testNode);
|
|
TEST_ASSERT_NULL(testNode->next);
|
|
int *temp = testNode->data;
|
|
TEST_ASSERT_EQUAL_INT(1, *temp);
|
|
|
|
data = 2;
|
|
|
|
// Test für nicht leeren Stack
|
|
testNode = push(testNode, &data);
|
|
TEST_ASSERT_NOT_NULL(&testNode);
|
|
TEST_ASSERT_NOT_NULL(testNode->next);
|
|
TEST_ASSERT_NULL(testNode->next->next);
|
|
temp = testNode->data;
|
|
TEST_ASSERT_EQUAL_INT(2, *temp);
|
|
testNode = testNode->next;
|
|
temp = testNode->data;
|
|
TEST_ASSERT_EQUAL_INT(1, *temp);
|
|
}
|
|
|
|
StackNode* setup(void *data, StackNode* next) {
|
|
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
|
|
if (node == NULL) {
|
|
perror("malloc failed");
|
|
exit(EXIT_FAILURE); // or handle the error differently
|
|
}
|
|
node->data = data;
|
|
node->next = next;
|
|
return node;
|
|
}
|
|
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
|
|
}
|
|
void test_pop2(void)
|
|
{
|
|
int x,y,z;
|
|
x = 1;
|
|
y = 2;
|
|
z = 3;
|
|
StackNode* node2 = setup(&z, NULL);
|
|
StackNode* node1 = setup(&y, node2);
|
|
StackNode* header = setup(&x, node1);
|
|
StackNode* temp;
|
|
|
|
temp = pop(header);
|
|
int after = 0;
|
|
while(temp)
|
|
{
|
|
after++;
|
|
temp = temp->next;
|
|
}
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(2, after);
|
|
TEST_ASSERT_NULL(node1->next);
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
void test_top2(void)
|
|
{
|
|
int x,y,z;
|
|
x = 1;
|
|
y = 2;
|
|
z = 3;
|
|
StackNode* node2 = setup(&z, NULL);
|
|
StackNode* node1 = setup(&y, node2);
|
|
StackNode* header = setup(&x, node1);
|
|
|
|
int data = *(int *)top(header);
|
|
TEST_ASSERT_EQUAL_INT(node2->data, data);
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
void test_clear()
|
|
{
|
|
int x,y,z;
|
|
x = 1;
|
|
y = 2;
|
|
z = 3;
|
|
StackNode* node2 = setup(&z, NULL);
|
|
StackNode* node1 = setup(&y, node2);
|
|
StackNode* header = setup(&x, node1);
|
|
StackNode* temp;
|
|
|
|
clearStack(header);
|
|
temp = header;
|
|
|
|
int after = 0;
|
|
while(temp)
|
|
{
|
|
after++;
|
|
temp = temp->next;
|
|
}
|
|
|
|
|
|
TEST_ASSERT_NULL(after);
|
|
}
|
|
|
|
void setUp(void)
|
|
{
|
|
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
|
}
|
|
|
|
void tearDown(void)
|
|
{
|
|
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
|
}
|
|
|
|
int main()
|
|
{
|
|
UNITY_BEGIN();
|
|
|
|
printf("============================\nStack tests\n============================\n");
|
|
|
|
RUN_TEST(test_push);
|
|
RUN_TEST(test_pop);
|
|
RUN_TEST(test_top);
|
|
RUN_TEST(test_clearStack);
|
|
|
|
return UNITY_END();
|
|
} |