#include #include "bintree.h" #include "unity.h" static int compare_ints(const void *const a, const void *const b) { return *(int *)a - *(int *)b; } static TreeNode *buildTree() { 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_INT(1, *(int *)tree->data); TEST_ASSERT_NULL(tree->left); TEST_ASSERT_NOT_NULL(tree->right); TEST_ASSERT_EQUAL_INT(3, *(int *)tree->right->data); TEST_ASSERT_NOT_NULL(tree->right->right); TEST_ASSERT_EQUAL_INT(4, *(int *)tree->right->right->data); TEST_ASSERT_NOT_NULL(tree->right->left); TEST_ASSERT_EQUAL_INT(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_INT(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_INT(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_INT(1, *value1); const int *const value2 = nextTreeData(NULL); TEST_ASSERT_NOT_NULL(value2); TEST_ASSERT_EQUAL_INT(2, *value2); const int *const value3 = nextTreeData(NULL); TEST_ASSERT_NOT_NULL(value3); TEST_ASSERT_EQUAL_INT(3, *value3); const int *const value4 = nextTreeData(NULL); TEST_ASSERT_NOT_NULL(value4); TEST_ASSERT_EQUAL_INT(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_INT(1, *value1a); const int *const value2a = nextTreeData(NULL); TEST_ASSERT_NOT_NULL(value2a); TEST_ASSERT_EQUAL_INT(2, *value2a); const int *const value1b = nextTreeData(buildTree()); TEST_ASSERT_NOT_NULL(value1b); TEST_ASSERT_EQUAL_INT(1, *value1b); const int *const value2b = nextTreeData(NULL); TEST_ASSERT_NOT_NULL(value2b); TEST_ASSERT_EQUAL_INT(2, *value2b); const int *const value3b = nextTreeData(NULL); TEST_ASSERT_NOT_NULL(value3b); TEST_ASSERT_EQUAL_INT(3, *value3b); const int *const value4b = nextTreeData(NULL); TEST_ASSERT_NOT_NULL(value4b); TEST_ASSERT_EQUAL_INT(4, *value4b); TEST_ASSERT_NULL(nextTreeData(NULL)); } static void test_treeSizeIsZeroForNULLTree(void) { TEST_ASSERT_EQUAL_INT(0, treeSize(NULL)); } static void test_treeSizeIsCorrectForTestTree(void) { TEST_ASSERT_EQUAL_INT(4, treeSize(buildTree())); } void setUp(void) { } void tearDown(void) { } int main() { UNITY_BEGIN(); printf("\n============================\nbinary tree tests\n============================\n"); 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(); }