Compare commits
6
Commits
39965a95c4
..
temp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3b951d0a9 | ||
|
|
f8c297f84d | ||
|
|
5b576589e3 | ||
|
|
bf0a0b3f40 | ||
|
|
d54dd3eb6f | ||
|
|
8051686a37 |
@@ -2,3 +2,10 @@ doble_initial.exe
|
|||||||
highscores.txt
|
highscores.txt
|
||||||
runStackTest.exe
|
runStackTest.exe
|
||||||
stack.o
|
stack.o
|
||||||
|
runNumbersTest.exe
|
||||||
|
numbers.o
|
||||||
|
.vscode/launch.json
|
||||||
|
.vscode/settings.json
|
||||||
|
*.o
|
||||||
|
*.exe
|
||||||
|
runBintreeTest
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#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),
|
||||||
@@ -12,7 +12,26 @@
|
|||||||
// 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)
|
||||||
{
|
{
|
||||||
|
TreeNode newNode = {data, NULL, NULL};
|
||||||
|
|
||||||
|
|
||||||
|
if(root == NULL)
|
||||||
|
{
|
||||||
|
return &newNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data < root->data)
|
||||||
|
{
|
||||||
|
root->left = addToTree(root->left, data, dataSize,compareFct, isDuplicate);
|
||||||
|
}
|
||||||
|
else if(data > root->data)
|
||||||
|
{
|
||||||
|
root->right = addToTree(root->right, 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.
|
||||||
@@ -26,11 +45,36 @@ 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;
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|||||||
@@ -32,19 +32,33 @@ unsigned int checkArray(unsigned int *array, unsigned int len, unsigned int numb
|
|||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
unsigned int array[len];
|
unsigned int *array = (unsigned int*)malloc(len * sizeof(unsigned int));
|
||||||
unsigned int randomNr;
|
int randomNr, counter;
|
||||||
|
|
||||||
|
if(array == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
|
counter = 0;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
randomNr = rand() % 2 * len + 1;
|
if (counter == 9)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
randomNr = rand() % (2 * len + 1);
|
||||||
|
counter++;
|
||||||
} while (!checkArray(array, i, randomNr));
|
} while (!checkArray(array, i, randomNr));
|
||||||
|
|
||||||
array[i] = randomNr;
|
array[i] = randomNr;
|
||||||
|
printf("%u ", array[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,6 +136,11 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
unsigned int temp[len];
|
unsigned int temp[len];
|
||||||
unsigned int duplicate = 0;
|
unsigned int duplicate = 0;
|
||||||
|
|
||||||
|
/*if(numbers == NULL || (sizeof(numbers) / sizeof(typeof(numbers)) != len))
|
||||||
|
{
|
||||||
|
return 0;S
|
||||||
|
}*/
|
||||||
|
|
||||||
for (int i = 0; i < len; i++)
|
for (int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
temp[i] = numbers[i];
|
temp[i] = numbers[i];
|
||||||
|
|||||||
Binary file not shown.
@@ -0,0 +1,60 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "bintree.h"
|
||||||
|
#include "unity.h"
|
||||||
|
|
||||||
|
|
||||||
|
void treeTest()
|
||||||
|
{
|
||||||
|
TreeNode root;
|
||||||
|
TreeNode node1;
|
||||||
|
TreeNode node2;
|
||||||
|
|
||||||
|
int dataRoot = 2;
|
||||||
|
int dataNode1 = 1;
|
||||||
|
int dataNode2 = 3;
|
||||||
|
|
||||||
|
|
||||||
|
root.data = &dataRoot;
|
||||||
|
root.left = &node1;
|
||||||
|
root.right = &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));
|
||||||
|
|
||||||
|
|
||||||
|
clearTree(&root);
|
||||||
|
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(treeTest);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
+15
-1
@@ -3,6 +3,20 @@
|
|||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "unity.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()
|
void duplicateTest()
|
||||||
{
|
{
|
||||||
unsigned int array[6] = {1, 4, 5, 2, 3, 1};
|
unsigned int array[6] = {1, 4, 5, 2, 3, 1};
|
||||||
@@ -26,7 +40,7 @@ int main()
|
|||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
|
|
||||||
printf("============================\nNumbers tests\n============================\n");
|
printf("============================\nNumbers tests\n============================\n");
|
||||||
|
RUN_TEST(createNumbersTest);
|
||||||
RUN_TEST(duplicateTest);
|
RUN_TEST(duplicateTest);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
|
|||||||
Reference in New Issue
Block a user