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;
85 unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53};
89 for (i = 0; i < ARRAY_CARDINALITY (table_size); i++)
91 size_t sz = table_size[i];
92 ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
96 char *str1 = xstrdup ("a");
97 char *str2 = hash_insert (ht, str1);
98 ASSERT (str1 != str2);
99 ASSERT (STREQ (str1, str2));
102 insert_new (ht, "b");
103 insert_new (ht, "c");
105 ASSERT (hash_do_for_each (ht, walk, &i) == 3);
108 void *buf[5] = { NULL };
109 ASSERT (hash_get_entries (ht, NULL, 0) == 0);
110 ASSERT (hash_get_entries (ht, buf, 5) == 3);
111 ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
113 ASSERT (hash_delete (ht, "a"));
114 ASSERT (hash_delete (ht, "a") == NULL);
115 ASSERT (hash_delete (ht, "b"));
116 ASSERT (hash_delete (ht, "c"));
118 ASSERT (hash_rehash (ht, 47));
119 ASSERT (hash_rehash (ht, 467));
121 /* Free an empty table. */
125 ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
128 insert_new (ht, "z");
129 insert_new (ht, "y");
130 insert_new (ht, "x");
131 insert_new (ht, "w");
132 insert_new (ht, "v");
133 insert_new (ht, "u");
136 ASSERT (hash_get_n_entries (ht) == 0);
139 /* Test pointer hashing. */
140 ht = hash_initialize (sz, NULL, NULL, NULL, NULL);
143 char *str = xstrdup ("a");
144 insert_new (ht, "a");
145 insert_new (ht, str);
146 ASSERT (hash_lookup (ht, str) == str);
152 /* Now, each entry is malloc'd. */
153 ht = hash_initialize (4651, NULL, hash_pjw, hash_compare_strings, hash_freer);
155 for (i = 0; i < 10000; i++)
157 unsigned int op = rand () % 10;
168 char const *p = uinttostr (i, buf);
169 insert_new (ht, xstrdup (p));
175 size_t n = hash_get_n_entries (ht);
176 ASSERT (hash_rehash (ht, n + rand () % 20));
182 size_t n = hash_get_n_entries (ht);
183 size_t delta = rand () % 20;
185 ASSERT (hash_rehash (ht, n - delta));
192 /* Delete a random entry. */
193 size_t n = hash_get_n_entries (ht);
196 size_t k = rand () % n;
199 for (p = hash_get_first (ht); k; --k, p = hash_get_next (ht, p))
204 v = hash_delete (ht, p);
211 ASSERT (hash_table_ok (ht));
216 hash_reset_tuning (&tuning);
217 tuning.shrink_threshold = 0.3;
218 tuning.shrink_factor = 0.707;
219 tuning.growth_threshold = 1.5;
220 tuning.growth_factor = 2.0;
221 tuning.is_n_buckets = true;
222 /* Invalid tuning. */
223 ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
227 /* Alternate tuning. */
228 tuning.growth_threshold = 0.89;
229 ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
232 for (i = 0; i < 10000; i++)
234 unsigned int op = rand () % 10;
245 char const *p = uinttostr (i, buf);
246 insert_new (ht, xstrdup (p));
252 size_t n = hash_get_n_entries (ht);
253 ASSERT (hash_rehash (ht, n + rand () % 20));
259 size_t n = hash_get_n_entries (ht);
260 size_t delta = rand () % 20;
262 ASSERT (hash_rehash (ht, n - delta));
269 /* Delete a random entry. */
270 size_t n = hash_get_n_entries (ht);
273 size_t k = rand () % n;
276 for (p = hash_get_first (ht); k; --k, p = hash_get_next (ht, p))
281 v = hash_delete (ht, p);
288 ASSERT (hash_table_ok (ht));