Skip to content

Commit 1495850

Browse files
Seyi007gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Seyi Kuforiji <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 69bc044 commit 1495850

File tree

4 files changed

+109
-124
lines changed

4 files changed

+109
-124
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,7 @@ CLAR_TEST_SUITES += u-hashmap
13581358
CLAR_TEST_SUITES += u-mem-pool
13591359
CLAR_TEST_SUITES += u-oid-array
13601360
CLAR_TEST_SUITES += u-oidmap
1361+
CLAR_TEST_SUITES += u-oidtree
13611362
CLAR_TEST_SUITES += u-prio-queue
13621363
CLAR_TEST_SUITES += u-reftable-tree
13631364
CLAR_TEST_SUITES += u-strbuf
@@ -1369,7 +1370,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
13691370
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
13701371
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
13711372

1372-
UNIT_TEST_PROGRAMS += t-oidtree
13731373
UNIT_TEST_PROGRAMS += t-reftable-basics
13741374
UNIT_TEST_PROGRAMS += t-reftable-block
13751375
UNIT_TEST_PROGRAMS += t-reftable-merged

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ clar_test_suites = [
66
'unit-tests/u-mem-pool.c',
77
'unit-tests/u-oid-array.c',
88
'unit-tests/u-oidmap.c',
9+
'unit-tests/u-oidtree.c',
910
'unit-tests/u-prio-queue.c',
1011
'unit-tests/u-reftable-tree.c',
1112
'unit-tests/u-strbuf.c',
@@ -51,7 +52,6 @@ clar_unit_tests = executable('unit-tests',
5152
test('unit-tests', clar_unit_tests)
5253

5354
unit_test_programs = [
54-
'unit-tests/t-oidtree.c',
5555
'unit-tests/t-reftable-basics.c',
5656
'unit-tests/t-reftable-block.c',
5757
'unit-tests/t-reftable-merged.c',

t/unit-tests/t-oidtree.c

Lines changed: 0 additions & 122 deletions
This file was deleted.

t/unit-tests/u-oidtree.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}

0 commit comments

Comments
 (0)