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)
178 for (j = 7 - 1; j >= 0; --j)
186 - hash - Honeyman's nice hashing function
189 honeyman_hash (gconstpointer key)
191 const gchar *name = (const gchar *) key;
195 g_assert (name != NULL);
196 g_assert (*name != 0);
198 size = strlen (name);
201 sum = (sum >> 7) ^ CrcTable[(sum ^ (*name++)) & 0x7f];
208 second_hash_cmp (gconstpointer a, gconstpointer b)
210 return strcmp (a, b) == 0;
216 one_hash (gconstpointer key)
223 not_even_foreach (gpointer key,
227 const char *_key = (const char *) key;
228 const char *_value = (const char *) value;
232 g_assert (_key != NULL);
233 g_assert (*_key != 0);
234 g_assert (_value != NULL);
235 g_assert (*_value != 0);
239 sprintf (val, "%d value", i);
240 g_assert (strcmp (_value, val) == 0);
242 g_assert ((i % 2) != 0);
248 remove_even_foreach (gpointer key,
252 const char *_key = (const char *) key;
253 const char *_value = (const char *) value;
257 g_assert (_key != NULL);
258 g_assert (*_key != 0);
259 g_assert (_value != NULL);
260 g_assert (*_value != 0);
264 sprintf (val, "%d value", i);
265 g_assert (strcmp (_value, val) == 0);
267 return ((i % 2) == 0) ? TRUE : FALSE;
274 second_hash_test (gconstpointer d)
276 gboolean simple_hash = GPOINTER_TO_INT (d);
279 char key[20] = "", val[20]="", *v, *orig_key, *orig_val;
285 h = g_hash_table_new_full (simple_hash ? one_hash : honeyman_hash,
288 g_assert (h != NULL);
289 for (i = 0; i < 20; i++)
291 sprintf (key, "%d", i);
292 g_assert (atoi (key) == i);
294 sprintf (val, "%d value", i);
295 g_assert (atoi (val) == i);
297 g_hash_table_insert (h, g_strdup (key), g_strdup (val));
300 g_assert (g_hash_table_size (h) == 20);
302 for (i = 0; i < 20; i++)
304 sprintf (key, "%d", i);
305 g_assert (atoi(key) == i);
307 v = (char *) g_hash_table_lookup (h, key);
309 g_assert (v != NULL);
311 g_assert (atoi (v) == i);
314 sprintf (key, "%d", 3);
315 g_hash_table_remove (h, key);
316 g_assert (g_hash_table_size (h) == 19);
317 g_hash_table_foreach_remove (h, remove_even_foreach, NULL);
318 g_assert (g_hash_table_size (h) == 9);
319 g_hash_table_foreach (h, not_even_foreach, NULL);
321 for (i = 0; i < 20; i++)
323 sprintf (key, "%d", i);
324 g_assert (atoi(key) == i);
326 sprintf (val, "%d value", i);
327 g_assert (atoi (val) == i);
329 orig_key = orig_val = NULL;
330 found = g_hash_table_lookup_extended (h, key,
332 (gpointer)&orig_val);
333 if ((i % 2) == 0 || i == 3)
341 g_assert (orig_key != NULL);
342 g_assert (strcmp (key, orig_key) == 0);
344 g_assert (orig_val != NULL);
345 g_assert (strcmp (val, orig_val) == 0);
348 g_hash_table_destroy (h);
352 find_first (gpointer key,
357 gint *test = user_data;
358 return (*v == *test);
363 direct_hash_test (void)
368 h = g_hash_table_new (NULL, NULL);
369 g_assert (h != NULL);
370 for (i = 1; i <= 20; i++)
371 g_hash_table_insert (h, GINT_TO_POINTER (i),
372 GINT_TO_POINTER (i + 42));
374 g_assert (g_hash_table_size (h) == 20);
376 for (i = 1; i <= 20; i++)
378 rc = GPOINTER_TO_INT (g_hash_table_lookup (h, GINT_TO_POINTER (i)));
381 g_assert ((rc - 42) == i);
384 g_hash_table_destroy (h);
388 int64_hash_test (void)
395 h = g_hash_table_new (g_int64_hash, g_int64_equal);
396 g_assert (h != NULL);
397 for (i = 0; i < 20; i++)
400 g_hash_table_insert (h, &values[i], GINT_TO_POINTER (i + 42));
403 g_assert (g_hash_table_size (h) == 20);
405 for (i = 0; i < 20; i++)
408 rc = GPOINTER_TO_INT (g_hash_table_lookup (h, &key));
410 g_assert_cmpint (rc, ==, i + 42);
413 g_hash_table_destroy (h);
417 double_hash_test (void)
424 h = g_hash_table_new (g_double_hash, g_double_equal);
425 g_assert (h != NULL);
426 for (i = 0; i < 20; i++)
428 values[i] = i + 42.5;
429 g_hash_table_insert (h, &values[i], GINT_TO_POINTER (i + 42));
432 g_assert (g_hash_table_size (h) == 20);
434 for (i = 0; i < 20; i++)
437 rc = GPOINTER_TO_INT (g_hash_table_lookup (h, &key));
439 g_assert_cmpint (rc, ==, i + 42);
442 g_hash_table_destroy (h);
446 string_free (gpointer data)
450 g_string_free (s, TRUE);
454 string_hash_test (void)
460 h = g_hash_table_new_full ((GHashFunc)g_string_hash, (GEqualFunc)g_string_equal, string_free, NULL);
461 g_assert (h != NULL);
462 for (i = 0; i < 20; i++)
464 s = g_string_new ("");
465 g_string_append_printf (s, "%d", i + 42);
466 g_string_append_c (s, '.');
467 g_string_prepend_unichar (s, 0x2301);
468 g_hash_table_insert (h, s, GINT_TO_POINTER (i + 42));
471 g_assert (g_hash_table_size (h) == 20);
473 s = g_string_new ("");
474 for (i = 0; i < 20; i++)
476 g_string_assign (s, "");
477 g_string_append_printf (s, "%d", i + 42);
478 g_string_append_c (s, '.');
479 g_string_prepend_unichar (s, 0x2301);
480 rc = GPOINTER_TO_INT (g_hash_table_lookup (h, s));
482 g_assert_cmpint (rc, ==, i + 42);
485 g_string_free (s, TRUE);
486 g_hash_table_destroy (h);
490 set_check (gpointer key,
496 g_assert_not_reached ();
498 g_assert_cmpint (atoi (key) % 7, ==, 2);
506 GHashTable *hash_table =
507 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
510 for (i = 2; i < 5000; i += 7)
512 char *s = g_strdup_printf ("%d", i);
513 g_hash_table_insert (hash_table, s, s);
517 g_hash_table_foreach (hash_table, set_check, &i);
518 g_assert_cmpint (i, ==, g_hash_table_size (hash_table));
520 /* this will cause the hash table to loose set nature */
521 g_hash_table_insert (hash_table, g_strdup ("a"), "b");
523 g_assert_cmpstr (g_hash_table_lookup (hash_table, "2"), ==, "2");
524 g_assert_cmpstr (g_hash_table_lookup (hash_table, "a"), ==, "b");
526 g_hash_table_destroy (hash_table);
531 test_hash_misc (void)
533 GHashTable *hash_table;
537 GList *keys, *values;
538 gint keys_len, values_len;
540 gpointer ikey, ivalue;
541 int result_array[10000];
543 hash_table = g_hash_table_new (my_hash, my_hash_equal);
544 fill_hash_table_and_array (hash_table);
545 pvalue = g_hash_table_find (hash_table, find_first, &value);
546 if (!pvalue || *pvalue != value)
547 g_assert_not_reached();
549 keys = g_hash_table_get_keys (hash_table);
551 g_assert_not_reached ();
553 values = g_hash_table_get_values (hash_table);
555 g_assert_not_reached ();
557 keys_len = g_list_length (keys);
558 values_len = g_list_length (values);
559 if (values_len != keys_len && keys_len != g_hash_table_size (hash_table))
560 g_assert_not_reached ();
563 g_list_free (values);
565 init_result_array (result_array);
566 g_hash_table_iter_init (&iter, hash_table);
567 for (i = 0; i < 10000; i++)
569 g_assert (g_hash_table_iter_next (&iter, &ikey, &ivalue));
571 handle_pair (ikey, ivalue, result_array);
574 g_hash_table_iter_remove (&iter);
576 g_assert (! g_hash_table_iter_next (&iter, &ikey, &ivalue));
577 g_assert (g_hash_table_size (hash_table) == 5000);
578 verify_result_array (result_array);
580 fill_hash_table_and_array (hash_table);
582 init_result_array (result_array);
583 g_hash_table_foreach (hash_table, my_hash_callback, result_array);
584 verify_result_array (result_array);
586 for (i = 0; i < 10000; i++)
587 g_hash_table_remove (hash_table, &array[i]);
589 fill_hash_table_and_array (hash_table);
591 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
592 g_hash_table_size (hash_table) != 5000)
593 g_assert_not_reached();
595 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
596 g_hash_table_destroy (hash_table);
599 static gint destroy_counter;
602 value_destroy (gpointer value)
613 gboolean abc_seen = FALSE;
614 gboolean cde_seen = FALSE;
615 gboolean xyz_seen = FALSE;
617 h = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, value_destroy);
618 g_hash_table_insert (h, "abc", "ABC");
619 g_hash_table_insert (h, "cde", "CDE");
620 g_hash_table_insert (h, "xyz", "XYZ");
622 g_assert_cmpint (g_hash_table_size (h), == , 3);
624 g_hash_table_iter_init (&iter, h);
626 while (g_hash_table_iter_next (&iter, (gpointer*)&key, (gpointer*)&value))
628 if (strcmp (key, "abc") == 0)
630 g_assert_cmpstr (value, ==, "ABC");
632 g_hash_table_iter_steal (&iter);
634 else if (strcmp (key, "cde") == 0)
636 g_assert_cmpstr (value, ==, "CDE");
639 else if (strcmp (key, "xyz") == 0)
641 g_assert_cmpstr (value, ==, "XYZ");
645 g_assert_cmpint (destroy_counter, ==, 0);
647 g_assert (g_hash_table_iter_get_hash_table (&iter) == h);
648 g_assert (abc_seen && cde_seen && xyz_seen);
649 g_assert_cmpint (g_hash_table_size (h), == , 2);
651 g_hash_table_ref (h);
652 g_hash_table_destroy (h);
653 g_assert_cmpint (g_hash_table_size (h), == , 0);
654 g_assert_cmpint (destroy_counter, ==, 2);
655 g_hash_table_insert (h, "uvw", "UVW");
656 g_hash_table_unref (h);
657 g_assert_cmpint (destroy_counter, ==, 3);
661 null_safe_str_hash (gconstpointer key)
666 return g_str_hash (key);
670 null_safe_str_equal (gconstpointer a, gconstpointer b)
672 return g_strcmp0 (a, b) == 0;
676 test_lookup_null_key (void)
683 g_test_bug ("642944");
685 h = g_hash_table_new (null_safe_str_hash, null_safe_str_equal);
686 g_hash_table_insert (h, "abc", "ABC");
688 res = g_hash_table_lookup_extended (h, NULL, &key, &value);
691 g_hash_table_insert (h, NULL, "NULL");
693 res = g_hash_table_lookup_extended (h, NULL, &key, &value);
695 g_assert_cmpstr (value, ==, "NULL");
697 g_hash_table_unref (h);
700 static gint destroy_key_counter;
703 key_destroy (gpointer key)
705 destroy_key_counter++;
709 test_remove_all (void)
713 h = g_hash_table_new_full (g_str_hash, g_str_equal, key_destroy, value_destroy);
714 g_hash_table_insert (h, "abc", "ABC");
715 g_hash_table_insert (h, "cde", "CDE");
716 g_hash_table_insert (h, "xyz", "XYZ");
719 destroy_key_counter = 0;
721 g_hash_table_steal_all (h);
722 g_assert_cmpint (destroy_counter, ==, 0);
723 g_assert_cmpint (destroy_key_counter, ==, 0);
725 g_hash_table_insert (h, "abc", "ABC");
726 g_hash_table_insert (h, "cde", "CDE");
727 g_hash_table_insert (h, "xyz", "XYZ");
729 g_hash_table_remove_all (h);
730 g_assert_cmpint (destroy_counter, ==, 3);
731 g_assert_cmpint (destroy_key_counter, ==, 3);
733 g_hash_table_unref (h);
742 hash_func (gconstpointer key)
744 const RefCountedKey *rkey = key;
746 return g_str_hash (rkey->key);
750 eq_func (gconstpointer a, gconstpointer b)
752 const RefCountedKey *aa = a;
753 const RefCountedKey *bb = b;
755 return g_strcmp0 (aa->key, bb->key) == 0;
759 key_unref (gpointer data)
761 RefCountedKey *key = data;
763 g_assert (key->ref_count > 0);
767 if (key->ref_count == 0)
771 static RefCountedKey *
772 key_ref (RefCountedKey *key)
779 static RefCountedKey *
780 key_new (const gchar *key)
784 rkey = g_new (RefCountedKey, 1);
793 set_ref_hash_test (void)
799 h = g_hash_table_new_full (hash_func, eq_func, key_unref, key_unref);
801 key1 = key_new ("a");
802 key2 = key_new ("a");
804 g_assert_cmpint (key1->ref_count, ==, 1);
805 g_assert_cmpint (key2->ref_count, ==, 1);
807 g_hash_table_insert (h, key_ref (key1), key_ref (key1));
809 g_assert_cmpint (key1->ref_count, ==, 3);
810 g_assert_cmpint (key2->ref_count, ==, 1);
812 g_hash_table_replace (h, key_ref (key2), key_ref (key2));
814 g_assert_cmpint (key1->ref_count, ==, 1);
815 g_assert_cmpint (key2->ref_count, ==, 3);
817 g_hash_table_remove (h, key1);
819 g_assert_cmpint (key1->ref_count, ==, 1);
820 g_assert_cmpint (key2->ref_count, ==, 1);
822 g_hash_table_unref (h);
831 value_destroy_insert (gpointer value)
833 g_hash_table_remove_all (h);
837 test_destroy_modify (void)
839 g_test_bug ("650459");
841 h = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, value_destroy_insert);
843 g_hash_table_insert (h, g_strdup ("a"), g_strdup ("b"));
844 g_hash_table_insert (h, g_strdup ("c"), g_strdup ("d"));
845 g_hash_table_insert (h, g_strdup ("e"), g_strdup ("f"));
846 g_hash_table_insert (h, g_strdup ("g"), g_strdup ("h"));
847 g_hash_table_insert (h, g_strdup ("h"), g_strdup ("k"));
848 g_hash_table_insert (h, g_strdup ("a"), g_strdup ("c"));
850 g_hash_table_remove (h, "c");
851 g_hash_table_remove (h, "e");
853 g_hash_table_unref (h);
857 find_str (gpointer key, gpointer value, gpointer data)
859 return g_str_equal (key, data);
868 hash = g_hash_table_new (g_str_hash, g_str_equal);
870 g_hash_table_insert (hash, "a", "A");
871 g_hash_table_insert (hash, "b", "B");
872 g_hash_table_insert (hash, "c", "C");
873 g_hash_table_insert (hash, "d", "D");
874 g_hash_table_insert (hash, "e", "E");
875 g_hash_table_insert (hash, "f", "F");
877 value = g_hash_table_find (hash, find_str, "a");
878 g_assert_cmpstr (value, ==, "A");
880 value = g_hash_table_find (hash, find_str, "b");
881 g_assert_cmpstr (value, ==, "B");
883 value = g_hash_table_find (hash, find_str, "c");
884 g_assert_cmpstr (value, ==, "C");
886 value = g_hash_table_find (hash, find_str, "d");
887 g_assert_cmpstr (value, ==, "D");
889 value = g_hash_table_find (hash, find_str, "e");
890 g_assert_cmpstr (value, ==, "E");
892 value = g_hash_table_find (hash, find_str, "f");
893 g_assert_cmpstr (value, ==, "F");
895 value = g_hash_table_find (hash, find_str, "0");
896 g_assert (value == NULL);
898 g_hash_table_unref (hash);
901 gboolean seen_key[6];
904 foreach_func (gpointer key, gpointer value, gpointer data)
906 seen_key[((char*)key)[0] - 'a'] = TRUE;
915 hash = g_hash_table_new (g_str_hash, g_str_equal);
917 g_hash_table_insert (hash, "a", "A");
918 g_hash_table_insert (hash, "b", "B");
919 g_hash_table_insert (hash, "c", "C");
920 g_hash_table_insert (hash, "d", "D");
921 g_hash_table_insert (hash, "e", "E");
922 g_hash_table_insert (hash, "f", "F");
924 for (i = 0; i < 6; i++)
927 g_hash_table_foreach (hash, foreach_func, NULL);
929 for (i = 0; i < 6; i++)
930 g_assert (seen_key[i]);
932 g_hash_table_unref (hash);
936 main (int argc, char *argv[])
938 g_test_init (&argc, &argv, NULL);
940 g_test_bug_base ("http://bugzilla.gnome.org/");
942 g_test_add_func ("/hash/misc", test_hash_misc);
943 g_test_add_data_func ("/hash/one", GINT_TO_POINTER (TRUE), second_hash_test);
944 g_test_add_data_func ("/hash/honeyman", GINT_TO_POINTER (FALSE), second_hash_test);
945 g_test_add_func ("/hash/direct", direct_hash_test);
946 g_test_add_func ("/hash/int64", int64_hash_test);
947 g_test_add_func ("/hash/double", double_hash_test);
948 g_test_add_func ("/hash/string", string_hash_test);
949 g_test_add_func ("/hash/set", set_hash_test);
950 g_test_add_func ("/hash/set-ref", set_ref_hash_test);
951 g_test_add_func ("/hash/ref", test_hash_ref);
952 g_test_add_func ("/hash/remove-all", test_remove_all);
953 g_test_add_func ("/hash/find", test_find);
954 g_test_add_func ("/hash/foreach", test_foreach);
956 /* tests for individual bugs */
957 g_test_add_func ("/hash/lookup-null-key", test_lookup_null_key);
958 g_test_add_func ("/hash/destroy-modify", test_destroy_modify);
960 return g_test_run ();