forked from freudenreichan/info2Praktikum-DobleSpiel
Polish bintree.c and implement tests
This commit is contained in:
3 files changed
+135
-11
No files matched your search
@@ -39,6 +39,7 @@ Startcode/doble_initial
|
||||
Startcode/doble
|
||||
Startcode/unitTests
|
||||
Startcode/highscores.txt
|
||||
runBintreeTests
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
|
||||
+4
-1
@@ -6,7 +6,7 @@
|
||||
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
|
||||
TreeNode *addToTree(TreeNode *root, const void *const data, const size_t dataSize, const CompareFctType compareFct, int *isDuplicate)
|
||||
{
|
||||
if(data == NULL || dataSize == 0 || compareFct == NULL)
|
||||
if(data == NULL || dataSize == 0)
|
||||
return root;
|
||||
|
||||
if(root == NULL)
|
||||
@@ -35,6 +35,9 @@ TreeNode *addToTree(TreeNode *root, const void *const data, const size_t dataSiz
|
||||
return root;
|
||||
}
|
||||
|
||||
if(compareFct == NULL)
|
||||
return root;
|
||||
|
||||
const int comparisonValue = compareFct(data, root->data);
|
||||
|
||||
if(comparisonValue > 0)
|
||||
|
||||
+130
-10
@@ -1,26 +1,138 @@
|
||||
#include <stdio.h>
|
||||
#include "bintree.h"
|
||||
#include "unity.h"
|
||||
|
||||
void test_erfolg(void)
|
||||
|
||||
static int compare_ints(const void *const a, const void *const b)
|
||||
{
|
||||
int number = 1;
|
||||
TEST_ASSERT_NOT_NULL(&number);
|
||||
return *(int *)a - *(int *)b;
|
||||
}
|
||||
|
||||
void test_fehler(void)
|
||||
static TreeNode *buildTree()
|
||||
{
|
||||
int *number = NULL;
|
||||
TEST_ASSERT_NOT_NULL(number);
|
||||
TreeNode *tree = NULL;
|
||||
|
||||
const int data1 = 1;
|
||||
tree = addToTree(NULL, &data1, sizeof(int), NULL, NULL);
|
||||
const int data2 = 3;
|
||||
tree = addToTree(tree, &data2, sizeof(int), compare_ints, NULL);
|
||||
const int data3 = 4;
|
||||
tree = addToTree(tree, &data3, sizeof(int), compare_ints, NULL);
|
||||
const int data4 = 2;
|
||||
tree = addToTree(tree, &data4, sizeof(int), compare_ints, NULL);
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
static void test_addToTreeAllNullReturnsNull(void)
|
||||
{
|
||||
TEST_ASSERT_NULL(addToTree(NULL, NULL, 0, NULL, NULL));
|
||||
}
|
||||
|
||||
static void test_addToTreeBuildsTreeInOrder(void)
|
||||
{
|
||||
const TreeNode *const tree = buildTree();
|
||||
|
||||
TEST_ASSERT_EQUAL(1, *(int *)tree->data);
|
||||
TEST_ASSERT_EQUAL(NULL, tree->left);
|
||||
TEST_ASSERT_NOT_NULL(tree->right);
|
||||
TEST_ASSERT_EQUAL(3, *(int *)tree->right->data);
|
||||
TEST_ASSERT_NOT_NULL(tree->right->right);
|
||||
TEST_ASSERT_EQUAL(4, *(int *)tree->right->right->data);
|
||||
TEST_ASSERT_NOT_NULL(tree->right->left);
|
||||
TEST_ASSERT_EQUAL(2, *(int *)tree->right->left->data);
|
||||
}
|
||||
|
||||
static void test_addToTreeReportsNoDuplicates(void)
|
||||
{
|
||||
int duplicates = -1;
|
||||
|
||||
TreeNode *tree = buildTree();
|
||||
const int data = -1;
|
||||
tree = addToTree(tree, &data, sizeof(int), compare_ints, &duplicates);
|
||||
|
||||
TEST_ASSERT_EQUAL(0, duplicates);
|
||||
}
|
||||
|
||||
static void test_addToTreeReportsDuplicates(void)
|
||||
{
|
||||
int duplicates = -1;
|
||||
|
||||
TreeNode *tree = buildTree();
|
||||
const int data = 1;
|
||||
tree = addToTree(tree, &data, sizeof(int), compare_ints, &duplicates);
|
||||
|
||||
TEST_ASSERT_EQUAL(1, duplicates);
|
||||
}
|
||||
|
||||
static void test_clearTree(void)
|
||||
{
|
||||
TreeNode *tree = buildTree();
|
||||
clearTree(tree);
|
||||
}
|
||||
|
||||
static void test_nextTreeDataForNullReturnsNull(void)
|
||||
{
|
||||
TEST_ASSERT_NULL(nextTreeData(NULL));
|
||||
}
|
||||
|
||||
static void test_nextTreeDataTraversesTree(void)
|
||||
{
|
||||
|
||||
const int *const value1 = nextTreeData(buildTree());
|
||||
TEST_ASSERT_NOT_NULL(value1);
|
||||
TEST_ASSERT_EQUAL(1, *value1);
|
||||
const int *const value2 = nextTreeData(NULL);
|
||||
TEST_ASSERT_NOT_NULL(value2);
|
||||
TEST_ASSERT_EQUAL(2, *value2);
|
||||
const int *const value3 = nextTreeData(NULL);
|
||||
TEST_ASSERT_NOT_NULL(value3);
|
||||
TEST_ASSERT_EQUAL(3, *value3);
|
||||
const int *const value4 = nextTreeData(NULL);
|
||||
TEST_ASSERT_NOT_NULL(value4);
|
||||
TEST_ASSERT_EQUAL(4, *value4);
|
||||
TEST_ASSERT_NULL(nextTreeData(NULL));
|
||||
}
|
||||
|
||||
static void test_nextTreeDataTraversesTwoTrees(void)
|
||||
{
|
||||
const int *const value1a = nextTreeData(buildTree());
|
||||
TEST_ASSERT_NOT_NULL(value1a);
|
||||
TEST_ASSERT_EQUAL(1, *value1a);
|
||||
const int *const value2a = nextTreeData(NULL);
|
||||
TEST_ASSERT_NOT_NULL(value2a);
|
||||
TEST_ASSERT_EQUAL(2, *value2a);
|
||||
const int *const value1b = nextTreeData(buildTree());
|
||||
TEST_ASSERT_NOT_NULL(value1b);
|
||||
TEST_ASSERT_EQUAL(1, *value1b);
|
||||
const int *const value2b = nextTreeData(NULL);
|
||||
TEST_ASSERT_NOT_NULL(value2b);
|
||||
TEST_ASSERT_EQUAL(2, *value2b);
|
||||
const int *const value3b = nextTreeData(NULL);
|
||||
TEST_ASSERT_NOT_NULL(value3b);
|
||||
TEST_ASSERT_EQUAL(3, *value3b);
|
||||
const int *const value4b = nextTreeData(NULL);
|
||||
TEST_ASSERT_NOT_NULL(value4b);
|
||||
TEST_ASSERT_EQUAL(4, *value4b);
|
||||
TEST_ASSERT_NULL(nextTreeData(NULL));
|
||||
}
|
||||
|
||||
static void test_treeSizeIsZeroForNULLTree(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL(0, treeSize(NULL));
|
||||
}
|
||||
|
||||
static void test_treeSizeIsCorrectForTestTree(void)
|
||||
{
|
||||
TEST_ASSERT_EQUAL(4, treeSize(buildTree()));
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
// TODO: Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
// TODO: Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
||||
}
|
||||
|
||||
int main()
|
||||
@@ -28,8 +140,16 @@ int main()
|
||||
UNITY_BEGIN();
|
||||
|
||||
printf("\n============================\nbinary tree tests\n============================\n");
|
||||
RUN_TEST(test_erfolg);
|
||||
RUN_TEST(test_fehler);
|
||||
RUN_TEST(test_addToTreeAllNullReturnsNull);
|
||||
RUN_TEST(test_addToTreeBuildsTreeInOrder);
|
||||
RUN_TEST(test_addToTreeReportsNoDuplicates);
|
||||
RUN_TEST(test_addToTreeReportsDuplicates);
|
||||
RUN_TEST(test_clearTree);
|
||||
RUN_TEST(test_nextTreeDataForNullReturnsNull);
|
||||
RUN_TEST(test_nextTreeDataTraversesTree);
|
||||
RUN_TEST(test_nextTreeDataTraversesTwoTrees);
|
||||
RUN_TEST(test_treeSizeIsZeroForNULLTree);
|
||||
RUN_TEST(test_treeSizeIsCorrectForTestTree);
|
||||
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in new issue
Block a user