Compare commits

..
5 Commits
4 changed files with 107 additions and 9 deletions

No files matched your search

+3 -1
View File
@@ -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"
# --------------------------
+6 -7
View File
@@ -12,12 +12,16 @@ 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;
@@ -32,7 +36,6 @@ StackNode *pop(StackNode *stack)
if(stack) {
StackNode* temp = stack;
stack = stack->below;
free(temp->data);
temp->stackData = NULL;
free(temp);
temp = NULL;
@@ -55,11 +58,7 @@ void *top(StackNode *stack)
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
if(stack) {
do {
stack = pop(stack);
} while (stack->below);
pop(stack);
}
while(stack)
stack = pop(stack);
}
+1 -1
View File
@@ -10,7 +10,7 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen
typedef struct StackNode {
StackNode* below;
struct StackNode* below;
void* stackData;
} StackNode;
+97
View File
@@ -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();
}