Skip to content

Commit 04595eb

Browse files
committed
Merge branch 'gt/unit-test-oid-array'
Another unit-test. * gt/unit-test-oid-array: t: port helper/test-oid-array.c to unit-tests/t-oid-array.c
2 parents 63b5fcd + a680635 commit 04595eb

File tree

8 files changed

+136
-175
lines changed

8 files changed

+136
-175
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,6 @@ TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
812812
TEST_BUILTINS_OBJS += test-match-trees.o
813813
TEST_BUILTINS_OBJS += test-mergesort.o
814814
TEST_BUILTINS_OBJS += test-mktemp.o
815-
TEST_BUILTINS_OBJS += test-oid-array.o
816815
TEST_BUILTINS_OBJS += test-online-cpus.o
817816
TEST_BUILTINS_OBJS += test-pack-mtimes.o
818817
TEST_BUILTINS_OBJS += test-parse-options.o
@@ -1340,6 +1339,7 @@ UNIT_TEST_PROGRAMS += t-example-decorate
13401339
UNIT_TEST_PROGRAMS += t-hash
13411340
UNIT_TEST_PROGRAMS += t-hashmap
13421341
UNIT_TEST_PROGRAMS += t-mem-pool
1342+
UNIT_TEST_PROGRAMS += t-oid-array
13431343
UNIT_TEST_PROGRAMS += t-oidmap
13441344
UNIT_TEST_PROGRAMS += t-oidtree
13451345
UNIT_TEST_PROGRAMS += t-prio-queue

t/helper/test-oid-array.c

-49
This file was deleted.

