generated from freudenreichan/info2Praktikum-DobleSpiel
stack Test fertig
This commit is contained in:
parent
51fcb12b63
commit
8ef242ae9b
BIN
Start_Windows/runStackTests.exe
Normal file
BIN
Start_Windows/runStackTests.exe
Normal file
Binary file not shown.
@ -23,14 +23,11 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
StackNode *new=malloc(sizeof(StackNode));
|
if(stack == NULL){
|
||||||
if(new ==NULL){
|
return NULL;
|
||||||
return new;
|
|
||||||
}
|
}
|
||||||
|
StackNode *new;
|
||||||
new = stack->dannach;
|
new = stack->dannach;
|
||||||
|
|
||||||
stack->dannach = NULL;
|
|
||||||
free(stack->data);
|
|
||||||
free(stack);
|
free(stack);
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
@ -38,17 +35,19 @@ StackNode *pop(StackNode *stack)
|
|||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if(stack == NULL){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
return stack->data;
|
return stack->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
if(stack == NULL){
|
while (stack != NULL) {
|
||||||
return;
|
StackNode *next = stack->dannach;
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
stack = next;
|
||||||
}
|
}
|
||||||
clearStack(stack->dannach);
|
|
||||||
free(stack->dannach);
|
|
||||||
free(stack->data);
|
|
||||||
free(stack);
|
|
||||||
}
|
}
|
||||||
@ -8,12 +8,12 @@ The latest element is taken from the stack. */
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct StackNode StackNode;
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
typedef struct Stack{
|
struct StackNode {
|
||||||
void* data ;
|
void* data;
|
||||||
StackNode* dannach;
|
StackNode* dannach;
|
||||||
}StackNode;
|
};
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data);
|
StackNode *push(StackNode *stack, void *data);
|
||||||
|
|||||||
BIN
Start_Windows/stack.o
Normal file
BIN
Start_Windows/stack.o
Normal file
Binary file not shown.
@ -6,13 +6,94 @@
|
|||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
|
|
||||||
|
static void pushNeuerStack(){
|
||||||
|
int wert = 2;
|
||||||
|
StackNode *tester = push(NULL,&wert);
|
||||||
|
|
||||||
|
int gespeicherterWert = *(int*)(tester->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(*(int*)(tester->data),2);
|
||||||
|
TEST_ASSERT_TRUE(tester->dannach == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
static void pushVorhandenerStack(){
|
static void pushVorhandenerStack(){
|
||||||
StackNode stack = {(int)1,NULL};
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
StackNode *tester = push(&stack,(int)2);
|
int a = 1;
|
||||||
|
stack = push(stack, &a);
|
||||||
|
|
||||||
|
int b = 2;
|
||||||
|
stack = push(stack,&b);
|
||||||
|
|
||||||
|
int gespeicherterWert = *(int*)(stack->data);
|
||||||
|
TEST_ASSERT_EQUAL_INT(*(int*)(stack->data),2);
|
||||||
|
TEST_ASSERT_EQUAL_INT(*(int*)(stack->dannach->data),1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void testTop(){
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
int a = 1;
|
||||||
|
int b = 2;
|
||||||
|
|
||||||
|
stack = push(stack, &a);
|
||||||
|
stack = push(stack, &b);
|
||||||
|
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *(int *)top(stack)); // top sollte b sein
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void testPop(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
int a = 1;
|
||||||
|
int b = 2;
|
||||||
|
|
||||||
|
stack = push(stack, &a);
|
||||||
|
stack = push(stack, &b);
|
||||||
|
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *(int *)top(stack)); // top sollte b sein
|
||||||
|
|
||||||
|
stack = pop(stack);
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *(int *)top(stack)); // top sollte a sein
|
||||||
|
|
||||||
|
stack = pop(stack);
|
||||||
|
TEST_ASSERT_NULL(stack); // leerer Stack nach pop
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void testPopLeererStack(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
stack = pop(stack);
|
||||||
|
TEST_ASSERT_NULL(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testClearStackAllesFrei(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
int *a = malloc(sizeof(int));
|
||||||
|
int *b = malloc(sizeof(int));
|
||||||
|
*a = 10;
|
||||||
|
*b = 20;
|
||||||
|
|
||||||
|
stack = push(stack, a);
|
||||||
|
stack = push(stack, b);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(1); // Wenn kein Crash und Speicher sauber, gilt als bestanden
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void){
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_INT(tester->data,2);
|
|
||||||
TEST_ASSERT_EQUAL_INT(tester->dannach->data,1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -21,8 +102,12 @@ int main(){
|
|||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
printf("\n============================\nStack tests\n============================\n");
|
printf("\n============================\nStack tests\n============================\n");
|
||||||
|
|
||||||
|
RUN_TEST(pushNeuerStack);
|
||||||
RUN_TEST(pushVorhandenerStack);
|
RUN_TEST(pushVorhandenerStack);
|
||||||
|
RUN_TEST(testTop);
|
||||||
|
RUN_TEST(testPop);
|
||||||
|
RUN_TEST(testPopLeererStack);
|
||||||
|
RUN_TEST(testClearStackAllesFrei);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user