Compare commits
14
Commits
6586a25fdb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a7a939fc4 | ||
|
|
44d96647bb | ||
|
|
8605da95b6 | ||
|
|
a3e12875aa | ||
|
|
b7fa24f2e9 | ||
|
|
b69cfb0640 | ||
|
|
686fe8d844 | ||
|
|
c37ce6327c | ||
|
|
111b9c5532 | ||
|
|
8e76b27760 | ||
|
|
a575259cac | ||
|
|
d391d60597 | ||
|
|
07796c1390 | ||
|
|
25da0d20df |
Binary file not shown.
@@ -51,26 +51,32 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
|
|
||||||
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root)
|
TreeNode *addToTreeRec(TreeNode *currentNode, TreeNode *newNode, CompareFctType compareFct, int *isDuplicate, const int root)
|
||||||
{
|
{
|
||||||
if ((currentNode == NULL))
|
if (currentNode == NULL)
|
||||||
{
|
{
|
||||||
if ((isDuplicate == NULL) || root)
|
if ((isDuplicate == NULL) || root)
|
||||||
{
|
{
|
||||||
|
if (isDuplicate != NULL)
|
||||||
|
{
|
||||||
|
*isDuplicate = 0;
|
||||||
|
}
|
||||||
|
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
*isDuplicate = 0;
|
||||||
return currentNode;
|
return currentNode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ((compareFct(currentNode->data, newNode->data) < 0))
|
else if (compareFct(currentNode->data, newNode->data) < 0)
|
||||||
{
|
{
|
||||||
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
currentNode->left = addToTreeRec(currentNode->left, newNode, compareFct, isDuplicate, 0);
|
||||||
}
|
}
|
||||||
else if ((compareFct(currentNode->data, newNode->data) > 0))
|
else if (compareFct(currentNode->data, newNode->data) > 0)
|
||||||
{
|
{
|
||||||
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
|
currentNode->right = addToTreeRec(currentNode->right, newNode, compareFct, isDuplicate, 0);
|
||||||
}
|
}
|
||||||
else if ((compareFct(currentNode->data, newNode->data) == 0))
|
else if (compareFct(currentNode->data, newNode->data) == 0)
|
||||||
{
|
{
|
||||||
if (isDuplicate == NULL)
|
if (isDuplicate == NULL)
|
||||||
{
|
{
|
||||||
@@ -101,13 +107,13 @@ void *nextTreeData(TreeNode *root)
|
|||||||
{
|
{
|
||||||
// Add tree to stack so that bigest entry is ontop
|
// Add tree to stack so that bigest entry is ontop
|
||||||
stackRoot = nextTreeDataRec(root, stackRoot);
|
stackRoot = nextTreeDataRec(root, stackRoot);
|
||||||
pointerToReturn = stackRoot;
|
pointerToReturn = top(stackRoot);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// return current top entry and then pop that top entry
|
// return current top entry and then pop that top entry
|
||||||
pointerToReturn = top(stackRoot);
|
|
||||||
stackRoot = pop(stackRoot);
|
stackRoot = pop(stackRoot);
|
||||||
|
pointerToReturn = top(stackRoot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -115,7 +115,7 @@ void test_nextTreeDataReturnsNextDataCorrectly(void)
|
|||||||
// check if nextTreeData returns Data correctly
|
// check if nextTreeData returns Data correctly
|
||||||
TEST_ASSERT_NOT_NULL(entry);
|
TEST_ASSERT_NOT_NULL(entry);
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL(score7, *(int *)nextTreeData(NULL));
|
TEST_ASSERT_EQUAL(score7, *(int *)entry);
|
||||||
TEST_ASSERT_EQUAL(score3, *(int *)nextTreeData(NULL));
|
TEST_ASSERT_EQUAL(score3, *(int *)nextTreeData(NULL));
|
||||||
TEST_ASSERT_EQUAL(score6, *(int *)nextTreeData(NULL));
|
TEST_ASSERT_EQUAL(score6, *(int *)nextTreeData(NULL));
|
||||||
TEST_ASSERT_EQUAL(score1, *(int *)nextTreeData(NULL));
|
TEST_ASSERT_EQUAL(score1, *(int *)nextTreeData(NULL));
|
||||||
|
|||||||
BIN
Binary file not shown.
@@ -39,13 +39,13 @@ $(program_obj_filesobj_files): %.o: %.c
|
|||||||
# echo "needs to be implemented"
|
# echo "needs to be implemented"
|
||||||
|
|
||||||
bintree: bintree.c
|
bintree: bintree.c
|
||||||
$(CC) $(FLAGS) -c bintree bintree.c
|
$(CC) $(FLAGS) -c bintree bintree.c
|
||||||
|
|
||||||
bintreeTests: stack.o bintree.o bintreeTests.c $(unityfolder)/unity.c
|
bintreeTests: stack.o bintree.o bintreeTests.c $(unityfolder)/unity.c
|
||||||
$(CC) $(FLAGS) -o runbintreeTests bintreeTests.c bintree.o stack.o $(unityfolder)/unity.c
|
$(CC) $(FLAGS) -o runbintreeTests bintreeTests.c bintree.o stack.o $(unityfolder)/unity.c
|
||||||
|
|
||||||
stack: stack.c
|
stack: stack.c
|
||||||
$(CC) $(FLAGS) -c stack stack.c
|
$(CC) $(FLAGS) -c stack stack.c
|
||||||
|
|
||||||
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
|
test_stack: stack.o test_stack.c $(unityfolder)/unity.c
|
||||||
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(unityfolder)/unity.c
|
$(CC) $(FLAGS) -o runstackTests test_stack.c stack.o $(unityfolder)/unity.c
|
||||||
|
|||||||
@@ -23,13 +23,22 @@ static int compareUInt(const void *a, const void *b)
|
|||||||
if (A > B) return 1;
|
if (A > B) return 1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// Sortiervergleich für qsort
|
|
||||||
int compareQsort(const void *a, const void *b)
|
static int existsInTree(TreeNode *root, unsigned int value)
|
||||||
{
|
{
|
||||||
unsigned int A = *(const unsigned int*)a;
|
if (!root) return 0;
|
||||||
unsigned int B = *(const unsigned int*)b;
|
|
||||||
if (A < B) return -1;
|
unsigned int *data;
|
||||||
if (A > B) return +1;
|
|
||||||
|
// Traversierung initialisieren
|
||||||
|
data = nextTreeData(root);
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
if (*data == value)
|
||||||
|
return 1;
|
||||||
|
} while ((data = nextTreeData(NULL)) != NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,56 +48,50 @@ unsigned int *createNumbers(unsigned int len)
|
|||||||
|
|
||||||
srand((unsigned int)time(NULL));
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
// Speicher für das Array
|
|
||||||
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
|
unsigned int *numbers = malloc(sizeof(unsigned int) * len);
|
||||||
if (!numbers) return NULL;
|
if (!numbers) return NULL;
|
||||||
|
|
||||||
TreeNode *root = NULL; // Baumwurzel
|
TreeNode *root = NULL;
|
||||||
unsigned int value;
|
unsigned int value;
|
||||||
int isDuplicate;
|
|
||||||
|
|
||||||
//Array mit eindeutigen Zufallszahlen füllen
|
// Array mit eindeutigen Zufallszahlen füllen
|
||||||
for (unsigned int i = 0; i < len; i++)
|
for (unsigned int i = 0; i < len; i++)
|
||||||
{
|
{
|
||||||
while (1)
|
do
|
||||||
{
|
{
|
||||||
value = (rand() % (2 * len)) + 1; // Zufallszahl 1..2*len
|
value = (rand() % (2 * len)) + 1;
|
||||||
isDuplicate = 0;
|
|
||||||
|
|
||||||
TreeNode *newRoot = addToTree(
|
|
||||||
root,
|
|
||||||
&value,
|
|
||||||
sizeof(unsigned int),
|
|
||||||
compareUInt,
|
|
||||||
&isDuplicate
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!isDuplicate)
|
|
||||||
{
|
|
||||||
// Neue Zahl - akzeptieren
|
|
||||||
root = newRoot;
|
|
||||||
numbers[i] = value;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Sonst neue Zahl generieren
|
|
||||||
}
|
}
|
||||||
|
while (existsInTree(root, value));
|
||||||
|
|
||||||
|
// Baum wie highscore benutzen (immer NULL)
|
||||||
|
root = addToTree(
|
||||||
|
root,
|
||||||
|
&value,
|
||||||
|
sizeof(unsigned int),
|
||||||
|
compareUInt,
|
||||||
|
NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
numbers[i] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
//genau eine Zufallszahl duplizieren
|
// genau eine Zufallszahl duplizieren
|
||||||
unsigned int idx1 = rand() % len;
|
unsigned int idx1 = rand() % len;
|
||||||
unsigned int idx2 = rand() % len;
|
unsigned int idx2;
|
||||||
|
|
||||||
while (idx2 == idx1)
|
do
|
||||||
|
{
|
||||||
idx2 = rand() % len;
|
idx2 = rand() % len;
|
||||||
|
}
|
||||||
|
while (idx2 == idx1);
|
||||||
|
|
||||||
numbers[idx2] = numbers[idx1];
|
numbers[idx2] = numbers[idx1];
|
||||||
|
|
||||||
// Baum wieder freigeben
|
|
||||||
clearTree(root);
|
clearTree(root);
|
||||||
|
|
||||||
return numbers;
|
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)
|
||||||
{
|
{
|
||||||
@@ -101,7 +104,7 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
memcpy(copy, numbers, len * sizeof(unsigned int));
|
memcpy(copy, numbers, len * sizeof(unsigned int));
|
||||||
|
|
||||||
// Sortieren
|
// Sortieren
|
||||||
qsort(copy, len, sizeof(unsigned int), compareQsort);
|
qsort(copy, len, sizeof(unsigned int), compareUInt);
|
||||||
|
|
||||||
// Doppelte Zahl finden
|
// Doppelte Zahl finden
|
||||||
unsigned int duplicate = 0;
|
unsigned int duplicate = 0;
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+20
-44
@@ -14,60 +14,36 @@ void tearDown(void) {
|
|||||||
// gehört zu unit-Grundaufbau
|
// gehört zu unit-Grundaufbau
|
||||||
}
|
}
|
||||||
|
|
||||||
// prüft, ob ein Array nur EIN Duplikat enthält
|
// Testet, ob createNumbers ein gültiges Array erzeugt
|
||||||
static int countDuplicates(const unsigned int *arr, unsigned int len) {
|
// und genau ein Duplikat enthalten ist
|
||||||
int count = 0;
|
void test_createNumbers_basic(void)
|
||||||
for (unsigned int i = 0; i < len; i++) {
|
|
||||||
for (unsigned int j = i + 1; j < len; j++) {
|
|
||||||
if (arr[i] == arr[j]) {
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prüfen, ob createNumbers ein korrektes Array liefert
|
|
||||||
static void test_createNumbers_basic(void)
|
|
||||||
{
|
{
|
||||||
unsigned int len = 100;
|
unsigned int len = 10;
|
||||||
unsigned int *arr = createNumbers(len);
|
unsigned int *numbers = createNumbers(len);
|
||||||
|
|
||||||
TEST_ASSERT_NOT_NULL(arr); // prüft ob Speicher korrekt
|
// Array muss existieren
|
||||||
|
TEST_ASSERT_NOT_NULL(numbers);
|
||||||
// Prüfen: Länge stimmt
|
|
||||||
|
|
||||||
// Prüfen: Array enthält GENAU EIN Duplikat
|
|
||||||
int dupCount = countDuplicates(arr, len);
|
|
||||||
TEST_ASSERT_EQUAL_INT(1, dupCount);
|
|
||||||
|
|
||||||
free(arr);
|
// Es muss genau EIN Duplikat geben
|
||||||
|
unsigned int duplicate = getDuplicate(numbers, len);
|
||||||
|
TEST_ASSERT_NOT_EQUAL(0, duplicate);
|
||||||
|
|
||||||
|
free(numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST 2: Prüfen, ob getDuplicate die richtige doppelte Zahl erkennt
|
/// Testet, ob getDuplicate die korrekte doppelte Zahl erkennt
|
||||||
static void test_getDuplicate_correctValue(void)
|
|
||||||
|
void test_getDuplicate_correctValue(void)
|
||||||
{
|
{
|
||||||
unsigned int len = 200;
|
unsigned int numbers[] = {1, 3, 5, 7, 3};
|
||||||
unsigned int *arr = createNumbers(len);
|
unsigned int len = 5;
|
||||||
|
|
||||||
TEST_ASSERT_NOT_NULL(arr);
|
unsigned int dup = getDuplicate(numbers, len);
|
||||||
|
|
||||||
unsigned int duplicate = getDuplicate(arr, len);
|
TEST_ASSERT_EQUAL(3, dup);
|
||||||
|
|
||||||
// Manuelle Kontrolle: Der gefundene Wert muss tatsächlich doppelt vorkommen
|
|
||||||
int occurrences = 0;
|
|
||||||
for (unsigned int i = 0; i < len; i++) {
|
|
||||||
if (arr[i] == duplicate) {
|
|
||||||
occurrences++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_ASSERT_EQUAL_INT(2, occurrences);
|
|
||||||
|
|
||||||
free(arr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST 3: getDuplicate gibt 0 aus bei Fehlern
|
// Testet Fehlerfälle von getDuplicate
|
||||||
static void test_getDuplicate_errors(void)
|
static void test_getDuplicate_errors(void)
|
||||||
{
|
{
|
||||||
TEST_ASSERT_EQUAL_UINT(0, getDuplicate(NULL, 10));
|
TEST_ASSERT_EQUAL_UINT(0, getDuplicate(NULL, 10));
|
||||||
|
|||||||
Reference in New Issue
Block a user