From patchwork Wed May 18 17:26:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Noah Goldstein X-Patchwork-Id: 54171 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 570B33858025 for ; Wed, 18 May 2022 17:28:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 570B33858025 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1652894906; bh=IlP377vkGfDJz97lj2ZXisxM22IRGQLxg3xkwKA4urQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=gadmmf2mJeBjN3xw8lx80lKdnf3LBdiev7xPD0JFtZ+nYGEOc846BXPrKuJncVHcc 1aRkfWXBU6n5xf4wT8XiRhP2SP7Op0OpPGJpopxZmE+TPiKb2veErVCTmjEP2LdzIs +vJoV0D/I0lpNVIExoev1O/RjELkI0CYVEx6nAeA= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by sourceware.org (Postfix) with ESMTPS id D517A3858025 for ; Wed, 18 May 2022 17:26:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D517A3858025 Received: by mail-io1-xd2c.google.com with SMTP id m6so3054490iob.4 for ; Wed, 18 May 2022 10:26:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IlP377vkGfDJz97lj2ZXisxM22IRGQLxg3xkwKA4urQ=; b=SxxW/cKWowYkqN9Ux1CdfAgJahD7lgTAcrFTqSCOXYWNkOS5dEPSJXUSLeTzh11XcV iwiGIfchSBDvrqhIu+6mBdn2jwbgCVmKF5eV2HHDTKMfIBuzn+WzAMTf4GPIhd+hKsSv 2oYCjCZpkZ+orFQ/gjueT9Nou87HZBmSQmYunR+2TSbsqKXbezQgqKrH2gh8gPcXoh2n eaohxX4gzx5u8VqDgWd8E7wgAjiyC60ob1XhnckQEwJmNnDb/lSeDbu1XJGk5DfeaYFJ vJjfFY9LbwZpPP0UeXXl3Xa3k3ikq4Al0AVmZD/dRFkZjYckRK+22nXfRBgTAwLm35AZ dsYA== X-Gm-Message-State: AOAM53153sHqZ9CaSbr+SkWrGNMkp5KF3DAeeZhW7xVRtjhZ1tTPdG0B HCEmmTUmLq2rH8/rWB/lEQyhGXivzCQ= X-Google-Smtp-Source: ABdhPJzMoSvhdip06tMh9rOW5a3dIga3SyHhNhJxuiy6+4NtOqnRpXNKuk0zV325c9SE/f5Cq/bkjw== X-Received: by 2002:a05:6638:3e9a:b0:32d:b131:8dbd with SMTP id ch26-20020a0566383e9a00b0032db1318dbdmr298993jab.265.1652894803005; Wed, 18 May 2022 10:26:43 -0700 (PDT) Received: from noah-tgl.. (node-17-161.flex.volo.net. [76.191.17.161]) by smtp.gmail.com with ESMTPSA id r2-20020a02b102000000b0032b3a78179fsm18598jah.99.2022.05.18.10.26.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 May 2022 10:26:42 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v10 3/6] nss: Add tests for the nss_hash in nss_hash.h Date: Wed, 18 May 2022 12:26:32 -0500 Message-Id: <20220518172635.291119-3-goldstein.w.n@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220518172635.291119-1-goldstein.w.n@gmail.com> References: <20220414041231.926415-1-goldstein.w.n@gmail.com> <20220518172635.291119-1-goldstein.w.n@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Noah Goldstein via Libc-alpha From: Noah Goldstein Reply-To: Noah Goldstein Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" If we want to further optimize the function tests are needed. Reviewed-by: Siddhesh Poyarekar --- nss/Makefile | 1 + nss/nss_hash.c | 16 +++++++++ nss/simple-nss-hash.h | 42 +++++++++++++++++++++++ nss/tst-nss-hash.c | 80 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 nss/simple-nss-hash.h create mode 100644 nss/tst-nss-hash.c diff --git a/nss/Makefile b/nss/Makefile index d8b06b44fb..a978e3927a 100644 --- a/nss/Makefile +++ b/nss/Makefile @@ -62,6 +62,7 @@ tests := \ test-digits-dots \ test-netdb \ tst-nss-getpwent \ + tst-nss-hash \ tst-nss-test1 \ tst-nss-test2 \ tst-nss-test4 \ diff --git a/nss/nss_hash.c b/nss/nss_hash.c index 27a348ea9b..f9e17d068a 100644 --- a/nss/nss_hash.c +++ b/nss/nss_hash.c @@ -75,4 +75,20 @@ __nss_hash (const void *keyarg, size_t len) return h; } +/* For testing/benchmarking purposes. */ +static uint32_t +__simple_nss_hash (const void *keyarg, size_t len) +{ + const unsigned char *key; + size_t i; + uint32_t h = 0; + key = keyarg; + + for (i = 0; i < len; ++i) + h = *key++ + 65599 * h; + + return h; +} + + libc_hidden_def (__nss_hash) diff --git a/nss/simple-nss-hash.h b/nss/simple-nss-hash.h new file mode 100644 index 0000000000..47708972e7 --- /dev/null +++ b/nss/simple-nss-hash.h @@ -0,0 +1,42 @@ +/* __simple_nss_hash for testing nss_hash function + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _SIMPLE_NSS_HASH_H +#define _SIMPLE_NSS_HASH_H 1 + +#include + +/* For testing/benchmarking purposes. Real implementation in + nss/nss_hash.c. */ +static uint32_t +__attribute__ ((unused)) +__simple_nss_hash (const void *keyarg, size_t len) +{ + const unsigned char *key; + size_t i; + uint32_t h = 0; + key = keyarg; + + for (i = 0; i < len; ++i) + h = *key++ + 65599 * h; + + return h; +} + + +#endif /* simple-nss-hash.h */ diff --git a/nss/tst-nss-hash.c b/nss/tst-nss-hash.c new file mode 100644 index 0000000000..5ec1f9b0c5 --- /dev/null +++ b/nss/tst-nss-hash.c @@ -0,0 +1,80 @@ +/* Test __nss_hash + Copyright (C) 2022 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include + +uint32_t __nss_hash (const void *__key, size_t __length); + +static int +do_fill_tests (size_t len, int fill) +{ + uint32_t expec, res; + char buf[len]; + memset (buf, fill, len); + + expec = __simple_nss_hash (buf, len); + res = __nss_hash (buf, len); + if (expec != res) + FAIL_EXIT1 ("FAIL: fill(%d) (%zu), %x != %x\n", fill, len, expec, res); + + return 0; +} + +static int +do_rand_tests (size_t len) +{ + uint32_t expec, res; + size_t i; + char buf[len]; + for (i = 0; i < len; ++i) + buf[i] = random (); + + expec = __simple_nss_hash (buf, len); + res = __nss_hash (buf, len); + if (expec != res) + FAIL_EXIT1 ("FAIL: random (%zu), %x != %x\n", len, expec, res); + + return 0; +} + +static int +do_test (void) +{ + size_t i, j; + for (i = 0; i < 100; ++i) + { + for (j = 0; j < 8192; ++j) + { + if (do_rand_tests (i)) + return 1; + + if (do_fill_tests (i, -1) || do_fill_tests (i, 1) + || do_fill_tests (i, 0x80) || do_fill_tests (i, 0x88)) + return 1; + } + } + return 0; +} + +#include