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