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

Adapt oidmap 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_oidmap__initialize` handles the to set up of the global oidmap map with predefined key-value pairs, and `test_oidmap__cleanup` frees the oidmap and its entries when all tests are completed. The test loops through all entries to detect multiple errors. With this change, it 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>

Seyi Kuforiji committed Feb 25, 2025 at 11:10 UTC 69bc044def16e8d8f85cd321b00ae53abe96a567
4 files changed +138 -183
Makefile
+1 -1
@@ -1357,6 +1357,7 @@ CLAR_TEST_SUITES += u-hash
1357 CLAR_TEST_SUITES += u-hashmap
1358 CLAR_TEST_SUITES += u-mem-pool
1359 CLAR_TEST_SUITES += u-oid-array
1360 +CLAR_TEST_SUITES += u-oidmap
1361 CLAR_TEST_SUITES += u-prio-queue
1362 CLAR_TEST_SUITES += u-reftable-tree
1363 CLAR_TEST_SUITES += u-strbuf
@@ -1368,7 +1369,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
1369 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1370 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
1371
1371 -UNIT_TEST_PROGRAMS += t-oidmap
1372 UNIT_TEST_PROGRAMS += t-oidtree
1373 UNIT_TEST_PROGRAMS += t-reftable-basics
1374 UNIT_TEST_PROGRAMS += t-reftable-block
t/meson.build
+1 -1
@@ -5,6 +5,7 @@ clar_test_suites = [
5 'unit-tests/u-hashmap.c',
6 'unit-tests/u-mem-pool.c',
7 'unit-tests/u-oid-array.c',
8 + 'unit-tests/u-oidmap.c',
9 'unit-tests/u-prio-queue.c',
10 'unit-tests/u-reftable-tree.c',
11 'unit-tests/u-strbuf.c',
@@ -50,7 +51,6 @@ clar_unit_tests = executable('unit-tests',
51 test('unit-tests', clar_unit_tests)
52
53 unit_test_programs = [
53 - 'unit-tests/t-oidmap.c',
54 'unit-tests/t-oidtree.c',
55 'unit-tests/t-reftable-basics.c',
56 'unit-tests/t-reftable-block.c',
t/unit-tests/t-oidmap.c deleted
-181
@@ -1,181 +0,0 @@
1 -#include "test-lib.h"
2 -#include "lib-oid.h"
3 -#include "oidmap.h"
4 -#include "hash.h"
5 -#include "hex.h"
6 -
7 -/*
8 - * Elements we will put in oidmap structs are made of a key: the entry.oid
9 - * field, which is of type struct object_id, and a value: the name field (could
10 - * be a refname for example).
11 - */
12 -struct test_entry {
13 - struct oidmap_entry entry;
14 - char name[FLEX_ARRAY];
15 -};
16 -
17 -static const char *const key_val[][2] = { { "11", "one" },
18 - { "22", "two" },
19 - { "33", "three" } };
20 -
21 -static void setup(void (*f)(struct oidmap *map))
22 -{
23 - struct oidmap map = OIDMAP_INIT;
24 - int ret = 0;
25 -
26 - for (size_t i = 0; i < ARRAY_SIZE(key_val); i++){
27 - struct test_entry *entry;
28 -
29 - FLEX_ALLOC_STR(entry, name, key_val[i][1]);
30 - if ((ret = get_oid_arbitrary_hex(key_val[i][0], &entry->entry.oid))) {
31 - free(entry);
32 - break;
33 - }
34 - entry = oidmap_put(&map, entry);
35 - if (!check(entry == NULL))
36 - free(entry);
37 - }
38 -
39 - if (!ret)
40 - f(&map);
41 - oidmap_free(&map, 1);
42 -}
43 -
44 -static void t_replace(struct oidmap *map)
45 -{
46 - struct test_entry *entry, *prev;
47 -
48 - FLEX_ALLOC_STR(entry, name, "un");
49 - if (get_oid_arbitrary_hex("11", &entry->entry.oid))
50 - return;
51 - prev = oidmap_put(map, entry);
52 - if (!check(prev != NULL))
53 - return;
54 - check_str(prev->name, "one");
55 - free(prev);
56 -
57 - FLEX_ALLOC_STR(entry, name, "deux");
58 - if (get_oid_arbitrary_hex("22", &entry->entry.oid))
59 - return;
60 - prev = oidmap_put(map, entry);
61 - if (!check(prev != NULL))
62 - return;
63 - check_str(prev->name, "two");
64 - free(prev);
65 -}
66 -
67 -static void t_get(struct oidmap *map)
68 -{
69 - struct test_entry *entry;
70 - struct object_id oid;
71 -
72 - if (get_oid_arbitrary_hex("22", &oid))
73 - return;
74 - entry = oidmap_get(map, &oid);
75 - if (!check(entry != NULL))
76 - return;
77 - check_str(entry->name, "two");
78 -
79 - if (get_oid_arbitrary_hex("44", &oid))
80 - return;
81 - check(oidmap_get(map, &oid) == NULL);
82 -
83 - if (get_oid_arbitrary_hex("11", &oid))
84 - return;
85 - entry = oidmap_get(map, &oid);
86 - if (!check(entry != NULL))
87 - return;
88 - check_str(entry->name, "one");
89 -}
90 -
91 -static void t_remove(struct oidmap *map)
92 -{
93 - struct test_entry *entry;
94 - struct object_id oid;
95 -
96 - if (get_oid_arbitrary_hex("11", &oid))
97 - return;
98 - entry = oidmap_remove(map, &oid);
99 - if (!check(entry != NULL))
100 - return;
101 - check_str(entry->name, "one");
102 - check(oidmap_get(map, &oid) == NULL);
103 - free(entry);
104 -
105 - if (get_oid_arbitrary_hex("22", &oid))
106 - return;
107 - entry = oidmap_remove(map, &oid);
108 - if (!check(entry != NULL))
109 - return;
110 - check_str(entry->name, "two");
111 - check(oidmap_get(map, &oid) == NULL);
112 - free(entry);
113 -
114 - if (get_oid_arbitrary_hex("44", &oid))
115 - return;
116 - check(oidmap_remove(map, &oid) == NULL);
117 -}
118 -
119 -static int key_val_contains(struct test_entry *entry, char seen[])
120 -{
121 - for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
122 - struct object_id oid;
123 -
124 - if (get_oid_arbitrary_hex(key_val[i][0], &oid))
125 - return -1;
126 -
127 - if (oideq(&entry->entry.oid, &oid)) {
128 - if (seen[i])
129 - return 2;
130 - seen[i] = 1;
131 - return 0;
132 - }
133 - }
134 - return 1;
135 -}
136 -
137 -static void t_iterate(struct oidmap *map)
138 -{
139 - struct oidmap_iter iter;
140 - struct test_entry *entry;
141 - char seen[ARRAY_SIZE(key_val)] = { 0 };
142 - int count = 0;
143 -
144 - oidmap_iter_init(map, &iter);
145 - while ((entry = oidmap_iter_next(&iter))) {
146 - int ret;
147 - if (!check_int((ret = key_val_contains(entry, seen)), ==, 0)) {
148 - switch (ret) {
149 - case -1:
150 - break; /* error message handled by get_oid_arbitrary_hex() */
151 - case 1:
152 - test_msg("obtained entry was not given in the input\n"
153 - " name: %s\n oid: %s\n",
154 - entry->name, oid_to_hex(&entry->entry.oid));
155 - break;
156 - case 2:
157 - test_msg("duplicate entry detected\n"
158 - " name: %s\n oid: %s\n",
159 - entry->name, oid_to_hex(&entry->entry.oid));
160 - break;
161 - default:
162 - test_msg("BUG: invalid return value (%d) from key_val_contains()",
163 - ret);
164 - break;
165 - }
166 - } else {
167 - count++;
168 - }
169 - }
170 - check_int(count, ==, ARRAY_SIZE(key_val));
171 - check_int(hashmap_get_size(&map->map), ==, ARRAY_SIZE(key_val));
172 -}
173 -
174 -int cmd_main(int argc UNUSED, const char **argv UNUSED)
175 -{
176 - TEST(setup(t_replace), "replace works");
177 - TEST(setup(t_get), "get works");
178 - TEST(setup(t_remove), "remove works");
179 - TEST(setup(t_iterate), "iterate works");
180 - return test_done();
181 -}
t/unit-tests/u-oidmap.c new
+136
@@ -0,0 +1,136 @@
1 +#include "unit-test.h"
2 +#include "lib-oid.h"
3 +#include "oidmap.h"
4 +#include "hash.h"
5 +#include "hex.h"
6 +
7 +/*
8 + * Elements we will put in oidmap structs are made of a key: the entry.oid
9 + * field, which is of type struct object_id, and a value: the name field (could
10 + * be a refname for example).
11 + */
12 +struct test_entry {
13 + struct oidmap_entry entry;
14 + char name[FLEX_ARRAY];
15 +};
16 +
17 +static const char *const key_val[][2] = { { "11", "one" },
18 + { "22", "two" },
19 + { "33", "three" } };
20 +
21 +static struct oidmap map;
22 +
23 +void test_oidmap__initialize(void)
24 +{
25 + oidmap_init(&map, 0);
26 +
27 + for (size_t i = 0; i < ARRAY_SIZE(key_val); i++){
28 + struct test_entry *entry;
29 +
30 + FLEX_ALLOC_STR(entry, name, key_val[i][1]);
31 + cl_parse_any_oid(key_val[i][0], &entry->entry.oid);
32 + cl_assert(oidmap_put(&map, entry) == NULL);
33 + }
34 +}
35 +
36 +void test_oidmap__cleanup(void)
37 +{
38 + oidmap_free(&map, 1);
39 +}
40 +
41 +void test_oidmap__replace(void)
42 +{
43 + struct test_entry *entry, *prev;
44 +
45 + FLEX_ALLOC_STR(entry, name, "un");
46 + cl_parse_any_oid("11", &entry->entry.oid);
47 + prev = oidmap_put(&map, entry);
48 + cl_assert(prev != NULL);
49 + cl_assert_equal_s(prev->name, "one");
50 + free(prev);
51 +
52 + FLEX_ALLOC_STR(entry, name, "deux");
53 + cl_parse_any_oid("22", &entry->entry.oid);
54 + prev = oidmap_put(&map, entry);
55 + cl_assert(prev != NULL);
56 + cl_assert_equal_s(prev->name, "two");
57 + free(prev);
58 +}
59 +
60 +void test_oidmap__get(void)
61 +{
62 + struct test_entry *entry;
63 + struct object_id oid;
64 +
65 + cl_parse_any_oid("22", &oid);
66 + entry = oidmap_get(&map, &oid);
67 + cl_assert(entry != NULL);
68 + cl_assert_equal_s(entry->name, "two");
69 +
70 + cl_parse_any_oid("44", &oid);
71 + cl_assert(oidmap_get(&map, &oid) == NULL);
72 +
73 + cl_parse_any_oid("11", &oid);
74 + entry = oidmap_get(&map, &oid);
75 + cl_assert(entry != NULL);
76 + cl_assert_equal_s(entry->name, "one");
77 +}
78 +
79 +void test_oidmap__remove(void)
80 +{
81 + struct test_entry *entry;
82 + struct object_id oid;
83 +
84 + cl_parse_any_oid("11", &oid);
85 + entry = oidmap_remove(&map, &oid);
86 + cl_assert(entry != NULL);
87 + cl_assert_equal_s(entry->name, "one");
88 + cl_assert(oidmap_get(&map, &oid) == NULL);
89 + free(entry);
90 +
91 + cl_parse_any_oid("22", &oid);
92 + entry = oidmap_remove(&map, &oid);
93 + cl_assert(entry != NULL);
94 + cl_assert_equal_s(entry->name, "two");
95 + cl_assert(oidmap_get(&map, &oid) == NULL);
96 + free(entry);
97 +
98 + cl_parse_any_oid("44", &oid);
99 + cl_assert(oidmap_remove(&map, &oid) == NULL);
100 +}
101 +
102 +static int key_val_contains(struct test_entry *entry, char seen[])
103 +{
104 + for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
105 + struct object_id oid;
106 +
107 + cl_parse_any_oid(key_val[i][0], &oid);
108 +
109 + if (oideq(&entry->entry.oid, &oid)) {
110 + if (seen[i])
111 + return 2;
112 + seen[i] = 1;
113 + return 0;
114 + }
115 + }
116 + return 1;
117 +}
118 +
119 +void test_oidmap__iterate(void)
120 +{
121 + struct oidmap_iter iter;
122 + struct test_entry *entry;
123 + char seen[ARRAY_SIZE(key_val)] = { 0 };
124 + int count = 0;
125 +
126 + oidmap_iter_init(&map, &iter);
127 + while ((entry = oidmap_iter_next(&iter))) {
128 + if (key_val_contains(entry, seen) != 0) {
129 + cl_failf("Unexpected entry: name = %s, oid = %s",
130 + entry->name, oid_to_hex(&entry->entry.oid));
131 + }
132 + count++;
133 + }
134 + cl_assert_equal_i(count, ARRAY_SIZE(key_val));
135 + cl_assert_equal_i(hashmap_get_size(&map.map), ARRAY_SIZE(key_val));
136 +}