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