t/helper/test-tool.c

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ static struct test_cmd cmds[] = {
4343
{ "match-trees", cmd__match_trees },
4444
{ "mergesort", cmd__mergesort },
4545
{ "mktemp", cmd__mktemp },
46-
{ "oid-array", cmd__oid_array },
4746
{ "online-cpus", cmd__online_cpus },
4847
{ "pack-mtimes", cmd__pack_mtimes },
4948
{ "parse-options", cmd__parse_options },

t/helper/test-tool.h

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ int cmd__scrap_cache_tree(int argc, const char **argv);
6464
int cmd__serve_v2(int argc, const char **argv);
6565
int cmd__sha1(int argc, const char **argv);
6666
int cmd__sha1_is_sha1dc(int argc, const char **argv);
67-
int cmd__oid_array(int argc, const char **argv);
6867
int cmd__sha256(int argc, const char **argv);
6968
int cmd__sigchain(int argc, const char **argv);
7069
int cmd__simple_ipc(int argc, const char **argv);

t/t0064-oid-array.sh

-122
This file was deleted.

t/unit-tests/lib-oid.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "strbuf.h"
44
#include "hex.h"
55

6-
static int init_hash_algo(void)
6+
int init_hash_algo(void)
77
{
88
static int algo = -1;
99

t/unit-tests/lib-oid.h

+8
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,13 @@
1313
* environment variable.
1414
*/
1515
int get_oid_arbitrary_hex(const char *s, struct object_id *oid);
16+
/*
17+
* Returns one of GIT_HASH_{SHA1, SHA256, UNKNOWN} based on the value of
18+
* GIT_TEST_DEFAULT_HASH environment variable. The fallback value in the
19+
* absence of GIT_TEST_DEFAULT_HASH is GIT_HASH_SHA1. It also uses
20+
* check(algo != GIT_HASH_UNKNOWN) before returning to verify if the
21+
* GIT_TEST_DEFAULT_HASH's value is valid or not.
22+
*/
23+
int init_hash_algo(void);
1624

1725
#endif /* LIB_OID_H */

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

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
3+
#include "test-lib.h"
4+
#include "lib-oid.h"
5+
#include "oid-array.h"
6+
#include "hex.h"
7+
8+
static int 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+
if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0))
14+
return -1;
15+
oid_array_append(array, &oid);
16+
}
17+
if (!check_uint(array->nr, ==, n))
18+
return -1;
19+
return 0;
20+
}
21+
22+
static int add_to_oid_array(const struct object_id *oid, void *data)
23+
{
24+
struct oid_array *array = data;
25+
26+
oid_array_append(array, oid);
27+
return 0;
28+
}
29+
30+
static void t_enumeration(const char **input_args, size_t input_sz,
31+
const char **expect_args, size_t expect_sz)
32+
{
33+
struct oid_array input = OID_ARRAY_INIT, expect = OID_ARRAY_INIT,
34+
actual = OID_ARRAY_INIT;
35+
size_t i;
36+
37+
if (fill_array(&input, input_args, input_sz))
38+
return;
39+
if (fill_array(&expect, expect_args, expect_sz))
40+
return;
41+
42+
oid_array_for_each_unique(&input, add_to_oid_array, &actual);
43+
if (!check_uint(actual.nr, ==, expect.nr))
44+
return;
45+
46+
for (i = 0; i < actual.nr; i++) {
47+
if (!check(oideq(&actual.oid[i], &expect.oid[i])))
48+
test_msg("expected: %s\n got: %s\n index: %" PRIuMAX,
49+
oid_to_hex(&expect.oid[i]), oid_to_hex(&actual.oid[i]),
50+
(uintmax_t)i);
51+
}
52+
53+
oid_array_clear(&actual);
54+
oid_array_clear(&input);
55+
oid_array_clear(&expect);
56+
}
57+
58+
#define TEST_ENUMERATION(input, expect, desc) \
59+
TEST(t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect)), \
60+
desc " works")
61+
62+
static void t_lookup(const char **input_hexes, size_t n, const char *query_hex,
63+
int lower_bound, int upper_bound)
64+
{
65+
struct oid_array array = OID_ARRAY_INIT;
66+
struct object_id oid_query;
67+
int ret;
68+
69+
if (!check_int(get_oid_arbitrary_hex(query_hex, &oid_query), ==, 0))
70+
return;
71+
if (fill_array(&array, input_hexes, n))
72+
return;
73+
ret = oid_array_lookup(&array, &oid_query);
74+
75+
if (!check_int(ret, <=, upper_bound) ||
76+
!check_int(ret, >=, lower_bound))
77+
test_msg("oid query for lookup: %s", oid_to_hex(&oid_query));
78+
79+
oid_array_clear(&array);
80+
}
81+
82+
#define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound, desc) \
83+
TEST(t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \
84+
lower_bound, upper_bound), \
85+
desc " works")
86+
87+
static void setup(void)
88+
{
89+
/* The hash algo is used by oid_array_lookup() internally */
90+
int algo = init_hash_algo();
91+
if (check_int(algo, !=, GIT_HASH_UNKNOWN))
92+
repo_set_hash_algo(the_repository, algo);
93+
}
94+
95+
int cmd_main(int argc UNUSED, const char **argv UNUSED)
96+
{
97+
const char *arr_input[] = { "88", "44", "aa", "55" };
98+
const char *arr_input_dup[] = { "88", "44", "aa", "55",
99+
"88", "44", "aa", "55",
100+
"88", "44", "aa", "55" };
101+
const char *res_sorted[] = { "44", "55", "88", "aa" };
102+
const char *nearly_55;
103+
104+
if (!TEST(setup(), "setup"))
105+
test_skip_all("hash algo initialization failed");
106+
107+
TEST_ENUMERATION(arr_input, res_sorted, "ordered enumeration");
108+
TEST_ENUMERATION(arr_input_dup, res_sorted,
109+
"ordered enumeration with duplicate suppression");
110+
111+
TEST_LOOKUP(arr_input, "55", 1, 1, "lookup");
112+
TEST_LOOKUP(arr_input, "33", INT_MIN, -1, "lookup non-existent entry");
113+
TEST_LOOKUP(arr_input_dup, "55", 3, 5, "lookup with duplicates");
114+
TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1,
115+
"lookup non-existent entry with duplicates");
116+
117+
nearly_55 = init_hash_algo() == GIT_HASH_SHA1 ?
118+
"5500000000000000000000000000000000000001" :
119+
"5500000000000000000000000000000000000000000000000000000000000001";
120+
TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0,
121+
"lookup with almost duplicate values");
122+
TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1,
123+
"lookup with single duplicate value");
124+
125+
return test_done();
126+
}

0 commit comments

Comments
 (0)