Compare commits

...

2 Commits

Author SHA1 Message Date
93a20fb4da Unit Test bintree 2025-12-16 12:56:57 +01:00
9c0db0ff31 Shuffle Funktion in numbers.c 2025-12-16 12:43:36 +01:00
11 changed files with 83 additions and 5 deletions

BIN
bintree.o Normal file

Binary file not shown.

BIN
doble.exe Normal file

Binary file not shown.

BIN
highscore.o Normal file

Binary file not shown.

View File

@ -1,5 +0,0 @@
player_name;4993
player_name;4992
player_name;4990
player_name;4953
player1;3999

BIN
main.o Normal file

Binary file not shown.

View File

@ -16,6 +16,18 @@ static int compareUInt(const void *a, const void *b)
return 0; 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, // 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. // außer zwei Einträgen (ein Duplikat). Verwendet den Binärbaum, um Duplikate zu vermeiden.
unsigned int *createNumbers(unsigned int len) unsigned int *createNumbers(unsigned int len)
@ -54,6 +66,9 @@ unsigned int *createNumbers(unsigned int len)
unsigned int duplicateIndex = rand() % (len - 1); unsigned int duplicateIndex = rand() % (len - 1);
arr[len - 1] = arr[duplicateIndex]; arr[len - 1] = arr[duplicateIndex];
// Shuffle das Array, damit das Duplikat nicht immer am Ende steht
shuffle(arr, len);
// Baum freigeben // Baum freigeben
clearTree(root); clearTree(root);
return arr; return arr;

BIN
numbers.o Normal file

Binary file not shown.

BIN
stack.o Normal file

Binary file not shown.

68
test_bintree.c Normal file
View 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

Binary file not shown.

BIN
timer.o Normal file

Binary file not shown.