Git fork

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

Adapt oidtree test script to clar framework by using clar assertions
where necessary. `cl_parse_any_oid()` ensures the hash algorithm is set
before parsing. This prevents issues from an uninitialized or invalid
hash algorithm.

Introduce 'test_oidtree__initialize` handles the to set up of the global
oidtree variable and `test_oidtree__cleanup` frees the oidtree when all
tests are completed.

With this change, `check_each` stops at the first error encountered,
making it easier to address it.

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
14958507 69bc044d

+109 -124
+1 -1
Makefile
··· 1358 1358 CLAR_TEST_SUITES += u-mem-pool 1359 1359 CLAR_TEST_SUITES += u-oid-array 1360 1360 CLAR_TEST_SUITES += u-oidmap 1361 + CLAR_TEST_SUITES += u-oidtree 1361 1362 CLAR_TEST_SUITES += u-prio-queue 1362 1363 CLAR_TEST_SUITES += u-reftable-tree 1363 1364 CLAR_TEST_SUITES += u-strbuf ··· 1369 1370 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o 1370 1371 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o 1371 1372 1372 - UNIT_TEST_PROGRAMS += t-oidtree 1373 1373 UNIT_TEST_PROGRAMS += t-reftable-basics 1374 1374 UNIT_TEST_PROGRAMS += t-reftable-block 1375 1375 UNIT_TEST_PROGRAMS += t-reftable-merged
+1 -1
t/meson.build
··· 6 6 'unit-tests/u-mem-pool.c', 7 7 'unit-tests/u-oid-array.c', 8 8 'unit-tests/u-oidmap.c', 9 + 'unit-tests/u-oidtree.c', 9 10 'unit-tests/u-prio-queue.c', 10 11 'unit-tests/u-reftable-tree.c', 11 12 'unit-tests/u-strbuf.c', ··· 51 52 test('unit-tests', clar_unit_tests) 52 53 53 54 unit_test_programs = [ 54 - 'unit-tests/t-oidtree.c', 55 55 'unit-tests/t-reftable-basics.c', 56 56 'unit-tests/t-reftable-block.c', 57 57 'unit-tests/t-reftable-merged.c',
-122
t/unit-tests/t-oidtree.c
··· 1 - #include "test-lib.h" 2 - #include "lib-oid.h" 3 - #include "oidtree.h" 4 - #include "hash.h" 5 - #include "hex.h" 6 - #include "strvec.h" 7 - 8 - #define FILL_TREE(tree, ...) \ 9 - do { \ 10 - const char *hexes[] = { __VA_ARGS__ }; \ 11 - if (fill_tree_loc(tree, hexes, ARRAY_SIZE(hexes))) \ 12 - return; \ 13 - } while (0) 14 - 15 - static int fill_tree_loc(struct oidtree *ot, const char *hexes[], size_t n) 16 - { 17 - for (size_t i = 0; i < n; i++) { 18 - struct object_id oid; 19 - if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0)) 20 - return -1; 21 - oidtree_insert(ot, &oid); 22 - } 23 - return 0; 24 - } 25 - 26 - static void check_contains(struct oidtree *ot, const char *hex, int expected) 27 - { 28 - struct object_id oid; 29 - 30 - if (!check_int(get_oid_arbitrary_hex(hex, &oid), ==, 0)) 31 - return; 32 - if (!check_int(oidtree_contains(ot, &oid), ==, expected)) 33 - test_msg("oid: %s", oid_to_hex(&oid)); 34 - } 35 - 36 - struct expected_hex_iter { 37 - size_t i; 38 - struct strvec expected_hexes; 39 - const char *query; 40 - }; 41 - 42 - static enum cb_next check_each_cb(const struct object_id *oid, void *data) 43 - { 44 - struct expected_hex_iter *hex_iter = data; 45 - struct object_id expected; 46 - 47 - if (!check_int(hex_iter->i, <, hex_iter->expected_hexes.nr)) { 48 - test_msg("error: extraneous callback for query: ('%s'), object_id: ('%s')", 49 - hex_iter->query, oid_to_hex(oid)); 50 - return CB_BREAK; 51 - } 52 - 53 - if (!check_int(get_oid_arbitrary_hex(hex_iter->expected_hexes.v[hex_iter->i], 54 - &expected), ==, 0)) 55 - ; /* the data is bogus and cannot be used */ 56 - else if (!check(oideq(oid, &expected))) 57 - test_msg("expected: %s\n got: %s\n query: %s", 58 - oid_to_hex(&expected), oid_to_hex(oid), hex_iter->query); 59 - 60 - hex_iter->i += 1; 61 - return CB_CONTINUE; 62 - } 63 - 64 - LAST_ARG_MUST_BE_NULL 65 - static void check_each(struct oidtree *ot, const char *query, ...) 66 - { 67 - struct object_id oid; 68 - struct expected_hex_iter hex_iter = { .expected_hexes = STRVEC_INIT, 69 - .query = query }; 70 - const char *arg; 71 - va_list hex_args; 72 - 73 - va_start(hex_args, query); 74 - while ((arg = va_arg(hex_args, const char *))) 75 - strvec_push(&hex_iter.expected_hexes, arg); 76 - va_end(hex_args); 77 - 78 - if (!check_int(get_oid_arbitrary_hex(query, &oid), ==, 0)) 79 - return; 80 - oidtree_each(ot, &oid, strlen(query), check_each_cb, &hex_iter); 81 - 82 - if (!check_int(hex_iter.i, ==, hex_iter.expected_hexes.nr)) 83 - test_msg("error: could not find some 'object_id's for query ('%s')", query); 84 - strvec_clear(&hex_iter.expected_hexes); 85 - } 86 - 87 - static void setup(void (*f)(struct oidtree *ot)) 88 - { 89 - struct oidtree ot; 90 - 91 - oidtree_init(&ot); 92 - f(&ot); 93 - oidtree_clear(&ot); 94 - } 95 - 96 - static void t_contains(struct oidtree *ot) 97 - { 98 - FILL_TREE(ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e"); 99 - check_contains(ot, "44", 0); 100 - check_contains(ot, "441", 0); 101 - check_contains(ot, "440", 0); 102 - check_contains(ot, "444", 1); 103 - check_contains(ot, "4440", 1); 104 - check_contains(ot, "4444", 0); 105 - } 106 - 107 - static void t_each(struct oidtree *ot) 108 - { 109 - FILL_TREE(ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e"); 110 - check_each(ot, "12300", "123", NULL); 111 - check_each(ot, "3211", NULL); /* should not reach callback */ 112 - check_each(ot, "3210", "321", NULL); 113 - check_each(ot, "32100", "321", NULL); 114 - check_each(ot, "32", "320", "321", NULL); 115 - } 116 - 117 - int cmd_main(int argc UNUSED, const char **argv UNUSED) 118 - { 119 - TEST(setup(t_contains), "oidtree insert and contains works"); 120 - TEST(setup(t_each), "oidtree each works"); 121 - return test_done(); 122 - }
+107
t/unit-tests/u-oidtree.c
··· 1 + #include "unit-test.h" 2 + #include "lib-oid.h" 3 + #include "oidtree.h" 4 + #include "hash.h" 5 + #include "hex.h" 6 + #include "strvec.h" 7 + 8 + static struct oidtree ot; 9 + 10 + #define FILL_TREE(tree, ...) \ 11 + do { \ 12 + const char *hexes[] = { __VA_ARGS__ }; \ 13 + if (fill_tree_loc(tree, hexes, ARRAY_SIZE(hexes))) \ 14 + return; \ 15 + } while (0) 16 + 17 + static int fill_tree_loc(struct oidtree *ot, const char *hexes[], size_t n) 18 + { 19 + for (size_t i = 0; i < n; i++) { 20 + struct object_id oid; 21 + cl_parse_any_oid(hexes[i], &oid); 22 + oidtree_insert(ot, &oid); 23 + } 24 + return 0; 25 + } 26 + 27 + static void check_contains(struct oidtree *ot, const char *hex, int expected) 28 + { 29 + struct object_id oid; 30 + 31 + cl_parse_any_oid(hex, &oid); 32 + cl_assert_equal_i(oidtree_contains(ot, &oid), expected); 33 + } 34 + 35 + struct expected_hex_iter { 36 + size_t i; 37 + struct strvec expected_hexes; 38 + const char *query; 39 + }; 40 + 41 + static enum cb_next check_each_cb(const struct object_id *oid, void *data) 42 + { 43 + struct expected_hex_iter *hex_iter = data; 44 + struct object_id expected; 45 + 46 + cl_assert(hex_iter->i < hex_iter->expected_hexes.nr); 47 + 48 + cl_parse_any_oid(hex_iter->expected_hexes.v[hex_iter->i], 49 + &expected); 50 + cl_assert_equal_s(oid_to_hex(oid), oid_to_hex(&expected)); 51 + hex_iter->i += 1; 52 + return CB_CONTINUE; 53 + } 54 + 55 + LAST_ARG_MUST_BE_NULL 56 + static void check_each(struct oidtree *ot, const char *query, ...) 57 + { 58 + struct object_id oid; 59 + struct expected_hex_iter hex_iter = { .expected_hexes = STRVEC_INIT, 60 + .query = query }; 61 + const char *arg; 62 + va_list hex_args; 63 + 64 + va_start(hex_args, query); 65 + while ((arg = va_arg(hex_args, const char *))) 66 + strvec_push(&hex_iter.expected_hexes, arg); 67 + va_end(hex_args); 68 + 69 + cl_parse_any_oid(query, &oid); 70 + oidtree_each(ot, &oid, strlen(query), check_each_cb, &hex_iter); 71 + 72 + if (hex_iter.i != hex_iter.expected_hexes.nr) 73 + cl_failf("error: could not find some 'object_id's for query ('%s')", query); 74 + 75 + strvec_clear(&hex_iter.expected_hexes); 76 + } 77 + 78 + void test_oidtree__initialize(void) 79 + { 80 + oidtree_init(&ot); 81 + } 82 + 83 + void test_oidtree__cleanup(void) 84 + { 85 + oidtree_clear(&ot); 86 + } 87 + 88 + void test_oidtree__contains(void) 89 + { 90 + FILL_TREE(&ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e"); 91 + check_contains(&ot, "44", 0); 92 + check_contains(&ot, "441", 0); 93 + check_contains(&ot, "440", 0); 94 + check_contains(&ot, "444", 1); 95 + check_contains(&ot, "4440", 1); 96 + check_contains(&ot, "4444", 0); 97 + } 98 + 99 + void test_oidtree__each(void) 100 + { 101 + FILL_TREE(&ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e"); 102 + check_each(&ot, "12300", "123", NULL); 103 + check_each(&ot, "3211", NULL); /* should not reach callback */ 104 + check_each(&ot, "3210", "321", NULL); 105 + check_each(&ot, "32100", "321", NULL); 106 + check_each(&ot, "32", "320", "321", NULL); 107 + }