Merge branch 'cc/test-oidmap'
authorJunio C Hamano <gitster@pobox.com>
Fri, 19 Jul 2019 18:30:19 +0000 (11:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 19 Jul 2019 18:30:19 +0000 (11:30 -0700)
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

Makefile
oidmap.c
t/helper/test-hashmap.c
t/helper/test-oidmap.c [new file with mode: 0644]
t/helper/test-tool.c
t/helper/test-tool.h
t/t0011-hashmap.sh
t/t0016-oidmap.sh [new file with mode: 0755]
index 98a0588416c4f3f7a5b48ad72a6c2fd54ff94120..11ccea40716d9bbcef731f093e530a9b1aee0013 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -721,6 +721,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
index b0841a0f5870bafdcaf3482ed725e43ddaf5d461..6d6e840d037657721a5aa975e2567c8415547d93 100644 (file)
--- a/oidmap.c
+++ b/oidmap.c
@@ -12,13 +12,6 @@ static int oidmap_neq(const void *hashmap_cmp_fn_data,
                      &((const struct oidmap_entry *) entry_or_key)->oid);
 }
 
-static int hash(const struct object_id *oid)
-{
-       int hash;
-       memcpy(&hash, oid->hash, sizeof(hash));
-       return hash;
-}
-
 void oidmap_init(struct oidmap *map, size_t initial_size)
 {
        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)
        if (!map->map.cmpfn)
                return NULL;
 
-       return hashmap_get_from_hash(&map->map, hash(key), key);
+       return hashmap_get_from_hash(&map->map, oidhash(key), key);
 }
 
 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)
        if (!map->map.cmpfn)
                oidmap_init(map, 0);
 
-       hashmap_entry_init(&entry, hash(key));
+       hashmap_entry_init(&entry, oidhash(key));
        return hashmap_remove(&map->map, &entry, key);
 }
 
@@ -57,6 +50,6 @@ void *oidmap_put(struct oidmap *map, void *entry)
        if (!map->map.cmpfn)
                oidmap_init(map, 0);
 
-       hashmap_entry_init(&to_put->internal_entry, hash(&to_put->oid));
+       hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
        return hashmap_put(&map->map, to_put);
 }
index 23d2b172fe708f711a15613e906637cd948324ef..aaf17b0ddf9e8ddd1270f613f8c71841c6644eb9 100644 (file)
@@ -173,14 +173,7 @@ int cmd__hashmap(int argc, const char **argv)
                        p2 = strtok(NULL, DELIM);
                }
 
-               if (!strcmp("hash", cmd) && p1) {
-
-                       /* print results of different hash functions */
-                       printf("%u %u %u %u\n",
-                              strhash(p1), memhash(p1, strlen(p1)),
-                              strihash(p1), memihash(p1, strlen(p1)));
-
-               } else if (!strcmp("add", cmd) && p1 && p2) {
+               if (!strcmp("add", cmd) && p1 && p2) {
 
                        /* create entry with key = p1, value = p2 */
                        entry = alloc_test_entry(hash, p1, p2);
diff --git a/t/helper/test-oidmap.c b/t/helper/test-oidmap.c
new file mode 100644 (file)
index 0000000..0acf999
--- /dev/null
@@ -0,0 +1,112 @@
+#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("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);
index 3f1f505e8937f391666a1b7e6d9b972a5f146974..9c96b3e3b10a99c20a1e6f5d4d0349c4fbcb0a10 100755 (executable)
@@ -9,15 +9,6 @@ test_hashmap() {
        test_cmp expect actual
 }
 
-test_expect_success 'hash functions' '
-
-test_hashmap "hash key1" "2215982743 2215982743 116372151 116372151" &&
-test_hashmap "hash key2" "2215982740 2215982740 116372148 116372148" &&
-test_hashmap "hash fooBarFrotz" "1383912807 1383912807 3189766727 3189766727" &&
-test_hashmap "hash foobarfrotz" "2862305959 2862305959 3189766727 3189766727"
-
-'
-
 test_expect_success 'put' '
 
 test_hashmap "put key1 value1
diff --git a/t/t0016-oidmap.sh b/t/t0016-oidmap.sh
new file mode 100755 (executable)
index 0000000..bbe719e
--- /dev/null
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+test_description='test oidmap'
+. ./test-lib.sh
+
+# This purposefully is very similar to t0011-hashmap.sh
+
+test_oidmap () {
+       echo "$1" | test-tool oidmap $3 >actual &&
+       echo "$2" >expect &&
+       test_cmp expect actual
+}
+
+
+test_expect_success 'setup' '
+
+       test_commit one &&
+       test_commit two &&
+       test_commit three &&
+       test_commit four
+
+'
+
+test_expect_success 'put' '
+
+test_oidmap "put one 1
+put two 2
+put invalidOid 4
+put three 3" "NULL
+NULL
+Unknown oid: invalidOid
+NULL"
+
+'
+
+test_expect_success 'replace' '
+
+test_oidmap "put one 1
+put two 2
+put three 3
+put invalidOid 4
+put two deux
+put one un" "NULL
+NULL
+NULL
+Unknown oid: invalidOid
+2
+1"
+
+'
+
+test_expect_success 'get' '
+
+test_oidmap "put one 1
+put two 2
+put three 3
+get two
+get four
+get invalidOid
+get one" "NULL
+NULL
+NULL
+2
+NULL
+Unknown oid: invalidOid
+1"
+
+'
+
+test_expect_success 'remove' '
+
+test_oidmap "put one 1
+put two 2
+put three 3
+remove one
+remove two
+remove invalidOid
+remove four" "NULL
+NULL
+NULL
+1
+2
+Unknown oid: invalidOid
+NULL"
+
+'
+
+test_expect_success 'iterate' '
+
+test_oidmap "put one 1
+put two 2
+put three 3
+iterate" "NULL
+NULL
+NULL
+$(git rev-parse two) 2
+$(git rev-parse one) 1
+$(git rev-parse three) 3"
+
+'
+
+test_done