1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1999 The Free Software Foundation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 #undef G_DISABLE_ASSERT
48 fill_hash_table_and_array (GHashTable *hash_table)
52 for (i = 0; i < 10000; i++)
55 g_hash_table_insert (hash_table, &array[i], &array[i]);
60 init_result_array (int result_array[10000])
64 for (i = 0; i < 10000; i++)
69 verify_result_array (int array[10000])
73 for (i = 0; i < 10000; i++)
74 g_assert (array[i] == i);
78 handle_pair (gpointer key, gpointer value, int result_array[10000])
82 g_assert (key == value);
86 g_assert (n >= 0 && n < 10000);
87 g_assert (result_array[n] == -1);
93 my_hash_callback_remove (gpointer key,
106 my_hash_callback_remove_test (gpointer key,
113 g_assert_not_reached ();
117 my_hash_callback (gpointer key,
121 handle_pair (key, value, user_data);
125 my_hash (gconstpointer key)
127 return (guint) *((const gint*) key);
131 my_hash_equal (gconstpointer a,
134 return *((const gint*) a) == *((const gint*) b);
140 * This is a simplified version of the pathalias hashing function.
141 * Thanks to Steve Belovin and Peter Honeyman
143 * hash a string into a long int. 31 bit crc (from andrew appel).
144 * the crc table is computed at run time by crcinit() -- we could
145 * precompute, but it takes 1 clock tick on a 750.
147 * This fast table calculation works only if POLY is a prime polynomial
148 * in the field of integers modulo 2. Since the coefficients of a
149 * 32-bit polynomial won't fit in a 32-bit word, the high-order bit is
150 * implicit. IT MUST ALSO BE THE CASE that the coefficients of orders
151 * 31 down to 25 are zero. Happily, we have candidates, from
152 * E. J. Watson, "Primitive Polynomials (Mod 2)", Math. Comp. 16 (1962):
153 * x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + x^0
156 * We reverse the bits to get:
157 * 111101010000000000000000000000001 but drop the last 1
159 * 010010000000000000000000000000001 ditto, for 31-bit crc
163 #define POLY 0x48000000L /* 31-bit polynomial (avoids sign problems) */
165 static guint CrcTable[128];
168 - crcinit - initialize tables for hash function
170 static void crcinit(void)
175 for (i = 0; i < 128; ++i) {
177 for (j = 7 - 1; j >= 0; --j)
185 - hash - Honeyman's nice hashing function
187 static guint honeyman_hash(gconstpointer key)
189 const gchar *name = (const gchar *) key;
193 g_assert (name != NULL);
194 g_assert (*name != 0);
199 sum = (sum >> 7) ^ CrcTable[(sum ^ (*name++)) & 0x7f];
206 static gboolean second_hash_cmp (gconstpointer a, gconstpointer b)
208 return (strcmp (a, b) == 0);
213 static guint one_hash(gconstpointer key)
219 static void not_even_foreach (gpointer key,
223 const char *_key = (const char *) key;
224 const char *_value = (const char *) value;
228 g_assert (_key != NULL);
229 g_assert (*_key != 0);
230 g_assert (_value != NULL);
231 g_assert (*_value != 0);
235 sprintf (val, "%d value", i);
236 g_assert (strcmp (_value, val) == 0);
238 g_assert ((i % 2) != 0);
243 static gboolean remove_even_foreach (gpointer key,
247 const char *_key = (const char *) key;
248 const char *_value = (const char *) value;
252 g_assert (_key != NULL);
253 g_assert (*_key != 0);
254 g_assert (_value != NULL);
255 g_assert (*_value != 0);
259 sprintf (val, "%d value", i);
260 g_assert (strcmp (_value, val) == 0);
262 return ((i % 2) == 0) ? TRUE : FALSE;
268 static void second_hash_test (gconstpointer d)
270 gboolean simple_hash = GPOINTER_TO_INT (d);
273 char key[20] = "", val[20]="", *v, *orig_key, *orig_val;
279 h = g_hash_table_new_full (simple_hash ? one_hash : honeyman_hash,
282 g_assert (h != NULL);
285 sprintf (key, "%d", i);
286 g_assert (atoi (key) == i);
288 sprintf (val, "%d value", i);
289 g_assert (atoi (val) == i);
291 g_hash_table_insert (h, g_strdup (key), g_strdup (val));
294 g_assert (g_hash_table_size (h) == 20);
298 sprintf (key, "%d", i);
299 g_assert (atoi(key) == i);
301 v = (char *) g_hash_table_lookup (h, key);
303 g_assert (v != NULL);
305 g_assert (atoi (v) == i);
308 sprintf (key, "%d", 3);
309 g_hash_table_remove (h, key);
310 g_assert (g_hash_table_size (h) == 19);
311 g_hash_table_foreach_remove (h, remove_even_foreach, NULL);
312 g_assert (g_hash_table_size (h) == 9);
313 g_hash_table_foreach (h, not_even_foreach, NULL);
317 sprintf (key, "%d", i);
318 g_assert (atoi(key) == i);
320 sprintf (val, "%d value", i);
321 g_assert (atoi (val) == i);
323 orig_key = orig_val = NULL;
324 found = g_hash_table_lookup_extended (h, key,
326 (gpointer)&orig_val);
327 if ((i % 2) == 0 || i == 3)
335 g_assert (orig_key != NULL);
336 g_assert (strcmp (key, orig_key) == 0);
338 g_assert (orig_val != NULL);
339 g_assert (strcmp (val, orig_val) == 0);
342 g_hash_table_destroy (h);
345 static gboolean find_first (gpointer key,
350 gint *test = user_data;
351 return (*v == *test);
355 static void direct_hash_test (void)
360 h = g_hash_table_new (NULL, NULL);
361 g_assert (h != NULL);
362 for (i=1; i<=20; i++)
364 g_hash_table_insert (h, GINT_TO_POINTER (i),
365 GINT_TO_POINTER (i + 42));
368 g_assert (g_hash_table_size (h) == 20);
370 for (i=1; i<=20; i++)
372 rc = GPOINTER_TO_INT (
373 g_hash_table_lookup (h, GINT_TO_POINTER (i)));
376 g_assert ((rc - 42) == i);
379 g_hash_table_destroy (h);
383 test_hash_misc (void)
385 GHashTable *hash_table;
389 GList *keys, *values;
390 gint keys_len, values_len;
392 gpointer ikey, ivalue;
393 int result_array[10000];
395 hash_table = g_hash_table_new (my_hash, my_hash_equal);
396 fill_hash_table_and_array (hash_table);
397 pvalue = g_hash_table_find (hash_table, find_first, &value);
398 if (!pvalue || *pvalue != value)
399 g_assert_not_reached();
401 keys = g_hash_table_get_keys (hash_table);
403 g_assert_not_reached ();
405 values = g_hash_table_get_values (hash_table);
407 g_assert_not_reached ();
409 keys_len = g_list_length (keys);
410 values_len = g_list_length (values);
411 if (values_len != keys_len && keys_len != g_hash_table_size (hash_table))
412 g_assert_not_reached ();
415 g_list_free (values);
417 init_result_array (result_array);
418 g_hash_table_iter_init (&iter, hash_table);
419 for (i = 0; i < 10000; i++)
421 g_assert (g_hash_table_iter_next (&iter, &ikey, &ivalue));
423 handle_pair (ikey, ivalue, result_array);
426 g_hash_table_iter_remove (&iter);
428 g_assert (! g_hash_table_iter_next (&iter, &ikey, &ivalue));
429 g_assert (g_hash_table_size (hash_table) == 5000);
430 verify_result_array (result_array);
432 fill_hash_table_and_array (hash_table);
434 init_result_array (result_array);
435 g_hash_table_foreach (hash_table, my_hash_callback, result_array);
436 verify_result_array (result_array);
438 for (i = 0; i < 10000; i++)
439 g_hash_table_remove (hash_table, &array[i]);
441 fill_hash_table_and_array (hash_table);
443 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
444 g_hash_table_size (hash_table) != 5000)
445 g_assert_not_reached();
447 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
448 g_hash_table_destroy (hash_table);
451 static gint destroy_counter;
454 value_destroy (gpointer value)
465 gboolean abc_seen = FALSE;
466 gboolean cde_seen = FALSE;
467 gboolean xyz_seen = FALSE;
469 h = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, value_destroy);
470 g_hash_table_insert (h, "abc", "ABC");
471 g_hash_table_insert (h, "cde", "CDE");
472 g_hash_table_insert (h, "xyz", "XYZ");
474 g_assert_cmpint (g_hash_table_size (h), == , 3);
476 g_hash_table_iter_init (&iter, h);
478 while (g_hash_table_iter_next (&iter, (gpointer*)&key, (gpointer*)&value))
480 if (strcmp (key, "abc") == 0)
482 g_assert_cmpstr (value, ==, "ABC");
484 g_hash_table_iter_steal (&iter);
486 else if (strcmp (key, "cde") == 0)
488 g_assert_cmpstr (value, ==, "CDE");
491 else if (strcmp (key, "xyz") == 0)
493 g_assert_cmpstr (value, ==, "XYZ");
497 g_assert_cmpint (destroy_counter, ==, 0);
499 g_assert (g_hash_table_iter_get_hash_table (&iter) == h);
500 g_assert (abc_seen && cde_seen && xyz_seen);
501 g_assert_cmpint (g_hash_table_size (h), == , 2);
503 g_hash_table_ref (h);
504 g_hash_table_destroy (h);
505 g_assert_cmpint (g_hash_table_size (h), == , 0);
506 g_assert_cmpint (destroy_counter, ==, 2);
507 g_hash_table_insert (h, "uvw", "UVW");
508 g_hash_table_unref (h);
509 g_assert_cmpint (destroy_counter, ==, 3);
513 main (int argc, char *argv[])
515 g_test_init (&argc, &argv, NULL);
517 g_test_add_func ("/hash/misc", test_hash_misc);
518 g_test_add_data_func ("/hash/one", GINT_TO_POINTER (TRUE), second_hash_test);
519 g_test_add_data_func ("/hash/honeyman", GINT_TO_POINTER (FALSE), second_hash_test);
520 g_test_add_func ("/hash/direct", direct_hash_test);
521 g_test_add_func ("/hash/ref", test_hash_ref);
523 return g_test_run ();