Git fork

t/unit-tests: convert oid-array test to use clar test framework

Adapt oid-array test script to clar framework by using clar assertions
where necessary. Remove descriptions from macros to reduce
redundancy, and move test input arrays to global scope for reuse across
multiple test functions. Introduce `test_oid_array__initialize()` to
explicitly initialize the hash algorithm.

These changes streamline the test suite, making individual tests
self-contained and reducing redundant code.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Seyi Kuforiji and committed by
Junio C Hamano
869a1edf a16a2ee3

+131 -128
+1 -1
Makefile
··· 1356 1356 CLAR_TEST_SUITES += u-hash 1357 1357 CLAR_TEST_SUITES += u-hashmap 1358 1358 CLAR_TEST_SUITES += u-mem-pool 1359 + CLAR_TEST_SUITES += u-oid-array 1359 1360 CLAR_TEST_SUITES += u-prio-queue 1360 1361 CLAR_TEST_SUITES += u-reftable-tree 1361 1362 CLAR_TEST_SUITES += u-strbuf ··· 1367 1368 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o 1368 1369 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o 1369 1370 1370 - UNIT_TEST_PROGRAMS += t-oid-array 1371 1371 UNIT_TEST_PROGRAMS += t-oidmap 1372 1372 UNIT_TEST_PROGRAMS += t-oidtree 1373 1373 UNIT_TEST_PROGRAMS += t-reftable-basics
+1 -1
t/meson.build
··· 4 4 'unit-tests/u-hash.c', 5 5 'unit-tests/u-hashmap.c', 6 6 'unit-tests/u-mem-pool.c', 7 + 'unit-tests/u-oid-array.c', 7 8 'unit-tests/u-prio-queue.c', 8 9 'unit-tests/u-reftable-tree.c', 9 10 'unit-tests/u-strbuf.c', ··· 49 50 test('unit-tests', clar_unit_tests) 50 51 51 52 unit_test_programs = [ 52 - 'unit-tests/t-oid-array.c', 53 53 'unit-tests/t-oidmap.c', 54 54 'unit-tests/t-oidtree.c', 55 55 'unit-tests/t-reftable-basics.c',
-126
t/unit-tests/t-oid-array.c
··· 1 - #define USE_THE_REPOSITORY_VARIABLE 2 - 3 - #include "test-lib.h" 4 - #include "lib-oid.h" 5 - #include "oid-array.h" 6 - #include "hex.h" 7 - 8 - static int fill_array(struct oid_array *array, const char *hexes[], size_t n) 9 - { 10 - for (size_t i = 0; i < n; i++) { 11 - struct object_id oid; 12 - 13 - if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0)) 14 - return -1; 15 - oid_array_append(array, &oid); 16 - } 17 - if (!check_uint(array->nr, ==, n)) 18 - return -1; 19 - return 0; 20 - } 21 - 22 - static int add_to_oid_array(const struct object_id *oid, void *data) 23 - { 24 - struct oid_array *array = data; 25 - 26 - oid_array_append(array, oid); 27 - return 0; 28 - } 29 - 30 - static void t_enumeration(const char **input_args, size_t input_sz, 31 - const char **expect_args, size_t expect_sz) 32 - { 33 - struct oid_array input = OID_ARRAY_INIT, expect = OID_ARRAY_INIT, 34 - actual = OID_ARRAY_INIT; 35 - size_t i; 36 - 37 - if (fill_array(&input, input_args, input_sz)) 38 - return; 39 - if (fill_array(&expect, expect_args, expect_sz)) 40 - return; 41 - 42 - oid_array_for_each_unique(&input, add_to_oid_array, &actual); 43 - if (!check_uint(actual.nr, ==, expect.nr)) 44 - return; 45 - 46 - for (i = 0; i < actual.nr; i++) { 47 - if (!check(oideq(&actual.oid[i], &expect.oid[i]))) 48 - test_msg("expected: %s\n got: %s\n index: %" PRIuMAX, 49 - oid_to_hex(&expect.oid[i]), oid_to_hex(&actual.oid[i]), 50 - (uintmax_t)i); 51 - } 52 - 53 - oid_array_clear(&actual); 54 - oid_array_clear(&input); 55 - oid_array_clear(&expect); 56 - } 57 - 58 - #define TEST_ENUMERATION(input, expect, desc) \ 59 - TEST(t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect)), \ 60 - desc " works") 61 - 62 - static void t_lookup(const char **input_hexes, size_t n, const char *query_hex, 63 - int lower_bound, int upper_bound) 64 - { 65 - struct oid_array array = OID_ARRAY_INIT; 66 - struct object_id oid_query; 67 - int ret; 68 - 69 - if (!check_int(get_oid_arbitrary_hex(query_hex, &oid_query), ==, 0)) 70 - return; 71 - if (fill_array(&array, input_hexes, n)) 72 - return; 73 - ret = oid_array_lookup(&array, &oid_query); 74 - 75 - if (!check_int(ret, <=, upper_bound) || 76 - !check_int(ret, >=, lower_bound)) 77 - test_msg("oid query for lookup: %s", oid_to_hex(&oid_query)); 78 - 79 - oid_array_clear(&array); 80 - } 81 - 82 - #define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound, desc) \ 83 - TEST(t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \ 84 - lower_bound, upper_bound), \ 85 - desc " works") 86 - 87 - static void setup(void) 88 - { 89 - /* The hash algo is used by oid_array_lookup() internally */ 90 - int algo = init_hash_algo(); 91 - if (check_int(algo, !=, GIT_HASH_UNKNOWN)) 92 - repo_set_hash_algo(the_repository, algo); 93 - } 94 - 95 - int cmd_main(int argc UNUSED, const char **argv UNUSED) 96 - { 97 - const char *arr_input[] = { "88", "44", "aa", "55" }; 98 - const char *arr_input_dup[] = { "88", "44", "aa", "55", 99 - "88", "44", "aa", "55", 100 - "88", "44", "aa", "55" }; 101 - const char *res_sorted[] = { "44", "55", "88", "aa" }; 102 - const char *nearly_55; 103 - 104 - if (!TEST(setup(), "setup")) 105 - test_skip_all("hash algo initialization failed"); 106 - 107 - TEST_ENUMERATION(arr_input, res_sorted, "ordered enumeration"); 108 - TEST_ENUMERATION(arr_input_dup, res_sorted, 109 - "ordered enumeration with duplicate suppression"); 110 - 111 - TEST_LOOKUP(arr_input, "55", 1, 1, "lookup"); 112 - TEST_LOOKUP(arr_input, "33", INT_MIN, -1, "lookup non-existent entry"); 113 - TEST_LOOKUP(arr_input_dup, "55", 3, 5, "lookup with duplicates"); 114 - TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1, 115 - "lookup non-existent entry with duplicates"); 116 - 117 - nearly_55 = init_hash_algo() == GIT_HASH_SHA1 ? 118 - "5500000000000000000000000000000000000001" : 119 - "5500000000000000000000000000000000000000000000000000000000000001"; 120 - TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0, 121 - "lookup with almost duplicate values"); 122 - TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1, 123 - "lookup with single duplicate value"); 124 - 125 - return test_done(); 126 - }
+129
t/unit-tests/u-oid-array.c
··· 1 + #define USE_THE_REPOSITORY_VARIABLE 2 + 3 + #include "unit-test.h" 4 + #include "lib-oid.h" 5 + #include "oid-array.h" 6 + #include "hex.h" 7 + 8 + static void fill_array(struct oid_array *array, const char *hexes[], size_t n) 9 + { 10 + for (size_t i = 0; i < n; i++) { 11 + struct object_id oid; 12 + 13 + cl_parse_any_oid(hexes[i], &oid); 14 + oid_array_append(array, &oid); 15 + } 16 + cl_assert_equal_i(array->nr, n); 17 + } 18 + 19 + static int add_to_oid_array(const struct object_id *oid, void *data) 20 + { 21 + struct oid_array *array = data; 22 + 23 + oid_array_append(array, oid); 24 + return 0; 25 + } 26 + 27 + static void t_enumeration(const char **input_args, size_t input_sz, 28 + const char **expect_args, size_t expect_sz) 29 + { 30 + struct oid_array input = OID_ARRAY_INIT, expect = OID_ARRAY_INIT, 31 + actual = OID_ARRAY_INIT; 32 + size_t i; 33 + 34 + fill_array(&input, input_args, input_sz); 35 + fill_array(&expect, expect_args, expect_sz); 36 + 37 + oid_array_for_each_unique(&input, add_to_oid_array, &actual); 38 + cl_assert_equal_i(actual.nr, expect.nr); 39 + 40 + for (i = 0; i < actual.nr; i++) 41 + cl_assert(oideq(&actual.oid[i], &expect.oid[i])); 42 + 43 + oid_array_clear(&actual); 44 + oid_array_clear(&input); 45 + oid_array_clear(&expect); 46 + } 47 + 48 + #define TEST_ENUMERATION(input, expect) \ 49 + t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect)); 50 + 51 + static void t_lookup(const char **input_hexes, size_t n, const char *query_hex, 52 + int lower_bound, int upper_bound) 53 + { 54 + struct oid_array array = OID_ARRAY_INIT; 55 + struct object_id oid_query; 56 + int ret; 57 + 58 + cl_parse_any_oid(query_hex, &oid_query); 59 + fill_array(&array, input_hexes, n); 60 + ret = oid_array_lookup(&array, &oid_query); 61 + 62 + cl_assert(ret <= upper_bound); 63 + cl_assert(ret >= lower_bound); 64 + 65 + oid_array_clear(&array); 66 + } 67 + 68 + #define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound) \ 69 + t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \ 70 + lower_bound, upper_bound); 71 + 72 + void test_oid_array__initialize(void) 73 + { 74 + /* The hash algo is used by oid_array_lookup() internally */ 75 + int algo = cl_setup_hash_algo(); 76 + repo_set_hash_algo(the_repository, algo); 77 + } 78 + 79 + static const char *arr_input[] = { "88", "44", "aa", "55" }; 80 + static const char *arr_input_dup[] = { "88", "44", "aa", "55", 81 + "88", "44", "aa", "55", 82 + "88", "44", "aa", "55" }; 83 + static const char *res_sorted[] = { "44", "55", "88", "aa" }; 84 + 85 + void test_oid_array__enumerate_unique(void) 86 + { 87 + TEST_ENUMERATION(arr_input, res_sorted); 88 + } 89 + 90 + void test_oid_array__enumerate_duplicate(void) 91 + { 92 + TEST_ENUMERATION(arr_input_dup, res_sorted); 93 + } 94 + 95 + void test_oid_array__lookup(void) 96 + { 97 + TEST_LOOKUP(arr_input, "55", 1, 1); 98 + } 99 + 100 + void test_oid_array__lookup_non_existent(void) 101 + { 102 + TEST_LOOKUP(arr_input, "33", INT_MIN, -1); 103 + } 104 + 105 + void test_oid_array__lookup_duplicates(void) 106 + { 107 + TEST_LOOKUP(arr_input_dup, "55", 3, 5); 108 + } 109 + 110 + void test_oid_array__lookup_non_existent_dup(void) 111 + { 112 + TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1); 113 + } 114 + 115 + void test_oid_array__lookup_almost_dup(void) 116 + { 117 + const char *nearly_55; 118 + 119 + nearly_55 = cl_setup_hash_algo() == GIT_HASH_SHA1 ? 120 + "5500000000000000000000000000000000000001" : 121 + "5500000000000000000000000000000000000000000000000000000000000001"; 122 + 123 + TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0); 124 + } 125 + 126 + void test_oid_array__lookup_single_dup(void) 127 + { 128 + TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1); 129 + }