2 * Copyright (C) 2009 Free Software Foundation
3 * Written by Jim Meyering
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #define STREQ(a, b) (strcmp (a, b) == 0)
32 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
34 #define ASSERT(expr) \
39 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
47 hash_compare_strings (void const *x, void const *y)
50 return STREQ (x, y) ? true : false;
60 insert_new (Hash_table *ht, void *ent)
62 void *e = hash_insert (ht, ent);
67 walk (void *ent, void *data)
70 unsigned int *map = data;
73 case 'a': *map |= 1; return true;
74 case 'b': *map |= 2; return true;
75 case 'c': *map |= 4; return true;
82 get_seed (char const *str, unsigned int *seed)
84 size_t len = strlen (str);
85 if (len == 0 || strspn (str, "0123456789") != len || 10 < len)
93 main (int argc, char **argv)
96 unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53};
103 if (get_seed (argv[1], &seed) != 0)
105 fprintf (stderr, "invalid seed: %s\n", argv[1]);
112 for (i = 0; i < ARRAY_CARDINALITY (table_size); i++)
114 size_t sz = table_size[i];
115 ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
117 insert_new (ht, "a");
119 char *str1 = xstrdup ("a");
120 char *str2 = hash_insert (ht, str1);
121 ASSERT (str1 != str2);
122 ASSERT (STREQ (str1, str2));
125 insert_new (ht, "b");
126 insert_new (ht, "c");
128 ASSERT (hash_do_for_each (ht, walk, &i) == 3);
131 void *buf[5] = { NULL };
132 ASSERT (hash_get_entries (ht, NULL, 0) == 0);
133 ASSERT (hash_get_entries (ht, buf, 5) == 3);
134 ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
136 ASSERT (hash_delete (ht, "a"));
137 ASSERT (hash_delete (ht, "a") == NULL);
138 ASSERT (hash_delete (ht, "b"));
139 ASSERT (hash_delete (ht, "c"));
141 ASSERT (hash_rehash (ht, 47));
142 ASSERT (hash_rehash (ht, 467));
144 /* Free an empty table. */
148 ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
151 insert_new (ht, "z");
152 insert_new (ht, "y");
153 insert_new (ht, "x");
154 insert_new (ht, "w");
155 insert_new (ht, "v");
156 insert_new (ht, "u");
159 ASSERT (hash_get_n_entries (ht) == 0);
162 /* Test pointer hashing. */
163 ht = hash_initialize (sz, NULL, NULL, NULL, NULL);
166 char *str = xstrdup ("a");
167 insert_new (ht, "a");
168 insert_new (ht, str);
169 ASSERT (hash_lookup (ht, str) == str);
175 /* Now, each entry is malloc'd. */
176 ht = hash_initialize (4651, NULL, hash_pjw, hash_compare_strings, hash_freer);
178 for (i = 0; i < 10000; i++)
180 unsigned int op = rand () % 10;
191 char const *p = uinttostr (i, buf);
192 insert_new (ht, xstrdup (p));
198 size_t n = hash_get_n_entries (ht);
199 ASSERT (hash_rehash (ht, n + rand () % 20));
205 size_t n = hash_get_n_entries (ht);
206 size_t delta = rand () % 20;
208 ASSERT (hash_rehash (ht, n - delta));
215 /* Delete a random entry. */
216 size_t n = hash_get_n_entries (ht);
219 size_t k = rand () % n;
222 for (p = hash_get_first (ht); k; --k, p = hash_get_next (ht, p))
227 v = hash_delete (ht, p);
234 ASSERT (hash_table_ok (ht));
239 hash_reset_tuning (&tuning);
240 tuning.shrink_threshold = 0.3;
241 tuning.shrink_factor = 0.707;
242 tuning.growth_threshold = 1.5;
243 tuning.growth_factor = 2.0;
244 tuning.is_n_buckets = true;
245 /* Invalid tuning. */
246 ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
250 /* Alternate tuning. */
251 tuning.growth_threshold = 0.89;
252 ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
255 for (i = 0; i < 10000; i++)
257 unsigned int op = rand () % 10;
268 char const *p = uinttostr (i, buf);
269 insert_new (ht, xstrdup (p));
275 size_t n = hash_get_n_entries (ht);
276 ASSERT (hash_rehash (ht, n + rand () % 20));
282 size_t n = hash_get_n_entries (ht);
283 size_t delta = rand () % 20;
285 ASSERT (hash_rehash (ht, n - delta));
292 /* Delete a random entry. */
293 size_t n = hash_get_n_entries (ht);
296 size_t k = rand () % n;
299 for (p = hash_get_first (ht); k; --k, p = hash_get_next (ht, p))
304 v = hash_delete (ht, p);
311 ASSERT (hash_table_ok (ht));