diff --git a/highscores.txt b/highscores.txt index 425930e..520fe3d 100644 --- a/highscores.txt +++ b/highscores.txt @@ -1,2 +1,2 @@ -player_name;18787 +Lukas;4985 player1;3999 diff --git a/makefile b/makefile index ebcaa37..e0e6a70 100644 --- a/makefile +++ b/makefile @@ -35,7 +35,9 @@ $(program_obj_filesobj_files): %.o: %.c # -------------------------- # Unit Tests # -------------------------- -unitTests: +stackTests: stack.o test_stack.c $(unityfolder)/unity.c + $(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c + echo "needs to be implemented" numbersTests: numbers.o numbersTests.c $(unityfolder)/unity.c $(CC) $(CFLAGS) -I$(unityfolder) -o runNumbersTests numbersTests.c numbers.o $(unityfolder)/unity.c diff --git a/stack.c b/stack.c index e3a90d4..61c4c01 100644 --- a/stack.c +++ b/stack.c @@ -10,6 +10,22 @@ // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data) { + if(!(stack)) { // check if stack is empty + stack = malloc(sizeof(StackNode)); + if(!stack) + return NULL; + stack->stackData = data; + stack->below = NULL; + return stack; + } + + StackNode* newStack = malloc(sizeof(StackNode)); + if(!newStack) + return stack; + newStack->below = stack; + newStack->stackData = data; + stack = newStack; + return stack; } @@ -17,17 +33,32 @@ StackNode *push(StackNode *stack, void *data) // freed by caller.) StackNode *pop(StackNode *stack) { - + if(stack) { + StackNode* temp = stack; + stack = stack->below; + temp->stackData = NULL; + free(temp); + temp = NULL; + return stack; + } else { + return NULL; + } } // Returns the data of the top element. void *top(StackNode *stack) { + if(stack) + return stack->stackData; + else + return NULL; } // Clears stack and releases all memory. void clearStack(StackNode *stack) { + while(stack) + stack = pop(stack); } \ No newline at end of file diff --git a/stack.h b/stack.h index f7d542d..faab5e5 100644 --- a/stack.h +++ b/stack.h @@ -9,6 +9,11 @@ The latest element is taken from the stack. */ //TODO: passenden Datentyp als struct anlegen +typedef struct StackNode { + struct StackNode* below; + void* stackData; +} StackNode; + // Pushes data as pointer onto the stack. StackNode *push(StackNode *stack, void *data); diff --git a/test_stack.c b/test_stack.c new file mode 100644 index 0000000..79d85cc --- /dev/null +++ b/test_stack.c @@ -0,0 +1,97 @@ +#include "stack.h" +#include "unity.h" + + +void test_firstNodeAddedCorrectly(void) { + printf("\nStarting first test...\n"); + StackNode* modelStack = malloc(sizeof(StackNode)); + int modelData = 5; + modelStack->below = NULL; + modelStack->stackData = &modelData; + + StackNode* testStack = NULL; + int testData = 5; + testStack = push(testStack, &testData); + TEST_ASSERT_EQUAL_INT(*(int*)modelStack->stackData, *(int*)testStack->stackData); + TEST_ASSERT_NULL(testStack->below); + clearStack(modelStack); + clearStack(testStack); +} + +void test_topReturnsCorrectValues(void) { + printf("Starting second test...\n"); + StackNode* testStack = NULL; + int data1 = 1; + testStack = push(testStack, &data1); + int* returnData1 = (int*) top(testStack); + int data2 = 0; + testStack = push(testStack, &data2); + int* returnData2 = (int*) top(testStack); + int data3 = 3; + testStack = push(testStack, &data3); + int* returnData3 = (int*) top(testStack); + + size_t counter = 0; + if(*returnData1 == 1) + counter++; + if(*returnData2 == 0) + counter++; + if(*returnData3 == 3) + counter++; + TEST_ASSERT_EQUAL_INT(3, counter); + clearStack(testStack); +} + +void test_popRemovesCorrectly(void) { + printf("Starting third test...\n"); + StackNode* testStack = NULL; + int data1 = 1; + testStack = push(testStack, &data1); + int data2 = 0; + testStack = push(testStack, &data2); + int data3 = 3; + testStack = push(testStack, &data3); + int data4 = 9; + testStack = push(testStack, &data4); + + int* returnData1 = (int*) top(testStack); + testStack = pop(testStack); + int* returnData2 = (int*) top(testStack); + testStack = pop(testStack); + int* returnData3 = (int*) top(testStack); + testStack = pop(testStack); + int* returnData4 = (int*) top(testStack); + testStack = pop(testStack); + + size_t counter = 0; + if(*returnData1 == 9) + counter++; + if(*returnData2 == 3) + counter++; + if(*returnData3 == 0) + counter++; + if(*returnData4 == 1) + counter++; + TEST_ASSERT_EQUAL_INT(4, counter); + clearStack(testStack); +} + +void setUp(void) { + +} + +void tearDown(void) { + +} + +int main(void) { + + UNITY_BEGIN(); + + printf("\n----------------------------Stack-Tests----------------------------\n"); + RUN_TEST(test_firstNodeAddedCorrectly); + RUN_TEST(test_topReturnsCorrectValues); + RUN_TEST(test_popRemovesCorrectly); + + return UNITY_END(); +} \ No newline at end of file