forked from freudenreichan/info2Praktikum-DobleSpiel
Stack.C fertig gestellt
This commit is contained in:
parent
d816f405f0
commit
4d27f910ff
2
makefile
2
makefile
@ -49,7 +49,7 @@ stackTests: stack.o stackTests.c $(unityfolder)/unity.c
|
|||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),!Windows_NT)
|
||||||
del /f *.o doble *.exe
|
del /f *.o doble *.exe
|
||||||
else
|
else
|
||||||
rm -f *.o doble *runNumbersTests
|
rm -f *.o doble *runNumbersTests
|
||||||
|
|||||||
17
stack.c
17
stack.c
@ -20,17 +20,28 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if(stack == NULL)
|
||||||
|
return NULL;
|
||||||
|
StackNode *next = stack->next;
|
||||||
|
free(stack);
|
||||||
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if(stack == NULL)
|
||||||
|
return NULL;
|
||||||
|
return stack->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
while(stack != NULL)
|
||||||
|
{
|
||||||
|
StackNode *next = stack->next;
|
||||||
|
free(stack);
|
||||||
|
stack = next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
30
stackTests.c
30
stackTests.c
@ -9,34 +9,20 @@ void tearDown(void){}
|
|||||||
|
|
||||||
void test_push_created_new_stacknode(void)
|
void test_push_created_new_stacknode(void)
|
||||||
{
|
{
|
||||||
StackNode test = {111,NULL};
|
int testdata1 = 111;
|
||||||
int testdata = 222;
|
StackNode test = {&testdata1,NULL};
|
||||||
void *data =&testdata;
|
int testdata2 = 222;
|
||||||
|
|
||||||
StackNode test1 = push(&test,data);
|
StackNode *test1 = push(&test,&testdata2);
|
||||||
unsigned int n = 50;
|
TEST_ASSERT_NOT_NULL(test1);
|
||||||
unsigned int* arr = createNumbers(n);
|
TEST_ASSERT_EQUAL_PTR(&testdata2,test1->data);
|
||||||
|
TEST_ASSERT_EQUAL_PTR(&test,test1->next);
|
||||||
TEST_ASSERT_NOT_NULL(arr);
|
|
||||||
|
|
||||||
// Ein paar Werte prüfen (dürfen alles sein, nur kein Segfault)
|
|
||||||
for (unsigned int i = 0; i < n; i++)
|
|
||||||
TEST_ASSERT_TRUE(arr[i] >= 0);
|
|
||||||
|
|
||||||
free(arr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
RUN_TEST(test_createNumbers_returns_valid_array);
|
RUN_TEST(test_push_created_new_stacknode);
|
||||||
RUN_TEST(test_createNumbers_contains_exactly_one_duplicate);
|
|
||||||
RUN_TEST(test_getDuplicated_finds_correct_duplicate);
|
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user