changeset 11632:5439d2ea6789

hash-tests: add a loop around the small tests * tests/test-hash.c (main): Repeat small tests with selected small initial table sizes.
author Jim Meyering <meyering@redhat.com>
date Thu, 18 Jun 2009 07:36:54 +0200
parents b7e9a8512f19
children e62851e0bf87
files ChangeLog tests/test-hash.c
diffstat 2 files changed, 54 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- 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
--- a/tests/test-hash.c
+++ b/tests/test-hash.c
@@ -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 @@
 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);