Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e4336ce97 | ||
|
|
e5c5f53ad4 | ||
|
|
5900d759bd | ||
|
|
6b3294d40b | ||
|
|
a018971c04 | ||
| 35b09294bc | |||
|
|
fc3508140a | ||
|
|
a3b951d0a9 | ||
| f8c297f84d | |||
|
|
804294b96e | ||
| 5b576589e3 | |||
| bf0a0b3f40 | |||
| d54dd3eb6f | |||
|
|
8051686a37 | ||
|
|
39965a95c4 | ||
|
|
8b0fa4601a | ||
|
|
f59489779b | ||
| 31a76162ee | |||
| b76ffa054a | |||
|
|
4cfe6d9c50 | ||
|
|
82c72eaf81 | ||
|
|
a027c070d2 | ||
|
|
94517bf236 |
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
doble_initial.exe
|
||||||
|
highscores.txt
|
||||||
|
runStackTest.exe
|
||||||
|
stack.o
|
||||||
|
runNumbersTest.exe
|
||||||
|
numbers.o
|
||||||
|
.vscode/launch.json
|
||||||
|
.vscode/settings.json
|
||||||
|
*.o
|
||||||
|
*.exe
|
||||||
|
runBintreeTest
|
||||||
Binary file not shown.
88
bintree.c
88
bintree.c
@ -1,18 +1,57 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
//TODO: binären Suchbaum implementieren
|
static StackNode *stack;
|
||||||
|
static TreeNode *tree = NULL;
|
||||||
|
// TODO: binären Suchbaum implementieren
|
||||||
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
/* * `addToTree`: fügt ein neues Element in den Baum ein (rekursiv),
|
||||||
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
* `clearTree`: gibt den gesamten Baum frei (rekursiv),
|
||||||
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
* `treeSize`: zählt die Knoten im Baum (rekursiv),
|
||||||
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
* `nextTreeData`: Traversierung mit Hilfe des zuvor implementierten Stacks. */
|
||||||
|
|
||||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
||||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||||
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
|
||||||
{
|
{
|
||||||
|
if (root == NULL)
|
||||||
|
{
|
||||||
|
TreeNode *newNode = malloc(sizeof(TreeNode));
|
||||||
|
newNode->data = malloc(dataSize);
|
||||||
|
if (newNode->data == NULL)
|
||||||
|
{
|
||||||
|
return NULL; // Fehler
|
||||||
|
}
|
||||||
|
memcpy(newNode->data, data, dataSize);
|
||||||
|
newNode->left = NULL;
|
||||||
|
newNode->right = NULL;
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmp = compareFct(root->data, data);
|
||||||
|
if (cmp < 0)
|
||||||
|
{
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else if (cmp > 0)
|
||||||
|
{
|
||||||
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (isDuplicate != NULL)
|
||||||
|
{
|
||||||
|
*isDuplicate = 1;
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
|
||||||
@ -20,17 +59,56 @@ 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)
|
||||||
|
{
|
||||||
|
clearStack(stack);
|
||||||
|
buildStack(root);
|
||||||
|
}
|
||||||
|
if(stack != NULL)
|
||||||
|
{
|
||||||
|
void* data = top(stack);
|
||||||
|
stack = pop(stack);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Releases all memory resources (including data copies).
|
// Releases all memory resources (including data copies).
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
if (root == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (root->left != NULL)
|
||||||
|
{
|
||||||
|
clearTree(root->left);
|
||||||
|
free(root->left);
|
||||||
|
root->left = NULL;
|
||||||
|
}
|
||||||
|
if (root->right != NULL)
|
||||||
|
{
|
||||||
|
clearTree(root->right);
|
||||||
|
free(root->right);
|
||||||
|
root->right = NULL;
|
||||||
|
}
|
||||||
|
root = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root)
|
unsigned int treeSize(const TreeNode *root)
|
||||||
{
|
{
|
||||||
|
return root == NULL ? 0 : treeSize(root->left) + treeSize(root->right) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void buildStack(TreeNode *root)
|
||||||
|
{
|
||||||
|
if (root == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
buildStack(root->left);
|
||||||
|
stack = push(stack, root->data);
|
||||||
|
buildStack(root->right);
|
||||||
}
|
}
|
||||||
@ -24,4 +24,6 @@ void clearTree(TreeNode *root);
|
|||||||
// Returns the number of entries in the tree given by root.
|
// Returns the number of entries in the tree given by root.
|
||||||
unsigned int treeSize(const TreeNode *root);
|
unsigned int treeSize(const TreeNode *root);
|
||||||
|
|
||||||
|
void buildStack(TreeNode *root);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -79,6 +79,8 @@ int addHighscore(const char *name, double timeInSeconds, unsigned int len)
|
|||||||
{
|
{
|
||||||
HighscoreEntry entry = createHighscoreEntry(name, calculateScore(timeInSeconds, len));
|
HighscoreEntry entry = createHighscoreEntry(name, calculateScore(timeInSeconds, len));
|
||||||
highscoreTree = addToTree(highscoreTree, &entry, sizeof(entry), compareHighscoreEntries, NULL);
|
highscoreTree = addToTree(highscoreTree, &entry, sizeof(entry), compareHighscoreEntries, NULL);
|
||||||
|
//HighscoreEntry *temp = highscoreTree->data;
|
||||||
|
//printf("%s%d\n", temp->name, temp->score);
|
||||||
|
|
||||||
return entry.score;
|
return entry.score;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1 +1,5 @@
|
|||||||
player1;3999
|
nick;9963
|
||||||
|
nick;9946
|
||||||
|
simon;4965
|
||||||
|
alex;2996
|
||||||
|
simon;2996
|
||||||
|
|||||||
108
makefile
108
makefile
@ -1,49 +1,61 @@
|
|||||||
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_files): %.o: %.c
|
$(program_obj_filesobj_files): %.o: %.c
|
||||||
$(CC) -c $(FLAGS) $^ -o $@
|
$(CC) -c $(FLAGS) $^ -o $@
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests:
|
stackTests: stack.o test_stack.c $(unityfolder)/unity.c
|
||||||
echo "needs to be implemented"
|
$(CC) $(FLAGS) -I$(unityfolder) -o runStackTest test_stack.c stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# numbers.c Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
clean:
|
numbersTests: numbers.o bintree.o stack.o test_numbers.c $(unityfolder)/unity.c
|
||||||
ifeq ($(OS),Windows_NT)
|
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o bintree.o stack.o $(unityfolder)/unity.c
|
||||||
del /f *.o doble
|
|
||||||
else
|
# --------------------------
|
||||||
rm -f *.o doble
|
# bintree.c Tests
|
||||||
|
# --------------------------
|
||||||
|
bintreeTests: bintree.o stack.o test_bintree.c $(unityfolder)/unity.c
|
||||||
|
$(CC) $(FLAGS) -I$(unityfolder) -o runBintreeTest test_bintree.c bintree.o stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# Clean
|
||||||
|
# --------------------------
|
||||||
|
clean:
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
del /f *.o doble
|
||||||
|
else
|
||||||
|
rm -f *.o doble
|
||||||
endif
|
endif
|
||||||
96
numbers.c
96
numbers.c
@ -5,22 +5,106 @@
|
|||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
//TODO: getDuplicate und createNumbers implementieren
|
// TODO: getDuplicate und createNumbers implementieren
|
||||||
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
||||||
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
|
* Sicherstellen, dass beim Befüllen keine Duplikate entstehen.
|
||||||
* Duplizieren eines zufälligen Eintrags im Array.
|
* Duplizieren eines zufälligen Eintrags im Array.
|
||||||
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
* in `getDuplicate()`: Sortieren des Arrays und Erkennen der doppelten Zahl durch Vergleich benachbarter Elemente. */
|
||||||
|
|
||||||
// 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.
|
||||||
unsigned int *createNumbers(unsigned int len)
|
void duplicateNumber(unsigned int *numbers, unsigned int len)
|
||||||
{
|
{
|
||||||
|
if (!numbers || len < 2)
|
||||||
|
return;
|
||||||
|
|
||||||
|
unsigned int numberPicked = rand() % len; // take random spot in array
|
||||||
|
unsigned int destination = rand() % len; // new spot for duplicated number
|
||||||
|
|
||||||
|
while (destination == numberPicked) // while same spot get a new one
|
||||||
|
destination = rand() % len;
|
||||||
|
|
||||||
|
numbers[destination] = numbers[numberPicked];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns only the only number in numbers which is present twice. Returns zero on errors.
|
/* for qsort
|
||||||
|
-1 num1 should come before num2
|
||||||
|
0 num1 and num2 are equal
|
||||||
|
1 num1 should come after num2
|
||||||
|
*/
|
||||||
|
int compare(const void *num1, const void *num2)
|
||||||
|
{
|
||||||
|
unsigned int temp1 = *(const unsigned int *)num1;
|
||||||
|
unsigned int temp2 = *(const unsigned int *)num2;
|
||||||
|
return (temp1 > temp2) - (temp1 < temp2);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int *createNumbers(unsigned int len)
|
||||||
|
{
|
||||||
|
if (len < 2)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
srand((unsigned)time(NULL));
|
||||||
|
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
unsigned int i = 0;
|
||||||
|
|
||||||
|
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
|
||||||
|
if (!numbers)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < len)
|
||||||
|
{
|
||||||
|
unsigned int random = (rand() % (2 * len)) + 1;
|
||||||
|
int duplicate = 0;
|
||||||
|
|
||||||
|
root = addToTree(root, &random, sizeof(unsigned int), compare, &duplicate);
|
||||||
|
if (!root)
|
||||||
|
{
|
||||||
|
free(numbers); // malloc-Fehler
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!duplicate)
|
||||||
|
numbers[i++] = random;
|
||||||
|
}
|
||||||
|
|
||||||
|
duplicateNumber(numbers, len);
|
||||||
|
clearTree(root);
|
||||||
|
return numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 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 (!numbers || len < 2)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int *copy = (unsigned int *)malloc(len * sizeof(unsigned int));
|
||||||
|
if (!copy)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(copy, numbers, len * sizeof(unsigned int));
|
||||||
|
qsort(copy, len, sizeof(unsigned int), compare);
|
||||||
|
|
||||||
|
unsigned int duplicate = 0;
|
||||||
|
for (unsigned int i = 1; i < len; i++)
|
||||||
|
{
|
||||||
|
if (copy[i] == copy[i - 1])
|
||||||
|
{
|
||||||
|
duplicate = copy[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(copy);
|
||||||
|
return duplicate;
|
||||||
}
|
}
|
||||||
32
stack.c
32
stack.c
@ -1,33 +1,53 @@
|
|||||||
#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 *newNode = malloc(sizeof(StackNode));
|
||||||
|
newNode->data = data;
|
||||||
|
newNode->next = stack; // Set the new node's next pointer to the current top of the stack.
|
||||||
|
return newNode; // Return the new node as the top of the 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)
|
||||||
{
|
{
|
||||||
|
if (stack == NULL)
|
||||||
|
{
|
||||||
|
return NULL; // Nothing to pop if stack is empty.
|
||||||
|
}
|
||||||
|
|
||||||
|
StackNode *tempNode = stack;
|
||||||
|
stack = stack->next; // Move the stack pointer to the next node.
|
||||||
|
free(tempNode); // Free the old top node.
|
||||||
|
|
||||||
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 NULL if stack is empty.
|
||||||
|
}
|
||||||
|
|
||||||
|
return stack->data; // Return the value of the top node.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clears stack and releases all memory.
|
// Clears stack and releases all memory.
|
||||||
void clearStack(StackNode *stack)
|
void clearStack(StackNode *stack)
|
||||||
{
|
{
|
||||||
|
while (stack != NULL)
|
||||||
|
{
|
||||||
|
stack = pop(stack); // Pop each element and free memory.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
54
stack.h
54
stack.h
@ -1,25 +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 {
|
||||||
// Pushes data as pointer onto the stack.
|
void *data;
|
||||||
StackNode *push(StackNode *stack, void *data);
|
struct Node* next;
|
||||||
|
} StackNode;
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
|
||||||
// freed by caller.)
|
// Pushes data as pointer onto the stack.
|
||||||
StackNode *pop(StackNode *stack);
|
StackNode *push(StackNode *stack, void *data);
|
||||||
|
|
||||||
// Returns the data of the top element.
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
void *top(StackNode *stack);
|
// freed by caller.)
|
||||||
|
StackNode *pop(StackNode *stack);
|
||||||
// Clears stack and releases all memory.
|
|
||||||
void clearStack(StackNode *stack);
|
// Returns the data of the top element.
|
||||||
|
void *top(StackNode *stack);
|
||||||
#endif
|
|
||||||
|
// Clears stack and releases all memory.
|
||||||
|
void clearStack(StackNode *stack);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
80
test_bintree.c
Normal file
80
test_bintree.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "bintree.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
|
||||||
|
void sizeTest()
|
||||||
|
{
|
||||||
|
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
|
||||||
|
TreeNode *node1 = (TreeNode *)malloc(sizeof(TreeNode));
|
||||||
|
TreeNode *node2 = (TreeNode *)malloc(sizeof(TreeNode));
|
||||||
|
|
||||||
|
int dataRoot = 2;
|
||||||
|
int dataNode1 = 1;
|
||||||
|
int dataNode2 = 3;
|
||||||
|
|
||||||
|
root->data = &dataRoot;
|
||||||
|
root->left = (TreeNode *)node1;
|
||||||
|
root->right = (TreeNode *)node2;
|
||||||
|
|
||||||
|
node1->data = &dataNode1;
|
||||||
|
node1->left = NULL;
|
||||||
|
node1->right = NULL;
|
||||||
|
|
||||||
|
node2->data = &dataNode2;
|
||||||
|
node2->left = NULL;
|
||||||
|
node2->right = NULL;
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(3,treeSize(root));
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearTest()
|
||||||
|
{
|
||||||
|
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
|
||||||
|
TreeNode *node1 = (TreeNode *)malloc(sizeof(TreeNode));
|
||||||
|
TreeNode *node2 = (TreeNode *)malloc(sizeof(TreeNode));
|
||||||
|
|
||||||
|
int dataRoot = 2;
|
||||||
|
int dataNode1 = 1;
|
||||||
|
int dataNode2 = 3;
|
||||||
|
|
||||||
|
root->data = &dataRoot;
|
||||||
|
root->left = (TreeNode *)node1;
|
||||||
|
root->right = (TreeNode *)node2;
|
||||||
|
|
||||||
|
node1->data = &dataNode1;
|
||||||
|
node1->left = NULL;
|
||||||
|
node1->right = NULL;
|
||||||
|
|
||||||
|
node2->data = &dataNode2;
|
||||||
|
node2->left = NULL;
|
||||||
|
node2->right = NULL;
|
||||||
|
|
||||||
|
clearTree(root);
|
||||||
|
root = NULL;
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(0,treeSize(root));
|
||||||
|
}
|
||||||
|
|
||||||
|
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("============================\nNumbers tests\n============================\n");
|
||||||
|
|
||||||
|
RUN_TEST(sizeTest);
|
||||||
|
RUN_TEST(clearTest);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
70
test_numbers.c
Normal file
70
test_numbers.c
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "numbers.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
void test_createNumbers()
|
||||||
|
{
|
||||||
|
unsigned int len = 6;
|
||||||
|
unsigned int *array = createNumbers(len);
|
||||||
|
TEST_ASSERT_NOT_NULL(array);
|
||||||
|
|
||||||
|
unsigned int duplicate = getDuplicate(array, len);
|
||||||
|
TEST_ASSERT_NOT_EQUAL(0, duplicate);
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
for (unsigned int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
if (array[i] == duplicate)
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL(2, counter);
|
||||||
|
|
||||||
|
free(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_createNumbers_values()
|
||||||
|
{
|
||||||
|
unsigned int len = 6;
|
||||||
|
unsigned int *array = createNumbers(len);
|
||||||
|
TEST_ASSERT_NOT_NULL(array);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(array[i] >= 1);
|
||||||
|
TEST_ASSERT_TRUE(array[i] <= 2 * len);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_getDuplicate()
|
||||||
|
{
|
||||||
|
unsigned int array[6] = {1, 2, 4, 4, 6, 7};
|
||||||
|
unsigned int duplicate = getDuplicate(array, 6);
|
||||||
|
TEST_ASSERT_EQUAL_UINT(4, duplicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
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("\n============================\n Numbers tests\n============================\n");
|
||||||
|
|
||||||
|
RUN_TEST(test_createNumbers);
|
||||||
|
RUN_TEST(test_createNumbers_values);
|
||||||
|
RUN_TEST(test_getDuplicate);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
156
test_stack.c
Normal file
156
test_stack.c
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stack.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
void test_push(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int data1 = 10, data2 = 20;
|
||||||
|
|
||||||
|
// Push elements to the stack
|
||||||
|
stack = push(stack, &data1);
|
||||||
|
stack = push(stack, &data2);
|
||||||
|
|
||||||
|
// Check if the stack is not empty
|
||||||
|
TEST_ASSERT_NOT_NULL(stack);
|
||||||
|
|
||||||
|
// Check if the top element is correct
|
||||||
|
int *topData = top(stack);
|
||||||
|
TEST_ASSERT_EQUAL_INT(20, *topData); // The last pushed element should be on top
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_push1(void)
|
||||||
|
{
|
||||||
|
StackNode *testNode = NULL;
|
||||||
|
int data = 1;
|
||||||
|
|
||||||
|
// Test für leeren Stack
|
||||||
|
testNode = push(testNode, &data);
|
||||||
|
TEST_ASSERT_NOT_NULL(&testNode);
|
||||||
|
TEST_ASSERT_NULL(testNode->next);
|
||||||
|
int *temp = testNode->data;
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *temp);
|
||||||
|
|
||||||
|
data = 2;
|
||||||
|
|
||||||
|
// Test für nicht leeren Stack
|
||||||
|
testNode = push(testNode, &data);
|
||||||
|
TEST_ASSERT_NOT_NULL(&testNode);
|
||||||
|
TEST_ASSERT_NOT_NULL(testNode->next);
|
||||||
|
TEST_ASSERT_NULL(testNode->next->next);
|
||||||
|
temp = testNode->data;
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, *temp);
|
||||||
|
testNode = testNode->next;
|
||||||
|
temp = testNode->data;
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
StackNode* setup(void *data, StackNode* next) {
|
||||||
|
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
|
||||||
|
if (node == NULL) {
|
||||||
|
perror("malloc failed");
|
||||||
|
exit(EXIT_FAILURE); // or handle the error differently
|
||||||
|
}
|
||||||
|
node->data = data;
|
||||||
|
node->next = next;
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
void test_pop(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int data1 = 10, data2 = 20;
|
||||||
|
|
||||||
|
// Push elements to the stack
|
||||||
|
stack = push(stack, &data1);
|
||||||
|
stack = push(stack, &data2);
|
||||||
|
|
||||||
|
// Pop the top element
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
// Check if the top element is now the first pushed element
|
||||||
|
int *topData = top(stack);
|
||||||
|
TEST_ASSERT_EQUAL_INT(10, *topData); // After popping, the first element should be on top
|
||||||
|
|
||||||
|
// Pop the last element
|
||||||
|
stack = pop(stack);
|
||||||
|
|
||||||
|
// Check if the stack is empty now
|
||||||
|
TEST_ASSERT_NULL(stack); // Stack should be NULL now
|
||||||
|
}
|
||||||
|
void test_pop2(void)
|
||||||
|
{
|
||||||
|
int x,y,z;
|
||||||
|
x = 1;
|
||||||
|
y = 2;
|
||||||
|
z = 3;
|
||||||
|
StackNode* node2 = setup(&z, NULL);
|
||||||
|
StackNode* node1 = setup(&y, node2);
|
||||||
|
StackNode* header = setup(&x, node1);
|
||||||
|
StackNode* temp;
|
||||||
|
|
||||||
|
temp = pop(header);
|
||||||
|
int after = 0;
|
||||||
|
while(temp)
|
||||||
|
{
|
||||||
|
after++;
|
||||||
|
temp = temp->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(2, after);
|
||||||
|
TEST_ASSERT_NULL(node1->next);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_top(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int data1 = 10, data2 = 20;
|
||||||
|
|
||||||
|
// Push elements to the stack
|
||||||
|
stack = push(stack, &data1);
|
||||||
|
stack = push(stack, &data2);
|
||||||
|
|
||||||
|
// Check the top element
|
||||||
|
int *topData = top(stack);
|
||||||
|
TEST_ASSERT_EQUAL_INT(20, *topData); // The top element should be 20 (last pushed)
|
||||||
|
|
||||||
|
// Pop the top element and check the new top
|
||||||
|
stack = pop(stack);
|
||||||
|
topData = top(stack);
|
||||||
|
TEST_ASSERT_EQUAL_INT(10, *topData); // Now the top element should be 10
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_top2(void)
|
||||||
|
{
|
||||||
|
int x,y,z;
|
||||||
|
x = 1;
|
||||||
|
y = 2;
|
||||||
|
z = 3;
|
||||||
|
StackNode* node2 = setup(&z, NULL);
|
||||||
|
StackNode* node1 = setup(&y, node2);
|
||||||
|
StackNode* header = setup(&x, node1);
|
||||||
|
|
||||||
|
int data = *(int *)top(header);
|
||||||
|
TEST_ASSERT_EQUAL_INT(node2->data, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
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_push);
|
||||||
|
RUN_TEST(test_pop);
|
||||||
|
RUN_TEST(test_top);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
10
unittest.h
Normal file
10
unittest.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#ifndef UNITTTESTS_H
|
||||||
|
#define UNITTTESTS_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
typedef int (*UnitTestType)(void);
|
||||||
|
|
||||||
|
#define RUN_UNIT_TEST(fct) printf("%80s: %d\n", #fct, fct())
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
x
Reference in New Issue
Block a user