about summary refs log tree commit diff
path: root/nss/tst-nss-hash.c
diff options
context:
space:
mode:
authorNoah Goldstein <goldstein.w.n@gmail.com>2022-05-19 17:18:00 -0500
committerNoah Goldstein <goldstein.w.n@gmail.com>2022-05-23 10:38:40 -0500
commit5f2f0f69777a9781ac65b5619b2abedd88703be2 (patch)
treea8da06889f3feab2a56983f002722dc82d787fe0 /nss/tst-nss-hash.c
parent6fd435485f270a8c76e47bf7377c1b872885e683 (diff)
downloadglibc-5f2f0f69777a9781ac65b5619b2abedd88703be2.tar.gz
glibc-5f2f0f69777a9781ac65b5619b2abedd88703be2.tar.xz
glibc-5f2f0f69777a9781ac65b5619b2abedd88703be2.zip
nss: Add tests for the nss_hash in nss_hash.h
If we want to further optimize the function tests are needed.
Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Diffstat (limited to 'nss/tst-nss-hash.c')
-rw-r--r--nss/tst-nss-hash.c80
1 files changed, 80 insertions, 0 deletions
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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/support.h>
+#include <support/check.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <nss.h>
+#include <simple-nss-hash.h>
+
+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 <support/test-driver.c>