--- a/test/avl_tests.c Mon Mar 06 16:22:42 2017 +0100 +++ b/test/avl_tests.c Sat Jul 15 19:20:06 2017 +0200 @@ -223,3 +223,62 @@ ucx_avl_free(tree3); ucx_avl_free(tree4); } + +static intmax_t dist_int(void* a, void* b, void* n) { + return ((intmax_t)a)-((intmax_t)b); +} + +UCX_TEST(test_ucx_avl_find) { + UcxAVLTree *tree = ucx_avl_new(ucx_ptrcmp); + + size_t len = 12; + int val[] = {10, 15, 3, 4, -30, 20, 14, -11, 12, -5, 1, 13}; + + for (size_t i = 0 ; i < len ; i++) { + ucx_avl_put(tree, val[i], &(val[i])); + } + + UCX_TEST_BEGIN + + void* ret; + + /* test present values */ + ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_CLOSEST); + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find closest failed"); + ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_EXACT); + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find exact failed"); + ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_LOWER_BOUNDED); + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find LB failed"); + ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_UPPER_BOUNDED); + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find UB failed"); + ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_CLOSEST); + UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find closest failed"); + ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_EXACT); + UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find exact failed"); + ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_LOWER_BOUNDED); + UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find LB failed"); + ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_UPPER_BOUNDED); + UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find UB failed"); + + /* test missing values */ + ret = ucx_avl_find(tree,(intptr_t)-10, dist_int, UCX_AVL_FIND_CLOSEST); + UCX_TEST_ASSERT(ret && *((int*)ret) == -11, "AVL find closest failed"); + ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_EXACT); + UCX_TEST_ASSERT(!ret, "AVL find exact failed"); + ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_LOWER_BOUNDED); + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find LB failed"); + ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_UPPER_BOUNDED); + UCX_TEST_ASSERT(ret && *((int*)ret) == -11, "AVL find UB failed"); + ret = ucx_avl_find(tree,(intptr_t)18, dist_int, UCX_AVL_FIND_CLOSEST); + UCX_TEST_ASSERT(ret && *((int*)ret) == 20, "AVL find closest failed"); + ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_EXACT); + UCX_TEST_ASSERT(!ret, "AVL find exact failed"); + ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_LOWER_BOUNDED); + UCX_TEST_ASSERT(ret && *((int*)ret) == 10, "AVL find LB failed"); + ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_UPPER_BOUNDED); + UCX_TEST_ASSERT(ret && *((int*)ret) == 4, "AVL find UB failed"); + + UCX_TEST_END + + ucx_avl_free(tree); +}