git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 10/10] t/helper/test-hashmap: use custom data instead of duplicate cmp functions

From
Stefan Beller <sbeller@google.com>
Date
Jul 1, 2017, 00:28 UTC
Message-ID
<20170701002838.22785-11-sbeller@google.com>
In-Reply-To
<20170701002838.22785-1-sbeller@google.com>

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>
---
 t/helper/test-hashmap.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c
index 095d7395f3..93ccfbb75f 100644
--- a/t/helper/test-hashmap.c
+++ b/t/helper/test-hashmap.c
@@ -13,20 +13,20 @@ static const char *get_value(const struct test_entry *e)
 	return e->key + strlen(e->key) + 1;
 }
 
-static int test_entry_cmp(const void *unused_cmp_data,
-			  const struct test_entry *e1,
-			  const struct test_entry *e2,
-			  const char* key)
+static int test_entry_cmp(const void *cmp_data,
+			  const void *entry,
+			  const void *entry_or_key,
+			  const void* keydata)
 {
-	return strcmp(e1->key, key ? key : e2->key);
-}
-
-static int test_entry_cmp_icase(const void *unused_cmp_data,
-				const struct test_entry *e1,
-				const struct test_entry *e2,
-				const char* key)
-{
-	return strcasecmp(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;
+
+	if (ignore_case)
+		return strcasecmp(e1->key, key ? key : e2->key);
+	else
+		return strcmp(e1->key, key ? key : e2->key);
 }
 
 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) {
 		/* test adding to the map */
 		for (j = 0; j < rounds; j++) {
-			hashmap_init(&map, (hashmap_cmp_fn) test_entry_cmp,
-				     NULL, 0);
+			hashmap_init(&map, test_entry_cmp, NULL, 0);
 
 			/* add entries */
 			for (i = 0; i < TEST_SIZE; i++) {
@@ -109,7 +108,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
 		}
 	} else {
 		/* 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) */
 		j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
@@ -151,8 +150,7 @@ int cmd_main(int argc, const char **argv)
 
 	/* init hash map */
 	icase = argc > 1 && !strcmp("ignorecase", argv[1]);
-	hashmap_init(&map, (hashmap_cmp_fn) (icase ? test_entry_cmp_icase
-			: test_entry_cmp), NULL, 0);
+	hashmap_init(&map, test_entry_cmp, &icase, 0);
 
 	/* process commands from stdin */
 	while (fgets(line, sizeof(line), stdin)) {
-- 
2.13.0.31.g9b732c453e
Previous: Stefan BellerNext: Stefan Beller
Message 7 of 12 in “Friday night special: hash map cleanup”
  1. 00/10 Friday night special: hash map cleanupStefan Beller, Jul 1, 2017
  2. 01/10 attr.c: drop hashmap_cmp_fn castStefan Beller, Jul 1, 2017
  3. 02/10 builtin/difftool.c: drop hashmap_cmp_fn castStefan Beller, Jul 1, 2017
  4. 03/10 builtin/describe: drop hashmap_cmp_fn castStefan Beller, Jul 1, 2017
  5. 04/10 config.c: drop hashmap_cmp_fn castStefan Beller, Jul 1, 2017
  6. 09/10 name-hash.c: drop hashmap_cmp_fn castStefan Beller, Jul 1, 2017
  7. 10/10 t/helper/test-hashmap: use custom data instead of duplicate cmp functionsStefan Beller, Jul 1, 2017
  8. 08/10 submodule-config.c: drop hashmap_cmp_fn castStefan Beller, Jul 1, 2017
  9. 06/10 patch-ids.c: drop hashmap_cmp_fn castStefan Beller, Jul 1, 2017
  10. 05/10 convert/sub-process: drop cast to hashmap_cmp_fnStefan Beller, Jul 1, 2017
  11. 07/10 remote.c: drop hashmap_cmp_fn castStefan Beller, Jul 1, 2017
  12. Junio C HamanoJul 5, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.