generated from freudenreichan/info2Praktikum-DobleSpiel
Test Stack geschrieben
This commit is contained in:
parent
127c7aa8e7
commit
2dc724e065
107
test_stack.c
107
test_stack.c
@ -1,61 +1,64 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
//Testfunkionen zu push, pull, top & clearStack schreiben
|
//Testfunkionen zu push, pull, top & clearStack schreiben
|
||||||
|
|
||||||
StackNode *push(StackNode *stack, void *data)
|
void test(char *name, int condition) {
|
||||||
{
|
if (condition) {
|
||||||
if (!data)
|
printf("[OK] %s\n", name);
|
||||||
{
|
} else {
|
||||||
return stack; //Nichts pushen
|
printf("[FAIL] %s\n", name);
|
||||||
}
|
}
|
||||||
//if(stack && data){
|
|
||||||
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
|
||||||
if(!t)
|
|
||||||
{
|
|
||||||
return NULL; //Speicherfehler
|
|
||||||
}
|
|
||||||
t->next = stack;
|
|
||||||
t->data = data;
|
|
||||||
return t; //Gibt den ersten StackNode des Stacks zurueck
|
|
||||||
//}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
int main() {
|
||||||
// freed by caller.)
|
|
||||||
StackNode *pop(StackNode *stack)
|
|
||||||
{
|
|
||||||
if(stack)
|
|
||||||
{
|
|
||||||
StackNode *t = stack->next; //Naechstes Element im Stack wird erstes Element
|
|
||||||
free(stack);
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the data of the top element.
|
StackNode *stack = NULL;
|
||||||
void *top(StackNode *stack)
|
|
||||||
{
|
|
||||||
if(stack)
|
|
||||||
{
|
|
||||||
return stack->data;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Werte dynamisch anlegen
|
||||||
void clearStack(StackNode *stack)
|
int *val1 = malloc(sizeof(int));
|
||||||
{
|
*val1 = 5;
|
||||||
while(stack)
|
stack = push(stack, val1);
|
||||||
{
|
test("push(5) legt 5 oben auf den Stack", *(int*)stack->data == 5);
|
||||||
StackNode *tmp = stack; //merkt sich den momentanen obersten Knoten
|
|
||||||
stack = stack->next; //setzt den obersten Knoten auf den Zweiten im Stack
|
int *val2 = malloc(sizeof(int));
|
||||||
free(tmp->data);
|
*val2 = 6;
|
||||||
free(tmp);
|
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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user