test_stack.c erstellt
This commit is contained in:
parent
cc927ad723
commit
00fafec3dc
4
stack.c
4
stack.c
@ -11,7 +11,7 @@
|
|||||||
StackNode *push(StackNode *stack, void *data)
|
StackNode *push(StackNode *stack, void *data)
|
||||||
{
|
{
|
||||||
StackNode *newNode = malloc(sizeof(StackNode));
|
StackNode *newNode = malloc(sizeof(StackNode));
|
||||||
if(*newNode == NULL)
|
if(newNode == NULL)
|
||||||
{
|
{
|
||||||
//printf("Fehler Bei Speicherallozierung!");
|
//printf("Fehler Bei Speicherallozierung!");
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -58,7 +58,7 @@ void clearStack(StackNode *stack)
|
|||||||
if(stack == NULL)
|
if(stack == NULL)
|
||||||
{
|
{
|
||||||
//printf("Fehlerhafte Adresse uebergeben");
|
//printf("Fehlerhafte Adresse uebergeben");
|
||||||
return NULL;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
StackNode *currentNode = stack;
|
StackNode *currentNode = stack;
|
||||||
|
|||||||
2
stack.h
2
stack.h
@ -11,7 +11,7 @@ The latest element is taken from the stack. */
|
|||||||
typedef struct StackNode
|
typedef struct StackNode
|
||||||
{
|
{
|
||||||
void *data;
|
void *data;
|
||||||
struct SatckNode *next;
|
struct StackNode *next;
|
||||||
} StackNode;
|
} StackNode;
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
|
|||||||
121
test_stack.c
Normal file
121
test_stack.c
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
void testeStackBeschreiben()
|
||||||
|
{
|
||||||
|
printf("=== Test: push() ===\n");
|
||||||
|
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int wert1 = 42;
|
||||||
|
stack = push(stack, &wert1);
|
||||||
|
|
||||||
|
int *topValue = (int *)(stack->data);
|
||||||
|
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);
|
||||||
|
|
||||||
|
if(topValue != NULL && *topValue == 12.25 && 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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
testeStackBeschreiben();
|
||||||
|
testepop();
|
||||||
|
testetop();
|
||||||
|
testeclearStack();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user