hash-tests: add a loop around the small tests
authorJim Meyering <meyering@redhat.com>
Thu, 18 Jun 2009 05:36:54 +0000 (07:36 +0200)
committerJim Meyering <meyering@redhat.com>
Thu, 18 Jun 2009 07:12:01 +0000 (09:12 +0200)
* tests/test-hash.c (main): Repeat small tests with selected
small initial table sizes.

ChangeLog
tests/test-hash.c

index e695428..1d69f22 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-06-18  Jim Meyering  <meyering@redhat.com>
+
+       hash-tests: add a loop around the small tests
+       * tests/test-hash.c (main): Repeat small tests with selected
+       small initial table sizes.
+
 2009-06-17  Eric Blake  <ebb9@byu.net>
 
        hash: minor cleanups
index e7066c0..2266545 100644 (file)
@@ -29,6 +29,7 @@
 #include <unistd.h>
 
 #define STREQ(a, b) (strcmp (a, b) == 0)
+#define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
 
 #define ASSERT(expr) \
   do                                                                        \
@@ -80,55 +81,60 @@ int
 main (void)
 {
   unsigned int i;
-  Hash_table *ht = hash_initialize (53, NULL, hash_pjw,
-                                   hash_compare_strings, NULL);
+  unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53};
+  Hash_table *ht;
   Hash_tuning tuning;
 
-  ASSERT (ht);
-  insert_new (ht, "a");
-  {
-    char *str1 = xstrdup ("a");
-    char *str2 = hash_insert (ht, str1);
-    ASSERT (str1 != str2);
-    ASSERT (STREQ (str1, str2));
-    free (str1);
-  }
-  insert_new (ht, "b");
-  insert_new (ht, "c");
-  i = 0;
-  ASSERT (hash_do_for_each (ht, walk, &i) == 3);
-  ASSERT (i == 7);
-  {
-    void *buf[5] = { NULL };
-    ASSERT (hash_get_entries (ht, NULL, 0) == 0);
-    ASSERT (hash_get_entries (ht, buf, 5) == 3);
-    ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
-  }
-  ASSERT (hash_delete (ht, "a"));
-  ASSERT (hash_delete (ht, "a") == NULL);
-  ASSERT (hash_delete (ht, "b"));
-  ASSERT (hash_delete (ht, "c"));
+  for (i = 0; i < ARRAY_CARDINALITY (table_size); i++)
+    {
+      size_t sz = table_size[i];
+      ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
+      ASSERT (ht);
+      insert_new (ht, "a");
+      {
+       char *str1 = xstrdup ("a");
+       char *str2 = hash_insert (ht, str1);
+       ASSERT (str1 != str2);
+       ASSERT (STREQ (str1, str2));
+       free (str1);
+      }
+      insert_new (ht, "b");
+      insert_new (ht, "c");
+      i = 0;
+      ASSERT (hash_do_for_each (ht, walk, &i) == 3);
+      ASSERT (i == 7);
+      {
+       void *buf[5] = { NULL };
+       ASSERT (hash_get_entries (ht, NULL, 0) == 0);
+       ASSERT (hash_get_entries (ht, buf, 5) == 3);
+       ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
+      }
+      ASSERT (hash_delete (ht, "a"));
+      ASSERT (hash_delete (ht, "a") == NULL);
+      ASSERT (hash_delete (ht, "b"));
+      ASSERT (hash_delete (ht, "c"));
 
-  ASSERT (hash_rehash (ht, 47));
-  ASSERT (hash_rehash (ht, 467));
+      ASSERT (hash_rehash (ht, 47));
+      ASSERT (hash_rehash (ht, 467));
 
-  /* Free an empty table. */
-  hash_clear (ht);
-  hash_free (ht);
+      /* Free an empty table. */
+      hash_clear (ht);
+      hash_free (ht);
 
-  ht = hash_initialize (53, NULL, hash_pjw, hash_compare_strings, NULL);
-  ASSERT (ht);
+      ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
+      ASSERT (ht);
 
-  insert_new (ht, "z");
-  insert_new (ht, "y");
-  insert_new (ht, "x");
-  insert_new (ht, "w");
-  insert_new (ht, "v");
-  insert_new (ht, "u");
+      insert_new (ht, "z");
+      insert_new (ht, "y");
+      insert_new (ht, "x");
+      insert_new (ht, "w");
+      insert_new (ht, "v");
+      insert_new (ht, "u");
 
-  hash_clear (ht);
-  ASSERT (hash_get_n_entries (ht) == 0);
-  hash_free (ht);
+      hash_clear (ht);
+      ASSERT (hash_get_n_entries (ht) == 0);
+      hash_free (ht);
+    }
 
   /* Now, each entry is malloc'd.  */
   ht = hash_initialize (4651, NULL, hash_pjw, hash_compare_strings, hash_freer);