test/avl_tests.c

changeset 243
2e74828c5e94
parent 225
a1a068c2c4ef
child 244
98dc2d3a9b1d
equal deleted inserted replaced
242:a3597d704421 243:2e74828c5e94
221 ucx_avl_free(tree1); 221 ucx_avl_free(tree1);
222 ucx_avl_free(tree2); 222 ucx_avl_free(tree2);
223 ucx_avl_free(tree3); 223 ucx_avl_free(tree3);
224 ucx_avl_free(tree4); 224 ucx_avl_free(tree4);
225 } 225 }
226
227 static intmax_t dist_int(void* a, void* b, void* n) {
228 return ((intmax_t)a)-((intmax_t)b);
229 }
230
231 UCX_TEST(test_ucx_avl_find) {
232 UcxAVLTree *tree = ucx_avl_new(ucx_ptrcmp);
233
234 size_t len = 12;
235 int val[] = {10, 15, 3, 4, -30, 20, 14, -11, 12, -5, 1, 13};
236
237 for (size_t i = 0 ; i < len ; i++) {
238 ucx_avl_put(tree, val[i], &(val[i]));
239 }
240
241 UCX_TEST_BEGIN
242
243 void* ret;
244
245 /* test present values */
246 ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_CLOSEST);
247 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find closest failed");
248 ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_EXACT);
249 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find exact failed");
250 ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_LOWER_BOUNDED);
251 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find LB failed");
252 ret = ucx_avl_find(tree,(intptr_t)-5, dist_int, UCX_AVL_FIND_UPPER_BOUNDED);
253 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find UB failed");
254 ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_CLOSEST);
255 UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find closest failed");
256 ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_EXACT);
257 UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find exact failed");
258 ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_LOWER_BOUNDED);
259 UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find LB failed");
260 ret = ucx_avl_find(tree,(intptr_t)12, dist_int, UCX_AVL_FIND_UPPER_BOUNDED);
261 UCX_TEST_ASSERT(ret && *((int*)ret) == 12, "AVL find UB failed");
262
263 /* test missing values */
264 ret = ucx_avl_find(tree,(intptr_t)-10, dist_int, UCX_AVL_FIND_CLOSEST);
265 UCX_TEST_ASSERT(ret && *((int*)ret) == -11, "AVL find closest failed");
266 ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_EXACT);
267 UCX_TEST_ASSERT(!ret, "AVL find exact failed");
268 ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_LOWER_BOUNDED);
269 UCX_TEST_ASSERT(ret && *((int*)ret) == -5, "AVL find LB failed");
270 ret = ucx_avl_find(tree,(intptr_t)-8, dist_int, UCX_AVL_FIND_UPPER_BOUNDED);
271 UCX_TEST_ASSERT(ret && *((int*)ret) == -11, "AVL find UB failed");
272 ret = ucx_avl_find(tree,(intptr_t)18, dist_int, UCX_AVL_FIND_CLOSEST);
273 UCX_TEST_ASSERT(ret && *((int*)ret) == 20, "AVL find closest failed");
274 ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_EXACT);
275 UCX_TEST_ASSERT(!ret, "AVL find exact failed");
276 ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_LOWER_BOUNDED);
277 UCX_TEST_ASSERT(ret && *((int*)ret) == 10, "AVL find LB failed");
278 ret = ucx_avl_find(tree,(intptr_t)7, dist_int, UCX_AVL_FIND_UPPER_BOUNDED);
279 UCX_TEST_ASSERT(ret && *((int*)ret) == 4, "AVL find UB failed");
280
281 UCX_TEST_END
282
283 ucx_avl_free(tree);
284 }

mercurial