Doble-Spiel/test_stack.c
2025-12-09 17:37:26 +01:00

141 lines
2.9 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void test_on_empty_stack(void)
{
char *status_a = "leer";
char *status_b = "leer";
StackNode *stack = NULL;
StackNode *a = pop(stack);
void* b = top(stack);
if (a == NULL)
{
status_a = "PASSED";
}
else status_a = "FAILED";
if (b == NULL)
{
status_b = "PASSED";
}
else status_b = "FAILED";
printf("Test on empty stack in pop: %s - Expected: NULL, Was: %p\n",status_a, (void*)a);
printf("Test on empty stack in top: %s - Expected: NULL, Was: %p\n",status_b, b);
}
void test_push(void)
{
int a[3] = 0;
StackNode *stack = NULL;
for (int i = 1; i < 4; i++)
{
int *var_push = malloc(sizeof(int));
if (var_push == NULL)
{
printf("Error in allocating memory in test_push!\n");
exit(EXIT_FAILURE);
}
*var_push = i;
stack = push(stack, var_push);
void *top_data = top(stack);
if (top_data != NULL && *(int*)top_data == i) {
a[i-1] = 1;
} else {
a[i-1] = 0;
}
}
if (a[0] == 1 && a[1] == 1 && a[2] == 1)
{
printf("Test push: SUCCEESS!\n");
}
else printf("Test pus: FAILED - Expected: all 1, Was: %d, %d, %d\n", a[0], a[1], a[2]);
stack = clearStack(stack);
}
void test_pop(void)
{
StackNode *stack = NULL;
int *var_push = malloc(sizeof(int));
if (var_push == NULL)
{
fprintf(stderr, "Error in allocating memory in test_pop! Exiting.\n");
exit(EXIT_FAILURE);
}
*var_push = 1;
stack = push(stack, var_push);
stack = pop(stack);
if (stack == NULL) printf("Test pop: SUCCESS!\n");
else printf("Test pop: FAIL! - Stack is not empty! Head: %p\n", (void*)stack);
stack = clearStack(stack);
}
void test_top(void)
{
StackNode *stack = NULL;
int *var_push = malloc(sizeof(int));
*var_push = 1;
stack = push(stack, var_push);
void *b = top(stack);
if (b != 0 && *(int*)b == 1) printf("Test top: SUCCESS!\n");
else printf("Test top: FAIL! - Expected 1, was %p\n", b);
stack = clearStack(stack);
}
void test_clearStack(void)
{
StackNode *stack = NULL;
for (int i = 1; i < 4; i++)
{
int *var_push = malloc(sizeof(int));
if (var_push == NULL)
{
fprintf(stderr, "Error in allocating memory in test_clearStack! Exiting.\n");
exit(EXIT_FAILURE);
}
*var_push = i;
stack = push(stack, var_push);
}
stack = clearStack(stack);
void *a = top(stack);
if (a == NULL) printf("Test clear: SUCESS!\n");
else printf("Test clear: FAILED! - Expected: NULL, Was:%p\n", a);
}
int main()
{
printf("============================\nStack Tests\n============================\n");
test_on_empty_stack();
test_push();
test_pop();
test_top();
test_clearStack();
return EXIT_SUCCESS;
}