forked from freudenreichan/info2Praktikum-DobleSpiel
122 lines
2.6 KiB
C
122 lines
2.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "unity.h"
|
|
#include "bintree.h"
|
|
|
|
/* ---------- Helper ---------- */
|
|
|
|
int compareInt(const void *a, const void *b)
|
|
{
|
|
int ia = *(const int *)a;
|
|
int ib = *(const int *)b;
|
|
return ia - ib;
|
|
}
|
|
|
|
void setUp(void){}
|
|
void tearDown(void){}
|
|
|
|
/* ---------- addToTree ---------- */
|
|
|
|
void test_addToTree_adds_first_element(void)
|
|
{
|
|
TreeNode *root = NULL;
|
|
int value = 5;
|
|
int isDup = -1;
|
|
|
|
root = addToTree(root, &value, sizeof(int), compareInt, &isDup);
|
|
|
|
TEST_ASSERT_NOT_NULL(root);
|
|
TEST_ASSERT_EQUAL_INT(0, isDup);
|
|
TEST_ASSERT_EQUAL_INT(1, treeSize(root));
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
void test_addToTree_detects_duplicate(void)
|
|
{
|
|
TreeNode *root = NULL;
|
|
int value = 5;
|
|
int isDup = -1;
|
|
|
|
root = addToTree(root, &value, sizeof(int), compareInt, &isDup);
|
|
root = addToTree(root, &value, sizeof(int), compareInt, &isDup);
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, isDup);
|
|
TEST_ASSERT_EQUAL_INT(1, treeSize(root));
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
/* ---------- treeSize ---------- */
|
|
|
|
void test_treeSize_counts_multiple_elements(void)
|
|
{
|
|
TreeNode *root = NULL;
|
|
int values[] = {5, 2, 8, 1, 3};
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL);
|
|
|
|
TEST_ASSERT_EQUAL_UINT(5, treeSize(root));
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
/* ---------- nextTreeData ---------- */
|
|
|
|
void test_nextTreeData_returns_sorted_order(void)
|
|
{
|
|
TreeNode *root = NULL;
|
|
int values[] = {5, 2, 8, 1, 3};
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
root = addToTree(root, &values[i], sizeof(int), compareInt, NULL);
|
|
|
|
int expected[] = {1, 2, 3, 5, 8};
|
|
|
|
int *val;
|
|
|
|
/* erster Aufruf initialisiert den Iterator */
|
|
val = (int *)nextTreeData(root);
|
|
TEST_ASSERT_NOT_NULL(val);
|
|
TEST_ASSERT_EQUAL_INT(expected[0], *val);
|
|
|
|
/* weitere Aufrufe liefern die nächsten Elemente */
|
|
for (int i = 1; i < 5; i++)
|
|
{
|
|
val = (int *)nextTreeData(NULL);
|
|
TEST_ASSERT_NOT_NULL(val);
|
|
TEST_ASSERT_EQUAL_INT(expected[i], *val);
|
|
}
|
|
|
|
/* danach muss NULL kommen */
|
|
TEST_ASSERT_NULL(nextTreeData(NULL));
|
|
|
|
clearTree(root);
|
|
}
|
|
|
|
/* ---------- clearTree ---------- */
|
|
|
|
void test_clearTree_on_empty_tree_does_not_crash(void)
|
|
{
|
|
clearTree(NULL);
|
|
TEST_PASS();
|
|
}
|
|
|
|
/* ---------- MAIN ---------- */
|
|
|
|
int main(void)
|
|
{
|
|
UNITY_BEGIN();
|
|
|
|
RUN_TEST(test_addToTree_adds_first_element);
|
|
RUN_TEST(test_addToTree_detects_duplicate);
|
|
|
|
RUN_TEST(test_treeSize_counts_multiple_elements);
|
|
|
|
RUN_TEST(test_nextTreeData_returns_sorted_order);
|
|
|
|
RUN_TEST(test_clearTree_on_empty_tree_does_not_crash);
|
|
|
|
return UNITY_END();
|
|
} |