]> git.ipfire.org Git - thirdparty/git.git/blobdiff - t/helper/test-hashmap.c
Merge branch 'en/merge-recursive-directory-rename-fixes'
[thirdparty/git.git] / t / helper / test-hashmap.c
index de2bd083b9b1f630450bc9ba9a2c308f9a3cc1aa..f38706216f44c3c881f1d52b250fe74d5140bb37 100644 (file)
@@ -5,6 +5,7 @@
 
 struct test_entry
 {
+       int padding; /* hashmap entry no longer needs to be the first member */
        struct hashmap_entry ent;
        /* key and value as two \0-terminated strings */
        char key[FLEX_ARRAY];
@@ -16,15 +17,17 @@ static const char *get_value(const struct test_entry *e)
 }
 
 static int test_entry_cmp(const void *cmp_data,
-                         const void *entry,
-                         const void *entry_or_key,
+                         const struct hashmap_entry *eptr,
+                         const struct hashmap_entry *entry_or_key,
                          const void *keydata)
 {
        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 struct test_entry *e1, *e2;
        const char *key = keydata;
 
+       e1 = container_of(eptr, const struct test_entry, ent);
+       e2 = container_of(entry_or_key, const struct test_entry, ent);
+
        if (ignore_case)
                return strcasecmp(e1->key, key ? key : e2->key);
        else
@@ -107,7 +110,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
                                hashmap_add(&map, &entries[i]->ent);
                        }
 
-                       hashmap_free(&map, 0);
+                       hashmap_free(&map);
                }
        } else {
                /* test map lookups */
@@ -127,7 +130,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
                        }
                }
 
-               hashmap_free(&map, 0);
+               hashmap_free(&map);
        }
 }
 
@@ -187,43 +190,44 @@ int cmd__hashmap(int argc, const char **argv)
                        entry = alloc_test_entry(hash, p1, p2);
 
                        /* add / replace entry */
-                       entry = hashmap_put(&map, &entry->ent);
+                       entry = hashmap_put_entry(&map, entry, ent);
 
                        /* print and free replaced entry, if any */
                        puts(entry ? get_value(entry) : "NULL");
                        free(entry);
 
                } else if (!strcmp("get", cmd) && p1) {
-
                        /* lookup entry in hashmap */
-                       entry = hashmap_get_from_hash(&map, hash, p1);
+                       entry = hashmap_get_entry_from_hash(&map, hash, p1,
+                                                       struct test_entry, ent);
 
                        /* print result */
                        if (!entry)
                                puts("NULL");
-                       while (entry) {
+                       hashmap_for_each_entry_from(&map, entry, ent)
                                puts(get_value(entry));
-                               entry = hashmap_get_next(&map, &entry->ent);
-                       }
 
                } else if (!strcmp("remove", cmd) && p1) {
 
                        /* setup static key */
                        struct hashmap_entry key;
+                       struct hashmap_entry *rm;
                        hashmap_entry_init(&key, hash);
 
                        /* remove entry from hashmap */
-                       entry = hashmap_remove(&map, &key, p1);
+                       rm = hashmap_remove(&map, &key, p1);
+                       entry = rm ? container_of(rm, struct test_entry, ent)
+                                       : NULL;
 
                        /* print result and free entry*/
                        puts(entry ? get_value(entry) : "NULL");
                        free(entry);
 
                } else if (!strcmp("iterate", cmd)) {
-
                        struct hashmap_iter iter;
-                       hashmap_iter_init(&map, &iter);
-                       while ((entry = hashmap_iter_next(&iter)))
+
+                       hashmap_for_each_entry(&map, &iter, entry,
+                                               ent /* member name */)
                                printf("%s %s\n", entry->key, get_value(entry));
 
                } else if (!strcmp("size", cmd)) {
@@ -258,6 +262,6 @@ int cmd__hashmap(int argc, const char **argv)
        }
 
        strbuf_release(&line);
-       hashmap_free(&map, 1);
+       hashmap_free_entries(&map, struct test_entry, ent);
        return 0;
 }