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