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