stackv1
This commit is contained in:
parent
2496c2c0ec
commit
28afa369d5
BIN
doble_initial
Executable file
BIN
doble_initial
Executable file
Binary file not shown.
@ -1 +1,5 @@
|
|||||||
|
player2;17901
|
||||||
|
player_name2;14920
|
||||||
|
player2;14844
|
||||||
|
player_name;6977
|
||||||
player1;3999
|
player1;3999
|
||||||
|
|||||||
43
makefile
43
makefile
@ -1,15 +1,15 @@
|
|||||||
CC = gcc
|
```CC = gcc
|
||||||
FLAGS = -g -Wall -lm
|
FLAGS = -g -Wall -lm
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
include makefile_windows.variables
|
include makefile_windows.variables
|
||||||
else
|
else
|
||||||
UNAME = $(shell uname)
|
UNAME = $(shell uname)
|
||||||
ifeq ($(UNAME),Linux)
|
ifeq ($(UNAME),Linux)
|
||||||
include makefile_linux.variables
|
include makefile_linux.variables
|
||||||
else
|
else
|
||||||
include makefile_mac.variables
|
include makefile_mac.variables
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
raylibfolder = ./raylib
|
raylibfolder = ./raylib
|
||||||
@ -19,7 +19,7 @@ unityfolder = ./unity
|
|||||||
# Initiales Programm bauen (zum ausprobieren)
|
# Initiales Programm bauen (zum ausprobieren)
|
||||||
# --------------------------
|
# --------------------------
|
||||||
doble_initial:
|
doble_initial:
|
||||||
$(CC) -o doble_initial $(BINARIES)/libdoble_complete.a
|
$(CC) -o doble_initial $(BINARIES)/libdoble_complete.a
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Selbst implementiertes Programm bauen
|
# Selbst implementiertes Programm bauen
|
||||||
@ -27,23 +27,36 @@ doble_initial:
|
|||||||
program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
|
program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
|
||||||
|
|
||||||
doble : main.o $(program_obj_files)
|
doble : main.o $(program_obj_files)
|
||||||
$(CC) $(FLAGS) $^ -o doble
|
$(CC) $(FLAGS) $^ -o doble
|
||||||
|
|
||||||
$(program_obj_filesobj_files): %.o: %.c
|
# HINWEIS: Hier war ein Tippfehler im Original (obj_filesobj_files), ich habe ihn korrigiert:
|
||||||
|
$(program_obj_files): %.o: %.c
|
||||||
$(CC) -c $(FLAGS) $^ -o $@
|
$(CC) -c $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests:
|
# Name des Test-Programms
|
||||||
echo "needs to be implemented"
|
TEST_BIN = test_runner
|
||||||
|
|
||||||
|
# Das Target 'unitTests' baut das Test-Programm und führt es aus
|
||||||
|
# Es benötigt unittest.o und stack.o (damit der Stack getestet werden kann)
|
||||||
|
unitTests: unittest.o stack.o
|
||||||
|
@echo "--- Erstelle Test-Executable ---"
|
||||||
|
$(CC) $(FLAGS) unittest.o stack.o -o $(TEST_BIN)
|
||||||
|
@echo "--- Starte Tests ---"
|
||||||
|
./$(TEST_BIN)
|
||||||
|
|
||||||
|
# Regel, um unittest.c zu kompilieren
|
||||||
|
unittest.o: test_stack.c
|
||||||
|
$(CC) -c $(FLAGS) test_stack.c -o unittest.o
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
del /f *.o doble
|
del /f *.o doble $(TEST_BIN)
|
||||||
else
|
else
|
||||||
rm -f *.o doble
|
rm -f *.o doble $(TEST_BIN)
|
||||||
endif
|
endif
|
||||||
32
stack.c
32
stack.c
@ -8,26 +8,50 @@
|
|||||||
* `clearStack`: gibt den gesamten Speicher frei. */
|
* `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) // es gibt bereits ein stack element, dieses wird übergeben + die daten, die in den stack müssen
|
||||||
{
|
{
|
||||||
|
if(!stack || !data)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
StackNode *newNode = calloc(1, sizeof(StackNode)); //Speicher reserviert für data und prev, jeweils nur zeiger
|
||||||
|
if (!newNode)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
newNode->prev = stack;
|
||||||
|
|
||||||
|
newNode->data = data;
|
||||||
|
|
||||||
|
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)
|
||||||
|
return 0;
|
||||||
|
StackNode *prev = stack->prev;
|
||||||
|
free(stack);
|
||||||
|
return prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
void *top(StackNode *stack)
|
void *top(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
if(!stack)
|
||||||
|
return 0;
|
||||||
|
return stack->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack) //Annahme: erstes Element: prev = NULL;
|
||||||
{
|
{
|
||||||
|
if(!stack)
|
||||||
|
return;
|
||||||
|
|
||||||
|
StackNode *stack_tmp = stack;
|
||||||
|
|
||||||
|
while(stack_tmp) {
|
||||||
|
stack_tmp = pop(stack_tmp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
4
stack.h
4
stack.h
@ -8,6 +8,10 @@ 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 node {
|
||||||
|
void* data;
|
||||||
|
struct node* prev;
|
||||||
|
} 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);
|
||||||
|
|||||||
89
test_stack.c
Normal file
89
test_stack.c
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
void test_pushFailsOnNullPointer(StackNode *stack, void *data) {
|
||||||
|
|
||||||
|
StackNode* test = push(NULL, data);
|
||||||
|
if (test != 0) {
|
||||||
|
printf("Pass test_pushFailsOnNullPointerStack\n");
|
||||||
|
} else
|
||||||
|
printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED Stac Node\n");
|
||||||
|
|
||||||
|
StackNode* test = push(stack, NULL);
|
||||||
|
if (test == 0) {
|
||||||
|
printf("Pass test_pushFailsOnNullPointerData\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf("Did Not Pass test_pushFailsOnNullPointerData EXPECTED 0\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_popFailsOnNullPointer() {
|
||||||
|
|
||||||
|
StackNode* test = pop(NULL);
|
||||||
|
if (test == 0) {
|
||||||
|
printf("Pass test_pushFailsOnNullPointerStack\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED 0\n");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_topFailsOnNullPointer() {
|
||||||
|
|
||||||
|
int* test = top(NULL);
|
||||||
|
if (test == 0) {
|
||||||
|
printf("Pass test_pushFailsOnNullPointerStack\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
printf("Did Not Pass test_pushFailsOnNullPointerStack EXPECTED 0\n");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
StackNode *stack0 = NULL;
|
||||||
|
int test0 = 3;
|
||||||
|
int* dataTest0 = &test0;
|
||||||
|
stack0->data = dataTest0;
|
||||||
|
|
||||||
|
char test1[5] = "test\0";
|
||||||
|
char* dataTest1 = &test1;
|
||||||
|
|
||||||
|
float test2 = 3.14;
|
||||||
|
float* dataTest2 = &test2;
|
||||||
|
|
||||||
|
printf("============================\nstack tests\n============================\n");
|
||||||
|
test_pushFailsOnNullPointer(stack0, dataTest0);
|
||||||
|
test_popFailsOnNullPointer();
|
||||||
|
test_topFailsOnNullPointer();
|
||||||
|
|
||||||
|
StackNode *stack1 = push(stack0, dataTest1);
|
||||||
|
if(strcmp(stack1->data ,dataTest1) == 0) {
|
||||||
|
printf("Pass test_pushString\n");
|
||||||
|
} else
|
||||||
|
printf("Fails test_pushString\n expected: %s\n was: %s\n", dataTest1, stack1->data);
|
||||||
|
|
||||||
|
StackNode *stack2 = push(stack1, dataTest2);
|
||||||
|
if(stack2->data == dataTest2) {
|
||||||
|
printf("Pass test_pushFloat\n");
|
||||||
|
} else
|
||||||
|
printf("Fails test_pushFloat\n expected: %f\n was: %d\n", dataTest2, stack2->data);
|
||||||
|
|
||||||
|
int array[10] = {1,2,3,4,5,6,7,8,9,10};
|
||||||
|
|
||||||
|
for(size_t i = 0; i<10; i++) {
|
||||||
|
stack2 = push(stack2, &array[i]);
|
||||||
|
}
|
||||||
|
for(size_t i = 0; i<10; i++) {
|
||||||
|
int* data = top(stack2);
|
||||||
|
printf("%d\n", data);
|
||||||
|
stack2 = pop(stack2);
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user