t/helper: add test-oidmap.c
authorChristian Couder <christian.couder@gmail.com>
Sat, 15 Jun 2019 10:06:59 +0000 (12:06 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 18 Jun 2019 01:11:41 +0000 (18:11 -0700)
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>
Makefile
t/helper/test-oidmap.c [new file with mode: 0644]
t/helper/test-tool.c
t/helper/test-tool.h
index 8a7e2353520ddd7e0c8074d2b32d0441d97c1597..5efc7700edbb696a43861b85c29d552c71f628fb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -727,6 +727,7 @@ TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
 TEST_BUILTINS_OBJS += test-match-trees.o
 TEST_BUILTINS_OBJS += test-mergesort.o
 TEST_BUILTINS_OBJS += test-mktemp.o
+TEST_BUILTINS_OBJS += test-oidmap.o
 TEST_BUILTINS_OBJS += test-online-cpus.o
 TEST_BUILTINS_OBJS += test-parse-options.o
 TEST_BUILTINS_OBJS += test-path-utils.o
diff --git a/t/helper/test-oidmap.c b/t/helper/test-oidmap.c
new file mode 100644 (file)
index 0000000..7036588
--- /dev/null
@@ -0,0 +1,126 @@
+#include "test-tool.h"
+#include "cache.h"
+#include "oidmap.h"
+#include "strbuf.h"
+
+/* key is an oid and value is a name (could be a refname for example) */
+struct test_entry {
+       struct oidmap_entry entry;
+       char name[FLEX_ARRAY];
+};
+
+#define DELIM " \t\r\n"
+
+/*
+ * Read stdin line by line and print result of commands to stdout:
+ *
+ * hash oidkey -> sha1hash(oidkey)
+ * put oidkey namevalue -> NULL / old namevalue
+ * get oidkey -> NULL / namevalue
+ * remove oidkey -> NULL / old namevalue
+ * iterate -> oidkey1 namevalue1\noidkey2 namevalue2\n...
+ *
+ */
+int cmd__oidmap(int argc, const char **argv)
+{
+       struct strbuf line = STRBUF_INIT;
+       struct oidmap map = OIDMAP_INIT;
+
+       setup_git_directory();
+
+       /* init oidmap */
+       oidmap_init(&map, 0);
+
+       /* process commands from stdin */
+       while (strbuf_getline(&line, stdin) != EOF) {
+               char *cmd, *p1 = NULL, *p2 = NULL;
+               struct test_entry *entry;
+               struct object_id oid;
+
+               /* break line into command and up to two parameters */
+               cmd = strtok(line.buf, DELIM);
+               /* ignore empty lines */
+               if (!cmd || *cmd == '#')
+                       continue;
+
+               p1 = strtok(NULL, DELIM);
+               if (p1)
+                       p2 = strtok(NULL, DELIM);
+
+               if (!strcmp("add", cmd) && p1 && p2) {
+
+                       if (get_oid(p1, &oid)) {
+                               printf("Unknown oid: %s\n", p1);
+                               continue;
+                       }
+
+                       /* create entry with oidkey from p1, value = p2 */
+                       FLEX_ALLOC_STR(entry, name, p2);
+                       oidcpy(&entry->entry.oid, &oid);
+
+                       /* add to oidmap */
+                       oidmap_put(&map, entry);
+
+               } else if (!strcmp("put", cmd) && p1 && p2) {
+
+                       if (get_oid(p1, &oid)) {
+                               printf("Unknown oid: %s\n", p1);
+                               continue;
+                       }
+
+                       /* create entry with oid_key = p1, name_value = p2 */
+                       FLEX_ALLOC_STR(entry, name, p2);
+                       oidcpy(&entry->entry.oid, &oid);
+
+                       /* add / replace entry */
+                       entry = oidmap_put(&map, entry);
+
+                       /* print and free replaced entry, if any */
+                       puts(entry ? entry->name : "NULL");
+                       free(entry);
+
+               } else if (!strcmp("get", cmd) && p1) {
+
+                       if (get_oid(p1, &oid)) {
+                               printf("Unknown oid: %s\n", p1);
+                               continue;
+                       }
+
+                       /* lookup entry in oidmap */
+                       entry = oidmap_get(&map, &oid);
+
+                       /* print result */
+                       puts(entry ? entry->name : "NULL");
+
+               } else if (!strcmp("remove", cmd) && p1) {
+
+                       if (get_oid(p1, &oid)) {
+                               printf("Unknown oid: %s\n", p1);
+                               continue;
+                       }
+
+                       /* remove entry from oidmap */
+                       entry = oidmap_remove(&map, &oid);
+
+                       /* print result and free entry*/
+                       puts(entry ? entry->name : "NULL");
+                       free(entry);
+
+               } else if (!strcmp("iterate", cmd)) {
+
+                       struct oidmap_iter iter;
+                       oidmap_iter_init(&map, &iter);
+                       while ((entry = oidmap_iter_next(&iter)))
+                               printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
+
+               } else {
+
+                       printf("Unknown command %s\n", cmd);
+
+               }
+       }
+
+       strbuf_release(&line);
+       oidmap_free(&map, 1);
+       return 0;
+}
index 087a8c0cc9da64d7bc276c3870b2d0faba4c2627..1eac25233f7ce62ecb00b2d1e3d06d1423c3581f 100644 (file)
@@ -35,6 +35,7 @@ static struct test_cmd cmds[] = {
        { "match-trees", cmd__match_trees },
        { "mergesort", cmd__mergesort },
        { "mktemp", cmd__mktemp },
+       { "oidmap", cmd__oidmap },
        { "online-cpus", cmd__online_cpus },
        { "parse-options", cmd__parse_options },
        { "path-utils", cmd__path_utils },
index 7e703f3038ae433c7d8b4ef5af51d9781d6bfffb..c7a46dc320e93b3bb5aef5f7fce3697c4558f814 100644 (file)
@@ -25,6 +25,7 @@ int cmd__lazy_init_name_hash(int argc, const char **argv);
 int cmd__match_trees(int argc, const char **argv);
 int cmd__mergesort(int argc, const char **argv);
 int cmd__mktemp(int argc, const char **argv);
+int cmd__oidmap(int argc, const char **argv);
 int cmd__online_cpus(int argc, const char **argv);
 int cmd__parse_options(int argc, const char **argv);
 int cmd__path_utils(int argc, const char **argv);