generated from freudenreichan/info2Praktikum-DobleSpiel
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63e7c18db6 | ||
|
|
2c35bafba9 | ||
|
|
bc24ca7b46 | ||
|
|
b3c9fec7ba | ||
|
|
c860a197ee | ||
|
|
bfcb9ed426 | ||
|
|
5859268475 | ||
|
|
be5c1754f9 |
+144
@@ -0,0 +1,144 @@
|
|||||||
|
#include "unity.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "bintree.h"
|
||||||
|
|
||||||
|
static int compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b); // a und b werden in int konvertiert und deren Werte miteinander verglichen
|
||||||
|
// returns 1 for a>b or -1 for a<b
|
||||||
|
// in bintree.c wird ueberprueft, ob compare eine positive oder eine negative Zahl zurueckgibt,
|
||||||
|
// wenn a groeßer b, positiv und dann wird links nach Teilbauemen gesucht
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUp() {}
|
||||||
|
void tearDown() {}
|
||||||
|
|
||||||
|
//Adds a single element to the tree
|
||||||
|
void test_add_single_element_to_Tree()
|
||||||
|
{
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int value = 5;
|
||||||
|
int duplicate = -1;
|
||||||
|
|
||||||
|
root = addToTree(root, &value, sizeof(int), compare, &duplicate);
|
||||||
|
|
||||||
|
TEST_ASSERT_NOT_NULL(root); //uberprueft, ob root dem Tree hinzugefuegt werden konnte
|
||||||
|
TEST_ASSERT_EQUAL_INT(5, *(int*)root->data); //ueberprueft, ob der Wert fuer data richtig uebernommen wurde
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, duplicate); //ueberprueft, ob isDuplicate 0 gesetzt wurde (neue Knoten -> isDuplicate sollte 0 sein)
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Adds multiplie elements to a tree
|
||||||
|
void test_add_multiple_elements_to_Tree()
|
||||||
|
{
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int value[] = {2, 5, 7, 9};
|
||||||
|
int duplicate = -1;
|
||||||
|
|
||||||
|
for(int j = 0; j < 4; ++j)
|
||||||
|
{
|
||||||
|
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(4, treeSize(root));
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Detects the size of a tree
|
||||||
|
void test_detect_empty_size()
|
||||||
|
{
|
||||||
|
TEST_ASSERT_EQUAL_INT(0, treeSize(NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
//checks, wether size of tree is correctly determined and wether clearTree() works
|
||||||
|
// Test: Duplikate nicht erlaubt (isDuplicate != NULL)
|
||||||
|
void test_detect_size() {
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
|
||||||
|
int duplicate = 0; // wird pro Einfügen gesetzt
|
||||||
|
|
||||||
|
for (int j = 0; j < 10; ++j) {
|
||||||
|
root = addToTree(root, &values[j], sizeof(int), compare, &duplicate);
|
||||||
|
if (duplicate) {
|
||||||
|
// Optional: prüfen, dass ein Duplikat erkannt wurde
|
||||||
|
TEST_ASSERT_TRUE(duplicate == 1);
|
||||||
|
}
|
||||||
|
duplicate = 0; // zurücksetzen für nächstes Einfügen
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prüfen der Baumgröße ohne Duplikate
|
||||||
|
TEST_ASSERT_EQUAL_INT(8, treeSize(root));
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Test: Duplikate erlaubt (isDuplicate == NULL)
|
||||||
|
void test_add_multiplie_elements_one_dup() {
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
|
||||||
|
|
||||||
|
for (int j = 0; j < 10; ++j) {
|
||||||
|
root = addToTree(root, &values[j], sizeof(int), compare, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Alle Werte inklusive Duplikate
|
||||||
|
TEST_ASSERT_EQUAL_INT(10, treeSize(root));
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Traverses the tree inorder to check wether nextTreeData works
|
||||||
|
// Hilfsfunktion: rekursive Inorder-Prüfung
|
||||||
|
void inorderCheck(TreeNode *node, int expected[], int *idx) {
|
||||||
|
if (node == NULL) return;
|
||||||
|
|
||||||
|
// Linken Teilbaum prüfen
|
||||||
|
inorderCheck(node->left, expected, idx);
|
||||||
|
|
||||||
|
// Aktuelles Element prüfen
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected[*idx], *(int*)node->data);
|
||||||
|
(*idx)++;
|
||||||
|
|
||||||
|
// Rechten Teilbaum prüfen
|
||||||
|
inorderCheck(node->right, expected, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_inorder() {
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int values[] = {5, 3, 7, 2, 4, 6, 8};
|
||||||
|
|
||||||
|
// Baum füllen
|
||||||
|
for (int i = 0; i < 7; i++) {
|
||||||
|
root = addToTree(root, &values[i], sizeof(int), compare, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Erwartete Inorder-Reihenfolge
|
||||||
|
int expected[] = {2,3,4,5,6,7,8};
|
||||||
|
|
||||||
|
int idx = 0;
|
||||||
|
inorderCheck(root, expected, &idx);
|
||||||
|
|
||||||
|
// Alle Einträge geprüft?
|
||||||
|
TEST_ASSERT_EQUAL_INT(7, idx);
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
RUN_TEST(test_add_single_element_to_Tree);
|
||||||
|
RUN_TEST(test_add_multiple_elements_to_Tree);
|
||||||
|
RUN_TEST(test_add_multiplie_elements_one_dup);
|
||||||
|
RUN_TEST(test_detect_empty_size);
|
||||||
|
RUN_TEST(test_detect_size);
|
||||||
|
RUN_TEST(test_inorder);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
@@ -31,7 +31,7 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
newNode->left = NULL;
|
newNode->left = NULL;
|
||||||
newNode->right = NULL;
|
newNode->right = NULL;
|
||||||
|
|
||||||
if(isDuplicate!= NULL)
|
if(isDuplicate!= NULL) //wenn Zeiger isDUplicate auf einen Wert zeigt, wird isDuplicate auf 0 gesetzt
|
||||||
{
|
{
|
||||||
*isDuplicate = 0;
|
*isDuplicate = 0;
|
||||||
}
|
}
|
||||||
@@ -66,14 +66,42 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
// push the top node and push all its left nodes.
|
// push the top node and push all its left nodes.
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
if(root == NULL)
|
static StackNode *stack = NULL;
|
||||||
{
|
|
||||||
|
|
||||||
|
// 1) Falls neuer Baum übergeben wurde → Initialisieren
|
||||||
|
if (root != NULL)
|
||||||
|
{
|
||||||
|
// alten Stack leeren
|
||||||
|
while (stack != NULL)
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
// alle linken Knoten pushen
|
||||||
|
while (root != NULL) {
|
||||||
|
stack = push(stack, root);
|
||||||
|
root = root->left;
|
||||||
}
|
}
|
||||||
stackNode.top(root);
|
}
|
||||||
|
|
||||||
|
// 2) Wenn Stack leer → fertig
|
||||||
|
if (stack == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// 3) Top-Knoten holen
|
||||||
|
TreeNode *node = (TreeNode *)top(stack);
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
// 4) Wenn rechter Teilbaum existiert → alle linken Knoten pushen
|
||||||
|
TreeNode *right = node->right;
|
||||||
|
while (right != NULL) {
|
||||||
|
stack = push(stack, right);
|
||||||
|
right = right->left;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5) Daten zurückgeben
|
||||||
|
return node->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
@@ -98,7 +126,7 @@ unsigned int treeSize(const TreeNode *root)
|
|||||||
{
|
{
|
||||||
if(root == NULL)
|
if(root == NULL)
|
||||||
{
|
{
|
||||||
return numNodes;
|
return 0;
|
||||||
}
|
}
|
||||||
return 1 + treeSize(root->left) + treeSize(root->right); //1, weil eine Wurzel gefunden wurde und dann immer plus eins fuer einen Teilbaum
|
return 1 + treeSize(root->left) + treeSize(root->right); //1, weil eine Wurzel gefunden wurde und dann immer plus eins fuer einen Teilbaum
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -1,2 +1,3 @@
|
|||||||
Silvana;9944
|
Silvana;9944
|
||||||
|
hannes;9910
|
||||||
player1;3999
|
player1;3999
|
||||||
|
|||||||
@@ -1,66 +1,66 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
|
FLAGS = -g -Wall -lm
|
||||||
|
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
include makefile_windows.variables
|
||||||
|
else
|
||||||
|
UNAME = $(shell uname)
|
||||||
|
ifeq ($(UNAME),Linux)
|
||||||
|
include makefile_linux.variables
|
||||||
|
else
|
||||||
|
include makefile_mac.variables
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
raylibfolder = ./raylib
|
raylibfolder = ./raylib
|
||||||
unityfolder = ./unity
|
unityfolder = ./unity
|
||||||
|
|
||||||
|
|
||||||
FLAGS = -g -Wall -I$(unityfolder)
|
|
||||||
|
|
||||||
|
|
||||||
ifeq ($(OS),Windows_NT)
|
|
||||||
include makefile_windows.variables
|
|
||||||
else
|
|
||||||
UNAME := $(shell uname)
|
|
||||||
ifeq ($(UNAME),Linux)
|
|
||||||
include makefile_linux.variables
|
|
||||||
else
|
|
||||||
include makefile_mac.variables
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Objektdateien
|
# Initiales Programm bauen (zum ausprobieren)
|
||||||
# --------------------------
|
# --------------------------
|
||||||
program_obj_files := stack.o bintree.o numbers.o timer.o highscore.o
|
|
||||||
|
|
||||||
|
|
||||||
%.o: %.c
|
|
||||||
$(CC) $(FLAGS) -c $< -o $@
|
|
||||||
|
|
||||||
|
|
||||||
doble: main.o $(program_obj_files)
|
|
||||||
$(CC) $(FLAGS) $^ -o doble
|
|
||||||
|
|
||||||
|
|
||||||
doble_initial:
|
doble_initial:
|
||||||
$(CC) -o doble_initial $(BINARIES)/libdoble_complete.a
|
$(CC) -o doble_initial $(BINARIES)/libdoble_complete.a
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# Selbst implementiertes Programm bauen
|
||||||
|
# --------------------------
|
||||||
|
program_obj_files = stack.o bintree.o numbers.o timer.o highscore.o
|
||||||
|
|
||||||
|
doble : main.o $(program_obj_files)
|
||||||
|
$(CC) $(FLAGS) $^ -o doble
|
||||||
|
|
||||||
|
$(program_obj_filesobj_files): %.o: %.c
|
||||||
|
$(CC) -c $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
unity_src = $(unityfolder)/unity.c
|
||||||
|
|
||||||
unitTests:
|
unitTests: numbersTest stackTest bintreeTest
|
||||||
@echo "needs to be implemented"
|
# ./runNumbersTest
|
||||||
|
# ./runStackTest
|
||||||
|
./runBintreeTest
|
||||||
|
|
||||||
|
numbersTest: numbers.o bintree.o stack.o numbersTest.c $(unity_src)
|
||||||
|
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runNumbersTest
|
||||||
|
|
||||||
binTreeTest: stack.o bintree.o binTreeTest.c $(unityfolder)/unity.c
|
stackTest: stack.o stackTest.c $(unity_src)
|
||||||
$(CC) $(FLAGS) -o runbinTreeTest binTreeTest.c bintree.o stack.o $(unityfolder)/unity.c
|
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runStackTests
|
||||||
|
|
||||||
|
binTreeTest: bintree.o binTreeTest.c $(unity_src) stack.o
|
||||||
|
$(CC) $(CFLAGS) $(LDFLAGS) -I$(unityfolder) $^ -o runBinTreeTest
|
||||||
|
|
||||||
test_numbers: numbers_no_tree.o bintree.o stack.o test_numbers.c $(unityfolder)/unity.c
|
%.o: %.c
|
||||||
$(CC) $(FLAGS) -o run_numbersTests test_numbers.c numbers_no_tree.o bintree.o stack.o $(unityfolder)/unity.c
|
$(CC) -c $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
|
|
||||||
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
|
|
||||||
$(CC) $(FLAGS) -o runtest_stack test_stack.c stack.o $(unityfolder)/unity.c
|
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Cleaning
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
del /f *.o doble runstackTests run_numbersTests runbintreeTests
|
del /f *.o doble
|
||||||
else
|
else
|
||||||
rm -f *.o doble runstackTests run_numbersTests runbintreeTests
|
rm -f *.o doble
|
||||||
endif
|
endif
|
||||||
@@ -14,13 +14,87 @@
|
|||||||
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
|
||||||
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
|
// Returns len random numbers between 1 and 2*len in random order,
|
||||||
|
// all different, except for exactly one duplicate (two entries the same).
|
||||||
|
// Uses your binary search tree implementation to check for duplicates while generating numbers.
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (len < 2)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int *numbers = malloc(len * sizeof(unsigned int));
|
||||||
|
if (!numbers)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
TreeNode *root = NULL; // Baum anfänglich leer
|
||||||
|
unsigned int count = 0;
|
||||||
|
|
||||||
|
// Zufallszahlen generieren, bis das Array voll ist
|
||||||
|
while (count < len) {
|
||||||
|
unsigned int random = (rand() % (2 * len)) + 1;
|
||||||
|
|
||||||
|
int duplicate = 0; // Anfangswert für Duplikat-Check
|
||||||
|
root = addToTree(root, &random, sizeof(random), compareFct, &duplicate);
|
||||||
|
|
||||||
|
if (root == NULL) {
|
||||||
|
free(numbers);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!duplicate) { // Zahl war neu → ins Array einfügen
|
||||||
|
numbers[count++] = random;
|
||||||
|
}
|
||||||
|
// duplicate == 1 → Zahl existiert schon, neue Zahl generieren
|
||||||
|
}
|
||||||
|
|
||||||
|
// Jetzt len eindeutige Zahlen erzeugt → ein Duplikat erzwingen
|
||||||
|
unsigned int idx1 = rand() % len;
|
||||||
|
unsigned int idx2 = rand() % len;
|
||||||
|
while (idx2 == idx1) // sicherstellen, dass es eine andere Position ist
|
||||||
|
idx2 = rand() % len;
|
||||||
|
|
||||||
|
numbers[idx2] = numbers[idx1];
|
||||||
|
|
||||||
|
// Baum wieder freigeben
|
||||||
|
clearTree(root);
|
||||||
|
|
||||||
|
return numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Jetzt len eindeutige Zahlen erzeugt ⇒ wir müssen ein Duplikat erzwingen
|
||||||
|
unsigned int idx1 = rand() % len;
|
||||||
|
unsigned int idx2 = rand() % len;
|
||||||
|
while (idx2 == idx1)
|
||||||
|
idx2 = rand() % len;
|
||||||
|
|
||||||
|
numbers[idx2] = numbers[idx1]; // zweites Exemplar
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
return numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
||||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||||
{
|
{
|
||||||
|
if(len>0)
|
||||||
|
{
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
for(unsigned int i=0;i<len;i++)
|
||||||
|
{
|
||||||
|
unsigned int v1 = numbers[i];
|
||||||
|
for(unsigned int j=i+1;j<len;j++)
|
||||||
|
{
|
||||||
|
unsigned int v2 = numbers[j];
|
||||||
|
if(v1==v2)
|
||||||
|
{
|
||||||
|
return v1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@@ -10,11 +10,7 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
if (!data)
|
if(stack && data){
|
||||||
{
|
|
||||||
return stack; //Nichts pushen
|
|
||||||
}
|
|
||||||
//if(stack && data){
|
|
||||||
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
StackNode *t = (StackNode *)malloc(sizeof(StackNode));
|
||||||
if(!t)
|
if(!t)
|
||||||
{
|
{
|
||||||
@@ -23,7 +19,7 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
t->next = stack;
|
t->next = stack;
|
||||||
t->data = data;
|
t->data = data;
|
||||||
return t; //Gibt den ersten StackNode des Stacks zurueck
|
return t; //Gibt den ersten StackNode des Stacks zurueck
|
||||||
//}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,11 +27,12 @@ StackNode *push(StackNode *stack, void *data)
|
|||||||
// freed by caller.)
|
// freed by caller.)
|
||||||
StackNode *pop(StackNode *stack)
|
StackNode *pop(StackNode *stack)
|
||||||
{
|
{
|
||||||
if(stack == NULL)
|
if(stack)
|
||||||
{
|
{
|
||||||
return NULL;
|
StackNode *t = stack->next; //Naechstes Element im Stack wird erstes Element
|
||||||
|
free(stack);
|
||||||
|
return t;
|
||||||
}
|
}
|
||||||
return stack->next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Returns the data of the top element.
|
||||||
@@ -53,8 +50,8 @@ void clearStack(StackNode *stack)
|
|||||||
{
|
{
|
||||||
while(stack)
|
while(stack)
|
||||||
{
|
{
|
||||||
StackNode *tmp = stack; //merkt sich den momentanen obersten Knoten
|
StackNode *tmp = stack;
|
||||||
stack = stack->next; //setzt den obersten Knoten auf den Zweiten im Stack
|
stack = stack->next;
|
||||||
free(tmp->data);
|
free(tmp->data);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,12 @@ 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 {
|
typedef struct Node{
|
||||||
void* data;
|
void* data;
|
||||||
struct StackNode *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);
|
||||||
|
|
||||||
|
|||||||
@@ -1,72 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "stack.h"
|
|
||||||
|
|
||||||
//Testfunkionen zu push, pull, top & clearStack schreiben
|
|
||||||
|
|
||||||
void setUp()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void tearDown()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void test(char *name, int condition) {
|
|
||||||
if (condition) {
|
|
||||||
printf("[OK] %s\n", name);
|
|
||||||
} else {
|
|
||||||
printf("[FAIL] %s\n", name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
|
|
||||||
StackNode *stack = NULL;
|
|
||||||
|
|
||||||
// Werte dynamisch anlegen
|
|
||||||
int *val1 = malloc(sizeof(int));
|
|
||||||
*val1 = 5;
|
|
||||||
stack = push(stack, val1);
|
|
||||||
test("push(5) legt 5 oben auf den Stack", *(int*)stack->data == 5);
|
|
||||||
|
|
||||||
int *val2 = malloc(sizeof(int));
|
|
||||||
*val2 = 6;
|
|
||||||
stack = push(stack, val2);
|
|
||||||
test("push(6) legt 6 oben auf den Stack", *(int*)stack->data == 6);
|
|
||||||
|
|
||||||
int *val3 = malloc(sizeof(int));
|
|
||||||
*val3 = 24;
|
|
||||||
stack = push(stack, val3);
|
|
||||||
test("push(24) legt 24 oben auf den Stack", *(int*)stack->data == 24);
|
|
||||||
|
|
||||||
// Test top()
|
|
||||||
int t = *(int*)top(stack);
|
|
||||||
test("top() liefert 24", t == 24);
|
|
||||||
|
|
||||||
// Test pop()
|
|
||||||
StackNode *tmp;
|
|
||||||
|
|
||||||
tmp = stack;
|
|
||||||
stack = pop(stack);
|
|
||||||
free(tmp->data); // Daten freigeben
|
|
||||||
free(tmp); // Knoten freigeben
|
|
||||||
test("pop() entfernt 24, 6 ist jetzt oben", *(int*)stack->data == 6);
|
|
||||||
|
|
||||||
tmp = stack;
|
|
||||||
stack = pop(stack);
|
|
||||||
free(tmp->data);
|
|
||||||
free(tmp);
|
|
||||||
test("pop() entfernt 6, 5 ist jetzt oben", *(int*)stack->data == 5);
|
|
||||||
|
|
||||||
tmp = stack;
|
|
||||||
stack = pop(stack);
|
|
||||||
free(tmp->data);
|
|
||||||
free(tmp);
|
|
||||||
test("pop() entfernt 5, Stack ist jetzt leer", stack == NULL);
|
|
||||||
|
|
||||||
// Am Ende Stack leeren (falls noch Elemente übrig)
|
|
||||||
clearStack(stack);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Binary file not shown.
Reference in New Issue
Block a user