generated from freudenreichan/info2Praktikum-DobleSpiel
72 lines
1.6 KiB
C
72 lines
1.6 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include "stack.h"
|
|
|
|
//Testfunkionen zu push, pull, top & clearStack schreiben
|
|
|
|
void setUp()
|
|
{
|
|
}
|
|
|
|
void tearDown()
|
|
{
|
|
}
|
|
|
|
void test(char *name, int condition) {
|
|
if (condition) {
|
|
printf("[OK] %s\n", name);
|
|
} else {
|
|
printf("[FAIL] %s\n", name);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
|
|
StackNode *stack = NULL;
|
|
|
|
// Werte dynamisch anlegen
|
|
int *val1 = malloc(sizeof(int));
|
|
*val1 = 5;
|
|
stack = push(stack, val1);
|
|
test("push(5) legt 5 oben auf den Stack", *(int*)stack->data == 5);
|
|
|
|
int *val2 = malloc(sizeof(int));
|
|
*val2 = 6;
|
|
stack = push(stack, val2);
|
|
test("push(6) legt 6 oben auf den Stack", *(int*)stack->data == 6);
|
|
|
|
int *val3 = malloc(sizeof(int));
|
|
*val3 = 24;
|
|
stack = push(stack, val3);
|
|
test("push(24) legt 24 oben auf den Stack", *(int*)stack->data == 24);
|
|
|
|
// Test top()
|
|
int t = *(int*)top(stack);
|
|
test("top() liefert 24", t == 24);
|
|
|
|
// Test pop()
|
|
StackNode *tmp;
|
|
|
|
tmp = stack;
|
|
stack = pop(stack);
|
|
free(tmp->data); // Daten freigeben
|
|
free(tmp); // Knoten freigeben
|
|
test("pop() entfernt 24, 6 ist jetzt oben", *(int*)stack->data == 6);
|
|
|
|
tmp = stack;
|
|
stack = pop(stack);
|
|
free(tmp->data);
|
|
free(tmp);
|
|
test("pop() entfernt 6, 5 ist jetzt oben", *(int*)stack->data == 5);
|
|
|
|
tmp = stack;
|
|
stack = pop(stack);
|
|
free(tmp->data);
|
|
free(tmp);
|
|
test("pop() entfernt 5, Stack ist jetzt leer", stack == NULL);
|
|
|
|
// Am Ende Stack leeren (falls noch Elemente übrig)
|
|
clearStack(stack);
|
|
|
|
return 0;
|
|
} |