DobleSpiel/test_stack.c
2025-12-09 10:31:12 +01:00

156 lines
3.0 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "unity.h"
#include "stack.h"
void setUp(void) {
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void) {
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
void testeStackBeschreiben()
{
printf("=== Test: push() ===\n");
StackNode *stack = NULL;
int wert1 = 42;
stack = push(stack, &wert1);
int *topValue = (int *)(stack->data);
TEST_ASSERT_NOT_NULL(topValue);
TEST_ASSERT_EQUAL_INT(42, topValue);
/*if(topValue != NULL && *topValue == 42)
{
printf("Test 1: Erstes Element erfolgreich gepusht!\n");
}
else
{
printf("Test 1: FEHLGESCHLAGEN!\n");
}*/
int wert2 = 12;
stack = push(stack, &wert2);
topValue = (int *)(stack->data);
int *secondValue = (int *)((stack->next)->data);
TEST_ASSERT_NOT_NULL(topValue);
TEST_ASSERT_EQUAL_INT(12, topValue);
TEST_ASSERT_NOT_NULL(secondValue);
TEST_ASSERT_EQUAL_INT(42, secondValue);
/*if(topValue != NULL && *topValue == 12 && secondValue != NULL && *secondValue == 42)
{
printf("Test 2: Zweites Element erfolgreich gepusht!\n");
}
else
{
printf("Test 2: FEHLGESCHLAGEN!\n");
}*/
printf("=== Ende Test: push() ===\n\n");
return;
}
void testepop()
{
printf("=== Test: pop() ===\n");
StackNode *stack = NULL;
int wert1 = 20;
int wert2 = 74;
stack = push(stack, &wert1);
stack = push(stack, &wert2);
stack = pop(stack);
int *topValue = (int *)(stack->data);
TEST_ASSERT_NOT_NULL(topValue);
TEST_ASSERT_EQUAL_INT(20, topValue);
/*if(topValue != NULL && *topValue == 20)
{
printf("Test: Erstes Element erfolgreich gelöscht!\n");
}
else
{
printf("Test 1: FEHLGESCHLAGEN!\n");
}*/
printf("=== Ende Test: pop() ===\n\n");
}
void testetop()
{
printf("=== Test: top() ===\n");
StackNode *stack = NULL;
int wert1 = 20;
int wert2 = 74;
stack = push(stack, &wert1);
stack = push(stack, &wert2);
int *topValue = top(stack);
TEST_ASSERT_NOT_NULL(topValue);
TEST_ASSERT_EQUAL_INT(74, topValue);
/*if(topValue != NULL && *topValue == 74)
{
printf("Test: top() gibt korrektes Element zurück!\n");
}
else
{
printf("Test: FEHLGESCHLAGEN!\n");
}*/
printf("=== Ende Test: top() ===\n\n");
}
void testeclearStack()
{
printf("=== Test: clearStack() ===\n");
StackNode *stack = NULL;
int wert1 = 20;
int wert2 = 74;
stack = push(stack, &wert1);
stack = push(stack, &wert2);
clearStack(stack);
printf("Test: clearStack() aufgerufen. Speicher freigegeben.\n");
printf("=== Ende Test: clearStack() ===\n\n");
}
int main()
{
UNITY_BEGIN();
//printf("...");
RUN_TEST(testeStackBeschreiben);
RUN_TEST(testepop);
RUN_TEST(testetop);
RUN_TEST(testeclearStack);
/*testeStackBeschreiben();
testepop();
testetop();
testeclearStack();*/
return UNITY_END();
}