Skip to content

Commit a3d71f2

Browse files
committed
Merge branch 'gt/unit-test-hashmap'
An existing test of hashmap API has been rewritten with the unit-test framework. * gt/unit-test-hashmap: t: port helper/test-hashmap.c to unit-tests/t-hashmap.c
2 parents f6df5e2 + 3469a23 commit a3d71f2

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
@@ -1335,6 +1335,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
13351335
UNIT_TEST_PROGRAMS += t-ctype
13361336
UNIT_TEST_PROGRAMS += t-example-decorate
13371337
UNIT_TEST_PROGRAMS += t-hash
1338+
UNIT_TEST_PROGRAMS += t-hashmap
13381339
UNIT_TEST_PROGRAMS += t-mem-pool
13391340
UNIT_TEST_PROGRAMS += t-oidmap
13401341
UNIT_TEST_PROGRAMS += t-oidtree

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)