2 * Copyright (C) 2009-2011 Free Software Foundation, Inc.
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/>. */
33 #define STREQ(a, b) (strcmp (a, b) == 0)
34 #define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
37 hash_compare_strings (void const *x, void const *y)
40 return STREQ (x, y) ? true : false;
50 insert_new (Hash_table *ht, const void *ent)
52 void *e = hash_insert (ht, ent);
57 walk (void *ent, void *data)
60 unsigned int *map = data;
63 case 'a': *map |= 1; return true;
64 case 'b': *map |= 2; return true;
65 case 'c': *map |= 4; return true;
72 get_seed (char const *str, unsigned int *seed)
74 size_t len = strlen (str);
75 if (len == 0 || strspn (str, "0123456789") != len || 10 < len)
83 main (int argc, char **argv)
87 unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53};
91 hash_reset_tuning (&tuning);
92 tuning.shrink_threshold = 0.3;
93 tuning.shrink_factor = 0.707;
94 tuning.growth_threshold = 1.5;
95 tuning.growth_factor = 2.0;
96 tuning.is_n_buckets = true;
101 if (get_seed (argv[1], &seed) != 0)
103 fprintf (stderr, "invalid seed: %s\n", argv[1]);
110 for (i = 0; i < ARRAY_CARDINALITY (table_size); i++)
112 size_t sz = table_size[i];
113 ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
115 insert_new (ht, "a");
117 char *str1 = xstrdup ("a");
118 char *str2 = hash_insert (ht, str1);
119 ASSERT (str1 != str2);
120 ASSERT (STREQ (str1, str2));
123 insert_new (ht, "b");
124 insert_new (ht, "c");
126 ASSERT (hash_do_for_each (ht, walk, &i) == 3);
129 void *buf[5] = { NULL };
130 ASSERT (hash_get_entries (ht, NULL, 0) == 0);
131 ASSERT (hash_get_entries (ht, buf, 5) == 3);
132 ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
134 ASSERT (hash_delete (ht, "a"));
135 ASSERT (hash_delete (ht, "a") == NULL);
136 ASSERT (hash_delete (ht, "b"));
137 ASSERT (hash_delete (ht, "c"));
139 ASSERT (hash_rehash (ht, 47));
140 ASSERT (hash_rehash (ht, 467));
142 /* Free an empty table. */
146 ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
149 insert_new (ht, "z");
150 insert_new (ht, "y");
151 insert_new (ht, "x");
152 insert_new (ht, "w");
153 insert_new (ht, "v");
154 insert_new (ht, "u");
157 ASSERT (hash_get_n_entries (ht) == 0);
160 /* Test pointer hashing. */
161 ht = hash_initialize (sz, NULL, NULL, NULL, NULL);
164 char *str = xstrdup ("a");
165 insert_new (ht, "a");
166 insert_new (ht, str);
167 ASSERT (hash_lookup (ht, str) == str);
173 hash_reset_tuning (&tuning);
174 tuning.shrink_threshold = 0.3;
175 tuning.shrink_factor = 0.707;
176 tuning.growth_threshold = 1.5;
177 tuning.growth_factor = 2.0;
178 tuning.is_n_buckets = true;
179 /* Invalid tuning. */
180 ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
184 /* Alternate tuning. */
185 tuning.growth_threshold = 0.89;
187 /* Run with default tuning, then with custom tuning settings. */
188 for (k = 0; k < 2; k++)
190 Hash_tuning const *tune = (k == 0 ? NULL : &tuning);
191 /* Now, each entry is malloc'd. */
192 ht = hash_initialize (4651, tune, hash_pjw,
193 hash_compare_strings, hash_freer);
195 for (i = 0; i < 10000; i++)
197 unsigned int op = rand () % 10;
208 char const *p = uinttostr (i, buf);
209 insert_new (ht, xstrdup (p));
215 size_t n = hash_get_n_entries (ht);
216 ASSERT (hash_rehash (ht, n + rand () % 20));
222 size_t n = hash_get_n_entries (ht);
223 size_t delta = rand () % 20;
225 ASSERT (hash_rehash (ht, n - delta));
232 /* Delete a random entry. */
233 size_t n = hash_get_n_entries (ht);
236 size_t kk = rand () % n;
239 for (p = hash_get_first (ht); kk;
240 --kk, p = hash_get_next (ht, p))
245 v = hash_delete (ht, p);
252 ASSERT (hash_table_ok (ht));