generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
2 Commits
bed93125ef
...
6dc857cc63
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dc857cc63 | ||
|
|
9363ed22aa |
28
.vscode/tasks.json
vendored
Normal file
28
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: gcc.exe build active file",
|
||||
"command": "C:\\ProgramData\\mingw64\\mingw64\\bin\\gcc.exe",
|
||||
"args": [
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${fileDirname}\\${fileBasenameNoExtension}.exe"
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"detail": "Task generated by Debugger."
|
||||
}
|
||||
],
|
||||
"version": "2.0.0"
|
||||
}
|
||||
BIN
doble_initial.exe
Normal file
BIN
doble_initial.exe
Normal file
Binary file not shown.
@ -1 +1,2 @@
|
||||
player_name;4980
|
||||
player1;3999
|
||||
|
||||
21
makefile
21
makefile
@ -35,15 +35,28 @@ $(program_obj_filesobj_files): %.o: %.c
|
||||
# --------------------------
|
||||
# Unit Tests
|
||||
# --------------------------
|
||||
unitTests:
|
||||
echo "needs to be implemented"
|
||||
tets_sources = test_stack.c stack.c
|
||||
test_objects = $(test_sources:.c=.o)
|
||||
|
||||
test_runner: $(test_objects)
|
||||
$(CC) $(FLAGS) $^ -o $@
|
||||
|
||||
unitTests: test_runner
|
||||
@echo "============================="
|
||||
@echo "Running Stack Unit Tests..."
|
||||
@echo "============================="
|
||||
./test_runner
|
||||
@echo "============================="
|
||||
@echo "Stack Unit Tests Finished."
|
||||
@echo "============================="
|
||||
|
||||
|
||||
# --------------------------
|
||||
# Clean
|
||||
# --------------------------
|
||||
clean:
|
||||
ifeq ($(OS),Windows_NT)
|
||||
del /f *.o doble
|
||||
del /f *.o doble test_runner
|
||||
else
|
||||
rm -f *.o doble
|
||||
rm -f *.o doble test_runner
|
||||
endif
|
||||
47
stack.c
47
stack.c
@ -10,24 +10,71 @@
|
||||
// Pushes data as pointer onto the stack.
|
||||
StackNode *push(StackNode *stack, void *data)
|
||||
{
|
||||
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
|
||||
|
||||
if(newNode == NULL)
|
||||
{
|
||||
perror("Fehler! Der Speicher für den neuen StackNode konnte nicht allokiert werden!\n");
|
||||
return stack;
|
||||
}
|
||||
|
||||
newNode->data = data; //saves data in newNode
|
||||
newNode->next = stack; //pushes the newNode on top of stack (newNode->next points to previous Top-Node)
|
||||
|
||||
return newNode;
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
perror("Fehler! Stack darf zum poppen nicht leer sein!\n");
|
||||
return stack;
|
||||
}
|
||||
|
||||
StackNode *old = stack; //temporary pointer to old top of stack
|
||||
|
||||
stack= stack->next; //update stack pointer to point to next element
|
||||
|
||||
free(old); //free the memory of the old StackNode (old->data is not beeing freed)
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
// Returns the data of the top element.
|
||||
void *top(StackNode *stack)
|
||||
{
|
||||
if (stack == NULL)
|
||||
{
|
||||
perror("Fehler! Kann von einem leeren Stack nicht das oberste Element lesen!\n");
|
||||
return NULL;
|
||||
}
|
||||
void *data = stack->data;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// Clears stack and releases all memory.
|
||||
void clearStack(StackNode *stack)
|
||||
{
|
||||
StackNode *current = stack;
|
||||
StackNode *next;
|
||||
|
||||
while (current != NULL)
|
||||
{
|
||||
next = current->next;
|
||||
|
||||
if (current->data != NULL)
|
||||
{
|
||||
free(current->data);
|
||||
current->data = NULL;
|
||||
}
|
||||
|
||||
free(current);
|
||||
current = NULL;
|
||||
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
6
stack.h
6
stack.h
@ -8,6 +8,12 @@ The latest element is taken from the stack. */
|
||||
#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.
|
||||
StackNode *push(StackNode *stack, void *data);
|
||||
|
||||
141
test_stack.c
Normal file
141
test_stack.c
Normal file
@ -0,0 +1,141 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "stack.h"
|
||||
|
||||
void test_on_empty_stack(void)
|
||||
{
|
||||
char *status_a = "leer";
|
||||
char *status_b = "leer";
|
||||
StackNode *stack = NULL;
|
||||
|
||||
StackNode *a = pop(stack);
|
||||
void* b = top(stack);
|
||||
|
||||
|
||||
if (a == NULL)
|
||||
{
|
||||
status_a = "PASSED";
|
||||
}
|
||||
else status_a = "FAILED";
|
||||
|
||||
if (b == NULL)
|
||||
{
|
||||
status_b = "PASSED";
|
||||
}
|
||||
else status_b = "FAILED";
|
||||
|
||||
printf("Test on empty stack in pop: %s - Expected: NULL, Was: %p\n",status_a, (void*)a);
|
||||
printf("Test on empty stack in top: %s - Expected: NULL, Was: %p\n",status_b, b);
|
||||
|
||||
}
|
||||
|
||||
void test_push(void)
|
||||
{
|
||||
int a[3] = 0;
|
||||
StackNode *stack = NULL;
|
||||
|
||||
for (int i = 1; i < 4; i++)
|
||||
{
|
||||
int *var_push = malloc(sizeof(int));
|
||||
|
||||
if (var_push == NULL)
|
||||
{
|
||||
printf("Error in allocating memory in test_push!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
*var_push = i;
|
||||
stack = push(stack, var_push);
|
||||
|
||||
void *top_data = top(stack);
|
||||
if (top_data != NULL && *(int*)top_data == i) {
|
||||
a[i-1] = 1;
|
||||
} else {
|
||||
a[i-1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (a[0] == 1 && a[1] == 1 && a[2] == 1)
|
||||
{
|
||||
printf("Test push: SUCCEESS!\n");
|
||||
}
|
||||
|
||||
else printf("Test pus: FAILED - Expected: all 1, Was: %d, %d, %d\n", a[0], a[1], a[2]);
|
||||
|
||||
stack = clearStack(stack);
|
||||
}
|
||||
|
||||
void test_pop(void)
|
||||
{
|
||||
StackNode *stack = NULL;
|
||||
int *var_push = malloc(sizeof(int));
|
||||
|
||||
if (var_push == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error in allocating memory in test_pop! Exiting.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
*var_push = 1;
|
||||
stack = push(stack, var_push);
|
||||
stack = pop(stack);
|
||||
|
||||
if (stack == NULL) printf("Test pop: SUCCESS!\n");
|
||||
|
||||
else printf("Test pop: FAIL! - Stack is not empty! Head: %p\n", (void*)stack);
|
||||
|
||||
stack = clearStack(stack);
|
||||
}
|
||||
|
||||
void test_top(void)
|
||||
{
|
||||
StackNode *stack = NULL;
|
||||
int *var_push = malloc(sizeof(int));
|
||||
*var_push = 1;
|
||||
stack = push(stack, var_push);
|
||||
void *b = top(stack);
|
||||
|
||||
if (b != 0 && *(int*)b == 1) printf("Test top: SUCCESS!\n");
|
||||
|
||||
else printf("Test top: FAIL! - Expected 1, was %p\n", b);
|
||||
|
||||
stack = clearStack(stack);
|
||||
}
|
||||
|
||||
void test_clearStack(void)
|
||||
{
|
||||
StackNode *stack = NULL;
|
||||
|
||||
for (int i = 1; i < 4; i++)
|
||||
{
|
||||
int *var_push = malloc(sizeof(int));
|
||||
|
||||
if (var_push == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error in allocating memory in test_clearStack! Exiting.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
*var_push = i;
|
||||
stack = push(stack, var_push);
|
||||
}
|
||||
|
||||
stack = clearStack(stack);
|
||||
void *a = top(stack);
|
||||
if (a == NULL) printf("Test clear: SUCESS!\n");
|
||||
|
||||
else printf("Test clear: FAILED! - Expected: NULL, Was:%p\n", a);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("============================\nStack Tests\n============================\n");
|
||||
|
||||
test_on_empty_stack();
|
||||
test_push();
|
||||
test_pop();
|
||||
test_top();
|
||||
test_clearStack();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user