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)
33 #define ASSERT(expr) \
38 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
46 hash_compare_strings (void const *x, void const *y)
48 return STREQ (x, y) ? true : false;
58 insert_new (Hash_table *ht, void *ent)
60 void *e = hash_insert (ht, ent);
65 walk (void *ent, void *data)
68 unsigned int *map = data;
71 case 'a': *map |= 1; return true;
72 case 'b': *map |= 2; return true;
73 case 'c': *map |= 4; return true;
83 Hash_table *ht = hash_initialize (53, NULL, hash_pjw,
84 hash_compare_strings, NULL);
90 char *str1 = xstrdup ("a");
91 char *str2 = hash_insert (ht, str1);
92 ASSERT (str1 != str2);
93 ASSERT (STREQ (str1, str2));
99 ASSERT (hash_do_for_each (ht, walk, &i) == 3);
102 void *buf[5] = { NULL };
103 ASSERT (hash_get_entries (ht, NULL, 0) == 0);
104 ASSERT (hash_get_entries (ht, buf, 5) == 3);
105 ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
107 ASSERT (hash_delete (ht, "a"));
108 ASSERT (hash_delete (ht, "a") == NULL);
109 ASSERT (hash_delete (ht, "b"));
110 ASSERT (hash_delete (ht, "c"));
112 ASSERT (hash_rehash (ht, 47));
113 ASSERT (hash_rehash (ht, 467));
115 /* Free an empty table. */
119 ht = hash_initialize (53, NULL, hash_pjw, hash_compare_strings, NULL);
122 insert_new (ht, "z");
123 insert_new (ht, "y");
124 insert_new (ht, "x");
125 insert_new (ht, "w");
126 insert_new (ht, "v");
127 insert_new (ht, "u");
130 ASSERT (hash_get_n_entries (ht) == 0);
133 /* Now, each entry is malloc'd. */
134 ht = hash_initialize (4651, NULL, hash_pjw, hash_compare_strings, hash_freer);
136 for (i = 0; i < 10000; i++)
138 unsigned int op = rand () % 10;
149 char const *p = uinttostr (i, buf);
150 insert_new (ht, xstrdup (p));
156 size_t n = hash_get_n_entries (ht);
157 ASSERT (hash_rehash (ht, n + rand () % 20));
163 size_t n = hash_get_n_entries (ht);
164 size_t delta = rand () % 20;
166 ASSERT (hash_rehash (ht, n - delta));
173 /* Delete a random entry. */
174 size_t n = hash_get_n_entries (ht);
177 size_t k = rand () % n;
180 for (p = hash_get_first (ht); k; --k, p = hash_get_next (ht, p))
185 v = hash_delete (ht, p);
192 ASSERT (hash_table_ok (ht));
197 hash_reset_tuning (&tuning);
198 tuning.shrink_threshold = 0.3;
199 tuning.shrink_factor = 0.707;
200 tuning.growth_threshold = 1.5;
201 tuning.growth_factor = 2.0;
202 tuning.is_n_buckets = true;
203 /* Invalid tuning. */
204 ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
208 /* Alternate tuning. */
209 tuning.growth_threshold = 0.89;
210 ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
213 for (i = 0; i < 10000; i++)
215 unsigned int op = rand () % 10;
226 char const *p = uinttostr (i, buf);
227 insert_new (ht, xstrdup (p));
233 size_t n = hash_get_n_entries (ht);
234 ASSERT (hash_rehash (ht, n + rand () % 20));
240 size_t n = hash_get_n_entries (ht);
241 size_t delta = rand () % 20;
243 ASSERT (hash_rehash (ht, n - delta));
250 /* Delete a random entry. */
251 size_t n = hash_get_n_entries (ht);
254 size_t k = rand () % n;
257 for (p = hash_get_first (ht); k; --k, p = hash_get_next (ht, p))
262 v = hash_delete (ht, p);
269 ASSERT (hash_table_ok (ht));