generated from freudenreichan/info2Praktikum-DobleSpiel
145 lines
4.0 KiB
C
145 lines
4.0 KiB
C
#include "unity.h"
|
|
#include <stdlib.h>
|
|
#include "bintree.h"
|
|
|
|
static int compare(const void *a, const void *b)
|
|
{
|
|
return (*(int *)a > *(int *)b) - (*(int *)a < *(int *)b); // a und b werden in int konvertiert und deren Werte miteinander verglichen
|
|
// returns 1 for a>b or -1 for a<b
|
|
// in bintree.c wird ueberprueft, ob compare eine positive oder eine negative Zahl zurueckgibt,
|
|
// wenn a groeßer b, positiv und dann wird links nach Teilbauemen gesucht
|
|
}
|
|
|
|
void setUp() {}
|
|
void tearDown() {}
|
|
|
|
//Adds a single element to the tree
|
|
void test_add_single_element_to_Tree()
|
|
{
|
|
TreeNode *root = NULL;
|
|
int value = 5;
|
|
int duplicate = -1;
|
|
|
|
root = addToTree(root, &value, sizeof(int), compare, &duplicate);
|
|
|
|
TEST_ASSERT_NOT_NULL(root); //uberprueft, ob root dem Tree hinzugefuegt werden konnte
|
|
TEST_ASSERT_EQUAL_INT(5, *(int*)root->data); //ueberprueft, ob der Wert fuer data richtig uebernommen wurde
|
|
TEST_ASSERT_EQUAL_INT(0, duplicate); //ueberprueft, ob isDuplicate 0 gesetzt wurde (neue Knoten -> isDuplicate sollte 0 sein)
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
//Adds multiplie elements to a tree
|
|
void test_add_multiple_elements_to_Tree()
|
|
{
|
|
TreeNode *root = NULL;
|
|
int value[] = {2, 5, 7, 9};
|
|
int duplicate = -1;
|
|
|
|
for(int j = 0; j < 4; ++j)
|
|
{
|
|
root = addToTree(root, &value[j], sizeof(int), compare, &duplicate);
|
|
}
|
|
|
|
TEST_ASSERT_EQUAL_INT(4, treeSize(root));
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
//Detects the size of a tree
|
|
void test_detect_empty_size()
|
|
{
|
|
TEST_ASSERT_EQUAL_INT(0, treeSize(NULL));
|
|
}
|
|
|
|
//checks, wether size of tree is correctly determined and wether clearTree() works
|
|
// Test: Duplikate nicht erlaubt (isDuplicate != NULL)
|
|
void test_detect_size() {
|
|
TreeNode *root = NULL;
|
|
int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
|
|
int duplicate = 0; // wird pro Einfügen gesetzt
|
|
|
|
for (int j = 0; j < 10; ++j) {
|
|
root = addToTree(root, &values[j], sizeof(int), compare, &duplicate);
|
|
if (duplicate) {
|
|
// Optional: prüfen, dass ein Duplikat erkannt wurde
|
|
TEST_ASSERT_TRUE(duplicate == 1);
|
|
}
|
|
duplicate = 0; // zurücksetzen für nächstes Einfügen
|
|
}
|
|
|
|
// Prüfen der Baumgröße ohne Duplikate
|
|
TEST_ASSERT_EQUAL_INT(8, treeSize(root));
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
|
|
// Test: Duplikate erlaubt (isDuplicate == NULL)
|
|
void test_add_multiplie_elements_one_dup() {
|
|
TreeNode *root = NULL;
|
|
int values[] = {1, 3, 1, 4, 5, 6, 7, 5, 9, 10};
|
|
|
|
for (int j = 0; j < 10; ++j) {
|
|
root = addToTree(root, &values[j], sizeof(int), compare, NULL);
|
|
}
|
|
|
|
// Alle Werte inklusive Duplikate
|
|
TEST_ASSERT_EQUAL_INT(10, treeSize(root));
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
|
|
//Traverses the tree inorder to check wether nextTreeData works
|
|
// Hilfsfunktion: rekursive Inorder-Prüfung
|
|
void inorderCheck(TreeNode *node, int expected[], int *idx) {
|
|
if (node == NULL) return;
|
|
|
|
// Linken Teilbaum prüfen
|
|
inorderCheck(node->left, expected, idx);
|
|
|
|
// Aktuelles Element prüfen
|
|
TEST_ASSERT_EQUAL_INT(expected[*idx], *(int*)node->data);
|
|
(*idx)++;
|
|
|
|
// Rechten Teilbaum prüfen
|
|
inorderCheck(node->right, expected, idx);
|
|
}
|
|
|
|
void test_inorder() {
|
|
TreeNode *root = NULL;
|
|
int values[] = {5, 3, 7, 2, 4, 6, 8};
|
|
|
|
// Baum füllen
|
|
for (int i = 0; i < 7; i++) {
|
|
root = addToTree(root, &values[i], sizeof(int), compare, NULL);
|
|
}
|
|
|
|
// Erwartete Inorder-Reihenfolge
|
|
int expected[] = {2,3,4,5,6,7,8};
|
|
|
|
int idx = 0;
|
|
inorderCheck(root, expected, &idx);
|
|
|
|
// Alle Einträge geprüft?
|
|
TEST_ASSERT_EQUAL_INT(7, idx);
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
UNITY_BEGIN();
|
|
|
|
RUN_TEST(test_add_single_element_to_Tree);
|
|
RUN_TEST(test_add_multiple_elements_to_Tree);
|
|
RUN_TEST(test_add_multiplie_elements_one_dup);
|
|
RUN_TEST(test_detect_empty_size);
|
|
RUN_TEST(test_detect_size);
|
|
RUN_TEST(test_inorder);
|
|
|
|
return UNITY_END();
|
|
}
|