Compare commits
10 Commits
main
...
simons_weg
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5900d759bd | ||
|
|
6b3294d40b | ||
|
|
a018971c04 | ||
| 35b09294bc | |||
|
|
fc3508140a | ||
|
|
a3b951d0a9 | ||
| f8c297f84d | |||
| 5b576589e3 | |||
| bf0a0b3f40 | |||
| d54dd3eb6f |
3
.gitignore
vendored
3
.gitignore
vendored
@ -6,3 +6,6 @@ runNumbersTest.exe
|
|||||||
numbers.o
|
numbers.o
|
||||||
.vscode/launch.json
|
.vscode/launch.json
|
||||||
.vscode/settings.json
|
.vscode/settings.json
|
||||||
|
*.o
|
||||||
|
*.exe
|
||||||
|
runBintreeTest
|
||||||
|
|||||||
71
bintree.c
71
bintree.c
@ -1,7 +1,10 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
|
|
||||||
|
static StackNode *stack;
|
||||||
|
static TreeNode *tree = NULL;
|
||||||
// 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),
|
||||||
@ -12,8 +15,34 @@
|
|||||||
// 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);
|
||||||
|
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; // Mark as duplicate if needed.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
@ -21,6 +50,21 @@ 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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,27 +80,32 @@ void clearTree(TreeNode *root)
|
|||||||
{
|
{
|
||||||
clearTree(root->left);
|
clearTree(root->left);
|
||||||
free(root->left);
|
free(root->left);
|
||||||
|
root->left = NULL;
|
||||||
}
|
}
|
||||||
else if (root->right != NULL)
|
if (root->right != NULL)
|
||||||
{
|
{
|
||||||
clearTree(root->right);
|
clearTree(root->right);
|
||||||
free(root->right);
|
free(root->right);
|
||||||
|
root->right = NULL;
|
||||||
}
|
}
|
||||||
root->data = 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)
|
||||||
{
|
{
|
||||||
int counterL, counterR = 0;
|
return root == NULL ? 0 : treeSize(root->left) + treeSize(root->right) + 1;
|
||||||
if (root->left != NULL)
|
|
||||||
{
|
|
||||||
counterL = treeSize(root->left) + 1;
|
|
||||||
}
|
|
||||||
else if (root->right != NULL)
|
|
||||||
{
|
|
||||||
counterR = treeSize(root->right) + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return counterL + counterR;
|
void buildStack(TreeNode *root)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (root == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
buildStack(root->left); // biggest first
|
||||||
|
stack = push(stack, root->data); // push
|
||||||
|
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
|
||||||
|
|||||||
8
makefile
8
makefile
@ -35,7 +35,7 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Unit Tests
|
# Unit Tests
|
||||||
# --------------------------
|
# --------------------------
|
||||||
unitTests: stack.o test_stack.c $(unityfolder)/unity.c
|
stackTests: 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
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
@ -44,6 +44,12 @@ unitTests: stack.o test_stack.c $(unityfolder)/unity.c
|
|||||||
numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c
|
numbersTests: numbers.o test_numbers.c $(unityfolder)/unity.c
|
||||||
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c
|
$(CC) $(FLAGS) -I$(unityfolder) -o runNumbersTest test_numbers.c numbers.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
|
# --------------------------
|
||||||
|
# bintree.c Tests
|
||||||
|
# --------------------------
|
||||||
|
bintreeTests: bintree.o test_bintree.c $(unityfolder)/unity.c
|
||||||
|
$(CC) $(FLAGS) -I$(unityfolder) -o runBintreeTest test_bintree.c bintree.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
35
numbers.c
35
numbers.c
@ -23,6 +23,8 @@ unsigned int checkArray(unsigned int *array, unsigned int len, unsigned int numb
|
|||||||
if (array[i] == number)
|
if (array[i] == number)
|
||||||
{
|
{
|
||||||
free = 0;
|
free = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,32 +35,37 @@ unsigned int *createNumbers(unsigned int len)
|
|||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
unsigned int *array = (unsigned int *)malloc(len * sizeof(unsigned int));
|
unsigned int *array = (unsigned int *)malloc(len * sizeof(unsigned int));
|
||||||
int randomNr, counter;
|
int randomNr, randomPos, filler;
|
||||||
|
|
||||||
|
|
||||||
if (array == NULL)
|
if (array == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL; // Fehler
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
array[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
counter = 0;
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if (counter == 9)
|
array[i] = (rand() % (2 * len))+ 1;
|
||||||
|
} while (!checkArray(array, i, array[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
randomPos = rand() % len;
|
||||||
|
randomNr = array[randomPos];
|
||||||
|
|
||||||
|
filler = randomPos;
|
||||||
|
while(filler == randomPos)
|
||||||
{
|
{
|
||||||
return NULL;
|
filler = rand() % len;
|
||||||
}
|
}
|
||||||
|
array[filler] = randomNr;
|
||||||
|
|
||||||
randomNr = rand() % (2 * len + 1);
|
|
||||||
counter++;
|
|
||||||
} while (!checkArray(array, i, randomNr));
|
|
||||||
|
|
||||||
array[i] = randomNr;
|
|
||||||
printf("%u ", array[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
62
stack.c
62
stack.c
@ -10,46 +10,24 @@
|
|||||||
// 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 *newNode = malloc(sizeof(StackNode));
|
||||||
|
newNode->data = data;
|
||||||
newNode = malloc(sizeof(StackNode));
|
newNode->next = stack; // Set the new node's next pointer to the current top of the stack.
|
||||||
newNode->value = *(int *)data;
|
return newNode; // Return the new node as the top of the stack.
|
||||||
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)
|
if (stack == NULL)
|
||||||
{
|
{
|
||||||
return stack;
|
return NULL; // Nothing to pop if stack is empty.
|
||||||
}
|
}
|
||||||
|
|
||||||
tempNode = stack;
|
StackNode *tempNode = stack;
|
||||||
while (tempNode->next->next != NULL)
|
stack = stack->next; // Move the stack pointer to the next node.
|
||||||
{
|
free(tempNode); // Free the old top node.
|
||||||
tempNode = tempNode->next;
|
|
||||||
}
|
|
||||||
free(tempNode->next);
|
|
||||||
tempNode->next = NULL;
|
|
||||||
|
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
@ -57,35 +35,19 @@ 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)
|
||||||
{
|
{
|
||||||
StackNode *tempNode;
|
|
||||||
|
|
||||||
if (stack == NULL)
|
if (stack == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL; // Return NULL if stack is empty.
|
||||||
}
|
}
|
||||||
|
|
||||||
tempNode = stack;
|
return stack->data; // Return the value of the top node.
|
||||||
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;
|
while (stack != NULL)
|
||||||
|
|
||||||
if (stack == NULL)
|
|
||||||
{
|
{
|
||||||
return;
|
stack = pop(stack); // Pop each element and free memory.
|
||||||
}
|
|
||||||
|
|
||||||
tempNode = stack;
|
|
||||||
while (tempNode != NULL)
|
|
||||||
{
|
|
||||||
tempNode = pop(tempNode);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
2
stack.h
2
stack.h
@ -9,7 +9,7 @@ The latest element is taken from the stack. */
|
|||||||
|
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
typedef struct Node {
|
typedef struct Node {
|
||||||
int value;
|
void *data;
|
||||||
struct Node* next;
|
struct Node* next;
|
||||||
} StackNode;
|
} StackNode;
|
||||||
|
|
||||||
|
|||||||
100
stackTest.c
Normal file
100
stackTest.c
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
#include "unity.h"
|
||||||
|
#include "stack.h" // Stack-Header-Datei
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Test Setup and Teardown Functions
|
||||||
|
void setUp(void) {
|
||||||
|
// Setup code (falls notwendig, wie Initialisierungen)
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {
|
||||||
|
// Cleanup code (falls notwendig)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test for push operation
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test for pop operation
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test for top operation
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test for clearStack operation
|
||||||
|
void test_clearStack(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int data1 = 10, data2 = 20;
|
||||||
|
|
||||||
|
// Push elements to the stack
|
||||||
|
stack = push(stack, &data1);
|
||||||
|
stack = push(stack, &data2);
|
||||||
|
|
||||||
|
// Clear the stack
|
||||||
|
clearStack(stack);
|
||||||
|
|
||||||
|
// The stack should be empty now
|
||||||
|
TEST_ASSERT_NULL(stack); // Stack should be NULL
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run all tests
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
// Run the individual test functions
|
||||||
|
RUN_TEST(test_push);
|
||||||
|
RUN_TEST(test_pop);
|
||||||
|
RUN_TEST(test_top);
|
||||||
|
RUN_TEST(test_clearStack);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
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;
|
||||||
|
|
||||||
|
TreeNode *ptr = root;
|
||||||
|
|
||||||
|
clearTree(ptr);
|
||||||
|
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();
|
||||||
|
}
|
||||||
127
test_stack.c
127
test_stack.c
@ -3,16 +3,33 @@
|
|||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
#include "unity.h"
|
#include "unity.h"
|
||||||
|
|
||||||
void test_push(void)
|
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;
|
StackNode *testNode = NULL;
|
||||||
int data = 1;
|
int data = 1;
|
||||||
|
|
||||||
// Test für leeren Stack
|
// Test für leeren Stack
|
||||||
testNode = push(NULL, &data);
|
testNode = push(testNode, &data);
|
||||||
TEST_ASSERT_NOT_NULL(&testNode);
|
TEST_ASSERT_NOT_NULL(&testNode);
|
||||||
TEST_ASSERT_NULL(testNode->next);
|
TEST_ASSERT_NULL(testNode->next);
|
||||||
TEST_ASSERT_EQUAL_INT(1, testNode->value);
|
int *temp = testNode->data;
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *temp);
|
||||||
|
|
||||||
data = 2;
|
data = 2;
|
||||||
|
|
||||||
@ -21,26 +38,53 @@ void test_push(void)
|
|||||||
TEST_ASSERT_NOT_NULL(&testNode);
|
TEST_ASSERT_NOT_NULL(&testNode);
|
||||||
TEST_ASSERT_NOT_NULL(testNode->next);
|
TEST_ASSERT_NOT_NULL(testNode->next);
|
||||||
TEST_ASSERT_NULL(testNode->next->next);
|
TEST_ASSERT_NULL(testNode->next->next);
|
||||||
TEST_ASSERT_EQUAL_INT(1, testNode->value);
|
temp = testNode->data;
|
||||||
TEST_ASSERT_EQUAL_INT(2, testNode->next->value);
|
TEST_ASSERT_EQUAL_INT(2, *temp);
|
||||||
|
testNode = testNode->next;
|
||||||
|
temp = testNode->data;
|
||||||
|
TEST_ASSERT_EQUAL_INT(1, *temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
StackNode* setup(int value, StackNode* next) {
|
StackNode* setup(void *data, StackNode* next) {
|
||||||
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
|
StackNode* node = malloc(sizeof(StackNode)); // allocate memory on heap
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
perror("malloc failed");
|
perror("malloc failed");
|
||||||
exit(EXIT_FAILURE); // or handle the error differently
|
exit(EXIT_FAILURE); // or handle the error differently
|
||||||
}
|
}
|
||||||
node->value = value;
|
node->data = data;
|
||||||
node->next = next;
|
node->next = next;
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
void test_pop(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int data1 = 10, data2 = 20;
|
||||||
|
|
||||||
void test_pop(void)
|
// 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)
|
||||||
{
|
{
|
||||||
StackNode* node2 = setup(3, NULL);
|
int x,y,z;
|
||||||
StackNode* node1 = setup(2, node2);
|
x = 1;
|
||||||
StackNode* header = setup(1, node1);
|
y = 2;
|
||||||
|
z = 3;
|
||||||
|
StackNode* node2 = setup(&z, NULL);
|
||||||
|
StackNode* node1 = setup(&y, node2);
|
||||||
|
StackNode* header = setup(&x, node1);
|
||||||
StackNode* temp;
|
StackNode* temp;
|
||||||
|
|
||||||
temp = pop(header);
|
temp = pop(header);
|
||||||
@ -56,21 +100,62 @@ void test_pop(void)
|
|||||||
TEST_ASSERT_NULL(node1->next);
|
TEST_ASSERT_NULL(node1->next);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_top(void)
|
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)
|
||||||
{
|
{
|
||||||
StackNode* node2 = setup(3, NULL);
|
int x,y,z;
|
||||||
StackNode* node1 = setup(2, node2);
|
x = 1;
|
||||||
StackNode* header = setup(1, node1);
|
y = 2;
|
||||||
|
z = 3;
|
||||||
|
StackNode* node2 = setup(&z, NULL);
|
||||||
|
StackNode* node1 = setup(&y, node2);
|
||||||
|
StackNode* header = setup(&x, node1);
|
||||||
|
|
||||||
int data = *(int *)top(header);
|
int data = *(int *)top(header);
|
||||||
TEST_ASSERT_EQUAL_INT(node2->value, data);
|
TEST_ASSERT_EQUAL_INT(node2->data, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_clearStack(void) {
|
||||||
|
StackNode *stack = NULL;
|
||||||
|
int data1 = 10, data2 = 20;
|
||||||
|
|
||||||
|
// Push elements to the stack
|
||||||
|
stack = push(stack, &data1);
|
||||||
|
stack = push(stack, &data2);
|
||||||
|
|
||||||
|
// Clear the stack
|
||||||
|
clearStack(stack);
|
||||||
|
|
||||||
|
// The stack should be empty now
|
||||||
|
TEST_ASSERT_NULL(stack); // Stack should be NULL
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_clear()
|
void test_clear()
|
||||||
{
|
{
|
||||||
StackNode* node2 = setup(3, NULL);
|
int x,y,z;
|
||||||
StackNode* node1 = setup(2, node2);
|
x = 1;
|
||||||
StackNode* header = setup(1, node1);
|
y = 2;
|
||||||
|
z = 3;
|
||||||
|
StackNode* node2 = setup(&z, NULL);
|
||||||
|
StackNode* node1 = setup(&y, node2);
|
||||||
|
StackNode* header = setup(&x, node1);
|
||||||
StackNode* temp;
|
StackNode* temp;
|
||||||
|
|
||||||
clearStack(header);
|
clearStack(header);
|
||||||
@ -106,7 +191,7 @@ int main()
|
|||||||
RUN_TEST(test_push);
|
RUN_TEST(test_push);
|
||||||
RUN_TEST(test_pop);
|
RUN_TEST(test_pop);
|
||||||
RUN_TEST(test_top);
|
RUN_TEST(test_top);
|
||||||
RUN_TEST(test_clear);
|
RUN_TEST(test_clearStack);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user