]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-hashmap.c
Merge branch 'en/merge-recursive-directory-rename-fixes'
[thirdparty/git.git] / t / helper / test-hashmap.c
CommitLineData
7c18cbd5 1#include "test-tool.h"
e8fa59b9 2#include "git-compat-util.h"
6a364ced 3#include "hashmap.h"
7e8089c9 4#include "strbuf.h"
6a364ced
KB
5
6struct test_entry
7{
e2b5038d 8 int padding; /* hashmap entry no longer needs to be the first member */
6a364ced
KB
9 struct hashmap_entry ent;
10 /* key and value as two \0-terminated strings */
11 char key[FLEX_ARRAY];
12};
13
14static const char *get_value(const struct test_entry *e)
15{
16 return e->key + strlen(e->key) + 1;
17}
18
6815d114 19static int test_entry_cmp(const void *cmp_data,
939af16e
EW
20 const struct hashmap_entry *eptr,
21 const struct hashmap_entry *entry_or_key,
6815d114 22 const void *keydata)
6a364ced 23{
6815d114 24 const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
939af16e 25 const struct test_entry *e1, *e2;
6815d114
SB
26 const char *key = keydata;
27
939af16e
EW
28 e1 = container_of(eptr, const struct test_entry, ent);
29 e2 = container_of(entry_or_key, const struct test_entry, ent);
30
6815d114
SB
31 if (ignore_case)
32 return strcasecmp(e1->key, key ? key : e2->key);
33 else
34 return strcmp(e1->key, key ? key : e2->key);
6a364ced
KB
35}
36
a6119f82
JK
37static struct test_entry *alloc_test_entry(unsigned int hash,
38 char *key, char *value)
6a364ced 39{
7daa825d
JK
40 size_t klen = strlen(key);
41 size_t vlen = strlen(value);
b6c4380d 42 struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
d22245a2 43 hashmap_entry_init(&entry->ent, hash);
6a364ced
KB
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
58static unsigned int hash(unsigned int method, unsigned int i, const char *key)
59{
3b9a2b07 60 unsigned int hash = 0;
6a364ced
KB
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]
7c18cbd5 84 * Usage: time echo "perfhashmap method rounds" | test-tool hashmap
6a364ced
KB
85 */
86static 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
aef6cf1e
JK
94 ALLOC_ARRAY(entries, TEST_SIZE);
95 ALLOC_ARRAY(hashes, TEST_SIZE);
6a364ced 96 for (i = 0; i < TEST_SIZE; i++) {
cbadf0ee 97 xsnprintf(buf, sizeof(buf), "%i", i);
7daa825d 98 entries[i] = alloc_test_entry(0, buf, "");
6a364ced
KB
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++) {
6815d114 105 hashmap_init(&map, test_entry_cmp, NULL, 0);
6a364ced
KB
106
107 /* add entries */
108 for (i = 0; i < TEST_SIZE; i++) {
d22245a2 109 hashmap_entry_init(&entries[i]->ent, hashes[i]);
b94e5c1d 110 hashmap_add(&map, &entries[i]->ent);
6a364ced
KB
111 }
112
c8e424c9 113 hashmap_free(&map);
6a364ced
KB
114 }
115 } else {
116 /* test map lookups */
6815d114 117 hashmap_init(&map, test_entry_cmp, NULL, 0);
6a364ced
KB
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++) {
d22245a2 122 hashmap_entry_init(&entries[i]->ent, hashes[i]);
b94e5c1d 123 hashmap_add(&map, &entries[i]->ent);
6a364ced
KB
124 }
125
126 for (j = 0; j < rounds; j++) {
127 for (i = 0; i < TEST_SIZE; i++) {
ab73a9d1
KB
128 hashmap_get_from_hash(&map, hashes[i],
129 entries[i]->key);
6a364ced
KB
130 }
131 }
132
c8e424c9 133 hashmap_free(&map);
6a364ced
KB
134 }
135}
136
6a364ced
KB
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
6a364ced 150 */
7c18cbd5 151int cmd__hashmap(int argc, const char **argv)
6a364ced 152{
7e8089c9 153 struct strbuf line = STRBUF_INIT;
6a364ced
KB
154 struct hashmap map;
155 int icase;
156
157 /* init hash map */
158 icase = argc > 1 && !strcmp("ignorecase", argv[1]);
6815d114 159 hashmap_init(&map, test_entry_cmp, &icase, 0);
6a364ced
KB
160
161 /* process commands from stdin */
7e8089c9 162 while (strbuf_getline(&line, stdin) != EOF) {
6a364ced 163 char *cmd, *p1 = NULL, *p2 = NULL;
a6119f82 164 unsigned int hash = 0;
6a364ced
KB
165 struct test_entry *entry;
166
167 /* break line into command and up to two parameters */
7e8089c9 168 cmd = strtok(line.buf, DELIM);
6a364ced
KB
169 /* ignore empty lines */
170 if (!cmd || *cmd == '#')
171 continue;
172
173 p1 = strtok(NULL, DELIM);
174 if (p1) {
6a364ced
KB
175 hash = icase ? strihash(p1) : strhash(p1);
176 p2 = strtok(NULL, DELIM);
6a364ced
KB
177 }
178
a1100d2c 179 if (!strcmp("add", cmd) && p1 && p2) {
6a364ced
KB
180
181 /* create entry with key = p1, value = p2 */
7daa825d 182 entry = alloc_test_entry(hash, p1, p2);
6a364ced
KB
183
184 /* add to hashmap */
b94e5c1d 185 hashmap_add(&map, &entry->ent);
6a364ced 186
7daa825d 187 } else if (!strcmp("put", cmd) && p1 && p2) {
6a364ced
KB
188
189 /* create entry with key = p1, value = p2 */
7daa825d 190 entry = alloc_test_entry(hash, p1, p2);
6a364ced
KB
191
192 /* add / replace entry */
404ab78e 193 entry = hashmap_put_entry(&map, entry, ent);
6a364ced
KB
194
195 /* print and free replaced entry, if any */
196 puts(entry ? get_value(entry) : "NULL");
197 free(entry);
198
7daa825d 199 } else if (!strcmp("get", cmd) && p1) {
6a364ced 200 /* lookup entry in hashmap */
f0e63c41
EW
201 entry = hashmap_get_entry_from_hash(&map, hash, p1,
202 struct test_entry, ent);
6a364ced
KB
203
204 /* print result */
f0e63c41 205 if (!entry)
6a364ced 206 puts("NULL");
23dee69f 207 hashmap_for_each_entry_from(&map, entry, ent)
6a364ced 208 puts(get_value(entry));
6a364ced 209
7daa825d 210 } else if (!strcmp("remove", cmd) && p1) {
6a364ced
KB
211
212 /* setup static key */
213 struct hashmap_entry key;
8a973d0b 214 struct hashmap_entry *rm;
6a364ced
KB
215 hashmap_entry_init(&key, hash);
216
217 /* remove entry from hashmap */
8a973d0b
EW
218 rm = hashmap_remove(&map, &key, p1);
219 entry = rm ? container_of(rm, struct test_entry, ent)
220 : NULL;
6a364ced
KB
221
222 /* print result and free entry*/
223 puts(entry ? get_value(entry) : "NULL");
224 free(entry);
225
226 } else if (!strcmp("iterate", cmd)) {
6a364ced 227 struct hashmap_iter iter;
87571c3f
EW
228
229 hashmap_for_each_entry(&map, &iter, entry,
87571c3f 230 ent /* member name */)
6a364ced
KB
231 printf("%s %s\n", entry->key, get_value(entry));
232
233 } else if (!strcmp("size", cmd)) {
234
235 /* print table sizes */
8b604d19
JH
236 printf("%u %u\n", map.tablesize,
237 hashmap_get_size(&map));
6a364ced 238
7daa825d 239 } else if (!strcmp("intern", cmd) && p1) {
7b64d42d
KB
240
241 /* test that strintern works */
242 const char *i1 = strintern(p1);
243 const char *i2 = strintern(p1);
244 if (strcmp(i1, p1))
245 printf("strintern(%s) returns %s\n", p1, i1);
246 else if (i1 == p1)
247 printf("strintern(%s) returns input pointer\n", p1);
248 else if (i1 != i2)
249 printf("strintern(%s) != strintern(%s)", i1, i2);
250 else
251 printf("%s\n", i1);
252
7daa825d 253 } else if (!strcmp("perfhashmap", cmd) && p1 && p2) {
6a364ced
KB
254
255 perf_hashmap(atoi(p1), atoi(p2));
256
6a364ced
KB
257 } else {
258
259 printf("Unknown command %s\n", cmd);
260
261 }
262 }
263
7e8089c9 264 strbuf_release(&line);
c8e424c9 265 hashmap_free_entries(&map, struct test_entry, ent);
6a364ced
KB
266 return 0;
267}