Skip to content

Commit 869a1ed

Browse files
Seyi007gitster
authored andcommitted
t/unit-tests: convert oid-array test to use clar test framework
Adapt oid-array test script to clar framework by using clar assertions where necessary. Remove descriptions from macros to reduce redundancy, and move test input arrays to global scope for reuse across multiple test functions. Introduce `test_oid_array__initialize()` to explicitly initialize the hash algorithm. These changes streamline the test suite, making individual tests self-contained and reducing redundant code. Mentored-by: Patrick Steinhardt <[email protected]> Signed-off-by: Seyi Kuforiji <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a16a2ee commit 869a1ed

File tree

4 files changed

+131
-128
lines changed

4 files changed

+131
-128
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ CLAR_TEST_SUITES += u-example-decorate
13561356
CLAR_TEST_SUITES += u-hash
13571357
CLAR_TEST_SUITES += u-hashmap
13581358
CLAR_TEST_SUITES += u-mem-pool
1359+
CLAR_TEST_SUITES += u-oid-array
13591360
CLAR_TEST_SUITES += u-prio-queue
13601361
CLAR_TEST_SUITES += u-reftable-tree
13611362
CLAR_TEST_SUITES += u-strbuf
@@ -1367,7 +1368,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
13671368
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
13681369
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
13691370

1370-
UNIT_TEST_PROGRAMS += t-oid-array
13711371
UNIT_TEST_PROGRAMS += t-oidmap
13721372
UNIT_TEST_PROGRAMS += t-oidtree
13731373
UNIT_TEST_PROGRAMS += t-reftable-basics

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ clar_test_suites = [
44
'unit-tests/u-hash.c',
55
'unit-tests/u-hashmap.c',
66
'unit-tests/u-mem-pool.c',
7+
'unit-tests/u-oid-array.c',
78
'unit-tests/u-prio-queue.c',
89
'unit-tests/u-reftable-tree.c',
910
'unit-tests/u-strbuf.c',
@@ -49,7 +50,6 @@ clar_unit_tests = executable('unit-tests',
4950
test('unit-tests', clar_unit_tests)
5051

5152
unit_test_programs = [
52-
'unit-tests/t-oid-array.c',
5353
'unit-tests/t-oidmap.c',
5454
'unit-tests/t-oidtree.c',
5555
'unit-tests/t-reftable-basics.c',

t/unit-tests/t-oid-array.c

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

t/unit-tests/u-oid-array.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
3+
#include "unit-test.h"
4+
#include "lib-oid.h"
5+
#include "oid-array.h"
6+
#include "hex.h"
7+
8+
static void fill_array(struct oid_array *array, const char *hexes[], size_t n)
9+
{
10+
for (size_t i = 0; i < n; i++) {
11+
struct object_id oid;
12+
13+
cl_parse_any_oid(hexes[i], &oid);
14+
oid_array_append(array, &oid);
15+
}
16+
cl_assert_equal_i(array->nr, n);
17+
}
18+
19+
static int add_to_oid_array(const struct object_id *oid, void *data)
20+
{
21+
struct oid_array *array = data;
22+
23+
oid_array_append(array, oid);
24+
return 0;
25+
}
26+
27+
static void t_enumeration(const char **input_args, size_t input_sz,
28+
const char **expect_args, size_t expect_sz)
29+
{
30+
struct oid_array input = OID_ARRAY_INIT, expect = OID_ARRAY_INIT,
31+
actual = OID_ARRAY_INIT;
32+
size_t i;
33+
34+
fill_array(&input, input_args, input_sz);
35+
fill_array(&expect, expect_args, expect_sz);
36+
37+
oid_array_for_each_unique(&input, add_to_oid_array, &actual);
38+
cl_assert_equal_i(actual.nr, expect.nr);
39+
40+
for (i = 0; i < actual.nr; i++)
41+
cl_assert(oideq(&actual.oid[i], &expect.oid[i]));
42+
43+
oid_array_clear(&actual);
44+
oid_array_clear(&input);
45+
oid_array_clear(&expect);
46+
}
47+
48+
#define TEST_ENUMERATION(input, expect) \
49+
t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect));
50+
51+
static void t_lookup(const char **input_hexes, size_t n, const char *query_hex,
52+
int lower_bound, int upper_bound)
53+
{
54+
struct oid_array array = OID_ARRAY_INIT;
55+
struct object_id oid_query;
56+
int ret;
57+
58+
cl_parse_any_oid(query_hex, &oid_query);
59+
fill_array(&array, input_hexes, n);
60+
ret = oid_array_lookup(&array, &oid_query);
61+
62+
cl_assert(ret <= upper_bound);
63+
cl_assert(ret >= lower_bound);
64+
65+
oid_array_clear(&array);
66+
}
67+
68+
#define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound) \
69+
t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \
70+
lower_bound, upper_bound);
71+
72+
void test_oid_array__initialize(void)
73+
{
74+
/* The hash algo is used by oid_array_lookup() internally */
75+
int algo = cl_setup_hash_algo();
76+
repo_set_hash_algo(the_repository, algo);
77+
}
78+
79+
static const char *arr_input[] = { "88", "44", "aa", "55" };
80+
static const char *arr_input_dup[] = { "88", "44", "aa", "55",
81+
"88", "44", "aa", "55",
82+
"88", "44", "aa", "55" };
83+
static const char *res_sorted[] = { "44", "55", "88", "aa" };
84+
85+
void test_oid_array__enumerate_unique(void)
86+
{
87+
TEST_ENUMERATION(arr_input, res_sorted);
88+
}
89+
90+
void test_oid_array__enumerate_duplicate(void)
91+
{
92+
TEST_ENUMERATION(arr_input_dup, res_sorted);
93+
}
94+
95+
void test_oid_array__lookup(void)
96+
{
97+
TEST_LOOKUP(arr_input, "55", 1, 1);
98+
}
99+
100+
void test_oid_array__lookup_non_existent(void)
101+
{
102+
TEST_LOOKUP(arr_input, "33", INT_MIN, -1);
103+
}
104+
105+
void test_oid_array__lookup_duplicates(void)
106+
{
107+
TEST_LOOKUP(arr_input_dup, "55", 3, 5);
108+
}
109+
110+
void test_oid_array__lookup_non_existent_dup(void)
111+
{
112+
TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1);
113+
}
114+
115+
void test_oid_array__lookup_almost_dup(void)
116+
{
117+
const char *nearly_55;
118+
119+
nearly_55 = cl_setup_hash_algo() == GIT_HASH_SHA1 ?
120+
"5500000000000000000000000000000000000001" :
121+
"5500000000000000000000000000000000000000000000000000000000000001";
122+
123+
TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0);
124+
}
125+
126+
void test_oid_array__lookup_single_dup(void)
127+
{
128+
TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1);
129+
}

0 commit comments

Comments
 (0)