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 <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Jun 30, 2017 at 17:28 UTC
6815d1143150f422fe2ad1a7ddcc6205bef006ae
1 file changed
+16
-18
t/helper/test-hashmap.c
+16
-18
@@ -13,20 +13,20 @@ static const char *get_value(const struct test_entry *e)
13
return e->key + strlen(e->key) + 1;
14
}
15
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)
20
{
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);
30
}
31
32
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)
96
if (method & TEST_ADD) {
97
/* test adding to the map */
98
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);
100
101
/* add entries */
102
for (i = 0; i < TEST_SIZE; i++) {
@@ -109,7 +108,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
108
}
109
} else {
110
/* test map lookups */
112
- hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, NULL, 0);
111
+ hashmap_init(&map, test_entry_cmp, NULL, 0);
112
113
/* fill the map (sparsely if specified) */
114
j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
@@ -151,8 +150,7 @@ int cmd_main(int argc, const char **argv)
150
151
/* init hash map */
152
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);
154
155
/* process commands from stdin */
156
while (fgets(line, sizeof(line), stdin)) {