Skip to content

Commit 3469a23

Browse files
spectre10gitster
authored andcommitted
t: port helper/test-hashmap.c to unit-tests/t-hashmap.c
helper/test-hashmap.c along with t0011-hashmap.sh test the hashmap.h library. Migrate them to the unit testing framework for better debugging, runtime performance and concise code. Along with the migration, make 'add' tests from the shell script order agnostic in unit tests, since they iterate over entries with the same keys and we do not guarantee the order. This was already done for the 'iterate' tests[1]. The helper/test-hashmap.c is still not removed because it contains a performance test meant to be run by the user directly (not used in t/perf). And it makes sense for such a utility to be a helper. [1]: e1e7a77 (t: sort output of hashmap iteration, 2019-07-30) Mentored-by: Christian Couder <[email protected]> Mentored-by: Kaartic Sivaraam <[email protected]> Helped-by: Josh Steadmon <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Phillip Wood <[email protected]> Signed-off-by: Ghanshyam Thakkar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5c9be4c commit 3469a23

File tree

4 files changed

+363
-357
lines changed

4 files changed

+363
-357
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
13361336
UNIT_TEST_PROGRAMS += t-ctype
13371337
UNIT_TEST_PROGRAMS += t-example-decorate
13381338
UNIT_TEST_PROGRAMS += t-hash
1339+
UNIT_TEST_PROGRAMS += t-hashmap
13391340
UNIT_TEST_PROGRAMS += t-mem-pool
13401341
UNIT_TEST_PROGRAMS += t-oidtree
13411342
UNIT_TEST_PROGRAMS += t-prio-queue

t/helper/test-hashmap.c

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ struct test_entry
1212
char key[FLEX_ARRAY];
1313
};
1414

15-
static const char *get_value(const struct test_entry *e)
16-
{
17-
return e->key + strlen(e->key) + 1;
18-
}
19-
2015
static int test_entry_cmp(const void *cmp_data,
2116
const struct hashmap_entry *eptr,
2217
const struct hashmap_entry *entry_or_key,
@@ -141,30 +136,16 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
141136
/*
142137
* Read stdin line by line and print result of commands to stdout:
143138
*
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
150-
*
151139
* perfhashmap method rounds -> test hashmap.[ch] performance
152140
*/
153141
int cmd__hashmap(int argc, const char **argv)
154142
{
155143
struct string_list parts = STRING_LIST_INIT_NODUP;
156144
struct strbuf line = STRBUF_INIT;
157-
int icase;
158-
struct hashmap map = HASHMAP_INIT(test_entry_cmp, &icase);
159-
160-
/* init hash map */
161-
icase = argc > 1 && !strcmp("ignorecase", argv[1]);
162145

163146
/* process commands from stdin */
164147
while (strbuf_getline(&line, stdin) != EOF) {
165148
char *cmd, *p1, *p2;
166-
unsigned int hash = 0;
167-
struct test_entry *entry;
168149

169150
/* break line into command and up to two parameters */
170151
string_list_setlen(&parts, 0);
@@ -180,84 +161,8 @@ int cmd__hashmap(int argc, const char **argv)
180161
cmd = parts.items[0].string;
181162
p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
182163
p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
183-
if (p1)
184-
hash = icase ? strihash(p1) : strhash(p1);
185-
186-
if (!strcmp("add", cmd) && p1 && p2) {
187-
188-
/* create entry with key = p1, value = p2 */
189-
entry = alloc_test_entry(hash, p1, p2);
190-
191-
/* add to hashmap */
192-
hashmap_add(&map, &entry->ent);
193-
194-
} else if (!strcmp("put", cmd) && p1 && p2) {
195-
196-
/* create entry with key = p1, value = p2 */
197-
entry = alloc_test_entry(hash, p1, p2);
198-
199-
/* add / replace entry */
200-
entry = hashmap_put_entry(&map, entry, ent);
201-
202-
/* print and free replaced entry, if any */
203-
puts(entry ? get_value(entry) : "NULL");
204-
free(entry);
205-
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);
210-
211-
/* print result */
212-
if (!entry)
213-
puts("NULL");
214-
hashmap_for_each_entry_from(&map, entry, ent)
215-
puts(get_value(entry));
216-
217-
} else if (!strcmp("remove", cmd) && p1) {
218-
219-
/* setup static key */
220-
struct hashmap_entry key;
221-
struct hashmap_entry *rm;
222-
hashmap_entry_init(&key, hash);
223-
224-
/* remove entry from hashmap */
225-
rm = hashmap_remove(&map, &key, p1);
226-
entry = rm ? container_of(rm, struct test_entry, ent)
227-
: NULL;
228-
229-
/* print result and free entry*/
230-
puts(entry ? get_value(entry) : "NULL");
231-
free(entry);
232-
233-
} else if (!strcmp("iterate", cmd)) {
234-
struct hashmap_iter iter;
235-
236-
hashmap_for_each_entry(&map, &iter, entry,
237-
ent /* member name */)
238-
printf("%s %s\n", entry->key, get_value(entry));
239-
240-
} else if (!strcmp("size", cmd)) {
241-
242-
/* print table sizes */
243-
printf("%u %u\n", map.tablesize,
244-
hashmap_get_size(&map));
245-
246-
} else if (!strcmp("intern", cmd) && p1) {
247-
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);
259164

260-
} else if (!strcmp("perfhashmap", cmd) && p1 && p2) {
165+
if (!strcmp("perfhashmap", cmd) && p1 && p2) {
261166

262167
perf_hashmap(atoi(p1), atoi(p2));
263168

@@ -270,6 +175,5 @@ int cmd__hashmap(int argc, const char **argv)
270175

271176
string_list_clear(&parts, 0);
272177
strbuf_release(&line);
273-
hashmap_clear_and_free(&map, struct test_entry, ent);
274178
return 0;
275179
}

0 commit comments

Comments
 (0)