Compare commits
No commits in common. "main" and "main" have entirely different histories.
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,8 +0,0 @@
|
|||||||
doble_initial.exe
|
|
||||||
highscores.txt
|
|
||||||
runStackTest.exe
|
|
||||||
stack.o
|
|
||||||
runNumbersTest.exe
|
|
||||||
numbers.o
|
|
||||||
.vscode/launch.json
|
|
||||||
.vscode/settings.json
|
|
||||||
36
bintree.c
36
bintree.c
@ -2,17 +2,16 @@
|
|||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
// TODO: binären Suchbaum implementieren
|
//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)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,36 +26,11 @@ void *nextTreeData(TreeNode *root)
|
|||||||
// 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);
|
|
||||||
}
|
|
||||||
else if (root->right != NULL)
|
|
||||||
{
|
|
||||||
clearTree(root->right);
|
|
||||||
free(root->right);
|
|
||||||
}
|
|
||||||
root->data = 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)
|
||||||
{
|
{
|
||||||
int counterL, counterR = 0;
|
|
||||||
if (root->left != NULL)
|
|
||||||
{
|
|
||||||
counterL = treeSize(root->left) + 1;
|
|
||||||
}
|
|
||||||
else if (root->right != NULL)
|
|
||||||
{
|
|
||||||
counterR = treeSize(root->right) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return counterL + counterR;
|
|
||||||
}
|
}
|
||||||
102
makefile
102
makefile
@ -1,55 +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:
|
||||||
$(CC) $(FLAGS) -I$(unityfolder) -o runStackTest test_stack.c stack.o $(unityfolder)/unity.c
|
echo "needs to be implemented"
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# numbers.c Tests
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c
|
clean:
|
||||||
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c
|
ifeq ($(OS),Windows_NT)
|
||||||
|
del /f *.o doble
|
||||||
# --------------------------
|
else
|
||||||
# Clean
|
rm -f *.o doble
|
||||||
# --------------------------
|
|
||||||
clean:
|
|
||||||
ifeq ($(OS),Windows_NT)
|
|
||||||
del /f *.o doble
|
|
||||||
else
|
|
||||||
rm -f *.o doble
|
|
||||||
endif
|
endif
|
||||||
145
numbers.c
145
numbers.c
@ -5,159 +5,22 @@
|
|||||||
#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 checkArray(unsigned int *array, unsigned int len, unsigned int number)
|
|
||||||
{
|
|
||||||
int free = 1;
|
|
||||||
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (array[i] == number)
|
|
||||||
{
|
|
||||||
free = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return free;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
|
||||||
unsigned int *array = (unsigned int*)malloc(len * sizeof(unsigned int));
|
|
||||||
int randomNr, counter;
|
|
||||||
|
|
||||||
if(array == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
counter = 0;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
if (counter == 9)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
randomNr = rand() % (2 * len + 1);
|
|
||||||
counter++;
|
|
||||||
} while (!checkArray(array, i, randomNr));
|
|
||||||
|
|
||||||
array[i] = randomNr;
|
|
||||||
printf("%u ", array[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
void merge(unsigned int arr[], unsigned int left, unsigned int mid, unsigned int right)
|
|
||||||
{
|
|
||||||
unsigned int i, j, k;
|
|
||||||
unsigned int n1 = mid - left + 1;
|
|
||||||
unsigned int n2 = right - mid;
|
|
||||||
|
|
||||||
// Create temporary arrays
|
|
||||||
unsigned int leftArr[n1], rightArr[n2];
|
|
||||||
|
|
||||||
// Copy data to temporary arrays
|
|
||||||
for (i = 0; i < n1; i++)
|
|
||||||
leftArr[i] = arr[left + i];
|
|
||||||
for (j = 0; j < n2; j++)
|
|
||||||
rightArr[j] = arr[mid + 1 + j];
|
|
||||||
|
|
||||||
// Merge the temporary arrays back into arr[left..right]
|
|
||||||
i = 0;
|
|
||||||
j = 0;
|
|
||||||
k = left;
|
|
||||||
while (i < n1 && j < n2)
|
|
||||||
{
|
|
||||||
if (leftArr[i] <= rightArr[j])
|
|
||||||
{
|
|
||||||
arr[k] = leftArr[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
arr[k] = rightArr[j];
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy the remaining elements of leftArr[], if any
|
|
||||||
while (i < n1)
|
|
||||||
{
|
|
||||||
arr[k] = leftArr[i];
|
|
||||||
i++;
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy the remaining elements of rightArr[], if any
|
|
||||||
while (j < n2)
|
|
||||||
{
|
|
||||||
arr[k] = rightArr[j];
|
|
||||||
j++;
|
|
||||||
k++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void mergeSort(unsigned int arr[], unsigned int left, unsigned int right)
|
|
||||||
{
|
|
||||||
if (left < right)
|
|
||||||
{
|
|
||||||
|
|
||||||
// Calculate the midpoint
|
|
||||||
unsigned int mid = left + (right - left) / 2;
|
|
||||||
|
|
||||||
// Sort first and second halves
|
|
||||||
mergeSort(arr, left, mid);
|
|
||||||
mergeSort(arr, mid + 1, right);
|
|
||||||
|
|
||||||
// Merge the sorted halves
|
|
||||||
merge(arr, left, mid, right);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
unsigned int temp[len];
|
|
||||||
unsigned int duplicate = 0;
|
|
||||||
|
|
||||||
/*if(numbers == NULL || (sizeof(numbers) / sizeof(typeof(numbers)) != len))
|
|
||||||
{
|
|
||||||
return 0;S
|
|
||||||
}*/
|
|
||||||
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
temp[i] = numbers[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sorting arr using mergesort
|
|
||||||
mergeSort(temp, 0, len - 1);
|
|
||||||
|
|
||||||
for (int i = 0; i < len - 1; i++)
|
|
||||||
{
|
|
||||||
duplicate = temp[i];
|
|
||||||
if (duplicate == temp[i + 1])
|
|
||||||
{
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return duplicate;
|
|
||||||
}
|
}
|
||||||
Binary file not shown.
66
stack.c
66
stack.c
@ -1,91 +1,33 @@
|
|||||||
#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;
|
|
||||||
|
|
||||||
newNode = malloc(sizeof(StackNode));
|
|
||||||
newNode->value = *(int *)data;
|
|
||||||
newNode->next = NULL;
|
|
||||||
|
|
||||||
if (stack == NULL)
|
|
||||||
{
|
|
||||||
stack = newNode;
|
|
||||||
return stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
tempNode = stack;
|
|
||||||
while (tempNode->next != NULL)
|
|
||||||
{
|
|
||||||
tempNode = tempNode->next;
|
|
||||||
}
|
|
||||||
tempNode->next = newNode;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (stack == NULL)
|
|
||||||
{
|
|
||||||
return stack;
|
|
||||||
}
|
|
||||||
|
|
||||||
tempNode = stack;
|
|
||||||
while (tempNode->next->next != NULL)
|
|
||||||
{
|
|
||||||
tempNode = tempNode->next;
|
|
||||||
}
|
|
||||||
free(tempNode->next);
|
|
||||||
tempNode->next = NULL;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (stack == NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
tempNode = stack;
|
|
||||||
while (tempNode->next != NULL)
|
|
||||||
{
|
|
||||||
tempNode = tempNode->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (stack == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tempNode = stack;
|
|
||||||
while (tempNode != NULL)
|
|
||||||
{
|
|
||||||
tempNode = pop(tempNode);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
54
stack.h
54
stack.h
@ -1,29 +1,25 @@
|
|||||||
#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 {
|
|
||||||
int value;
|
// Pushes data as pointer onto the stack.
|
||||||
struct Node* next;
|
StackNode *push(StackNode *stack, void *data);
|
||||||
} StackNode;
|
|
||||||
|
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
||||||
// Pushes data as pointer onto the stack.
|
// freed by caller.)
|
||||||
StackNode *push(StackNode *stack, void *data);
|
StackNode *pop(StackNode *stack);
|
||||||
|
|
||||||
// Deletes the top element of the stack (latest added element) and releases its memory. (Pointer to data has to be
|
// Returns the data of the top element.
|
||||||
// freed by caller.)
|
void *top(StackNode *stack);
|
||||||
StackNode *pop(StackNode *stack);
|
|
||||||
|
// Clears stack and releases all memory.
|
||||||
// Returns the data of the top element.
|
void clearStack(StackNode *stack);
|
||||||
void *top(StackNode *stack);
|
|
||||||
|
#endif
|
||||||
// Clears stack and releases all memory.
|
|
||||||
void clearStack(StackNode *stack);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
@ -1,47 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "numbers.h"
|
|
||||||
#include "unity.h"
|
|
||||||
|
|
||||||
void createNumbersTest()
|
|
||||||
{
|
|
||||||
unsigned int *array;
|
|
||||||
unsigned int len = 6;
|
|
||||||
|
|
||||||
array = createNumbers(len);
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
printf("%u ", array[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
TEST_ASSERT_NOT_NULL(array);
|
|
||||||
}
|
|
||||||
|
|
||||||
void duplicateTest()
|
|
||||||
{
|
|
||||||
unsigned int array[6] = {1, 4, 5, 2, 3, 1};
|
|
||||||
unsigned int len = 6;
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_INT(1, getDuplicate(array, len));
|
|
||||||
}
|
|
||||||
|
|
||||||
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(createNumbersTest);
|
|
||||||
RUN_TEST(duplicateTest);
|
|
||||||
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
112
test_stack.c
112
test_stack.c
@ -1,112 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "stack.h"
|
|
||||||
#include "unity.h"
|
|
||||||
|
|
||||||
void test_push(void)
|
|
||||||
{
|
|
||||||
StackNode *testNode;
|
|
||||||
int data = 1;
|
|
||||||
|
|
||||||
// Test für leeren Stack
|
|
||||||
testNode = push(NULL, &data);
|
|
||||||
TEST_ASSERT_NOT_NULL(&testNode);
|
|
||||||
TEST_ASSERT_NULL(testNode->next);
|
|
||||||
TEST_ASSERT_EQUAL_INT(1, testNode->value);
|
|
||||||
|
|
||||||
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);
|
|
||||||
TEST_ASSERT_EQUAL_INT(1, testNode->value);
|
|
||||||
TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
StackNode* setup(int value, 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->value = value;
|
|
||||||
node->next = next;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_pop(void)
|
|
||||||
{
|
|
||||||
StackNode* node2 = setup(3, NULL);
|
|
||||||
StackNode* node1 = setup(2, node2);
|
|
||||||
StackNode* header = setup(1, 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* node2 = setup(3, NULL);
|
|
||||||
StackNode* node1 = setup(2, node2);
|
|
||||||
StackNode* header = setup(1, node1);
|
|
||||||
|
|
||||||
int data = *(int *)top(header);
|
|
||||||
TEST_ASSERT_EQUAL_INT(node2->value, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void test_clear()
|
|
||||||
{
|
|
||||||
StackNode* node2 = setup(3, NULL);
|
|
||||||
StackNode* node1 = setup(2, node2);
|
|
||||||
StackNode* header = setup(1, node1);
|
|
||||||
StackNode* temp;
|
|
||||||
|
|
||||||
clearStack(header);
|
|
||||||
temp = header;
|
|
||||||
|
|
||||||
int after = 0;
|
|
||||||
while(temp)
|
|
||||||
{
|
|
||||||
after++;
|
|
||||||
temp = temp->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TEST_ASSERT_NULL(after);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
RUN_TEST(test_clear);
|
|
||||||
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
10
unittest.h
10
unittest.h
@ -1,10 +0,0 @@
|
|||||||
#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