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>
maint
Stefan Beller 2017-06-30 17:28:38 -07:00 committed by Junio C Hamano
parent 56a14ea7ac
commit 6815d11431
1 changed files with 15 additions and 17 deletions

View File

@ -13,20 +13,20 @@ static const char *get_value(const struct test_entry *e)
return e->key + strlen(e->key) + 1; return e->key + strlen(e->key) + 1;
} }


static int test_entry_cmp(const void *unused_cmp_data, static int test_entry_cmp(const void *cmp_data,
const struct test_entry *e1, const void *entry,
const struct test_entry *e2, const void *entry_or_key,
const char* key) const void *keydata)
{ {
return strcmp(e1->key, key ? key : e2->key); const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
} const struct test_entry *e1 = entry;
const struct test_entry *e2 = entry_or_key;
const char *key = keydata;


static int test_entry_cmp_icase(const void *unused_cmp_data, if (ignore_case)
const struct test_entry *e1, return strcasecmp(e1->key, key ? key : e2->key);
const struct test_entry *e2, else
const char* key) return strcmp(e1->key, key ? key : e2->key);
{
return strcasecmp(e1->key, key ? key : e2->key);
} }


static struct test_entry *alloc_test_entry(int hash, char *key, int klen, 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)
if (method & TEST_ADD) { if (method & TEST_ADD) {
/* test adding to the map */ /* test adding to the map */
for (j = 0; j < rounds; j++) { for (j = 0; j < rounds; j++) {
hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp, hashmap_init(&map, test_entry_cmp, NULL, 0);
NULL, 0);


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


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


/* init hash map */ /* init hash map */
icase = argc > 1 && !strcmp("ignorecase", argv[1]); icase = argc > 1 && !strcmp("ignorecase", argv[1]);
hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase hashmap_init(&map, test_entry_cmp, &icase, 0);
: test_entry_cmp), NULL, 0);


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