]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-hashmap.c
Use new HASHMAP_INIT macro to simplify hashmap initialization
[thirdparty/git.git] / t / helper / test-hashmap.c
1 #include "test-tool.h"
2 #include "git-compat-util.h"
3 #include "hashmap.h"
4 #include "strbuf.h"
5
6 struct test_entry
7 {
8 int padding; /* hashmap entry no longer needs to be the first member */
9 struct hashmap_entry ent;
10 /* key and value as two \0-terminated strings */
11 char key[FLEX_ARRAY];
12 };
13
14 static const char *get_value(const struct test_entry *e)
15 {
16 return e->key + strlen(e->key) + 1;
17 }
18
19 static int test_entry_cmp(const void *cmp_data,
20 const struct hashmap_entry *eptr,
21 const struct hashmap_entry *entry_or_key,
22 const void *keydata)
23 {
24 const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
25 const struct test_entry *e1, *e2;
26 const char *key = keydata;
27
28 e1 = container_of(eptr, const struct test_entry, ent);
29 e2 = container_of(entry_or_key, const struct test_entry, ent);
30
31 if (ignore_case)
32 return strcasecmp(e1->key, key ? key : e2->key);
33 else
34 return strcmp(e1->key, key ? key : e2->key);
35 }
36
37 static struct test_entry *alloc_test_entry(unsigned int hash,
38 char *key, char *value)
39 {
40 size_t klen = strlen(key);
41 size_t vlen = strlen(value);
42 struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
43 hashmap_entry_init(&entry->ent, hash);
44 memcpy(entry->key, key, klen + 1);
45 memcpy(entry->key + klen + 1, value, vlen + 1);
46 return entry;
47 }
48
49 #define HASH_METHOD_FNV 0
50 #define HASH_METHOD_I 1
51 #define HASH_METHOD_IDIV10 2
52 #define HASH_METHOD_0 3
53 #define HASH_METHOD_X2 4
54 #define TEST_SPARSE 8
55 #define TEST_ADD 16
56 #define TEST_SIZE 100000
57
58 static unsigned int hash(unsigned int method, unsigned int i, const char *key)
59 {
60 unsigned int hash = 0;
61 switch (method & 3)
62 {
63 case HASH_METHOD_FNV:
64 hash = strhash(key);
65 break;
66 case HASH_METHOD_I:
67 hash = i;
68 break;
69 case HASH_METHOD_IDIV10:
70 hash = i / 10;
71 break;
72 case HASH_METHOD_0:
73 hash = 0;
74 break;
75 }
76
77 if (method & HASH_METHOD_X2)
78 hash = 2 * hash;
79 return hash;
80 }
81
82 /*
83 * Test performance of hashmap.[ch]
84 * Usage: time echo "perfhashmap method rounds" | test-tool hashmap
85 */
86 static void perf_hashmap(unsigned int method, unsigned int rounds)
87 {
88 struct hashmap map;
89 char buf[16];
90 struct test_entry **entries;
91 unsigned int *hashes;
92 unsigned int i, j;
93
94 ALLOC_ARRAY(entries, TEST_SIZE);
95 ALLOC_ARRAY(hashes, TEST_SIZE);
96 for (i = 0; i < TEST_SIZE; i++) {
97 xsnprintf(buf, sizeof(buf), "%i", i);
98 entries[i] = alloc_test_entry(0, buf, "");
99 hashes[i] = hash(method, i, entries[i]->key);
100 }
101
102 if (method & TEST_ADD) {
103 /* test adding to the map */
104 for (j = 0; j < rounds; j++) {
105 hashmap_init(&map, test_entry_cmp, NULL, 0);
106
107 /* add entries */
108 for (i = 0; i < TEST_SIZE; i++) {
109 hashmap_entry_init(&entries[i]->ent, hashes[i]);
110 hashmap_add(&map, &entries[i]->ent);
111 }
112
113 hashmap_clear(&map);
114 }
115 } else {
116 /* test map lookups */
117 hashmap_init(&map, test_entry_cmp, NULL, 0);
118
119 /* fill the map (sparsely if specified) */
120 j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
121 for (i = 0; i < j; i++) {
122 hashmap_entry_init(&entries[i]->ent, hashes[i]);
123 hashmap_add(&map, &entries[i]->ent);
124 }
125
126 for (j = 0; j < rounds; j++) {
127 for (i = 0; i < TEST_SIZE; i++) {
128 hashmap_get_from_hash(&map, hashes[i],
129 entries[i]->key);
130 }
131 }
132
133 hashmap_clear(&map);
134 }
135 }
136
137 #define DELIM " \t\r\n"
138
139 /*
140 * Read stdin line by line and print result of commands to stdout:
141 *
142 * hash key -> strhash(key) memhash(key) strihash(key) memihash(key)
143 * put key value -> NULL / old value
144 * get key -> NULL / value
145 * remove key -> NULL / old value
146 * iterate -> key1 value1\nkey2 value2\n...
147 * size -> tablesize numentries
148 *
149 * perfhashmap method rounds -> test hashmap.[ch] performance
150 */
151 int cmd__hashmap(int argc, const char **argv)
152 {
153 struct strbuf line = STRBUF_INIT;
154 int icase;
155 struct hashmap map = HASHMAP_INIT(test_entry_cmp, &icase);
156
157 /* init hash map */
158 icase = argc > 1 && !strcmp("ignorecase", argv[1]);
159
160 /* process commands from stdin */
161 while (strbuf_getline(&line, stdin) != EOF) {
162 char *cmd, *p1 = NULL, *p2 = NULL;
163 unsigned int hash = 0;
164 struct test_entry *entry;
165
166 /* break line into command and up to two parameters */
167 cmd = strtok(line.buf, DELIM);
168 /* ignore empty lines */
169 if (!cmd || *cmd == '#')
170 continue;
171
172 p1 = strtok(NULL, DELIM);
173 if (p1) {
174 hash = icase ? strihash(p1) : strhash(p1);
175 p2 = strtok(NULL, DELIM);
176 }
177
178 if (!strcmp("add", cmd) && p1 && p2) {
179
180 /* create entry with key = p1, value = p2 */
181 entry = alloc_test_entry(hash, p1, p2);
182
183 /* add to hashmap */
184 hashmap_add(&map, &entry->ent);
185
186 } else if (!strcmp("put", cmd) && p1 && p2) {
187
188 /* create entry with key = p1, value = p2 */
189 entry = alloc_test_entry(hash, p1, p2);
190
191 /* add / replace entry */
192 entry = hashmap_put_entry(&map, entry, ent);
193
194 /* print and free replaced entry, if any */
195 puts(entry ? get_value(entry) : "NULL");
196 free(entry);
197
198 } else if (!strcmp("get", cmd) && p1) {
199 /* lookup entry in hashmap */
200 entry = hashmap_get_entry_from_hash(&map, hash, p1,
201 struct test_entry, ent);
202
203 /* print result */
204 if (!entry)
205 puts("NULL");
206 hashmap_for_each_entry_from(&map, entry, ent)
207 puts(get_value(entry));
208
209 } else if (!strcmp("remove", cmd) && p1) {
210
211 /* setup static key */
212 struct hashmap_entry key;
213 struct hashmap_entry *rm;
214 hashmap_entry_init(&key, hash);
215
216 /* remove entry from hashmap */
217 rm = hashmap_remove(&map, &key, p1);
218 entry = rm ? container_of(rm, struct test_entry, ent)
219 : NULL;
220
221 /* print result and free entry*/
222 puts(entry ? get_value(entry) : "NULL");
223 free(entry);
224
225 } else if (!strcmp("iterate", cmd)) {
226 struct hashmap_iter iter;
227
228 hashmap_for_each_entry(&map, &iter, entry,
229 ent /* member name */)
230 printf("%s %s\n", entry->key, get_value(entry));
231
232 } else if (!strcmp("size", cmd)) {
233
234 /* print table sizes */
235 printf("%u %u\n", map.tablesize,
236 hashmap_get_size(&map));
237
238 } else if (!strcmp("intern", cmd) && p1) {
239
240 /* test that strintern works */
241 const char *i1 = strintern(p1);
242 const char *i2 = strintern(p1);
243 if (strcmp(i1, p1))
244 printf("strintern(%s) returns %s\n", p1, i1);
245 else if (i1 == p1)
246 printf("strintern(%s) returns input pointer\n", p1);
247 else if (i1 != i2)
248 printf("strintern(%s) != strintern(%s)", i1, i2);
249 else
250 printf("%s\n", i1);
251
252 } else if (!strcmp("perfhashmap", cmd) && p1 && p2) {
253
254 perf_hashmap(atoi(p1), atoi(p2));
255
256 } else {
257
258 printf("Unknown command %s\n", cmd);
259
260 }
261 }
262
263 strbuf_release(&line);
264 hashmap_clear_and_free(&map, struct test_entry, ent);
265 return 0;
266 }