Compare commits

..

No commits in common. "036158fc59945abd480b7febd3ee9e4254212a04" and "94e489cfae5dfcf2cae2baadc91337ca6bfefe78" have entirely different histories.

4 changed files with 9 additions and 47 deletions

View File

@ -35,9 +35,7 @@ $(program_obj_filesobj_files): %.o: %.c
# --------------------------
# Unit Tests
# --------------------------
stackTests: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(CFLAGS) -I$(unityfolder) -o runStackTests test_stack.c stack.o $(unityfolder)/unity.c
unitTests:
echo "needs to be implemented"
# --------------------------

13
stack.c
View File

@ -12,16 +12,12 @@ 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;
@ -36,6 +32,7 @@ StackNode *pop(StackNode *stack)
if(stack) {
StackNode* temp = stack;
stack = stack->below;
free(temp->data);
temp->stackData = NULL;
free(temp);
temp = NULL;
@ -58,7 +55,11 @@ void *top(StackNode *stack)
// Clears stack and releases all memory.
void clearStack(StackNode *stack)
{
while(stack)
stack = pop(stack);
if(stack) {
do {
stack = pop(stack);
} while (stack->below);
pop(stack);
}
}

View File

@ -10,7 +10,7 @@ The latest element is taken from the stack. */
//TODO: passenden Datentyp als struct anlegen
typedef struct StackNode {
struct StackNode* below;
StackNode* below;
void* stackData;
} StackNode;

View File

@ -1,37 +0,0 @@
#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 setUp(void) {
}
void tearDown(void) {
}
int main(void) {
UNITY_BEGIN();
printf("\n----------------------------Stack-Tests----------------------------\n");
RUN_TEST(test_firstNodeAddedCorrectly);
return UNITY_END();
}