Unit Test bintree

This commit is contained in:
Nicolas Reckert 2025-12-16 12:56:57 +01:00
parent 9c0db0ff31
commit 93a20fb4da
3 changed files with 68 additions and 1 deletions

View File

@ -1 +0,0 @@

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.