tests/test_list.c

changeset 1507
f4010cda9a2a
parent 1495
beee442be85a
child 1508
dfc0ddd9571e
equal deleted inserted replaced
1506:2a4339475bcf 1507:f4010cda9a2a
479 // special case - size 1, element matches 479 // special case - size 1, element matches
480 s = 40; 480 s = 40;
481 CX_TEST_ASSERT(0 == cx_array_binary_search_inf(array, 1, sizeof(int), &s, cx_cmp_int)); 481 CX_TEST_ASSERT(0 == cx_array_binary_search_inf(array, 1, sizeof(int), &s, cx_cmp_int));
482 CX_TEST_ASSERT(0 == cx_array_binary_search_sup(array, 1, sizeof(int), &s, cx_cmp_int)); 482 CX_TEST_ASSERT(0 == cx_array_binary_search_sup(array, 1, sizeof(int), &s, cx_cmp_int));
483 CX_TEST_ASSERT(0 == cx_array_binary_search(array, 1, sizeof(int), &s, cx_cmp_int)); 483 CX_TEST_ASSERT(0 == cx_array_binary_search(array, 1, sizeof(int), &s, cx_cmp_int));
484 }
485 }
486
487 CX_TEST(test_array_binary_search_with_duplicates) {
488 int array[18] = {
489 40, 50, 55, 55, 55, 57, 57, 58, 60,
490 62, 64, 65, 70, 70, 70, 78, 80, 90
491 };
492 int s;
493 CX_TEST_DO {
494 // exact matches (largest index on duplicate)
495 s = 40;
496 CX_TEST_ASSERT(0 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
497 s = 50;
498 CX_TEST_ASSERT(1 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
499 s = 55;
500 CX_TEST_ASSERT(4 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
501 s = 57;
502 CX_TEST_ASSERT(6 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
503 s = 58;
504 CX_TEST_ASSERT(7 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
505 s = 65;
506 CX_TEST_ASSERT(11 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
507 s = 70;
508 CX_TEST_ASSERT(14 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
509 s = 78;
510 CX_TEST_ASSERT(15 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
511
512 // not found
513 s = 30;
514 CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
515 s = 75;
516 CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
517 s = 52;
518 CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
519 s = 100;
520 CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int));
521
522 // infimum
523 s = 55; // also yields an exact match
524 CX_TEST_ASSERT(4 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int));
525 s = 56;
526 CX_TEST_ASSERT(4 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int));
527 s = 75;
528 CX_TEST_ASSERT(14 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int));
529
530 // supremum (smallest index)
531 s = 52;
532 CX_TEST_ASSERT(2 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int));
533 s = 66;
534 CX_TEST_ASSERT(12 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int));
535 s = 75;
536 CX_TEST_ASSERT(15 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int));
537 s = 70; // exact match, we want the smallest index
538 CX_TEST_ASSERT(12 == cx_array_binary_search_sup(array, 18, sizeof(int), &s, cx_cmp_int));
484 } 539 }
485 } 540 }
486 541
487 typedef struct node { 542 typedef struct node {
488 struct node *next; 543 struct node *next;
3342 cx_test_register(suite, test_array_reserve); 3397 cx_test_register(suite, test_array_reserve);
3343 cx_test_register(suite, test_array_reserve_unsupported_width); 3398 cx_test_register(suite, test_array_reserve_unsupported_width);
3344 cx_test_register(suite, test_array_insert_sorted); 3399 cx_test_register(suite, test_array_insert_sorted);
3345 cx_test_register(suite, test_array_insert_unique); 3400 cx_test_register(suite, test_array_insert_unique);
3346 cx_test_register(suite, test_array_binary_search); 3401 cx_test_register(suite, test_array_binary_search);
3402 cx_test_register(suite, test_array_binary_search_with_duplicates);
3347 3403
3348 cx_test_register(suite, test_list_arl_create); 3404 cx_test_register(suite, test_list_arl_create);
3349 cx_test_register(suite, test_list_arl_create_simple); 3405 cx_test_register(suite, test_list_arl_create_simple);
3350 cx_test_register(suite, test_list_arl_create_simple_for_pointers); 3406 cx_test_register(suite, test_list_arl_create_simple_for_pointers);
3351 cx_test_register(suite, test_list_parl_destroy_no_destr); 3407 cx_test_register(suite, test_list_parl_destroy_no_destr);

mercurial