From ae156c0bf8058d3c1568c1ed573a1319a451ac7e Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 18 Jun 2009 07:36:54 +0200 Subject: [PATCH] hash-tests: add a loop around the small tests * tests/test-hash.c (main): Repeat small tests with selected small initial table sizes. --- ChangeLog | 6 ++++ tests/test-hash.c | 90 +++++++++++++++++++++++++++++-------------------------- 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/ChangeLog b/ChangeLog index e69542871..1d69f2292 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2009-06-18 Jim Meyering + + 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 hash: minor cleanups diff --git a/tests/test-hash.c b/tests/test-hash.c index e7066c04e..2266545cb 100644 --- a/tests/test-hash.c +++ b/tests/test-hash.c @@ -29,6 +29,7 @@ #include #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); -- 2.11.0