Skip to content

Commit 6815d11

Browse files
stefanbellergitster
authored andcommitted
t/helper/test-hashmap: use custom data instead of duplicate cmp functions
With the new field that is passed to the compare function, we can pass through flags there instead of having multiple compare functions. Also drop the cast to hashmap_cmp_fn. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56a14ea commit 6815d11

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

t/helper/test-hashmap.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ static const char *get_value(const struct test_entry *e)
1313
return e->key + strlen(e->key) + 1;
1414
}
1515

16-
static int test_entry_cmp(const void *unused_cmp_data,
17-
const struct test_entry *e1,
18-
const struct test_entry *e2,
19-
const char* key)
16+
static int test_entry_cmp(const void *cmp_data,
17+
const void *entry,
18+
const void *entry_or_key,
19+
const void *keydata)
2020
{
21-
return strcmp(e1->key, key ? key : e2->key);
22-
}
23-
24-
static int test_entry_cmp_icase(const void *unused_cmp_data,
25-
const struct test_entry *e1,
26-
const struct test_entry *e2,
27-
const char* key)
28-
{
29-
return strcasecmp(e1->key, key ? key : e2->key);
21+
const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
22+
const struct test_entry *e1 = entry;
23+
const struct test_entry *e2 = entry_or_key;
24+
const char *key = keydata;
25+
26+
if (ignore_case)
27+
return strcasecmp(e1->key, key ? key : e2->key);
28+
else
29+
return strcmp(e1->key, key ? key : e2->key);
3030
}
3131

3232
static struct test_entry *alloc_test_entry(int hash, char *key, int klen,
@@ -96,8 +96,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
9696
if (method & TEST_ADD) {
9797
/* test adding to the map */
9898
for (j = 0; j < rounds; j++) {
99-
hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp,
100-
NULL, 0);
99+
hashmap_init(&map, test_entry_cmp, NULL, 0);
101100

102101
/* add entries */
103102
for (i = 0; i < TEST_SIZE; i++) {
@@ -109,7 +108,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
109108
}
110109
} else {
111110
/* test map lookups */
112-
hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0);
111+
hashmap_init(&map, test_entry_cmp, NULL, 0);
113112

114113
/* fill the map (sparsely if specified) */
115114
j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
@@ -151,8 +150,7 @@ int cmd_main(int argc, const char **argv)
151150

152151
/* init hash map */
153152
icase = argc > 1 && !strcmp("ignorecase", argv[1]);
154-
hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase
155-
: test_entry_cmp), NULL, 0);
153+
hashmap_init(&map, test_entry_cmp, &icase, 0);
156154

157155
/* process commands from stdin */
158156
while (fgets(line, sizeof(line), stdin)) {

0 commit comments

Comments
 (0)