1.1 --- a/test/avl_tests.c Mon Mar 06 16:22:42 2017 +0100 1.2 +++ b/test/avl_tests.c Sat Jul 15 19:20:06 2017 +0200 1.3 @@ -223,3 +223,62 @@ 1.4 ucx_avl_free(tree3); 1.5 ucx_avl_free(tree4); 1.6 } 1.7 + 1.8 +static intmax_t dist_int(void* a, void* b, void* n) { 1.9 + return ((intmax_t)a)-((intmax_t)b); 1.10 +} 1.11 + 1.12 +UCX_TEST(test_ucx_avl_find) { 1.13 + UcxAVLTree *tree = ucx_avl_new(ucx_ptrcmp); 1.14 + 1.15 + size_t len = 12; 1.16 + int val[] = {10, 15, 3, 4, -30, 20, 14, -11, 12, -5, 1, 13}; 1.17 + 1.18 + for (size_t i = 0 ; i < len ; i++) { 1.19 + ucx_avl_put(tree, val[i], &(val[i])); 1.20 + } 1.21 + 1.22 + UCX_TEST_BEGIN 1.23 + 1.24 + void* ret; 1.25 + 1.26 + /* test present values */ 1.27 + ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_CLOSEST); 1.28 + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find closest failed"); 1.29 + ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_EXACT); 1.30 + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find exact failed"); 1.31 + ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_LOWER_BOUNDED); 1.32 + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find LB failed"); 1.33 + ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_UPPER_BOUNDED); 1.34 + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find UB failed"); 1.35 + ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_CLOSEST); 1.36 + UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find closest failed"); 1.37 + ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_EXACT); 1.38 + UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find exact failed"); 1.39 + ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_LOWER_BOUNDED); 1.40 + UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find LB failed"); 1.41 + ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_UPPER_BOUNDED); 1.42 + UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find UB failed"); 1.43 + 1.44 + /* test missing values */ 1.45 + ret = ucx_avl_find(tree,(intptr_t)-10, dist_int, UCX_AVL_FIND_CLOSEST); 1.46 + UCX_TEST_ASSERT(ret && *((int*)ret) == -11, "AVL find closest failed"); 1.47 + ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_EXACT); 1.48 + UCX_TEST_ASSERT(!ret, "AVL find exact failed"); 1.49 + ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_LOWER_BOUNDED); 1.50 + UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find LB failed"); 1.51 + ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_UPPER_BOUNDED); 1.52 + UCX_TEST_ASSERT(ret && *((int*)ret) == -11, "AVL find UB failed"); 1.53 + ret = ucx_avl_find(tree,(intptr_t)18, dist_int, UCX_AVL_FIND_CLOSEST); 1.54 + UCX_TEST_ASSERT(ret && *((int*)ret) == 20, "AVL find closest failed"); 1.55 + ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_EXACT); 1.56 + UCX_TEST_ASSERT(!ret, "AVL find exact failed"); 1.57 + ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_LOWER_BOUNDED); 1.58 + UCX_TEST_ASSERT(ret && *((int*)ret) == 10, "AVL find LB failed"); 1.59 + ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_UPPER_BOUNDED); 1.60 + UCX_TEST_ASSERT(ret && *((int*)ret) == 4, "AVL find UB failed"); 1.61 + 1.62 + UCX_TEST_END 1.63 + 1.64 + ucx_avl_free(tree); 1.65 +}