generated from freudenreichan/info2Praktikum-DobleSpiel
stack sowie die unit tests mit nicole und lena gemacht
This commit is contained in:
parent
8a29c029ff
commit
75a227eae0
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"stdio.h": "c",
|
||||||
|
"unity.h": "c"
|
||||||
|
}
|
||||||
|
}
|
||||||
5
makefile
5
makefile
@ -36,8 +36,11 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
test_numbers:
|
test_numbers:
|
||||||
$(CC) -o test_numbers test__numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
|
$(CC) -o test_numbers test_numbers.c numbers.c $(unityfolder)/unity.c $(FLAGS)
|
||||||
|
|
||||||
|
test_stack:
|
||||||
|
$(CC) -o test_stack test_stack.c stack.c $(unityfolder)/unity.c $(FLAGS)
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
54
stack.c
54
stack.c
@ -1,39 +1,65 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
//TODO: grundlegende Stackfunktionen implementieren:
|
// TODO: grundlegende Stackfunktionen implementieren:
|
||||||
/* * `push`: legt ein Element oben auf den Stack,
|
/* * `push`: legt ein Element oben auf den Stack,
|
||||||
* `pop`: entfernt das oberste Element,
|
* `pop`: entfernt das oberste Element,
|
||||||
* `top`: liefert das oberste Element zurück,
|
* `top`: liefert das oberste Element zurück,
|
||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
* `clearStack`: gibt den gesamten Speicher frei. */
|
||||||
|
|
||||||
|
static StackNode *createNewElement()
|
||||||
|
{
|
||||||
|
return malloc(sizeof(StackNode));
|
||||||
|
}
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
typedef struct stackNode
|
|
||||||
{
|
|
||||||
void *data;
|
|
||||||
struct stackNode *next;
|
|
||||||
} StackNode;
|
|
||||||
|
|
||||||
StackNode *push(StackNode *stack, void *data)
|
StackNode *push(StackNode *stack, void *data)
|
||||||
{
|
{
|
||||||
|
StackNode *node = createNewElement();
|
||||||
|
if (node != NULL)
|
||||||
|
{
|
||||||
|
node->data = data;
|
||||||
|
node->next = stack;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if (stack != NULL)
|
||||||
|
{
|
||||||
|
StackNode *currentElement = stack;
|
||||||
|
stack = stack->next;
|
||||||
|
|
||||||
|
free(currentElement);
|
||||||
|
}
|
||||||
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 stack->data;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
StackNode *nextElement = stack;
|
||||||
|
|
||||||
}
|
while (stack != NULL)
|
||||||
|
{
|
||||||
|
nextElement = stack->next;
|
||||||
|
|
||||||
|
free(stack);
|
||||||
|
|
||||||
|
stack = nextElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
5
stack.h
5
stack.h
@ -8,6 +8,11 @@ The latest element is taken from the stack. */
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
|
typedef struct stackNode
|
||||||
|
{
|
||||||
|
void *data;
|
||||||
|
struct stackNode *next;
|
||||||
|
} StackNode;
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *push(StackNode *stack, void *data);
|
StackNode *push(StackNode *stack, void *data);
|
||||||
|
|||||||
38
test_stack.c
Normal file
38
test_stack.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "unity/unity.h"
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
void test_stackOrderIsCorrect()
|
||||||
|
{
|
||||||
|
int expectedValues[] = {1, 2, 3, 4};
|
||||||
|
const unsigned int exprectedLen = sizeof(expectedValues) / sizeof(expectedValues[0]);
|
||||||
|
unsigned int observedLen = 0;
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
|
||||||
|
for(int i = 0; i < exprectedLen; i++)
|
||||||
|
{
|
||||||
|
stack = push(stack, &expectedValues[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = exprectedLen-1; i >= 0 && stack != NULL; i--)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL(expectedValues[i], *(int *)top(stack));
|
||||||
|
observedLen++;
|
||||||
|
stack = pop(stack);
|
||||||
|
}
|
||||||
|
TEST_ASSERT_EQUAL_UINT32(exprectedLen, observedLen);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp(void) {}
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_stackOrderIsCorrect);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user