test_bintree komplett

This commit is contained in:
Maximilian Ott 2025-12-13 11:03:58 +01:00
parent 58abc8a774
commit c47a735954
2 changed files with 107 additions and 0 deletions

View File

@ -42,6 +42,10 @@ unsigned int *createNumbers(unsigned int len)
}
//TODO: sicherstellen, dass im Array keine doppelten Zahlen vorhanden sind (Max)
position = rand() % len;
duplicate_value = array[position];

103
test_bintree.c Normal file
View File

@ -0,0 +1,103 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "bintree.h"
// Vergleichsfunktion für den Binärbaum (nur für Tests)
static int testCompareFct(const void *argument1, const void *argument2)
{
unsigned int x = *(const unsigned int *)argument1;
unsigned int y = *(const unsigned int *)argument2;
if (x > y) return 1;
if (x < y) return -1;
return 0;
}
// Unit-Test für die Vergleichsfunktion
void test_compareFct(void)
{
unsigned int a = 3, b = 5, c = 3;
assert(testCompareFct(&a, &b) < 0);
assert(testCompareFct(&b, &a) > 0);
assert(testCompareFct(&a, &c) == 0);
}
// Test für Einfügen und Baumgröße
void test_addToTree_and_treeSize(void)
{
TreeNode *root = NULL;
int isDuplicate;
int values[] = {5, 3, 7, 2, 4, 6, 8};
int n = sizeof(values) / sizeof(values[0]);
for (int i = 0; i < n; i++)
{
root = addToTree(root, &values[i], sizeof(int),
testCompareFct, &isDuplicate);
assert(isDuplicate == 0);
}
assert(treeSize(root) == 7);
clearTree(root);
}
// Test für Duplikaterkennung
void test_duplicates(void)
{
TreeNode *root = NULL;
int isDuplicate;
int value = 5;
root = addToTree(root, &value, sizeof(int),
testCompareFct, &isDuplicate);
assert(isDuplicate == 0);
root = addToTree(root, &value, sizeof(int),
testCompareFct, &isDuplicate);
assert(isDuplicate == 1);
clearTree(root);
}
// Test für Inorder-Traversierung
void test_nextTreeData(void)
{
TreeNode *root = NULL;
int isDuplicate;
int values[] = {5, 3, 7, 2, 4, 6, 8};
int expected[] = {2, 3, 4, 5, 6, 7, 8};
for (int i = 0; i < 7; i++)
{
root = addToTree(root, &values[i], sizeof(int),
testCompareFct, &isDuplicate);
}
for (int i = 0; i < 7; i++)
{
int *data = (int *)nextTreeData(root);
assert(data != NULL);
assert(*data == expected[i]);
}
assert(nextTreeData(NULL) == NULL);
clearTree(root);
}
int main(void)
{
test_compareFct();
test_addToTree_and_treeSize();
test_duplicates();
test_nextTreeData();
printf("Alle Unit-Tests erfolgreich bestanden.\n");
return 0;
}