t/unit-tests: convert hashmap test to use clar test framework
Adapts hashmap test script to clar framework by using clar assertions where necessary. 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
Jan 31, 2025 at 23:14 UTC
38b066ee7685d0074d3430284f975addda934c17
3 files changed
+114
-116
Makefile
+1
-1
@@ -1341,6 +1341,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
1341
1342
CLAR_TEST_SUITES += u-ctype
1343
CLAR_TEST_SUITES += u-hash
1344
+CLAR_TEST_SUITES += u-hashmap
1345
CLAR_TEST_SUITES += u-mem-pool
1346
CLAR_TEST_SUITES += u-prio-queue
1347
CLAR_TEST_SUITES += u-reftable-tree
@@ -1351,7 +1352,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
1352
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1353
1354
UNIT_TEST_PROGRAMS += t-example-decorate
1354
-UNIT_TEST_PROGRAMS += t-hashmap
1355
UNIT_TEST_PROGRAMS += t-oid-array
1356
UNIT_TEST_PROGRAMS += t-oidmap
1357
UNIT_TEST_PROGRAMS += t-oidtree
t/meson.build
+1
-1
@@ -1,6 +1,7 @@
1
clar_test_suites = [
2
'unit-tests/u-ctype.c',
3
'unit-tests/u-hash.c',
4
+ 'unit-tests/u-hashmap.c',
5
'unit-tests/u-mem-pool.c',
6
'unit-tests/u-prio-queue.c',
7
'unit-tests/u-reftable-tree.c',
@@ -45,7 +46,6 @@ test('unit-tests', clar_unit_tests)
46
47
unit_test_programs = [
48
'unit-tests/t-example-decorate.c',
48
- 'unit-tests/t-hashmap.c',
49
'unit-tests/t-oid-array.c',
50
'unit-tests/t-oidmap.c',
51
'unit-tests/t-oidtree.c',
t/unit-tests/u-hashmap.c
renamed
+112
-114
@@ -1,4 +1,4 @@
1
-#include "test-lib.h"
1
+#include "unit-test.h"
2
#include "hashmap.h"
3
#include "strbuf.h"
4
@@ -83,23 +83,23 @@ static void t_replace(struct hashmap *map, unsigned int ignore_case)
83
struct test_entry *entry;
84
85
entry = alloc_test_entry("key1", "value1", ignore_case);
86
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
86
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
87
88
entry = alloc_test_entry(ignore_case ? "Key1" : "key1", "value2",
89
ignore_case);
90
entry = hashmap_put_entry(map, entry, ent);
91
- if (check(entry != NULL))
92
- check_str(get_value(entry), "value1");
91
+ cl_assert(entry != NULL);
92
+ cl_assert_equal_s(get_value(entry), "value1");
93
free(entry);
94
95
entry = alloc_test_entry("fooBarFrotz", "value3", ignore_case);
96
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
96
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
97
98
entry = alloc_test_entry(ignore_case ? "FOObarFrotz" : "fooBarFrotz",
99
"value4", ignore_case);
100
entry = hashmap_put_entry(map, entry, ent);
101
- if (check(entry != NULL))
102
- check_str(get_value(entry), "value3");
101
+ cl_assert(entry != NULL);
102
+ cl_assert_equal_s(get_value(entry), "value3");
103
free(entry);
104
}
105
@@ -122,20 +122,18 @@ static void t_get(struct hashmap *map, unsigned int ignore_case)
122
for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
123
entry = alloc_test_entry(key_val[i][0], key_val[i][1],
124
ignore_case);
125
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
125
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
126
}
127
128
for (size_t i = 0; i < ARRAY_SIZE(query); i++) {
129
entry = get_test_entry(map, query[i][0], ignore_case);
130
- if (check(entry != NULL))
131
- check_str(get_value(entry), query[i][1]);
132
- else
133
- test_msg("query key: %s", query[i][0]);
130
+ cl_assert(entry != NULL);
131
+ cl_assert_equal_s(get_value(entry), query[i][1]);
132
}
133
136
- check_pointer_eq(get_test_entry(map, "notInMap", ignore_case), NULL);
137
- check_int(map->tablesize, ==, 64);
138
- check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val));
134
+ cl_assert_equal_p(get_test_entry(map, "notInMap", ignore_case), NULL);
135
+ cl_assert_equal_i(map->tablesize, 64);
136
+ cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val));
137
}
138
139
static void t_add(struct hashmap *map, unsigned int ignore_case)
@@ -165,39 +163,19 @@ static void t_add(struct hashmap *map, unsigned int ignore_case)
163
164
hashmap_for_each_entry_from(map, entry, ent)
165
{
168
- int ret;
169
- if (!check_int((ret = key_val_contains(
170
- key_val, seen,
171
- ARRAY_SIZE(key_val), entry)),
172
- ==, 0)) {
173
- switch (ret) {
174
- case 1:
175
- test_msg("found entry was not given in the input\n"
176
- " key: %s\n value: %s",
177
- entry->key, get_value(entry));
178
- break;
179
- case 2:
180
- test_msg("duplicate entry detected\n"
181
- " key: %s\n value: %s",
182
- entry->key, get_value(entry));
183
- break;
184
- }
185
- } else {
186
- count++;
187
- }
166
+ int ret = key_val_contains(key_val, seen,
167
+ ARRAY_SIZE(key_val), entry);
168
+ cl_assert_equal_i(ret, 0);
169
+ count++;
170
}
189
- check_int(count, ==, 2);
171
+ cl_assert_equal_i(count, 2);
172
}
173
192
- for (size_t i = 0; i < ARRAY_SIZE(seen); i++) {
193
- if (!check_int(seen[i], ==, 1))
194
- test_msg("following key-val pair was not iterated over:\n"
195
- " key: %s\n value: %s",
196
- key_val[i][0], key_val[i][1]);
197
- }
174
+ for (size_t i = 0; i < ARRAY_SIZE(seen); i++)
175
+ cl_assert_equal_i(seen[i], 1);
176
199
- check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val));
200
- check_pointer_eq(get_test_entry(map, "notInMap", ignore_case), NULL);
177
+ cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val));
178
+ cl_assert_equal_p(get_test_entry(map, "notInMap", ignore_case), NULL);
179
}
180
181
static void t_remove(struct hashmap *map, unsigned int ignore_case)
@@ -211,24 +189,25 @@ static void t_remove(struct hashmap *map, unsigned int ignore_case)
189
190
for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
191
entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case);
214
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
192
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
193
}
194
195
for (size_t i = 0; i < ARRAY_SIZE(remove); i++) {
196
entry = alloc_test_entry(remove[i][0], "", ignore_case);
197
removed = hashmap_remove_entry(map, entry, ent, remove[i][0]);
220
- if (check(removed != NULL))
221
- check_str(get_value(removed), remove[i][1]);
198
+ cl_assert(removed != NULL);
199
+ cl_assert_equal_s(get_value(removed), remove[i][1]);
200
free(entry);
201
free(removed);
202
}
203
204
entry = alloc_test_entry("notInMap", "", ignore_case);
227
- check_pointer_eq(hashmap_remove_entry(map, entry, ent, "notInMap"), NULL);
205
+ cl_assert_equal_p(hashmap_remove_entry(map, entry, ent, "notInMap"), NULL);
206
free(entry);
207
230
- check_int(map->tablesize, ==, 64);
231
- check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val) - ARRAY_SIZE(remove));
208
+ cl_assert_equal_i(map->tablesize, 64);
209
+ cl_assert_equal_i(hashmap_get_size(map),
210
+ ARRAY_SIZE(key_val) - ARRAY_SIZE(remove));
211
}
212
213
static void t_iterate(struct hashmap *map, unsigned int ignore_case)
@@ -242,38 +221,21 @@ static void t_iterate(struct hashmap *map, unsigned int ignore_case)
221
222
for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
223
entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case);
245
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
224
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
225
}
226
227
hashmap_for_each_entry(map, &iter, entry, ent /* member name */)
228
{
250
- int ret;
251
- if (!check_int((ret = key_val_contains(key_val, seen,
252
- ARRAY_SIZE(key_val),
253
- entry)), ==, 0)) {
254
- switch (ret) {
255
- case 1:
256
- test_msg("found entry was not given in the input\n"
257
- " key: %s\n value: %s",
258
- entry->key, get_value(entry));
259
- break;
260
- case 2:
261
- test_msg("duplicate entry detected\n"
262
- " key: %s\n value: %s",
263
- entry->key, get_value(entry));
264
- break;
265
- }
266
- }
229
+ int ret = key_val_contains(key_val, seen,
230
+ ARRAY_SIZE(key_val),
231
+ entry);
232
+ cl_assert(ret == 0);
233
}
234
269
- for (size_t i = 0; i < ARRAY_SIZE(seen); i++) {
270
- if (!check_int(seen[i], ==, 1))
271
- test_msg("following key-val pair was not iterated over:\n"
272
- " key: %s\n value: %s",
273
- key_val[i][0], key_val[i][1]);
274
- }
235
+ for (size_t i = 0; i < ARRAY_SIZE(seen); i++)
236
+ cl_assert_equal_i(seen[i], 1);
237
276
- check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val));
238
+ cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val));
239
}
240
241
static void t_alloc(struct hashmap *map, unsigned int ignore_case)
@@ -284,17 +246,17 @@ static void t_alloc(struct hashmap *map, unsigned int ignore_case)
246
char *key = xstrfmt("key%d", i);
247
char *value = xstrfmt("value%d", i);
248
entry = alloc_test_entry(key, value, ignore_case);
287
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
249
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
250
free(key);
251
free(value);
252
}
291
- check_int(map->tablesize, ==, 64);
292
- check_int(hashmap_get_size(map), ==, 51);
253
+ cl_assert_equal_i(map->tablesize, 64);
254
+ cl_assert_equal_i(hashmap_get_size(map), 51);
255
256
entry = alloc_test_entry("key52", "value52", ignore_case);
295
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
296
- check_int(map->tablesize, ==, 256);
297
- check_int(hashmap_get_size(map), ==, 52);
257
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
258
+ cl_assert_equal_i(map->tablesize, 256);
259
+ cl_assert_equal_i(hashmap_get_size(map), 52);
260
261
for (int i = 1; i <= 12; i++) {
262
char *key = xstrfmt("key%d", i);
@@ -302,27 +264,27 @@ static void t_alloc(struct hashmap *map, unsigned int ignore_case)
264
265
entry = alloc_test_entry(key, "", ignore_case);
266
removed = hashmap_remove_entry(map, entry, ent, key);
305
- if (check(removed != NULL))
306
- check_str(value, get_value(removed));
267
+ cl_assert(removed != NULL);
268
+ cl_assert_equal_s(value, get_value(removed));
269
free(key);
270
free(value);
271
free(entry);
272
free(removed);
273
}
312
- check_int(map->tablesize, ==, 256);
313
- check_int(hashmap_get_size(map), ==, 40);
274
+ cl_assert_equal_i(map->tablesize, 256);
275
+ cl_assert_equal_i(hashmap_get_size(map), 40);
276
277
entry = alloc_test_entry("key40", "", ignore_case);
278
removed = hashmap_remove_entry(map, entry, ent, "key40");
317
- if (check(removed != NULL))
318
- check_str("value40", get_value(removed));
319
- check_int(map->tablesize, ==, 64);
320
- check_int(hashmap_get_size(map), ==, 39);
279
+ cl_assert(removed != NULL);
280
+ cl_assert_equal_s("value40", get_value(removed));
281
+ cl_assert_equal_i(map->tablesize, 64);
282
+ cl_assert_equal_i(hashmap_get_size(map), 39);
283
free(entry);
284
free(removed);
285
}
286
325
-static void t_intern(void)
287
+void test_hashmap__intern(void)
288
{
289
const char *values[] = { "value1", "Value1", "value2", "value2" };
290
@@ -330,32 +292,68 @@ static void t_intern(void)
292
const char *i1 = strintern(values[i]);
293
const char *i2 = strintern(values[i]);
294
333
- if (!check(!strcmp(i1, values[i])))
334
- test_msg("strintern(%s) returns %s\n", values[i], i1);
335
- else if (!check(i1 != values[i]))
336
- test_msg("strintern(%s) returns input pointer\n",
337
- values[i]);
338
- else if (!check_pointer_eq(i1, i2))
339
- test_msg("address('%s') != address('%s'), so strintern('%s') != strintern('%s')",
340
- i1, i2, values[i], values[i]);
341
- else
342
- check_str(i1, values[i]);
295
+ cl_assert_equal_s(i1, values[i]);
296
+ cl_assert(i1 != values[i]);
297
+ cl_assert_equal_p(i1, i2);
298
}
299
}
300
346
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
301
+void test_hashmap__replace_case_sensitive(void)
302
+{
303
+ setup(t_replace, 0);
304
+}
305
+
306
+void test_hashmap__replace_case_insensitive(void)
307
+{
308
+ setup(t_replace, 1);
309
+}
310
+
311
+void test_hashmap__get_case_sensitive(void)
312
+{
313
+ setup(t_get, 0);
314
+}
315
+
316
+void test_hashmap__get_case_insensitive(void)
317
+{
318
+ setup(t_get, 1);
319
+}
320
+
321
+void test_hashmap__add_case_sensitive(void)
322
+{
323
+ setup(t_add, 0);
324
+}
325
+
326
+void test_hashmap__add_case_insensitive(void)
327
+{
328
+ setup(t_add, 1);
329
+}
330
+
331
+void test_hashmap__remove_case_sensitive(void)
332
+{
333
+ setup(t_remove, 0);
334
+}
335
+
336
+void test_hashmap__remove_case_insensitive(void)
337
+{
338
+ setup(t_remove, 1);
339
+}
340
+
341
+void test_hashmap__iterate_case_sensitive(void)
342
+{
343
+ setup(t_iterate, 0);
344
+}
345
+
346
+void test_hashmap__iterate_case_insensitive(void)
347
+{
348
+ setup(t_iterate, 1);
349
+}
350
+
351
+void test_hashmap__alloc_case_sensitive(void)
352
+{
353
+ setup(t_alloc, 0);
354
+}
355
+
356
+void test_hashmap__alloc_case_insensitive(void)
357
{
348
- TEST(setup(t_replace, 0), "replace works");
349
- TEST(setup(t_replace, 1), "replace (case insensitive) works");
350
- TEST(setup(t_get, 0), "get works");
351
- TEST(setup(t_get, 1), "get (case insensitive) works");
352
- TEST(setup(t_add, 0), "add works");
353
- TEST(setup(t_add, 1), "add (case insensitive) works");
354
- TEST(setup(t_remove, 0), "remove works");
355
- TEST(setup(t_remove, 1), "remove (case insensitive) works");
356
- TEST(setup(t_iterate, 0), "iterate works");
357
- TEST(setup(t_iterate, 1), "iterate (case insensitive) works");
358
- TEST(setup(t_alloc, 0), "grow / shrink works");
359
- TEST(t_intern(), "string interning works");
360
- return test_done();
358
+ setup(t_alloc, 1);
359
}