Skip to content

Commit c62bff2

Browse files
committed
Merge branch 'cc/test-oidmap'
Extend the test coverage a bit. * cc/test-oidmap: t0016: add 'remove' subcommand test test-oidmap: remove 'add' subcommand test-hashmap: remove 'hash' command oidmap: use sha1hash() instead of static hash() function t: add t0016-oidmap.sh t/helper: add test-oidmap.c
2 parents 4308d81 + fbec05c commit c62bff2

File tree

8 files changed

+221
-27
lines changed

8 files changed

+221
-27
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
721721
TEST_BUILTINS_OBJS += test-match-trees.o
722722
TEST_BUILTINS_OBJS += test-mergesort.o
723723
TEST_BUILTINS_OBJS += test-mktemp.o
724+
TEST_BUILTINS_OBJS += test-oidmap.o
724725
TEST_BUILTINS_OBJS += test-online-cpus.o
725726
TEST_BUILTINS_OBJS += test-parse-options.o
726727
TEST_BUILTINS_OBJS += test-path-utils.o

oidmap.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ static int oidmap_neq(const void *hashmap_cmp_fn_data,
1212
&((const struct oidmap_entry *) entry_or_key)->oid);
1313
}
1414

15-
static int hash(const struct object_id *oid)
16-
{
17-
int hash;
18-
memcpy(&hash, oid->hash, sizeof(hash));
19-
return hash;
20-
}
21-
2215
void oidmap_init(struct oidmap *map, size_t initial_size)
2316
{
2417
hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
@@ -36,7 +29,7 @@ void *oidmap_get(const struct oidmap *map, const struct object_id *key)
3629
if (!map->map.cmpfn)
3730
return NULL;
3831

39-
return hashmap_get_from_hash(&map->map, hash(key), key);
32+
return hashmap_get_from_hash(&map->map, oidhash(key), key);
4033
}
4134

4235
void *oidmap_remove(struct oidmap *map, const struct object_id *key)
@@ -46,7 +39,7 @@ void *oidmap_remove(struct oidmap *map, const struct object_id *key)
4639
if (!map->map.cmpfn)
4740
oidmap_init(map, 0);
4841

49-
hashmap_entry_init(&entry, hash(key));
42+
hashmap_entry_init(&entry, oidhash(key));
5043
return hashmap_remove(&map->map, &entry, key);
5144
}
5245

@@ -57,6 +50,6 @@ void *oidmap_put(struct oidmap *map, void *entry)
5750
if (!map->map.cmpfn)
5851
oidmap_init(map, 0);
5952

60-
hashmap_entry_init(&to_put->internal_entry, hash(&to_put->oid));
53+
hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
6154
return hashmap_put(&map->map, to_put);
6255
}

t/helper/test-hashmap.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,7 @@ int cmd__hashmap(int argc, const char **argv)
173173
p2 = strtok(NULL, DELIM);
174174
}
175175

