t/helper: add test-oidmap.c
This new helper is very similar to "test-hashmap.c" and will help test how `struct oidmap` from oidmap.{c,h} can be used. Helped-by: SZEDER Gábor <szeder.dev@gmail.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Christian Couder committed
Jun 15, 2019 at 12:06 UTC
11510decd020c3cb8936432d4d4bfb214492fcc4
4 files changed
+129
Makefile
+1
@@ -727,6 +727,7 @@ TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
727
TEST_BUILTINS_OBJS += test-match-trees.o
728
TEST_BUILTINS_OBJS += test-mergesort.o
729
TEST_BUILTINS_OBJS += test-mktemp.o
730
+TEST_BUILTINS_OBJS += test-oidmap.o
731
TEST_BUILTINS_OBJS += test-online-cpus.o
732
TEST_BUILTINS_OBJS += test-parse-options.o
733
TEST_BUILTINS_OBJS += test-path-utils.o
t/helper/test-oidmap.c
new
+126
@@ -0,0 +1,126 @@
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("add", 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 oidkey from p1, value = p2 */
58
+ FLEX_ALLOC_STR(entry, name, p2);
59
+ oidcpy(&entry->entry.oid, &oid);
60
+
61
+ /* add to oidmap */
62
+ oidmap_put(&map, entry);
63
+
64
+ } else if (!strcmp("put", cmd) && p1 && p2) {
65
+
66
+ if (get_oid(p1, &oid)) {
67
+ printf("Unknown oid: %s\n", p1);
68
+ continue;
69
+ }
70
+
71
+ /* create entry with oid_key = p1, name_value = p2 */
72
+ FLEX_ALLOC_STR(entry, name, p2);
73
+ oidcpy(&entry->entry.oid, &oid);
74
+
75
+ /* add / replace entry */
76
+ entry = oidmap_put(&map, entry);
77
+
78
+ /* print and free replaced entry, if any */
79
+ puts(entry ? entry->name : "NULL");
80
+ free(entry);
81
+
82
+ } else if (!strcmp("get", cmd) && p1) {
83
+
84
+ if (get_oid(p1, &oid)) {
85
+ printf("Unknown oid: %s\n", p1);
86
+ continue;
87
+ }
88
+
89
+ /* lookup entry in oidmap */
90
+ entry = oidmap_get(&map, &oid);
91
+
92
+ /* print result */
93
+ puts(entry ? entry->name : "NULL");
94
+
95
+ } else if (!strcmp("remove", cmd) && p1) {
96
+
97
+ if (get_oid(p1, &oid)) {
98
+ printf("Unknown oid: %s\n", p1);
99
+ continue;
100
+ }
101
+
102
+ /* remove entry from oidmap */
103
+ entry = oidmap_remove(&map, &oid);
104
+
105
+ /* print result and free entry*/
106
+ puts(entry ? entry->name : "NULL");
107
+ free(entry);
108
+
109
+ } else if (!strcmp("iterate", cmd)) {
110
+
111
+ struct oidmap_iter iter;
112
+ oidmap_iter_init(&map, &iter);
113
+ while ((entry = oidmap_iter_next(&iter)))
114
+ printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
115
+
116
+ } else {
117
+
118
+ printf("Unknown command %s\n", cmd);
119
+
120
+ }
121
+ }
122
+
123
+ strbuf_release(&line);
124
+ oidmap_free(&map, 1);
125
+ return 0;
126
+}
t/helper/test-tool.c
+1
@@ -35,6 +35,7 @@ static struct test_cmd cmds[] = {
35
{ "match-trees", cmd__match_trees },
36
{ "mergesort", cmd__mergesort },
37
{ "mktemp", cmd__mktemp },
38
+ { "oidmap", cmd__oidmap },
39
{ "online-cpus", cmd__online_cpus },
40
{ "parse-options", cmd__parse_options },
41
{ "path-utils", cmd__path_utils },
t/helper/test-tool.h
+1
@@ -25,6 +25,7 @@ int cmd__lazy_init_name_hash(int argc, const char **argv);
25
int cmd__match_trees(int argc, const char **argv);
26
int cmd__mergesort(int argc, const char **argv);
27
int cmd__mktemp(int argc, const char **argv);
28
+int cmd__oidmap(int argc, const char **argv);
29
int cmd__online_cpus(int argc, const char **argv);
30
int cmd__parse_options(int argc, const char **argv);
31
int cmd__path_utils(int argc, const char **argv);