3 Commits
Author SHA1 Message Date
maysi98860 31a76162ee Merge pull request 'simons_zweig' (#1) from simons_zweig into main
Reviewed-on: #1
2025-11-25 12:55:32 +00:00
Simon 4cfe6d9c50 added test_push and fixed stack cast 2025-11-24 17:53:43 +01:00
Simon 82c72eaf81 first tests 2025-11-24 16:05:31 +01:00
6 changed files with 247 additions and 269 deletions
+4 -4
View File
@@ -1,4 +1,4 @@
doble_initial.exe doble_initial.exe
highscores.txt highscores.txt
runStackTest.exe runStackTest.exe
stack.o stack.o
+48 -48
View File
@@ -1,49 +1,49 @@
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
unityfolder = ./unity 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
# -------------------------- # --------------------------
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 $(program_obj_filesobj_files): %.o: %.c
$(CC) -c $(FLAGS) $^ -o $@ $(CC) -c $(FLAGS) $^ -o $@
# -------------------------- # --------------------------
# Unit Tests # Unit Tests
# -------------------------- # --------------------------
unitTests: stack.o test_stack.c $(unityfolder)/unity.c unitTests: stack.o test_stack.c $(unityfolder)/unity.c
$(CC) $(FLAGS) -I$(unityfolder) -o runStackTest test_stack.c stack.o $(unityfolder)/unity.c $(CC) $(FLAGS) -I$(unityfolder) -o runStackTest test_stack.c stack.o $(unityfolder)/unity.c
# -------------------------- # --------------------------
# Clean # Clean
# -------------------------- # --------------------------
clean: clean:
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
del /f *.o doble del /f *.o doble
else else
rm -f *.o doble rm -f *.o doble
endif endif
+90 -90
View File
@@ -1,91 +1,91 @@
#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. */
// 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 *tempNode, *newNode; StackNode *tempNode, *newNode;
newNode = malloc(sizeof(StackNode)); newNode = malloc(sizeof(StackNode));
newNode->value = 3; newNode->value = *(int *)data;
newNode->next = NULL; newNode->next = NULL;
if (stack == NULL) if (stack == NULL)
{ {
stack = newNode; stack = newNode;
return stack; return stack;
} }
tempNode = stack; tempNode = stack;
while (tempNode->next != NULL) while (tempNode->next != NULL)
{ {
tempNode = tempNode->next; tempNode = tempNode->next;
} }
tempNode->next = newNode; tempNode->next = newNode;
return stack; 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)
{ {
StackNode *tempNode; StackNode *tempNode;
if (stack == NULL) if (stack == NULL)
{ {
return stack; return stack;
} }
tempNode = stack; tempNode = stack;
while (tempNode->next->next != NULL) while (tempNode->next->next != NULL)
{ {
tempNode = tempNode->next; tempNode = tempNode->next;
} }
free(tempNode->next); free(tempNode->next);
tempNode->next = NULL; tempNode->next = NULL;
return stack; return stack;
} }
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack) void *top(StackNode *stack)
{ {
StackNode *tempNode; StackNode *tempNode;
if (stack == NULL) if (stack == NULL)
{ {
return NULL; return NULL;
} }
tempNode = stack; tempNode = stack;
while (tempNode->next != NULL) while (tempNode->next != NULL)
{ {
tempNode = tempNode->next; tempNode = tempNode->next;
} }
return &tempNode->value; return &tempNode->value;
} }
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
StackNode *tempNode; StackNode *tempNode;
if (stack == NULL) if (stack == NULL)
{ {
return; return;
} }
tempNode = stack; tempNode = stack;
while (tempNode != NULL) while (tempNode != NULL)
{ {
tempNode = pop(tempNode); tempNode = pop(tempNode);
} }
} }
+29 -29
View File
@@ -1,29 +1,29 @@
#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 //TODO: passenden Datentyp als struct anlegen
typedef struct Node { typedef struct Node {
int value; int value;
struct Node* next; struct Node* next;
} StackNode; } 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);
// 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);
// Returns the data of the top element. // Returns the data of the top element.
void *top(StackNode *stack); void *top(StackNode *stack);
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack); void clearStack(StackNode *stack);
#endif #endif
+66 -88
View File
@@ -1,89 +1,67 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "stack.h" #include "stack.h"
#include "unity.h" #include "unity.h"
StackNode* setup(int value, StackNode* next) { void test_push(void)
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap {
if (node == NULL) { StackNode *testNode;
perror("malloc failed"); int data = 1;
exit(EXIT_FAILURE); // or handle the error differently
} // Test für leeren Stack
node->value = value; testNode = push(NULL, &data);
node->next = next; TEST_ASSERT_NOT_NULL(&testNode);
return node; TEST_ASSERT_NULL(testNode->next);
} TEST_ASSERT_EQUAL_INT(1, testNode->value);
void test_pop(void) data = 2;
{
StackNode* node2 = setup(3, NULL); // Test für nicht leeren Stack
StackNode* node1 = setup(2, node2); testNode = push(testNode, &data);
StackNode* header = setup(1, node1); TEST_ASSERT_NOT_NULL(&testNode);
StackNode* temp; TEST_ASSERT_NOT_NULL(testNode->next);
TEST_ASSERT_NULL(testNode->next->next);
temp = pop(header); TEST_ASSERT_EQUAL_INT(1, testNode->value);
int after = 0; TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
while(temp) }
{
after++; void test_pop(void)
temp = temp->next; {
}
TEST_ASSERT_NULL(pop(NULL));
TEST_ASSERT_EQUAL_INT(2, after); }
TEST_ASSERT_NULL(node1->next);
} void test_top(void)
{
void test_top(void)
{ TEST_ASSERT_NULL(top(NULL));
StackNode* node2 = setup(3, NULL); }
StackNode* node1 = setup(2, node2);
StackNode* header = setup(1, node1); void test_clear(void)
{
int data = *(int *)top(header);
TEST_ASSERT_EQUAL_INT(node2->value, data); // TEST_ASSERT_NULL(clearStack(NULL));
} }
void test_clear() void setUp(void) {
{ // Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
StackNode* node2 = setup(3, NULL); }
StackNode* node1 = setup(2, node2);
StackNode* header = setup(1, node1); void tearDown(void) {
StackNode* temp; // Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
clearStack(header);
temp = header; int main()
{
int after = 0; UNITY_BEGIN();
while(temp)
{ printf("============================\nStack tests\n============================\n");
after++;
temp = temp->next; RUN_TEST(test_push);
} RUN_TEST(test_pop);
RUN_TEST(test_top);
RUN_TEST(test_clear);
TEST_ASSERT_NULL(after);
} return UNITY_END();
void setUp(void)
{
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
}
void tearDown(void)
{
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
}
int main()
{
UNITY_BEGIN();
printf("============================\nStack tests\n============================\n");
RUN_TEST(test_pop);
RUN_TEST(test_top);
RUN_TEST(test_clear);
return UNITY_END();
} }
+10 -10
View File
@@ -1,10 +1,10 @@
#ifndef UNITTTESTS_H #ifndef UNITTTESTS_H
#define UNITTTESTS_H #define UNITTTESTS_H
#include <stdio.h> #include <stdio.h>
typedef int (*UnitTestType)(void); typedef int (*UnitTestType)(void);
#define RUN_UNIT_TEST(fct) printf("%80s: %d\n", #fct, fct()) #define RUN_UNIT_TEST(fct) printf("%80s: %d\n", #fct, fct())
#endif #endif