A3_DobleSpiel/bintreeTests.c
DESKTOP-0TGNH6T\alial ccd912be91 bintree and tests
2026-06-08 16:45:03 +02:00

180 lines
4.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "unity.h"
#include "bintree.h"
static int compareUnsignedInt(const void *arg1, const void *arg2)
{
unsigned int number1 = *(const unsigned int *)arg1;
unsigned int number2 = *(const unsigned int *)arg2;
if (number1 < number2)
{
return -1;
}
if (number1 > number2)
{
return 1;
}
return 0;
}
void test_addToTreeCreatesRootNodeAndCopiesData(void)
{
TreeNode *root = NULL;
unsigned int value = 10;
root = addToTree(root, &value, sizeof(unsigned int), compareUnsignedInt, NULL);
value = 99;
TEST_ASSERT_NOT_NULL(root);
TEST_ASSERT_NOT_NULL(root->data);
TEST_ASSERT_EQUAL_UINT32(10, *(unsigned int *)root->data);
TEST_ASSERT_NULL(root->left);
TEST_ASSERT_NULL(root->right);
clearTree(root);
}
void test_addToTreeInsertsSmallerValueOnLeftSide(void)
{
TreeNode *root = NULL;
unsigned int value1 = 10;
unsigned int value2 = 5;
root = addToTree(root, &value1, sizeof(unsigned int), compareUnsignedInt, NULL);
root = addToTree(root, &value2, sizeof(unsigned int), compareUnsignedInt, NULL);
TEST_ASSERT_NOT_NULL(root);
TEST_ASSERT_NOT_NULL(root->left);
TEST_ASSERT_EQUAL_UINT32(5, *(unsigned int *)root->left->data);
clearTree(root);
}
void test_addToTreeInsertsGreaterValueOnRightSide(void)
{
TreeNode *root = NULL;
unsigned int value1 = 10;
unsigned int value2 = 20;
root = addToTree(root, &value1, sizeof(unsigned int), compareUnsignedInt, NULL);
root = addToTree(root, &value2, sizeof(unsigned int), compareUnsignedInt, NULL);
TEST_ASSERT_NOT_NULL(root);
TEST_ASSERT_NOT_NULL(root->right);
TEST_ASSERT_EQUAL_UINT32(20, *(unsigned int *)root->right->data);
clearTree(root);
}
void test_addToTreeDetectsDuplicateWhenFlagIsUsed(void)
{
TreeNode *root = NULL;
unsigned int value1 = 10;
unsigned int value2 = 10;
int isDuplicate = 0;
root = addToTree(root, &value1, sizeof(unsigned int), compareUnsignedInt, &isDuplicate);
TEST_ASSERT_EQUAL_INT(0, isDuplicate);
root = addToTree(root, &value2, sizeof(unsigned int), compareUnsignedInt, &isDuplicate);
TEST_ASSERT_EQUAL_INT(1, isDuplicate);
TEST_ASSERT_EQUAL_UINT32(1, treeSize(root));
clearTree(root);
}
void test_addToTreeAcceptsDuplicateWhenFlagIsNull(void)
{
TreeNode *root = NULL;
unsigned int value = 10;
root = addToTree(root, &value, sizeof(unsigned int), compareUnsignedInt, NULL);
root = addToTree(root, &value, sizeof(unsigned int), compareUnsignedInt, NULL);
TEST_ASSERT_EQUAL_UINT32(2, treeSize(root));
clearTree(root);
}
void test_treeSizeReturnsCorrectNumberOfNodes(void)
{
TreeNode *root = NULL;
unsigned int values[] = {10, 5, 15, 3, 7};
for (unsigned int i = 0; i < 5; i++)
{
root = addToTree(root, &values[i], sizeof(unsigned int), compareUnsignedInt, NULL);
}
TEST_ASSERT_EQUAL_UINT32(5, treeSize(root));
clearTree(root);
}
void test_nextTreeDataReturnsValuesInSortedOrder(void)
{
TreeNode *root = NULL;
unsigned int values[] = {10, 5, 15, 3, 7};
unsigned int expectedValues[] = {3, 5, 7, 10, 15};
for (unsigned int i = 0; i < 5; i++)
{
root = addToTree(root, &values[i], sizeof(unsigned int), compareUnsignedInt, NULL);
}
for (unsigned int i = 0; i < 5; i++)
{
unsigned int *currentValue = NULL;
if (i == 0)
{
currentValue = (unsigned int *)nextTreeData(root);
}
else
{
currentValue = (unsigned int *)nextTreeData(NULL);
}
TEST_ASSERT_NOT_NULL(currentValue);
TEST_ASSERT_EQUAL_UINT32(expectedValues[i], *currentValue);
}
TEST_ASSERT_NULL(nextTreeData(NULL));
clearTree(root);
}
void test_clearTreeDoesNotCrashOnNull(void)
{
clearTree(NULL);
TEST_PASS();
}
void setUp(void)
{
}
void tearDown(void)
{
}
int main(void)
{
UNITY_BEGIN();
printf("\n============================\nBinary tree tests\n============================\n");
RUN_TEST(test_addToTreeCreatesRootNodeAndCopiesData);
RUN_TEST(test_addToTreeInsertsSmallerValueOnLeftSide);
RUN_TEST(test_addToTreeInsertsGreaterValueOnRightSide);
RUN_TEST(test_addToTreeDetectsDuplicateWhenFlagIsUsed);
RUN_TEST(test_addToTreeAcceptsDuplicateWhenFlagIsNull);
RUN_TEST(test_treeSizeReturnsCorrectNumberOfNodes);
RUN_TEST(test_nextTreeDataReturnsValuesInSortedOrder);
RUN_TEST(test_clearTreeDoesNotCrashOnNull);
return UNITY_END();
}