Doble-Spiel/test_bintree.c

103 lines
2.2 KiB
C

#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;
}