Team5_Doble/test_stack.c
Yannik Baumgärtner 416f44ae74 Endlich fertig
2025-12-09 08:00:32 +01:00

45 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "stack.h"
int main(void)
{
StackNode *s = NULL;
// push 3 integers
int *a = malloc(sizeof(int)); *a = 1; s = push(s, a);
int *b = malloc(sizeof(int)); *b = 2; s = push(s, b);
int *c = malloc(sizeof(int)); *c = 3; s = push(s, c);
// top should be c (3)
int *topv = (int *)top(s);
if(topv == NULL || *topv != 3) { fprintf(stderr, "stack top expected 3\n"); return 1; }
// pop -> now top should be 2
s = pop(s);
topv = (int *)top(s);
if(topv == NULL || *topv != 2) { fprintf(stderr, "stack top expected 2 after pop\n"); return 2; }
// pop -> now top should be 1
s = pop(s);
topv = (int *)top(s);
if(topv == NULL || *topv != 1) { fprintf(stderr, "stack top expected 1 after pop\n"); return 3; }
// pop last
s = pop(s);
if(s != NULL) { fprintf(stderr, "stack expected empty after popping all\n"); return 4; }
// free stored data (stack does not free data)
free(a); free(b); free(c);
// test clearStack on empty and small stacks
s = push(s, malloc(sizeof(int)));
s = push(s, malloc(sizeof(int)));
clearStack(s); // clearStack must free nodes but not payload; free payloads not necessary because we leaked intentionally for test of API
// Note: above payloads are not freed (stack API spec); this test ensures clearStack doesn't crash.
// Success
printf("test_stack: OK\n");
return 0;
}