Unit Tests für den Stack erstellt
This commit is contained in:
parent
2c10463852
commit
0e7802fb30
@ -1,7 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
#include "stack.h"
|
||||
|
||||
//TODO: grundlegende Stackfunktionen implementieren:
|
||||
//DONE: grundlegende Stackfunktionen implementieren:
|
||||
/* * `push`: legt ein Element oben auf den Stack,
|
||||
* `pop`: entfernt das oberste Element,
|
||||
* `top`: liefert das oberste Element zurück,
|
||||
@ -10,12 +10,12 @@
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data)
|
||||
{
|
||||
// ,-→ Wenn keine Daten angegeben wird die Liste unverändert zurückgegeben; Ist das sinnvoll? kein Anzeichen, kein Rückgabewert...
|
||||
// ,-→ Wenn keine Daten angegeben wird die Liste unverändert zurückgegeben
|
||||
if (data != NULL)
|
||||
{
|
||||
StackNode *newNode;
|
||||
newNode = (StackNode *)malloc(sizeof(StackNode));
|
||||
newNode->value = data;
|
||||
newNode->data = data;
|
||||
|
||||
// ,-→ bedeutet Liste war leer - das neue Element hat keinen Nachfolger (next = NULL)
|
||||
if (stack == NULL) {
|
||||
@ -25,11 +25,12 @@ StackNode *push(StackNode *stack, void *data)
|
||||
}
|
||||
stack = newNode;
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||
// freed by caller.)
|
||||
// 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)
|
||||
{
|
||||
if(stack != NULL)
|
||||
@ -44,7 +45,11 @@ StackNode *pop(StackNode *stack)
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
return stack->value;
|
||||
if (stack != NULL) {
|
||||
return stack->data;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
|
||||
@ -10,7 +10,7 @@ The latest element is taken from the stack. */
|
||||
//DONE: passenden Datentyp als struct anlegen
|
||||
typedef struct Node
|
||||
{
|
||||
void *value;
|
||||
void *data;
|
||||
struct Node* next;
|
||||
} StackNode;
|
||||
|
||||
|
||||
167
I2_Dobble/stackTest.c
Normal file
167
I2_Dobble/stackTest.c
Normal file
@ -0,0 +1,167 @@
|
||||
#include "stack.h"
|
||||
#include "unity/unity.h"
|
||||
#include "unity/unity_internals.h"
|
||||
|
||||
/* wird nicht mehr gebraucht
|
||||
|
||||
// Ein Blick in den Stack - listet den Stack-Inhalt der Reihe nach auf
|
||||
void inspectStack(StackNode *stack)
|
||||
{
|
||||
if (stack != NULL)
|
||||
{
|
||||
printf("Der Stack enthält die folgenden Elemente: %d", *(int*)stack->data);
|
||||
while (stack->next != NULL)
|
||||
{
|
||||
printf(" %d", *(int*)stack->next->data);
|
||||
stack = stack->next;
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Der Stack ist leer\n");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Setup-Funktionen von Unity, die zumindest als Platzhalter definiert sein müssen
|
||||
void setUp(void) {};
|
||||
void tearDown(void) {};
|
||||
|
||||
void test_createNodeAbortsOnZeroData(void)
|
||||
{
|
||||
void *data = NULL;
|
||||
StackNode *stack = NULL;
|
||||
stack = push(stack, data);
|
||||
TEST_ASSERT_EQUAL_PTR(stack, NULL);
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
void test_createdNodeIsFirstInList(void)
|
||||
{
|
||||
int data = 42;
|
||||
StackNode *stack = NULL;
|
||||
stack = push(stack, &data);
|
||||
TEST_ASSERT_NOT_NULL(stack);
|
||||
TEST_ASSERT_EQUAL_PTR(&data, stack->data);
|
||||
TEST_ASSERT_NULL(stack->next);
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
void test_createNodeCorrectOrder(void)
|
||||
{
|
||||
int data[3] = {0, 1, 2};
|
||||
StackNode *stack = NULL;
|
||||
stack = push(stack, &data[2]);
|
||||
stack = push(stack, &data[1]);
|
||||
stack = push(stack, &data[0]);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(stack);
|
||||
TEST_ASSERT_EQUAL_PTR(&data[0], stack->data);
|
||||
TEST_ASSERT_EQUAL_PTR(&data[1], stack->next->data);
|
||||
TEST_ASSERT_EQUAL_PTR(&data[2], stack->next->next->data);
|
||||
TEST_ASSERT_NULL(stack->next->next->next);
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
void test_removeNodeAbortsOnZero(void)
|
||||
{
|
||||
StackNode *stack = NULL;
|
||||
TEST_ASSERT_NULL(pop(stack));
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
void test_removeOnlyNodeEmptysList(void)
|
||||
{
|
||||
int data = 42;
|
||||
StackNode *stack = NULL;
|
||||
stack = pop(push(stack, &data));
|
||||
|
||||
TEST_ASSERT_NULL(stack);
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
void test_removeNodeRemovesFirst(void)
|
||||
{
|
||||
int data[2] = {0, 1};
|
||||
StackNode *stack = NULL;
|
||||
stack = push(stack, &data[1]);
|
||||
stack = push(stack, &data[0]);
|
||||
stack = pop(stack);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(stack);
|
||||
TEST_ASSERT_EQUAL_PTR(&data[1], stack->data);
|
||||
TEST_ASSERT_NULL(stack->next);
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
void test_outputTopFailsOnZero(void)
|
||||
{
|
||||
StackNode *stack = NULL;
|
||||
|
||||
TEST_ASSERT_NULL(top(stack));
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
void test_outputTopCorrect(void)
|
||||
{
|
||||
int data[2] = {0, 1};
|
||||
StackNode *stack = NULL;
|
||||
stack = push(stack, &data[1]);
|
||||
stack = push(stack, &data[0]);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(stack);
|
||||
TEST_ASSERT_EQUAL_PTR(top(stack), stack->data);
|
||||
|
||||
clearStack(stack);
|
||||
}
|
||||
|
||||
|
||||
/* Speicher freigabe lässt sich nicht mit Unity testen, am besten Programm mit valgrind ausführen
|
||||
void test_clearStackFreesMemory(void)
|
||||
{
|
||||
int data[2] = {22, 17};
|
||||
StackNode *lowNode = (StackNode *)malloc(sizeof(StackNode));
|
||||
lowNode->next = NULL;
|
||||
lowNode->data = &data[0];
|
||||
StackNode *highNode = (StackNode *)malloc(sizeof(StackNode));
|
||||
highNode->next = lowNode;
|
||||
highNode->data = &data[1];
|
||||
StackNode *stack = highNode;
|
||||
|
||||
clearStack(stack);
|
||||
|
||||
TEST_ASSERT_NULL(lowNode);
|
||||
TEST_ASSERT_NULL(highNode);
|
||||
TEST_ASSERT_NULL(stack);
|
||||
}
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("\n============================\n Stack tests\n============================\n");
|
||||
printf("-> Create Nodes (push)\n");
|
||||
RUN_TEST(test_createNodeAbortsOnZeroData);
|
||||
RUN_TEST(test_createdNodeIsFirstInList);
|
||||
RUN_TEST(test_createNodeCorrectOrder);
|
||||
|
||||
printf("\n-> Remove Nodes (pop)\n");
|
||||
RUN_TEST(test_removeNodeAbortsOnZero);
|
||||
RUN_TEST(test_removeOnlyNodeEmptysList);
|
||||
RUN_TEST(test_removeNodeRemovesFirst);
|
||||
|
||||
printf("\n-> Output Top (top)\n");
|
||||
RUN_TEST(test_outputTopFailsOnZero);
|
||||
RUN_TEST(test_outputTopCorrect);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user