Compare commits
2 Commits
be3cb9e2fc
...
93a20fb4da
| Author | SHA1 | Date | |
|---|---|---|---|
| 93a20fb4da | |||
| 9c0db0ff31 |
BIN
highscore.o
Normal file
BIN
highscore.o
Normal file
Binary file not shown.
@ -1,5 +0,0 @@
|
||||
player_name;4993
|
||||
player_name;4992
|
||||
player_name;4990
|
||||
player_name;4953
|
||||
player1;3999
|
||||
15
numbers.c
15
numbers.c
@ -16,6 +16,18 @@ static int compareUInt(const void *a, const void *b)
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- Hilfsfunktion: Shuffle des Arrays (Fisher-Yates) ------------------
|
||||
static void shuffle(unsigned int *array, unsigned int len)
|
||||
{
|
||||
for (unsigned int i = len - 1; i > 0; i--)
|
||||
{
|
||||
unsigned int j = rand() % (i + 1);
|
||||
unsigned int temp = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Gibt ein Array mit len zufälligen Zahlen zwischen 1 und 2*len zurück, die alle unterschiedlich sind,
|
||||
// außer zwei Einträgen (ein Duplikat). Verwendet den Binärbaum, um Duplikate zu vermeiden.
|
||||
unsigned int *createNumbers(unsigned int len)
|
||||
@ -54,6 +66,9 @@ unsigned int *createNumbers(unsigned int len)
|
||||
unsigned int duplicateIndex = rand() % (len - 1);
|
||||
arr[len - 1] = arr[duplicateIndex];
|
||||
|
||||
// Shuffle das Array, damit das Duplikat nicht immer am Ende steht
|
||||
shuffle(arr, len);
|
||||
|
||||
// Baum freigeben
|
||||
clearTree(root);
|
||||
return arr;
|
||||
|
||||
68
test_bintree.c
Normal file
68
test_bintree.c
Normal file
@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "bintree.h"
|
||||
|
||||
// Hilfsfunktion: Vergleich von int
|
||||
static int compareInt(const void *a, const void *b) {
|
||||
int ia = *(const int *)a;
|
||||
int ib = *(const int *)b;
|
||||
if (ia < ib) return -1;
|
||||
if (ia > ib) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("===== TEST BINTREE =====\n");
|
||||
|
||||
TreeNode *root = NULL;
|
||||
int isDup;
|
||||
|
||||
// Test 1: addToTree - Einfügen eindeutiger Werte
|
||||
int val1 = 10, val2 = 5, val3 = 15;
|
||||
root = addToTree(root, &val1, sizeof(int), compareInt, &isDup);
|
||||
if (isDup != 0) { printf("FAIL: Erstes Einfügen sollte kein Duplikat sein\n"); return 1; }
|
||||
root = addToTree(root, &val2, sizeof(int), compareInt, &isDup);
|
||||
if (isDup != 0) { printf("FAIL: Zweites Einfügen sollte kein Duplikat sein\n"); return 1; }
|
||||
root = addToTree(root, &val3, sizeof(int), compareInt, &isDup);
|
||||
if (isDup != 0) { printf("FAIL: Drittes Einfügen sollte kein Duplikat sein\n"); return 1; }
|
||||
printf("PASS: addToTree - eindeutige Werte\n");
|
||||
|
||||
// Test 2: addToTree - Duplikat hinzufügen
|
||||
int dup = 10;
|
||||
root = addToTree(root, &dup, sizeof(int), compareInt, &isDup);
|
||||
if (isDup != 1) { printf("FAIL: Duplikat sollte erkannt werden\n"); return 1; }
|
||||
printf("PASS: addToTree - Duplikat erkannt\n");
|
||||
|
||||
// Test 3: treeSize
|
||||
unsigned int size = treeSize(root);
|
||||
if (size != 3) { printf("FAIL: treeSize sollte 3 sein, ist %u\n", size); return 1; }
|
||||
printf("PASS: treeSize\n");
|
||||
|
||||
// Test 4: nextTreeData - Iteration (in-order: 5, 10, 15)
|
||||
void *data = nextTreeData(root);
|
||||
if (data == NULL || *(int*)data != 5) { printf("FAIL: Erstes Element sollte 5 sein\n"); return 1; }
|
||||
data = nextTreeData(NULL);
|
||||
if (data == NULL || *(int*)data != 10) { printf("FAIL: Zweites Element sollte 10 sein\n"); return 1; }
|
||||
data = nextTreeData(NULL);
|
||||
if (data == NULL || *(int*)data != 15) { printf("FAIL: Drittes Element sollte 15 sein\n"); return 1; }
|
||||
data = nextTreeData(NULL);
|
||||
if (data != NULL) { printf("FAIL: Nach dem letzten Element sollte NULL kommen\n"); return 1; }
|
||||
printf("PASS: nextTreeData - Iteration\n");
|
||||
|
||||
// Test 5: clearTree
|
||||
clearTree(root);
|
||||
root = NULL;
|
||||
// Nach clearTree sollte treeSize 0 sein (aber da root NULL, testen wir indirekt)
|
||||
printf("PASS: clearTree (no crash)\n");
|
||||
|
||||
// Test 6: Leerer Baum
|
||||
size = treeSize(NULL);
|
||||
if (size != 0) { printf("FAIL: Leerer Baum sollte Größe 0 haben\n"); return 1; }
|
||||
data = nextTreeData(NULL);
|
||||
if (data != NULL) { printf("FAIL: Leerer Baum sollte NULL zurückgeben\n"); return 1; }
|
||||
printf("PASS: Leerer Baum\n");
|
||||
|
||||
printf("ALL BINTREE TESTS PASSED\n");
|
||||
return 0;
|
||||
}
|
||||
BIN
test_bintree.exe
Normal file
BIN
test_bintree.exe
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user