diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f36e144 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "stdio.h": "c", + "stdlib.h": "c" + } +} \ No newline at end of file diff --git a/makefile b/makefile index 5b2e04b..49f367b 100644 --- a/makefile +++ b/makefile @@ -37,6 +37,8 @@ doble_initial: # -------------------------- # Unit Tests # -------------------------- +stackTests: stack.o test_stack.c $(unityfolder)/unity.c + $(CC) $(CFLAGS) -I$(unityfolder) -o stackTests test_stack.c stack.o $(unityfolder)/unity.c ${LDFLAGS} unitTests: echo "needs to be implemented" diff --git a/stack.c b/stack.c index e3a90d4..1814599 100644 --- a/stack.c +++ b/stack.c @@ -8,26 +8,53 @@ * `clearStack`: gibt den gesamten Speicher frei. */ // Pushes data as pointer onto the stack. -StackNode *push(StackNode *stack, void *data) +StackNode* push(StackNode* stack, void* data) { - + //pointer onto new struct + StackNode* newNode = malloc(sizeof(StackNode)); + newNode->data = data; + //*stack := first node of the stack; is now the second to first node + newNode->nextNode = stack; + return newNode; } // 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* pop(StackNode* stack) { + //getting the second to first node + StackNode* tmp = stack->nextNode; + // data should be freed by the caller + // free(stack->data); + // stack->data = NULL; + free(stack); + stack = NULL; + + return tmp; } // Returns the data of the top element. -void *top(StackNode *stack) +void* top(StackNode* stack) { + //No stack --> No data-pointer + if (stack == NULL) return NULL; + return stack->data; } // Clears stack and releases all memory. -void clearStack(StackNode *stack) +void clearStack(StackNode* stack) { + if (stack == NULL) return; + if (stack->nextNode != NULL) clearStack(stack->nextNode); + + // date should be freed by the caller. + // free(stack->data); + // stack->data = NULL; + + free(stack); + // technically not needed (dangling pointer) + stack = NULL; } \ No newline at end of file diff --git a/stack.h b/stack.h index f7d542d..4c3a3a7 100644 --- a/stack.h +++ b/stack.h @@ -7,17 +7,20 @@ The latest element is taken from the stack. */ #include -//TODO: passenden Datentyp als struct anlegen +typedef struct StackNode { + void* data; + struct StackNode* nextNode; +} StackNode; // Pushes data as pointer onto the stack. -StackNode *push(StackNode *stack, void *data); +StackNode* push(StackNode *stack, void *data); // 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* pop(StackNode *stack); // Returns the data of the top element. -void *top(StackNode *stack); +void* top(StackNode *stack); // Clears stack and releases all memory. void clearStack(StackNode *stack); diff --git a/stackTests b/stackTests new file mode 100644 index 0000000..be175e3 Binary files /dev/null and b/stackTests differ diff --git a/test_stack.c b/test_stack.c new file mode 100644 index 0000000..9b77f35 --- /dev/null +++ b/test_stack.c @@ -0,0 +1,77 @@ +#include +#include +#include "unity.h" +#include "stack.h" + + +void test_topReturnsCorrectValue(void) { + // 1. Arrange + int* value1; + int* value2; + int* outVal1; + int* outVal2; + value1 = malloc(sizeof(int)); + value2 = malloc(sizeof(int)); + StackNode* startNode = NULL; + + // 2. Act + *value1 = 1234; + *value2 = 5678; + startNode = push(startNode, value1); + // new top node is node 2 + startNode = push(startNode, value2); + outVal2 = top(startNode); + // node 1 should now be the top node + startNode = pop(startNode); + outVal1 = top(startNode); + + // 3. Assert + //Also tests for the functionality of 'push' + TEST_ASSERT_EQUAL_INT32(*value1, *outVal1); + TEST_ASSERT_EQUAL_INT32(*value2, *outVal2); + + free(value1); + value1 = NULL; + free(value2); + value2 = NULL; + clearStack(startNode); +} + +void test_popRemovesElements(void) { + // 1. Arrange + StackNode* startNode = NULL; + + // 2. Act + // Entering some Elements into Stack (could be added manually for testing) + startNode = push(startNode, NULL); + startNode = push(startNode, NULL); + startNode = push(startNode, NULL); + startNode = push(startNode, NULL); + // Now removing created Elements + startNode = pop(startNode); + startNode = pop(startNode); + startNode = pop(startNode); + startNode = pop(startNode); + + // 3. Assert + TEST_ASSERT_NULL(startNode); +} + +void setUp(void){ + +} + +void tearDown(void){ + +} + +int main(void) { + UNITY_BEGIN(); + + printf("\n============================\nStack tests\n============================\n"); + + RUN_TEST(test_topReturnsCorrectValue); + RUN_TEST(test_popRemovesElements); + + return UNITY_END(); +} \ No newline at end of file