diff -r 3012e9b4214e -r d375d8056262 tests/test_list.c --- a/tests/test_list.c Tue Sep 17 19:38:41 2024 +0200 +++ b/tests/test_list.c Tue Sep 17 23:11:17 2024 +0200 @@ -146,6 +146,36 @@ } } +CX_TEST(test_array_binary_search) { + int array[18] = { + 40, 50, 51, 52, 54, 56, 57, 58, 60, + 62, 64, 65, 70, 75, 77, 78, 80, 90 + }; + + CX_TEST_DO { + cx_for_n(i, 18) { + CX_TEST_ASSERT(i == cx_array_binary_search(array, 18, sizeof(int), &array[i], cx_cmp_int)); + } + + int s = 58; + CX_TEST_ASSERT(7 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); + s = 60; + CX_TEST_ASSERT(8 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); + s = 59; + CX_TEST_ASSERT(7 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); + CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); + s = 79; + CX_TEST_ASSERT(15 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); + CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); + s = 66; + CX_TEST_ASSERT(11 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); + CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); + s = 69; + CX_TEST_ASSERT(11 == cx_array_binary_search_inf(array, 18, sizeof(int), &s, cx_cmp_int)); + CX_TEST_ASSERT(18 == cx_array_binary_search(array, 18, sizeof(int), &s, cx_cmp_int)); + } +} + typedef struct node { struct node *next; struct node *prev; @@ -1582,6 +1612,7 @@ cx_test_register(suite, test_array_add); cx_test_register(suite, test_array_insert_sorted); + cx_test_register(suite, test_array_binary_search); cx_test_register(suite, test_list_arl_create); cx_test_register(suite, test_list_arl_create_simple);