diff --git a/Start_Windows/makefile b/Start_Windows/makefile index b67312a..b3d0844 100644 --- a/Start_Windows/makefile +++ b/Start_Windows/makefile @@ -26,6 +26,9 @@ $(program_obj_filesobj_files): %.o: %.c unitTests: echo "needs to be implemented" +stackTests: stack.o stackTests.c $(unityfolder)/unity.c + $(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests stackTests.c stack.o $(unityfolder)/unity.c + # -------------------------- # Clean # -------------------------- diff --git a/Start_Windows/stack.c b/Start_Windows/stack.c index 6383694..a89f014 100644 --- a/Start_Windows/stack.c +++ b/Start_Windows/stack.c @@ -10,24 +10,45 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { - + StackNode *new=malloc(sizeof(StackNode)); + if(new ==NULL){ + return new; + } + new->data = data; + new->dannach = stack; + return new; } // Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be // freed by caller.) StackNode *pop(StackNode *stack) { + StackNode *new=malloc(sizeof(StackNode)); + if(new ==NULL){ + return new; + } + new = stack->dannach; + stack->dannach = NULL; + free(stack->data); + free(stack); + return new; } // Returns the data of the top element. void *top(StackNode *stack) { - + return stack->data; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { - + if(stack == NULL){ + return; + } + clearStack(stack->dannach); + free(stack->dannach); + free(stack->data); + free(stack); } \ No newline at end of file diff --git a/Start_Windows/stack.h b/Start_Windows/stack.h index 45714c3..ac7f301 100644 --- a/Start_Windows/stack.h +++ b/Start_Windows/stack.h @@ -10,9 +10,9 @@ The latest element is taken from the stack. */ //TODO: passenden Datentyp als struct anlegen -typedef struct{ - int items[MAX_STACK_SIZE]; - int top; +typedef struct Stack{ + void* data ; + StackNode* dannach; }StackNode; // Pushes data as pointer onto the stack. diff --git a/Start_Windows/stackTests.c b/Start_Windows/stackTests.c new file mode 100644 index 0000000..0a6e4bd --- /dev/null +++ b/Start_Windows/stackTests.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include "unity.h" +#include "stack.h" + + +static void pushVorhandenerStack(){ + StackNode stack = {(int)1,NULL}; + + StackNode *tester = push(&stack,(int)2); + + TEST_ASSERT_EQUAL_INT(tester->data,2); + TEST_ASSERT_EQUAL_INT(tester->dannach->data,1); +} + + + +int main(){ + UNITY_BEGIN(); + printf("\n============================\nStack tests\n============================\n"); + + RUN_TEST(pushVorhandenerStack); + + + return UNITY_END(); +} \ No newline at end of file