git-p4: fix typos
[git/gitster.git] / t / helper / test-hashmap.c
blob2912899558eafa595b7d644293d8e7b660993789
1 #include "test-tool.h"
2 #include "git-compat-util.h"
3 #include "hashmap.h"
4 #include "strbuf.h"
5 #include "string-list.h"
7 struct test_entry
9 int padding; /* hashmap entry no longer needs to be the first member */
10 struct hashmap_entry ent;
11 /* key and value as two \0-terminated strings */
12 char key[FLEX_ARRAY];
15 static const char *get_value(const struct test_entry *e)
17 return e->key + strlen(e->key) + 1;
20 static int test_entry_cmp(const void *cmp_data,
21 const struct hashmap_entry *eptr,
22 const struct hashmap_entry *entry_or_key,
23 const void *keydata)
25 const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
26 const struct test_entry *e1, *e2;
27 const char *key = keydata;
29 e1 = container_of(eptr, const struct test_entry, ent);
30 e2 = container_of(entry_or_key, const struct test_entry, ent);
32 if (ignore_case)
33 return strcasecmp(e1->key, key ? key : e2->key);
34 else
35 return strcmp(e1->key, key ? key : e2->key);
38 static struct test_entry *alloc_test_entry(unsigned int hash,
39 const char *key,
40 const char *value)
42 size_t klen = strlen(key);
43 size_t vlen = strlen(value);
44 struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
45 hashmap_entry_init(&entry->ent, hash);
46 memcpy(entry->key, key, klen + 1);
47 memcpy(entry->key + klen + 1, value, vlen + 1);
48 return entry;
51 #define HASH_METHOD_FNV 0
52 #define HASH_METHOD_I 1
53 #define HASH_METHOD_IDIV10 2
54 #define HASH_METHOD_0 3
55 #define HASH_METHOD_X2 4
56 #define TEST_SPARSE 8
57 #define TEST_ADD 16
58 #define TEST_SIZE 100000
60 static unsigned int hash(unsigned int method, unsigned int i, const char *key)
62 unsigned int hash = 0;
63 switch (method & 3)
65 case HASH_METHOD_FNV:
66 hash = strhash(key);
67 break;
68 case HASH_METHOD_I:
69 hash = i;
70 break;
71 case HASH_METHOD_IDIV10:
72 hash = i / 10;
73 break;
74 case HASH_METHOD_0:
75 hash = 0;
76 break;
79 if (method & HASH_METHOD_X2)
80 hash = 2 * hash;
81 return hash;
85 * Test performance of hashmap.[ch]
86 * Usage: time echo "perfhashmap method rounds" | test-tool hashmap
88 static void perf_hashmap(unsigned int method, unsigned int rounds)
90 struct hashmap map;
91 char buf[16];
92 struct test_entry **entries;
93 unsigned int *hashes;
94 unsigned int i, j;
96 ALLOC_ARRAY(entries, TEST_SIZE);
97 ALLOC_ARRAY(hashes, TEST_SIZE);
98 for (i = 0; i < TEST_SIZE; i++) {
99 xsnprintf(buf, sizeof(buf), "%i", i);
100 entries[i] = alloc_test_entry(0, buf, "");
101 hashes[i] = hash(method, i, entries[i]->key);
104 if (method & TEST_ADD) {
105 /* test adding to the map */
106 for (j = 0; j < rounds; j++) {
107 hashmap_init(&map, test_entry_cmp, NULL, 0);
109 /* add entries */
110 for (i = 0; i < TEST_SIZE; i++) {
111 hashmap_entry_init(&entries[i]->ent, hashes[i]);
112 hashmap_add(&map, &entries[i]->ent);
115 hashmap_clear(&map);
117 } else {
118 /* test map lookups */
119 hashmap_init(&map, test_entry_cmp, NULL, 0);
121 /* fill the map (sparsely if specified) */
122 j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
123 for (i = 0; i < j; i++) {
124 hashmap_entry_init(&entries[i]->ent, hashes[i]);
125 hashmap_add(&map, &entries[i]->ent);
128 for (j = 0; j < rounds; j++) {
129 for (i = 0; i < TEST_SIZE; i++) {
130 hashmap_get_from_hash(&map, hashes[i],
131 entries[i]->key);
135 hashmap_clear(&map);
139 #define DELIM " \t\r\n"
142 * Read stdin line by line and print result of commands to stdout:
144 * hash key -> strhash(key) memhash(key) strihash(key) memihash(key)
145 * put key value -> NULL / old value
146 * get key -> NULL / value
147 * remove key -> NULL / old value
148 * iterate -> key1 value1\nkey2 value2\n...
149 * size -> tablesize numentries
151 * perfhashmap method rounds -> test hashmap.[ch] performance
153 int cmd__hashmap(int argc, const char **argv)
155 struct string_list parts = STRING_LIST_INIT_NODUP;
156 struct strbuf line = STRBUF_INIT;
157 int icase;
158 struct hashmap map = HASHMAP_INIT(test_entry_cmp, &icase);
160 /* init hash map */
161 icase = argc > 1 && !strcmp("ignorecase", argv[1]);
163 /* process commands from stdin */
164 while (strbuf_getline(&line, stdin) != EOF) {
165 char *cmd, *p1, *p2;
166 unsigned int hash = 0;
167 struct test_entry *entry;
169 /* break line into command and up to two parameters */
170 string_list_setlen(&parts, 0);
171 string_list_split_in_place(&parts, line.buf, DELIM, 2);
172 string_list_remove_empty_items(&parts, 0);
174 /* ignore empty lines */
175 if (!parts.nr)
176 continue;
177 if (!*parts.items[0].string || *parts.items[0].string == '#')
178 continue;
180 cmd = parts.items[0].string;
181 p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
182 p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
183 if (p1)
184 hash = icase ? strihash(p1) : strhash(p1);
186 if (!strcmp("add", cmd) && p1 && p2) {
188 /* create entry with key = p1, value = p2 */
189 entry = alloc_test_entry(hash, p1, p2);
191 /* add to hashmap */
192 hashmap_add(&map, &entry->ent);
194 } else if (!strcmp("put", cmd) && p1 && p2) {
196 /* create entry with key = p1, value = p2 */
197 entry = alloc_test_entry(hash, p1, p2);
199 /* add / replace entry */
200 entry = hashmap_put_entry(&map, entry, ent);
202 /* print and free replaced entry, if any */
203 puts(entry ? get_value(entry) : "NULL");
204 free(entry);
206 } else if (!strcmp("get", cmd) && p1) {
207 /* lookup entry in hashmap */
208 entry = hashmap_get_entry_from_hash(&map, hash, p1,
209 struct test_entry, ent);
211 /* print result */
212 if (!entry)
213 puts("NULL");
214 hashmap_for_each_entry_from(&map, entry, ent)
215 puts(get_value(entry));
217 } else if (!strcmp("remove", cmd) && p1) {
219 /* setup static key */
220 struct hashmap_entry key;
221 struct hashmap_entry *rm;
222 hashmap_entry_init(&key, hash);
224 /* remove entry from hashmap */
225 rm = hashmap_remove(&map, &key, p1);
226 entry = rm ? container_of(rm, struct test_entry, ent)
227 : NULL;
229 /* print result and free entry*/
230 puts(entry ? get_value(entry) : "NULL");
231 free(entry);
233 } else if (!strcmp("iterate", cmd)) {
234 struct hashmap_iter iter;
236 hashmap_for_each_entry(&map, &iter, entry,
237 ent /* member name */)
238 printf("%s %s\n", entry->key, get_value(entry));
240 } else if (!strcmp("size", cmd)) {
242 /* print table sizes */
243 printf("%u %u\n", map.tablesize,
244 hashmap_get_size(&map));
246 } else if (!strcmp("intern", cmd) && p1) {
248 /* test that strintern works */
249 const char *i1 = strintern(p1);
250 const char *i2 = strintern(p1);
251 if (strcmp(i1, p1))
252 printf("strintern(%s) returns %s\n", p1, i1);
253 else if (i1 == p1)
254 printf("strintern(%s) returns input pointer\n", p1);
255 else if (i1 != i2)
256 printf("strintern(%s) != strintern(%s)", i1, i2);
257 else
258 printf("%s\n", i1);
260 } else if (!strcmp("perfhashmap", cmd) && p1 && p2) {
262 perf_hashmap(atoi(p1), atoi(p2));
264 } else {
266 printf("Unknown command %s\n", cmd);
271 string_list_clear(&parts, 0);
272 strbuf_release(&line);
273 hashmap_clear_and_free(&map, struct test_entry, ent);
274 return 0;