From patchwork Sat Jun 15 10:06:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Christian Couder X-Patchwork-Id: 10997045 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0101713AF for ; Sat, 15 Jun 2019 10:07:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E45352877B for ; Sat, 15 Jun 2019 10:07:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D8644287A9; Sat, 15 Jun 2019 10:07:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 49FE62877B for ; Sat, 15 Jun 2019 10:07:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726417AbfFOKHc (ORCPT ); Sat, 15 Jun 2019 06:07:32 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:42954 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725944AbfFOKHb (ORCPT ); Sat, 15 Jun 2019 06:07:31 -0400 Received: by mail-wr1-f67.google.com with SMTP id x17so4950824wrl.9 for ; Sat, 15 Jun 2019 03:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fJuEfzZKSEOuEOdJZwyNmKyUvX450cFJ9Dk/GASFAG4=; b=ExqbyTmEoH+Gy8NTxRMtyK0v7m1EacX/k4ctukThZAQbtcNOoa+RwXowesn46p/eb0 gAyTip8y5OPdgROjXgn02PCXyTpWkEVN0rSFON2rShHmSCzj/k4WkFqMrEb+MQZ+wHlN 3cwyd/keMm08LZYwrutZoyOvwCFCoroLD8Ld2RSDvCbA8TGa2BGjxz6YN91jbliApXuu 28hdO/ZIOEan7HsW5/ZtZi9pYFz+OwDWlPiHvGLNE0Keabj+SW+0U57RzPci07XQyG9k SwcAIMbREVRxg60i9pSB/uhNSK+WHBkNk/883QsKIw9qBhPR7wcs8+hZDzOnPqsPqAU/ /h/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fJuEfzZKSEOuEOdJZwyNmKyUvX450cFJ9Dk/GASFAG4=; b=nZoQK82uIpiX/yN7+BddTf6fSEsnz1NGDSo7rGavdyQoC5C0ASBPos/4x86p9yBYFM 4FQ1xSlIxCIXkE2uQrNXZaSvlqI8yNEyHL+Inv9vAAmbTXaDetMbPIRYdOXi3BAMuvVp aiInCoKdShM26Rbj0DRdEBE9mreQH9+zhMwoVJW4nD3E/Ud52uySR0MuHfQXkGYYkjXu bjIDmKjB6ioYry65tdejoUTgO8HGNHV9iezPsmxtSParZqpyPC8E2TDqAR90h9e+Bu7y JLkjNd6b5vaZLw0PxVjTzHSc9wrViY/H7CV+mlv1pXbKLEbZz/gEgu5X3bzneILgFJXg Cpxg== X-Gm-Message-State: APjAAAX7Rn/esVBiNfPGI+Qt7t1jtygyksyEXMOWGhBduqJDEmoimQMp 2KrG0I8lK6gvmzDltFQl0qB90qB1pq0= X-Google-Smtp-Source: APXvYqxMRfj0I93mKgk38MzO+CUE2WVKD8Js2bMzplT9ZI4wceMquhSLdnhV//sngYGDu5QoJqO9+Q== X-Received: by 2002:adf:f544:: with SMTP id j4mr21486773wrp.150.1560593249580; Sat, 15 Jun 2019 03:07:29 -0700 (PDT) Received: from localhost.localdomain ([2a04:cec0:1017:d205:2143:78cf:c452:5744]) by smtp.gmail.com with ESMTPSA id x11sm8091576wmg.23.2019.06.15.03.07.28 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sat, 15 Jun 2019 03:07:28 -0700 (PDT) From: Christian Couder X-Google-Original-From: Christian Couder To: git@vger.kernel.org Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan , =?utf-8?q?SZEDER_G=C3=A1bor?= , Jeff King , Christian Couder Subject: [PATCH v4 1/4] t/helper: add test-oidmap.c Date: Sat, 15 Jun 2019 12:06:59 +0200 Message-Id: <20190615100702.20762-2-chriscool@tuxfamily.org> X-Mailer: git-send-email 2.22.0.3.g82edbe9b01.dirty In-Reply-To: <20190615100702.20762-1-chriscool@tuxfamily.org> References: <20190615100702.20762-1-chriscool@tuxfamily.org> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Helped-by: Jeff King Signed-off-by: Christian Couder --- Makefile | 1 + t/helper/test-oidmap.c | 126 +++++++++++++++++++++++++++++++++++++++++ t/helper/test-tool.c | 1 + t/helper/test-tool.h | 1 + 4 files changed, 129 insertions(+) create mode 100644 t/helper/test-oidmap.c diff --git a/Makefile b/Makefile index 8a7e235352..5efc7700ed 100644 --- 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 index 0000000000..7036588175 --- /dev/null +++ b/t/helper/test-oidmap.c @@ -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; +} diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c index 087a8c0cc9..1eac25233f 100644 --- a/t/helper/test-tool.c +++ b/t/helper/test-tool.c @@ -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 }, diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h index 7e703f3038..c7a46dc320 100644 --- a/t/helper/test-tool.h +++ b/t/helper/test-tool.h @@ -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); From patchwork Sat Jun 15 10:07:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Christian Couder X-Patchwork-Id: 10997049 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 03C7B76 for ; Sat, 15 Jun 2019 10:07:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E64502877B for ; Sat, 15 Jun 2019 10:07:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DA8D4287A9; Sat, 15 Jun 2019 10:07:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8392A2877B for ; Sat, 15 Jun 2019 10:07:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726500AbfFOKHe (ORCPT ); Sat, 15 Jun 2019 06:07:34 -0400 Received: from mail-wr1-f46.google.com ([209.85.221.46]:41441 "EHLO mail-wr1-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726327AbfFOKHd (ORCPT ); Sat, 15 Jun 2019 06:07:33 -0400 Received: by mail-wr1-f46.google.com with SMTP id c2so4958718wrm.8 for ; Sat, 15 Jun 2019 03:07:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SyVFC55PUGYeCqaeYnd9U0cJnl2EDBbPcu8akeBYGPM=; b=bnreQY/qAwhW+h/KnZSu4Chuqhyb5M2jv7EIk4KcisoqWxzma0KxOKSm/qWKshhCKY o3kEOlpoE0meOy3gPpYepYoDAh3OIY93fROmFGEmEdCg+PgbG1eZGtbMBAQ/eEWP7eRB o84H0PRxBb5E6ByfOvE89lNMkwteb4qjyzq6uV2+3YEJJo48or1B9qi/NLJU8XdRAstP FUdrGDCFCpY0DVYthActDhGWQ9lmxa8qwdopFbQsdQhPeZpqyncca0TQvlU1EVBMglXe +/C4s6bcgHTnvYLSzKJCUyJ0lTLUVK3Qds7CIO76r/DvHmwp3vbSYtDYq74D7YI4DU0k 883A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SyVFC55PUGYeCqaeYnd9U0cJnl2EDBbPcu8akeBYGPM=; b=kOCgct880IYD+jRj9Topre5df6u1rdl9FbqinkywI/Z0zZqyrUw+0XkjBtvnGSD22W lgaSXonF6n3QaSmwQe/SMmlKwfiu6uAZuc8YeRWsCN/YfJYPK/SrNEiXIOPoGNI6GLGz 7SGAOB9VY6/h35FdWGEqxcNBiIehXkCfwj+HS3BYhPyXCLaBFk4ZkWFHiQ+W92BLfivd scnDvfwWssvVuKHqh5xvlvZ2mVtPXrphkLZcYvMeqNRytw1gXMK5TCR6Ai4Bbm1FC29G daqSKg9GXwaCbsniujqNMGbiFndwzFZguf5dk6fAfgjyeM8s5AOMhs2J3i8wz4qVVvfd uAlQ== X-Gm-Message-State: APjAAAWKEh7KqQIVcXMduzWY1A2fOPH46I+A64oK7SR45jeaE0AB571z sgfbHUJOgZ64Ehu7oXfGkVqL+X5pWD0= X-Google-Smtp-Source: APXvYqyTExfIAazBXZ8ZKqk+dYKuKW8O8glU9fgcVDRqElx8N92XhphR7OUGvAcfhDL2yGSFzNqe4Q== X-Received: by 2002:adf:fa4c:: with SMTP id y12mr53450141wrr.282.1560593251093; Sat, 15 Jun 2019 03:07:31 -0700 (PDT) Received: from localhost.localdomain ([2a04:cec0:1017:d205:2143:78cf:c452:5744]) by smtp.gmail.com with ESMTPSA id x11sm8091576wmg.23.2019.06.15.03.07.29 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sat, 15 Jun 2019 03:07:30 -0700 (PDT) From: Christian Couder X-Google-Original-From: Christian Couder To: git@vger.kernel.org Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan , =?utf-8?q?SZEDER_G=C3=A1bor?= , Jeff King , Christian Couder , Christian Couder Subject: [PATCH v4 2/4] t: add t0016-oidmap.sh Date: Sat, 15 Jun 2019 12:07:00 +0200 Message-Id: <20190615100702.20762-3-chriscool@tuxfamily.org> X-Mailer: git-send-email 2.22.0.3.g82edbe9b01.dirty In-Reply-To: <20190615100702.20762-1-chriscool@tuxfamily.org> References: <20190615100702.20762-1-chriscool@tuxfamily.org> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Christian Couder Add actual tests for operations using `struct oidmap` from oidmap.{c,h}. Helped-by: SZEDER Gábor Helped-by: Jeff King Signed-off-by: Christian Couder --- t/t0016-oidmap.sh | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100755 t/t0016-oidmap.sh diff --git a/t/t0016-oidmap.sh b/t/t0016-oidmap.sh new file mode 100755 index 0000000000..af17264ce3 --- /dev/null +++ b/t/t0016-oidmap.sh @@ -0,0 +1,84 @@ +#!/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 '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 From patchwork Sat Jun 15 10:07:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Couder X-Patchwork-Id: 10997047 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 036B876 for ; Sat, 15 Jun 2019 10:07:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E78862877B for ; Sat, 15 Jun 2019 10:07:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DBE23287A9; Sat, 15 Jun 2019 10:07:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 880CF2877B for ; Sat, 15 Jun 2019 10:07:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726521AbfFOKHe (ORCPT ); Sat, 15 Jun 2019 06:07:34 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:40558 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725944AbfFOKHd (ORCPT ); Sat, 15 Jun 2019 06:07:33 -0400 Received: by mail-wr1-f68.google.com with SMTP id p11so4972535wre.7 for ; Sat, 15 Jun 2019 03:07:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hkKcsL9j/PSirfGj1G9mnA8V/6BZHp6lnji/hAv0xIo=; b=dvjDs7w1qDOKHFxKqhGkkgOe0K3Nau53rnBR9X/yvHkjMwg/rUHLvTapbjg8Q8shu8 IzQyWvMlO/mxEo3W0fshxcgoPRXlYXO/XBrisJaPitidfh4kJzBKg6bI+r6oBSs8Umxy o7QlsO3gztsU+x7n4aOZdSmw+59TmD8xXGRs5wfk9DMybwVtwskO/sizlF0DycWRS5/G wHs54Q885LDEtAqmCvNGmZy/SnbIM8IBQbAYfIR/WqBbwGEl1kRGasDKKLRVyk0d6yNF VMr30Iz7FvHYQLrSzNjjWUP8dAfCkLO8E1yXTmb9d2whd1NmlOtG6WJ9wAh0q5kzVOgb IdNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hkKcsL9j/PSirfGj1G9mnA8V/6BZHp6lnji/hAv0xIo=; b=h7Mv9ew08gDvB/fuX4naGKnIzsaprJbzgDtJe2yBLkIal1Sg+aQiwdq8EjQmAgDwXh 62t6pAJ5BDTvZDMWsUd25q1P1ZiWZVKyuhYU5bl8AAh+VQZU7CEFik7OeZU3vlkTa/OG qLiLZtpL1DQWzgZfOUfA43ZIFVXt4nYXNcjPO1iYBWAMgQF2IS0K+UxcGAKt1eGJaFPL iev4ttP3nYvpg0ii4x7wsqz3gYIdsuODYijFdWFZTn2DeS25gNCbzSfMhrOwSx6bhdJZ N18GeNKa+CURxLZAFAx5FL4Jaqhuej/kcywSYVUgTtJMidg2xu5n4ebWg91rL2S0eMwo 3elQ== X-Gm-Message-State: APjAAAWDeUyp+PHZUQdQGAGuOs5N6v25RpvjmqiCPoFJE8EBGolMRwST THcVuJWstqki85PVVtojbsupL2d/Pe0= X-Google-Smtp-Source: APXvYqz4VH9MXVvKxysU0iEU47S1sIQHBD4I601GkIFKIb/Pl4zVLVk7c9Ovt9o4AAKTpRzOnuwZdw== X-Received: by 2002:adf:e8d0:: with SMTP id k16mr55097836wrn.31.1560593252348; Sat, 15 Jun 2019 03:07:32 -0700 (PDT) Received: from localhost.localdomain ([2a04:cec0:1017:d205:2143:78cf:c452:5744]) by smtp.gmail.com with ESMTPSA id x11sm8091576wmg.23.2019.06.15.03.07.31 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sat, 15 Jun 2019 03:07:31 -0700 (PDT) From: Christian Couder X-Google-Original-From: Christian Couder To: git@vger.kernel.org Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan , =?utf-8?q?SZEDER_G=C3=A1bor?= , Jeff King , Christian Couder , Christian Couder Subject: [PATCH v4 3/4] oidmap: use sha1hash() instead of static hash() function Date: Sat, 15 Jun 2019 12:07:01 +0200 Message-Id: <20190615100702.20762-4-chriscool@tuxfamily.org> X-Mailer: git-send-email 2.22.0.3.g82edbe9b01.dirty In-Reply-To: <20190615100702.20762-1-chriscool@tuxfamily.org> References: <20190615100702.20762-1-chriscool@tuxfamily.org> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Christian Couder Get rid of the static hash() function in oidmap.c which is redundant with sha1hash(). Use sha1hash() directly instead. Let's be more consistent and not use several hash functions doing nearly exactly the same thing. Signed-off-by: Christian Couder --- oidmap.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/oidmap.c b/oidmap.c index b0841a0f58..01c206aaef 100644 --- 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, sha1hash(key->hash), 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, sha1hash(key->hash)); 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, sha1hash(to_put->oid.hash)); return hashmap_put(&map->map, to_put); } From patchwork Sat Jun 15 10:07:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Couder X-Patchwork-Id: 10997051 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AE39013AF for ; Sat, 15 Jun 2019 10:07:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F3512877B for ; Sat, 15 Jun 2019 10:07:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 934F2287A9; Sat, 15 Jun 2019 10:07:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 385612877B for ; Sat, 15 Jun 2019 10:07:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726524AbfFOKHh (ORCPT ); Sat, 15 Jun 2019 06:07:37 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:39005 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726452AbfFOKHg (ORCPT ); Sat, 15 Jun 2019 06:07:36 -0400 Received: by mail-wr1-f65.google.com with SMTP id x4so4971863wrt.6 for ; Sat, 15 Jun 2019 03:07:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LiUqHXDjxSKD/+MwLSo6BGNIeP2ud7iV2M365NojWDY=; b=BOSyERTVSknhCp8LNEHL0Afwq9bR0hos1ht35CgajdSBgwi/r62Xe7ne4cNmc4A6SL m8GkIWCG05a6rL6x4J9FZbG7mt0LUmTzI9AZ1+C2TwlhR0NDeLBpP1ZZEBca4gQwDs3x drjf55q8/9zlQWftnBTafROH3VxrS8rCFPOinMbZqhI30QwnkEdg1+Q/P3sdzgHAyb1c LXBuzMc8/gfwaaroFXN0ZWBXLx0P6X13pvH7f4FRCCTWv469U8Bz6a8oT23buKk37puV qOxZSabThZgzxCG9wsSSDJ+pE3XlY6Q0LYZvFecOKqLBb/khLDv0m3zUkdFYXxbpEOZv JfBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LiUqHXDjxSKD/+MwLSo6BGNIeP2ud7iV2M365NojWDY=; b=SS9mDDaF8Mv7lRAhbt655LwrXPboIgWdpF56qcdXY04WwSMjxjYZfga2l/QCj59pkm TGW0lcbEZMbP34JmnR/RO2bfBxg42eXszW4F5NtdNWoHkVXW5Daqcd9M5YxfOIRTn7/c s7rWWYvhKJeYDU68w51jTQEZJHZf3q6VhZHKOdEPlrfqxHRcnipm4eA+2c0E1eFwss/1 pAlfqnKBqdEL1V948GNvo43qgvS41X1UV/DxGKvATMUuitb0DI3x4JhrOesCembqO5sV DRMYH4pIhY7gsqb25NY69rvTDcGg0k7hPCas4FPaO6Ul8eS/p1DL06lIVBu6E1/CCrCz tgIg== X-Gm-Message-State: APjAAAXRv8dnIhLhFGqNXUh/iuyiHj0SieB0CJuuVi5D2l16SAlIIkPw aaDGX95PrQL0VoFZQZL23g4bMTjG9G0= X-Google-Smtp-Source: APXvYqxnvCmTj1Kke0qT2JdwBEeoS5SdrqcupxGkb2PBV9mxQb8mI5otnYg8/o8OprAmuuGDxYAlzw== X-Received: by 2002:a5d:6352:: with SMTP id b18mr19567095wrw.6.1560593253630; Sat, 15 Jun 2019 03:07:33 -0700 (PDT) Received: from localhost.localdomain ([2a04:cec0:1017:d205:2143:78cf:c452:5744]) by smtp.gmail.com with ESMTPSA id x11sm8091576wmg.23.2019.06.15.03.07.32 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Sat, 15 Jun 2019 03:07:33 -0700 (PDT) From: Christian Couder X-Google-Original-From: Christian Couder To: git@vger.kernel.org Cc: Junio C Hamano , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBC?= =?utf-8?b?amFybWFzb24=?= , Jonathan Tan , =?utf-8?q?SZEDER_G=C3=A1bor?= , Jeff King , Christian Couder Subject: [PATCH v4 4/4] test-hashmap: remove 'hash' command Date: Sat, 15 Jun 2019 12:07:02 +0200 Message-Id: <20190615100702.20762-5-chriscool@tuxfamily.org> X-Mailer: git-send-email 2.22.0.3.g82edbe9b01.dirty In-Reply-To: <20190615100702.20762-1-chriscool@tuxfamily.org> References: <20190615100702.20762-1-chriscool@tuxfamily.org> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If hashes like strhash() are updated, for example to use a different hash algorithm, we should not have to be updating t0011 to change out the hashes. As long as hashmap can store and retrieve values, and that it performs well, we should not care what are the values of the hashes. Let's just focus on the externally visible behavior instead. Suggested-by: Jeff King Signed-off-by: Christian Couder --- t/helper/test-hashmap.c | 9 +-------- t/t0011-hashmap.sh | 9 --------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c index 23d2b172fe..aaf17b0ddf 100644 --- a/t/helper/test-hashmap.c +++ b/t/helper/test-hashmap.c @@ -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/t0011-hashmap.sh b/t/t0011-hashmap.sh index 3f1f505e89..9c96b3e3b1 100755 --- a/t/t0011-hashmap.sh +++ b/t/t0011-hashmap.sh @@ -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