Skip to content

Commit 69bc044

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

File tree

4 files changed

+138
-183
lines changed

4 files changed

+138
-183
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,7 @@ CLAR_TEST_SUITES += u-hash
13571357
CLAR_TEST_SUITES += u-hashmap
13581358
CLAR_TEST_SUITES += u-mem-pool
13591359
CLAR_TEST_SUITES += u-oid-array
1360+
CLAR_TEST_SUITES += u-oidmap
13601361
CLAR_TEST_SUITES += u-prio-queue
13611362
CLAR_TEST_SUITES += u-reftable-tree
13621363
CLAR_TEST_SUITES += u-strbuf
@@ -1368,7 +1369,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
13681369
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
13691370
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
13701371

1371-
UNIT_TEST_PROGRAMS += t-oidmap
13721372
UNIT_TEST_PROGRAMS += t-oidtree
13731373
UNIT_TEST_PROGRAMS += t-reftable-basics
13741374
UNIT_TEST_PROGRAMS += t-reftable-block

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ clar_test_suites = [
55
'unit-tests/u-hashmap.c',
66
'unit-tests/u-mem-pool.c',
77
'unit-tests/u-oid-array.c',
8+
'unit-tests/u-oidmap.c',
89
'unit-tests/u-prio-queue.c',
910
'unit-tests/u-reftable-tree.c',
1011
'unit-tests/u-strbuf.c',
@@ -50,7 +51,6 @@ clar_unit_tests = executable('unit-tests',
5051
test('unit-tests', clar_unit_tests)
5152

5253
unit_test_programs = [
53-
'unit-tests/t-oidmap.c',
5454
'unit-tests/t-oidtree.c',
5555
'unit-tests/t-reftable-basics.c',
5656
'unit-tests/t-reftable-block.c',

t/unit-tests/t-oidmap.c

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

t/unit-tests/u-oidmap.c

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

0 commit comments

Comments
 (0)