176-
if (!strcmp("hash", cmd) && p1) {
177-
178-
/* print results of different hash functions */
179-
printf("%u %u %u %u\n",
180-
strhash(p1), memhash(p1, strlen(p1)),
181-
strihash(p1), memihash(p1, strlen(p1)));
182-
183-
} else if (!strcmp("add", cmd) && p1 && p2) {
176+
if (!strcmp("add", cmd) && p1 && p2) {
184177

185178
/* create entry with key = p1, value = p2 */
186179
entry = alloc_test_entry(hash, p1, p2);

t/helper/test-oidmap.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#include "test-tool.h"
2+
#include "cache.h"
3+
#include "oidmap.h"
4+
#include "strbuf.h"
5+
6+
/* key is an oid and value is a name (could be a refname for example) */
7+
struct test_entry {
8+
struct oidmap_entry entry;
9+
char name[FLEX_ARRAY];
10+
};
11+
12+
#define DELIM " \t\r\n"
13+
14+
/*
15+
* Read stdin line by line and print result of commands to stdout:
16+
*
17+
* hash oidkey -> sha1hash(oidkey)
18+
* put oidkey namevalue -> NULL / old namevalue
19+
* get oidkey -> NULL / namevalue
20+
* remove oidkey -> NULL / old namevalue
21+
* iterate -> oidkey1 namevalue1\noidkey2 namevalue2\n...
22+
*
23+
*/
24+
int cmd__oidmap(int argc, const char **argv)
25+
{
26+
struct strbuf line = STRBUF_INIT;
27+
struct oidmap map = OIDMAP_INIT;
28+
29+
setup_git_directory();
30+
31+
/* init oidmap */
32+
oidmap_init(&map, 0);
33+
34+
/* process commands from stdin */
35+
while (strbuf_getline(&line, stdin) != EOF) {
36+
char *cmd, *p1 = NULL, *p2 = NULL;
37+
struct test_entry *entry;
38+
struct object_id oid;
39+
40+
/* break line into command and up to two parameters */
41+
cmd = strtok(line.buf, DELIM);
42+
/* ignore empty lines */
43+
if (!cmd || *cmd == '#')
44+
continue;
45+
46+
p1 = strtok(NULL, DELIM);
47+
if (p1)
48+
p2 = strtok(NULL, DELIM);
49+
50+
if (!strcmp("put", cmd) && p1 && p2) {
51+
52+
if (get_oid(p1, &oid)) {
53+
printf("Unknown oid: %s\n", p1);
54+
continue;
55+
}
56+
57+
/* create entry with oid_key = p1, name_value = p2 */
58+
FLEX_ALLOC_STR(entry, name, p2);
59+
oidcpy(&entry->entry.oid, &oid);
60+
61+
/* add / replace entry */
62+
entry = oidmap_put(&map, entry);
63+
64+
/* print and free replaced entry, if any */
65+
puts(entry ? entry->name : "NULL");
66+
free(entry);
67+
68+
} else if (!strcmp("get", cmd) && p1) {
69+
70+
if (get_oid(p1, &oid)) {
71+
printf("Unknown oid: %s\n", p1);
72+
continue;
73+
}
74+
75+
/* lookup entry in oidmap */
76+
entry = oidmap_get(&map, &oid);
77+
78+
/* print result */
79+
puts(entry ? entry->name : "NULL");
80+
81+
} else if (!strcmp("remove", cmd) && p1) {
82+
83+
if (get_oid(p1, &oid)) {
84+
printf("Unknown oid: %s\n", p1);
85+
continue;
86+
}
87+
88+
/* remove entry from oidmap */
89+
entry = oidmap_remove(&map, &oid);
90+
91+
/* print result and free entry*/
92+
puts(entry ? entry->name : "NULL");
93+
free(entry);
94+
95+
} else if (!strcmp("iterate", cmd)) {
96+
97+
struct oidmap_iter iter;
98+
oidmap_iter_init(&map, &iter);
99+
while ((entry = oidmap_iter_next(&iter)))
100+
printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
101+
102+
} else {
103+
104+
printf("Unknown command %s\n", cmd);
105+
106+
}
107+
}
108+
109+
strbuf_release(&line);
110+
oidmap_free(&map, 1);
111+
return 0;
112+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static struct test_cmd cmds[] = {
3535
{ "match-trees", cmd__match_trees },
3636
{ "mergesort", cmd__mergesort },
3737
{ "mktemp", cmd__mktemp },
38+
{ "oidmap", cmd__oidmap },
3839
{ "online-cpus", cmd__online_cpus },
3940
{ "parse-options", cmd__parse_options },
4041
{ "path-utils", cmd__path_utils },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int cmd__lazy_init_name_hash(int argc, const char **argv);
2525
int cmd__match_trees(int argc, const char **argv);
2626
int cmd__mergesort(int argc, const char **argv);
2727
int cmd__mktemp(int argc, const char **argv);
28+
int cmd__oidmap(int argc, const char **argv);
2829
int cmd__online_cpus(int argc, const char **argv);
2930
int cmd__parse_options(int argc, const char **argv);
3031
int cmd__path_utils(int argc, const char **argv);

t/t0011-hashmap.sh

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ test_hashmap() {
99
test_cmp expect actual
1010
}
1111

12-
test_expect_success 'hash functions' '
13-
14-
test_hashmap "hash key1" "2215982743 2215982743 116372151 116372151" &&
15-
test_hashmap "hash key2" "2215982740 2215982740 116372148 116372148" &&
16-
test_hashmap "hash fooBarFrotz" "1383912807 1383912807 3189766727 3189766727" &&
17-
test_hashmap "hash foobarfrotz" "2862305959 2862305959 3189766727 3189766727"
18-
19-
'
20-
2112
test_expect_success 'put' '
2213
2314
test_hashmap "put key1 value1

t/t0016-oidmap.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/sh
2+
3+
test_description='test oidmap'
4+
. ./test-lib.sh
5+
6+
# This purposefully is very similar to t0011-hashmap.sh
7+
8+
test_oidmap () {
9+
echo "$1" | test-tool oidmap $3 >actual &&
10+
echo "$2" >expect &&
11+
test_cmp expect actual
12+
}
13+
14+
15+
test_expect_success 'setup' '
16+
17+
test_commit one &&
18+
test_commit two &&
19+
test_commit three &&
20+
test_commit four
21+
22+
'
23+
24+
test_expect_success 'put' '
25+
26+
test_oidmap "put one 1
27+
put two 2
28+
put invalidOid 4
29+
put three 3" "NULL
30+
NULL
31+
Unknown oid: invalidOid
32+
NULL"
33+
34+
'
35+
36+
test_expect_success 'replace' '
37+
38+
test_oidmap "put one 1
39+
put two 2
40+
put three 3
41+
put invalidOid 4
42+
put two deux
43+
put one un" "NULL
44+
NULL
45+
NULL
46+
Unknown oid: invalidOid
47+
2
48+
1"
49+
50+
'
51+
52+
test_expect_success 'get' '
53+
54+
test_oidmap "put one 1
55+
put two 2
56+
put three 3
57+
get two
58+
get four
59+
get invalidOid
60+
get one" "NULL
61+
NULL
62+
NULL
63+
2
64+
NULL
65+
Unknown oid: invalidOid
66+
1"
67+
68+
'
69+
70+
test_expect_success 'remove' '
71+
72+
test_oidmap "put one 1
73+
put two 2
74+
put three 3
75+
remove one
76+
remove two
77+
remove invalidOid
78+
remove four" "NULL
79+
NULL
80+
NULL
81+
1
82+
2
83+
Unknown oid: invalidOid
84+
NULL"
85+
86+
'
87+
88+
test_expect_success 'iterate' '
89+
90+
test_oidmap "put one 1
91+
put two 2
92+
put three 3
93+
iterate" "NULL
94+
NULL
95+
NULL
96+
$(git rev-parse two) 2
97+
$(git rev-parse one) 1
98+
$(git rev-parse three) 3"
99+
100+
'
101+
102+
test_done

0 commit comments

Comments
 (0)