tests: test-hash: allow seed selection via a command line argument
authorJim Meyering <meyering@redhat.com>
Fri, 19 Jun 2009 14:56:48 +0000 (16:56 +0200)
committerJim Meyering <meyering@redhat.com>
Fri, 19 Jun 2009 14:56:48 +0000 (16:56 +0200)
* tests/test-hash.c (get_seed): New function.
(main): Use it.

ChangeLog
tests/test-hash.c

index 841eba4..876467c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-06-19  Jim Meyering  <meyering@redhat.com>
+
+       tests: test-hash: allow seed selection via a command line argument
+       * tests/test-hash.c (get_seed): New function.
+       (main): Use it.
+
 2009-06-19  Eric Blake  <ebb9@byu.net>
 
        hash: avoid memory leak on allocation failure
index 6bb9652..83ffdf7 100644 (file)
@@ -78,14 +78,37 @@ walk (void *ent, void *data)
   return false;
 }
 
+static int
+get_seed (char const *str, unsigned int *seed)
+{
+  size_t len = strlen (str);
+  if (len == 0 || strspn (str, "0123456789") != len || 10 < len)
+    return 1;
+
+  *seed = atoi (str);
+  return 0;
+}
+
 int
-main (void)
+main (int argc, char **argv)
 {
   unsigned int i;
   unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53};
   Hash_table *ht;
   Hash_tuning tuning;
 
+  if (1 < argc)
+    {
+      unsigned int seed;
+      if (get_seed (argv[1], &seed) != 0)
+       {
+         fprintf (stderr, "invalid seed: %s\n", argv[1]);
+         exit (EXIT_FAILURE);
+       }
+
+      srand (seed);
+    }
+
   for (i = 0; i < ARRAY_CARDINALITY (table_size); i++)
     {
       size_t sz = table_size[i];