#include "../src/tree.c" #include "../src/testing.h" static int test_tree_new(void) { int rc = -1; const struct Tree *t = NULL; test_start("tree_new()"); { testing("simple allocation"); const size_t size = sizeof(long long); if (tree_new(size, &t)) { logerr("tree_new()"); goto out; } assert(t->value_size == size); tree_free(&t); test_ok(); } rc = 0; out: if (t != NULL) { tree_free(&t); } return rc; } static int test_tree_free(void) { int rc = -1; const struct Tree *t = NULL; test_start("tree_free()"); { testing("*t becomes NULL again after tree_free(&t)"); assert(t == NULL); if (tree_new(1U, &t)) { logerr("tree_new()"); goto out; } assert(t != NULL); tree_free(&t); assert(t == NULL); test_ok(); } rc = 0; out: if (t != NULL) { tree_free(&t); } return rc; } static void test_tree_contains(void) { const size_t value_size = sizeof(int); /* ASCII representation of the tree: 12 __/ \__ / \ 8 18 / \ / 5 11 17 / 4 **/ // Values that don't exist on the tree const int n0 = 0; const int n1 = 1; const int n2 = 2; const int n3 = 3; const int n6 = 6; const int n7 = 7; const int n9 = 9; const int n10 = 10; const int n13 = 13; const int n14 = 14; const int n15 = 15; const int n16 = 16; const int n19 = 19; // Values that DO exist on the tree const int y12 = 12; const int y8 = 8; const int y18 = 18; const int y5 = 5; const int y11 = 11; const int y17 = 17; const int y4 = 4; const struct Tree *const t = &(struct Tree) { .value = &y12, .value_size = value_size, .height = 4U, .left = &(struct Tree) { .value = &y8, .value_size = value_size, .left = &(struct Tree) { .value = &y5, .value_size = value_size, .left = &(struct Tree) { .value = &y4, .value_size = value_size, .left = NULL, .right = NULL, .height = 1U }, .right = NULL, .height = 2U, }, .right = &(struct Tree) { .value = &y11, .value_size = value_size, .left = NULL, .right = NULL, .height = 1U, }, .height = 3U, }, .right = &(struct Tree) { .value = &y18, .value_size = value_size, .left = &(struct Tree) { .value = &y17, .value_size = value_size, .left = NULL, .right = NULL, .height = 1U, }, .right = NULL, .height = 2U, }, }; assert(!tree_contains(t, &n0)); assert(!tree_contains(t, &n1)); assert(!tree_contains(t, &n2)); assert(!tree_contains(t, &n3)); assert( tree_contains(t, &y4)); assert( tree_contains(t, &y5)); assert(!tree_contains(t, &n6)); assert(!tree_contains(t, &n7)); assert( tree_contains(t, &y8)); assert(!tree_contains(t, &n9)); assert(!tree_contains(t, &n10)); assert( tree_contains(t, &y11)); assert( tree_contains(t, &y12)); assert(!tree_contains(t, &n13)); assert(!tree_contains(t, &n14)); assert(!tree_contains(t, &n15)); assert(!tree_contains(t, &n16)); assert( tree_contains(t, &y17)); assert( tree_contains(t, &y18)); assert(!tree_contains(t, &n19)); } int main(void) { int rc = EXIT_FAILURE; if (test_tree_new()) { logerr("test_tree_new()"); goto out; } if (test_tree_free()) { logerr("test_tree_free()"); goto out; } test_tree_contains(); rc = EXIT_SUCCESS; out: return rc; }