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