126 lines
3.8 KiB
C
126 lines
3.8 KiB
C
#include "unity.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "bintree.h"
|
|
|
|
int compareUnsignedInt(const void *a, const void *b) {
|
|
unsigned int x = *(unsigned int *)a;
|
|
unsigned int y = *(unsigned int *)b;
|
|
|
|
if (x < y)
|
|
return -1;
|
|
if (x > y)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
TreeNode *root = NULL;
|
|
|
|
void setUp(void) {
|
|
root = NULL; // vor jedem Test leeren
|
|
}
|
|
|
|
void tearDown(void) { clearTree(&root); }
|
|
|
|
// Test, ob addToTree Knoten korrekt hinzufügt
|
|
|
|
/*TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize,
|
|
CompareFctType compareFct, int *isDuplicate) */
|
|
void test_addToTree_basic(void) {
|
|
int isDuplicate;
|
|
unsigned int testInt = 10;
|
|
root = addToTree(root, &testInt, sizeof(testInt), compareUnsignedInt,
|
|
&isDuplicate);
|
|
TEST_ASSERT_NOT_NULL(root); // Knoten wurde erfolgreich erzeugt
|
|
TEST_ASSERT_EQUAL_UINT(
|
|
10,
|
|
*(unsigned int *)root
|
|
->data); // Datenzeiger wurde richtig gesetzt, void pointer auf
|
|
// unsigned int pointer casten, mit *wird der Wert abgerufen
|
|
TEST_ASSERT_EQUAL_INT(0, isDuplicate); // kein Duplikat
|
|
TEST_ASSERT_EQUAL_UINT(1, treeSize(root)); // der tree hat einen Eintrag
|
|
}
|
|
|
|
// Test, dass Duplikate erkannt werden
|
|
void test_addToTree_duplicate(void) {
|
|
int isDuplicate;
|
|
unsigned int val1 = 10, val2 = 10; // Duplikate
|
|
root = addToTree(root, &val1, sizeof(val1), compareUnsignedInt,
|
|
&isDuplicate); // val 1 zum leeren Baum hinzufügen
|
|
TEST_ASSERT_EQUAL_INT(0, isDuplicate); // erster Knoten->kein Duplikat
|
|
root = addToTree(root, &val2, sizeof(val2), compareUnsignedInt,
|
|
&isDuplicate); // val 2 hinzufügen
|
|
TEST_ASSERT_EQUAL_INT(1, isDuplicate); // Duplikat erkannt
|
|
TEST_ASSERT_EQUAL_UINT(1,
|
|
treeSize(root)); // Duplikate wurde nicht hinzugefügt
|
|
}
|
|
|
|
// Test nextTreeData Traversierung
|
|
void test_nextTreeData_in_order(void) {
|
|
unsigned int values[] = {20, 10, 30}; // erwartete Ausgabe: 10 -> 20 -> 30
|
|
int isDuplicate;
|
|
for (int i = 0; i < 3; i++) {
|
|
root = addToTree(root, &values[i], sizeof(values[i]), compareUnsignedInt,
|
|
&isDuplicate); // Baum füllen
|
|
}
|
|
|
|
unsigned int expected[] = {10, 20, 30}; // erwartet in Order Reihenfolge
|
|
int valueID = 0;
|
|
void *data;
|
|
|
|
// Neue Iteration starten
|
|
data = nextTreeData(root);
|
|
while (data != NULL) {
|
|
TEST_ASSERT_EQUAL_UINT(expected[valueID],
|
|
*(unsigned int *)data); // entspricht erwartetem Wert
|
|
|
|
valueID++;
|
|
data = nextTreeData(NULL); // weitere Elemente abrufen
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL_INT(3, valueID); // alle 3 Knoten besucht
|
|
}
|
|
|
|
// Testet, dass clearTree Speicher freigibt und Root auf NULL setzt
|
|
void test_clearTree_sets_root_null(void) {
|
|
int isDuplicate;
|
|
unsigned int val1 = 10, val2 = 20;
|
|
|
|
root = addToTree(root, &val1, sizeof(val1), compareUnsignedInt, &isDuplicate);
|
|
root = addToTree(root, &val2, sizeof(val2), compareUnsignedInt, &isDuplicate);
|
|
|
|
// Vor dem Clear prüfen, dass Root nicht NULL ist
|
|
TEST_ASSERT_NOT_NULL(root);
|
|
|
|
clearTree(&root);
|
|
|
|
// Nach dem Clear muss Root auf NULL gesetzt sein
|
|
TEST_ASSERT_NULL(root);
|
|
}
|
|
|
|
// Test treeSize zählt korrekt
|
|
void test_treeSize(void) {
|
|
unsigned int testInts[] = {10, 20, 5};
|
|
int isDuplicate;
|
|
for (int i = 0; i < 3; i++) {
|
|
root = addToTree(root, &testInts[i], sizeof(testInts[i]),
|
|
compareUnsignedInt, &isDuplicate);
|
|
}
|
|
TEST_ASSERT_EQUAL_UINT(3, treeSize(root));
|
|
}
|
|
|
|
int main(void) {
|
|
UNITY_BEGIN();
|
|
|
|
printf(
|
|
"\n------------------------binarytree test------------------------\n\n");
|
|
|
|
RUN_TEST(test_addToTree_basic);
|
|
RUN_TEST(test_addToTree_duplicate);
|
|
RUN_TEST(test_nextTreeData_in_order);
|
|
RUN_TEST(test_clearTree_sets_root_null);
|
|
RUN_TEST(test_treeSize);
|
|
return UNITY_END();
|
|
} |