forked from freudenreichan/info2Praktikum-DobleSpiel
Mein Teil (Sollte Fertig sein)
This commit is contained in:
1 parent
e7a872252a
commit
89067a7ba6
4 files changed
+100
-14
No files matched your search
@@ -34,6 +34,8 @@ $(program_obj_files): %.o: %.c
|
|||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
|
test_stack: test_stack.c stack.o stack.h
|
||||||
|
$(CC) $(FLAGS) -I$(unityfolder) test_stack.c stack.o $(unityfolder)/unity.c -o test_stack.exe
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests:
|
unitTests:
|
||||||
echo "needs to be implemented"
|
echo "needs to be implemented"
|
||||||
|
|||||||
+22
-9
@@ -1,33 +1,46 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
//TODO: grundlegende Stackfunktionen implementieren:
|
|
||||||
/* * `push`: legt ein Element oben auf den Stack,
|
|
||||||
* `pop`: entfernt das oberste Element,
|
|
||||||
* `top`: liefert das oberste Element zurück,
|
|
||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
|
||||||
|
if (newNode == NULL) {
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
newNode->data = data;
|
||||||
|
newNode->next = stack;
|
||||||
|
return newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
StackNode *nextNode = stack->next;
|
||||||
|
free(stack);
|
||||||
|
return nextNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 *nextNode = stack->next;
|
||||||
|
free(stack);
|
||||||
|
stack = nextNode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+8
-5
@@ -1,13 +1,16 @@
|
|||||||
#ifndef STACK_H
|
#ifndef STACK_H
|
||||||
#define STACK_H
|
#define STACK_H
|
||||||
|
|
||||||
/* A stack is a special type of queue which uses the LIFO (last in, first out) principle.
|
/* A stack is a special type of queue which uses the LIFO (Last In, First Out) principle.
|
||||||
This means that with each new element all other elements are pushed deeper into the stack.
|
This means that with each new element all other elements are pushed deeper into the stack.
|
||||||
The latest element is taken from the stack. */
|
The latest element is taken from the stack. */
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
//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);
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
void setUp(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
void tearDown(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_empty_stack(void)
|
||||||
|
{
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
assert(top(stack) == NULL);
|
||||||
|
assert(pop(stack) == NULL);
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_push_top_pop_int(void)
|
||||||
|
{
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int a = 10;
|
||||||
|
int b = 20;
|
||||||
|
int c = 30;
|
||||||
|
|
||||||
|
stack = push(stack, &a);
|
||||||
|
assert(top(stack) == &a);
|
||||||
|
assert(*(int *)top(stack) == 10);
|
||||||
|
|
||||||
|
stack = push(stack, &b);
|
||||||
|
stack = push(stack, &c);
|
||||||
|
assert(*(int *)top(stack) == 30);
|
||||||
|
|
||||||
|
stack = pop(stack);
|
||||||
|
assert(*(int *)top(stack) == 20);
|
||||||
|
|
||||||
|
stack = pop(stack);
|
||||||
|
assert(*(int *)top(stack) == 10);
|
||||||
|
|
||||||
|
stack = pop(stack);
|
||||||
|
assert(stack == NULL);
|
||||||
|
assert(top(stack) == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_clear_stack(void)
|
||||||
|
{
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int values[] = {1, 2, 3, 4, 5};
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
stack = push(stack, &values[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(*(int *)top(stack) == 5);
|
||||||
|
|
||||||
|
clearStack(stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
test_empty_stack();
|
||||||
|
test_push_top_pop_int();
|
||||||
|
test_clear_stack();
|
||||||
|
|
||||||
|
printf("Alle Stack-Tests erfolgreich bestanden!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user