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
46 fill_hash_table_and_array (GHashTable *hash_table)
50 for (i = 0; i < 10000; i++)
53 g_hash_table_insert (hash_table, &array[i], &array[i]);
58 init_result_array (int result_array[10000])
62 for (i = 0; i < 10000; i++)
67 verify_result_array (int array[10000])
71 for (i = 0; i < 10000; i++)
72 g_assert (array[i] == i);
76 handle_pair (gpointer key, gpointer value, int result_array[10000])
80 g_assert (key == value);
84 g_assert (n >= 0 && n < 10000);
85 g_assert (result_array[n] == -1);
91 my_hash_callback_remove (gpointer key,
104 my_hash_callback_remove_test (gpointer key,
111 g_assert_not_reached ();
115 my_hash_callback (gpointer key,
119 handle_pair (key, value, user_data);
123 my_hash (gconstpointer key)
125 return (guint) *((const gint*) key);
129 my_hash_equal (gconstpointer a,
132 return *((const gint*) a) == *((const gint*) b);
138 * This is a simplified version of the pathalias hashing function.
139 * Thanks to Steve Belovin and Peter Honeyman
141 * hash a string into a long int. 31 bit crc (from andrew appel).
142 * the crc table is computed at run time by crcinit() -- we could
143 * precompute, but it takes 1 clock tick on a 750.
145 * This fast table calculation works only if POLY is a prime polynomial
146 * in the field of integers modulo 2. Since the coefficients of a
147 * 32-bit polynomial won't fit in a 32-bit word, the high-order bit is
148 * implicit. IT MUST ALSO BE THE CASE that the coefficients of orders
149 * 31 down to 25 are zero. Happily, we have candidates, from
150 * E. J. Watson, "Primitive Polynomials (Mod 2)", Math. Comp. 16 (1962):
151 * x^32 + x^7 + x^5 + x^3 + x^2 + x^1 + x^0
154 * We reverse the bits to get:
155 * 111101010000000000000000000000001 but drop the last 1
157 * 010010000000000000000000000000001 ditto, for 31-bit crc
161 #define POLY 0x48000000L /* 31-bit polynomial (avoids sign problems) */
163 static guint CrcTable[128];
166 - crcinit - initialize tables for hash function
168 static void crcinit(void)
173 for (i = 0; i < 128; ++i)
176 for (j = 7 - 1; j >= 0; --j)
184 - hash - Honeyman's nice hashing function
187 honeyman_hash (gconstpointer key)
189 const gchar *name = (const gchar *) key;
193 g_assert (name != NULL);
194 g_assert (*name != 0);
196 size = strlen (name);
199 sum = (sum >> 7) ^ CrcTable[(sum ^ (*name++)) & 0x7f];
206 second_hash_cmp (gconstpointer a, gconstpointer b)
208 return strcmp (a, b) == 0;
214 one_hash (gconstpointer key)
221 not_even_foreach (gpointer key,
225 const char *_key = (const char *) key;
226 const char *_value = (const char *) value;
230 g_assert (_key != NULL);
231 g_assert (*_key != 0);
232 g_assert (_value != NULL);
233 g_assert (*_value != 0);
237 sprintf (val, "%d value", i);
238 g_assert (strcmp (_value, val) == 0);
240 g_assert ((i % 2) != 0);
246 remove_even_foreach (gpointer key,
250 const char *_key = (const char *) key;
251 const char *_value = (const char *) value;
255 g_assert (_key != NULL);
256 g_assert (*_key != 0);
257 g_assert (_value != NULL);
258 g_assert (*_value != 0);
262 sprintf (val, "%d value", i);
263 g_assert (strcmp (_value, val) == 0);
265 return ((i % 2) == 0) ? TRUE : FALSE;
272 second_hash_test (gconstpointer d)
274 gboolean simple_hash = GPOINTER_TO_INT (d);
277 char key[20] = "", val[20]="", *v, *orig_key, *orig_val;
283 h = g_hash_table_new_full (simple_hash ? one_hash : honeyman_hash,
286 g_assert (h != NULL);
287 for (i = 0; i < 20; i++)
289 sprintf (key, "%d", i);
290 g_assert (atoi (key) == i);
292 sprintf (val, "%d value", i);
293 g_assert (atoi (val) == i);
295 g_hash_table_insert (h, g_strdup (key), g_strdup (val));
298 g_assert (g_hash_table_size (h) == 20);
300 for (i = 0; i < 20; i++)
302 sprintf (key, "%d", i);
303 g_assert (atoi(key) == i);
305 v = (char *) g_hash_table_lookup (h, key);
307 g_assert (v != NULL);
309 g_assert (atoi (v) == i);
312 sprintf (key, "%d", 3);
313 g_hash_table_remove (h, key);
314 g_assert (g_hash_table_size (h) == 19);
315 g_hash_table_foreach_remove (h, remove_even_foreach, NULL);
316 g_assert (g_hash_table_size (h) == 9);
317 g_hash_table_foreach (h, not_even_foreach, NULL);
319 for (i = 0; i < 20; i++)
321 sprintf (key, "%d", i);
322 g_assert (atoi(key) == i);
324 sprintf (val, "%d value", i);
325 g_assert (atoi (val) == i);
327 orig_key = orig_val = NULL;
328 found = g_hash_table_lookup_extended (h, key,
330 (gpointer)&orig_val);
331 if ((i % 2) == 0 || i == 3)
339 g_assert (orig_key != NULL);
340 g_assert (strcmp (key, orig_key) == 0);
342 g_assert (orig_val != NULL);
343 g_assert (strcmp (val, orig_val) == 0);
346 g_hash_table_destroy (h);
350 find_first (gpointer key,
355 gint *test = user_data;
356 return (*v == *test);
361 direct_hash_test (void)
366 h = g_hash_table_new (NULL, NULL);
367 g_assert (h != NULL);
368 for (i = 1; i <= 20; i++)
369 g_hash_table_insert (h, GINT_TO_POINTER (i),
370 GINT_TO_POINTER (i + 42));
372 g_assert (g_hash_table_size (h) == 20);
374 for (i = 1; i <= 20; i++)
376 rc = GPOINTER_TO_INT (g_hash_table_lookup (h, GINT_TO_POINTER (i)));
379 g_assert ((rc - 42) == i);
382 g_hash_table_destroy (h);
386 int64_hash_test (void)
393 h = g_hash_table_new (g_int64_hash, g_int64_equal);
394 g_assert (h != NULL);
395 for (i = 0; i < 20; i++)
398 g_hash_table_insert (h, &values[i], GINT_TO_POINTER (i + 42));
401 g_assert (g_hash_table_size (h) == 20);
403 for (i = 0; i < 20; i++)
406 rc = GPOINTER_TO_INT (g_hash_table_lookup (h, &key));
408 g_assert_cmpint (rc, ==, i + 42);
411 g_hash_table_destroy (h);
415 double_hash_test (void)
422 h = g_hash_table_new (g_double_hash, g_double_equal);
423 g_assert (h != NULL);
424 for (i = 0; i < 20; i++)
426 values[i] = i + 42.5;
427 g_hash_table_insert (h, &values[i], GINT_TO_POINTER (i + 42));
430 g_assert (g_hash_table_size (h) == 20);
432 for (i = 0; i < 20; i++)
435 rc = GPOINTER_TO_INT (g_hash_table_lookup (h, &key));
437 g_assert_cmpint (rc, ==, i + 42);
440 g_hash_table_destroy (h);
444 string_free (gpointer data)
448 g_string_free (s, TRUE);
452 string_hash_test (void)
458 h = g_hash_table_new_full ((GHashFunc)g_string_hash, (GEqualFunc)g_string_equal, string_free, NULL);
459 g_assert (h != NULL);
460 for (i = 0; i < 20; i++)
462 s = g_string_new ("");
463 g_string_append_printf (s, "%d", i + 42);
464 g_string_append_c (s, '.');
465 g_string_prepend_unichar (s, 0x2301);
466 g_hash_table_insert (h, s, GINT_TO_POINTER (i + 42));
469 g_assert (g_hash_table_size (h) == 20);
471 s = g_string_new ("");
472 for (i = 0; i < 20; i++)
474 g_string_assign (s, "");
475 g_string_append_printf (s, "%d", i + 42);
476 g_string_append_c (s, '.');
477 g_string_prepend_unichar (s, 0x2301);
478 rc = GPOINTER_TO_INT (g_hash_table_lookup (h, s));
480 g_assert_cmpint (rc, ==, i + 42);
483 g_string_free (s, TRUE);
484 g_hash_table_destroy (h);
488 set_check (gpointer key,
494 g_assert_not_reached ();
496 g_assert_cmpint (atoi (key) % 7, ==, 2);
504 GHashTable *hash_table =
505 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
508 for (i = 2; i < 5000; i += 7)
510 char *s = g_strdup_printf ("%d", i);
511 g_hash_table_add (hash_table, s);
515 g_hash_table_foreach (hash_table, set_check, &i);
516 g_assert_cmpint (i, ==, g_hash_table_size (hash_table));
518 g_assert (g_hash_table_contains (hash_table, "2"));
519 g_assert (g_hash_table_contains (hash_table, "9"));
520 g_assert (!g_hash_table_contains (hash_table, "a"));
522 /* this will cause the hash table to loose set nature */
523 g_hash_table_insert (hash_table, g_strdup ("a"), "b");
525 g_assert_cmpstr (g_hash_table_lookup (hash_table, "2"), ==, "2");
526 g_assert_cmpstr (g_hash_table_lookup (hash_table, "a"), ==, "b");
528 g_hash_table_destroy (hash_table);
533 test_hash_misc (void)
535 GHashTable *hash_table;
539 GList *keys, *values;
540 gint keys_len, values_len;
542 gpointer ikey, ivalue;
543 int result_array[10000];
546 hash_table = g_hash_table_new (my_hash, my_hash_equal);
547 fill_hash_table_and_array (hash_table);
548 pvalue = g_hash_table_find (hash_table, find_first, &value);
549 if (!pvalue || *pvalue != value)
550 g_assert_not_reached();
552 keys = g_hash_table_get_keys (hash_table);
554 g_assert_not_reached ();
556 values = g_hash_table_get_values (hash_table);
558 g_assert_not_reached ();
560 keys_len = g_list_length (keys);
561 values_len = g_list_length (values);
562 if (values_len != keys_len && keys_len != g_hash_table_size (hash_table))
563 g_assert_not_reached ();
566 g_list_free (values);
568 init_result_array (result_array);
569 g_hash_table_iter_init (&iter, hash_table);
570 for (i = 0; i < 10000; i++)
572 g_assert (g_hash_table_iter_next (&iter, &ikey, &ivalue));
574 handle_pair (ikey, ivalue, result_array);
577 g_hash_table_iter_remove (&iter);
579 g_assert (! g_hash_table_iter_next (&iter, &ikey, &ivalue));
580 g_assert (g_hash_table_size (hash_table) == 5000);
581 verify_result_array (result_array);
583 fill_hash_table_and_array (hash_table);
585 init_result_array (result_array);
586 g_hash_table_foreach (hash_table, my_hash_callback, result_array);
587 verify_result_array (result_array);
589 for (i = 0; i < 10000; i++)
590 g_hash_table_remove (hash_table, &array[i]);
592 fill_hash_table_and_array (hash_table);
594 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
595 g_hash_table_size (hash_table) != 5000)
596 g_assert_not_reached();
598 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
599 g_hash_table_destroy (hash_table);
601 hash_table = g_hash_table_new (my_hash, my_hash_equal);
602 fill_hash_table_and_array (hash_table);
606 g_hash_table_iter_init (&iter, hash_table);
607 for (i = 0; i < 10000; i++)
609 g_assert (g_hash_table_iter_next (&iter, &ikey, &ivalue));
610 g_hash_table_iter_replace (&iter, &n_array[0]);
613 g_hash_table_iter_init (&iter, hash_table);
614 for (i = 0; i < 10000; i++)
616 g_assert (g_hash_table_iter_next (&iter, &ikey, &ivalue));
618 g_assert (ivalue == &n_array[0]);
621 g_hash_table_destroy (hash_table);
624 static gint destroy_counter;
627 value_destroy (gpointer value)
638 gboolean abc_seen = FALSE;
639 gboolean cde_seen = FALSE;
640 gboolean xyz_seen = FALSE;
642 h = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, value_destroy);
643 g_hash_table_insert (h, "abc", "ABC");
644 g_hash_table_insert (h, "cde", "CDE");
645 g_hash_table_insert (h, "xyz", "XYZ");
647 g_assert_cmpint (g_hash_table_size (h), == , 3);
649 g_hash_table_iter_init (&iter, h);
651 while (g_hash_table_iter_next (&iter, (gpointer*)&key, (gpointer*)&value))
653 if (strcmp (key, "abc") == 0)
655 g_assert_cmpstr (value, ==, "ABC");
657 g_hash_table_iter_steal (&iter);
659 else if (strcmp (key, "cde") == 0)
661 g_assert_cmpstr (value, ==, "CDE");
664 else if (strcmp (key, "xyz") == 0)
666 g_assert_cmpstr (value, ==, "XYZ");
670 g_assert_cmpint (destroy_counter, ==, 0);
672 g_assert (g_hash_table_iter_get_hash_table (&iter) == h);
673 g_assert (abc_seen && cde_seen && xyz_seen);
674 g_assert_cmpint (g_hash_table_size (h), == , 2);
676 g_hash_table_ref (h);
677 g_hash_table_destroy (h);
678 g_assert_cmpint (g_hash_table_size (h), == , 0);
679 g_assert_cmpint (destroy_counter, ==, 2);
680 g_hash_table_insert (h, "uvw", "UVW");
681 g_hash_table_unref (h);
682 g_assert_cmpint (destroy_counter, ==, 3);
686 null_safe_str_hash (gconstpointer key)
691 return g_str_hash (key);
695 null_safe_str_equal (gconstpointer a, gconstpointer b)
697 return g_strcmp0 (a, b) == 0;
701 test_lookup_null_key (void)
708 g_test_bug ("642944");
710 h = g_hash_table_new (null_safe_str_hash, null_safe_str_equal);
711 g_hash_table_insert (h, "abc", "ABC");
713 res = g_hash_table_lookup_extended (h, NULL, &key, &value);
716 g_hash_table_insert (h, NULL, "NULL");
718 res = g_hash_table_lookup_extended (h, NULL, &key, &value);
720 g_assert_cmpstr (value, ==, "NULL");
722 g_hash_table_unref (h);
725 static gint destroy_key_counter;
728 key_destroy (gpointer key)
730 destroy_key_counter++;
734 test_remove_all (void)
739 h = g_hash_table_new_full (g_str_hash, g_str_equal, key_destroy, value_destroy);
740 g_hash_table_insert (h, "abc", "ABC");
741 g_hash_table_insert (h, "cde", "CDE");
742 g_hash_table_insert (h, "xyz", "XYZ");
745 destroy_key_counter = 0;
747 g_hash_table_steal_all (h);
748 g_assert_cmpint (destroy_counter, ==, 0);
749 g_assert_cmpint (destroy_key_counter, ==, 0);
751 g_hash_table_insert (h, "abc", "ABC");
752 g_hash_table_insert (h, "cde", "CDE");
753 g_hash_table_insert (h, "xyz", "XYZ");
755 res = g_hash_table_steal (h, "nosuchkey");
757 g_assert_cmpint (destroy_counter, ==, 0);
758 g_assert_cmpint (destroy_key_counter, ==, 0);
760 res = g_hash_table_steal (h, "xyz");
762 g_assert_cmpint (destroy_counter, ==, 0);
763 g_assert_cmpint (destroy_key_counter, ==, 0);
765 g_hash_table_remove_all (h);
766 g_assert_cmpint (destroy_counter, ==, 2);
767 g_assert_cmpint (destroy_key_counter, ==, 2);
769 g_hash_table_unref (h);
778 hash_func (gconstpointer key)
780 const RefCountedKey *rkey = key;
782 return g_str_hash (rkey->key);
786 eq_func (gconstpointer a, gconstpointer b)
788 const RefCountedKey *aa = a;
789 const RefCountedKey *bb = b;
791 return g_strcmp0 (aa->key, bb->key) == 0;
795 key_unref (gpointer data)
797 RefCountedKey *key = data;
799 g_assert (key->ref_count > 0);
803 if (key->ref_count == 0)
807 static RefCountedKey *
808 key_ref (RefCountedKey *key)
815 static RefCountedKey *
816 key_new (const gchar *key)
820 rkey = g_new (RefCountedKey, 1);
829 set_ref_hash_test (void)
835 h = g_hash_table_new_full (hash_func, eq_func, key_unref, key_unref);
837 key1 = key_new ("a");
838 key2 = key_new ("a");
840 g_assert_cmpint (key1->ref_count, ==, 1);
841 g_assert_cmpint (key2->ref_count, ==, 1);
843 g_hash_table_insert (h, key_ref (key1), key_ref (key1));
845 g_assert_cmpint (key1->ref_count, ==, 3);
846 g_assert_cmpint (key2->ref_count, ==, 1);
848 g_hash_table_replace (h, key_ref (key2), key_ref (key2));
850 g_assert_cmpint (key1->ref_count, ==, 1);
851 g_assert_cmpint (key2->ref_count, ==, 3);
853 g_hash_table_remove (h, key1);
855 g_assert_cmpint (key1->ref_count, ==, 1);
856 g_assert_cmpint (key2->ref_count, ==, 1);
858 g_hash_table_unref (h);
871 GPtrArray *fake_free_data;
874 fake_free (gpointer dead)
878 for (i = 0; i < fake_free_data->len; i++)
880 FakeFreeData *ffd = g_ptr_array_index (fake_free_data, i);
882 if (ffd->string == (gchar *) dead)
884 g_assert (!ffd->freed);
890 g_assert_not_reached ();
894 value_destroy_insert (gpointer value)
896 g_hash_table_remove_all (h);
900 test_destroy_modify (void)
905 g_test_bug ("650459");
907 fake_free_data = g_ptr_array_new ();
909 h = g_hash_table_new_full (g_str_hash, g_str_equal, fake_free, value_destroy_insert);
911 ffd = g_new0 (FakeFreeData, 1);
912 ffd->string = g_strdup ("a");
913 g_ptr_array_add (fake_free_data, ffd);
914 g_hash_table_insert (h, ffd->string, "b");
916 ffd = g_new0 (FakeFreeData, 1);
917 ffd->string = g_strdup ("c");
918 g_ptr_array_add (fake_free_data, ffd);
919 g_hash_table_insert (h, ffd->string, "d");
921 ffd = g_new0 (FakeFreeData, 1);
922 ffd->string = g_strdup ("e");
923 g_ptr_array_add (fake_free_data, ffd);
924 g_hash_table_insert (h, ffd->string, "f");
926 ffd = g_new0 (FakeFreeData, 1);
927 ffd->string = g_strdup ("g");
928 g_ptr_array_add (fake_free_data, ffd);
929 g_hash_table_insert (h, ffd->string, "h");
931 ffd = g_new0 (FakeFreeData, 1);
932 ffd->string = g_strdup ("h");
933 g_ptr_array_add (fake_free_data, ffd);
934 g_hash_table_insert (h, ffd->string, "k");
936 ffd = g_new0 (FakeFreeData, 1);
937 ffd->string = g_strdup ("a");
938 g_ptr_array_add (fake_free_data, ffd);
939 g_hash_table_insert (h, ffd->string, "c");
941 g_hash_table_remove (h, "c");
943 /* that removed everything... */
944 for (i = 0; i < fake_free_data->len; i++)
946 FakeFreeData *ffd = g_ptr_array_index (fake_free_data, i);
948 g_assert (ffd->freed);
949 g_free (ffd->string);
953 g_ptr_array_unref (fake_free_data);
955 /* ... so this is a no-op */
956 g_hash_table_remove (h, "e");
958 g_hash_table_unref (h);
962 find_str (gpointer key, gpointer value, gpointer data)
964 return g_str_equal (key, data);
973 hash = g_hash_table_new (g_str_hash, g_str_equal);
975 g_hash_table_insert (hash, "a", "A");
976 g_hash_table_insert (hash, "b", "B");
977 g_hash_table_insert (hash, "c", "C");
978 g_hash_table_insert (hash, "d", "D");
979 g_hash_table_insert (hash, "e", "E");
980 g_hash_table_insert (hash, "f", "F");
982 value = g_hash_table_find (hash, find_str, "a");
983 g_assert_cmpstr (value, ==, "A");
985 value = g_hash_table_find (hash, find_str, "b");
986 g_assert_cmpstr (value, ==, "B");
988 value = g_hash_table_find (hash, find_str, "c");
989 g_assert_cmpstr (value, ==, "C");
991 value = g_hash_table_find (hash, find_str, "d");
992 g_assert_cmpstr (value, ==, "D");
994 value = g_hash_table_find (hash, find_str, "e");
995 g_assert_cmpstr (value, ==, "E");
997 value = g_hash_table_find (hash, find_str, "f");
998 g_assert_cmpstr (value, ==, "F");
1000 value = g_hash_table_find (hash, find_str, "0");
1001 g_assert (value == NULL);
1003 g_hash_table_unref (hash);
1006 gboolean seen_key[6];
1009 foreach_func (gpointer key, gpointer value, gpointer data)
1011 seen_key[((char*)key)[0] - 'a'] = TRUE;
1020 hash = g_hash_table_new (g_str_hash, g_str_equal);
1022 g_hash_table_insert (hash, "a", "A");
1023 g_hash_table_insert (hash, "b", "B");
1024 g_hash_table_insert (hash, "c", "C");
1025 g_hash_table_insert (hash, "d", "D");
1026 g_hash_table_insert (hash, "e", "E");
1027 g_hash_table_insert (hash, "f", "F");
1029 for (i = 0; i < 6; i++)
1030 seen_key[i] = FALSE;
1032 g_hash_table_foreach (hash, foreach_func, NULL);
1034 for (i = 0; i < 6; i++)
1035 g_assert (seen_key[i]);
1037 g_hash_table_unref (hash);
1046 gint noccupied; /* nnodes + tombstones */
1052 GHashFunc hash_func;
1053 GEqualFunc key_equal_func;
1054 volatile gint ref_count;
1056 #ifndef G_DISABLE_ASSERT
1059 GDestroyNotify key_destroy_func;
1060 GDestroyNotify value_destroy_func;
1064 count_keys (GHashTable *h, gint *unused, gint *occupied, gint *tombstones)
1071 for (i = 0; i < h->size; i++)
1073 if (h->hashes[i] == 0)
1075 else if (h->hashes[i] == 1)
1083 check_data (GHashTable *h)
1087 for (i = 0; i < h->size; i++)
1089 if (h->hashes[i] < 2)
1091 g_assert (h->keys[i] == NULL);
1092 g_assert (h->values[i] == NULL);
1096 g_assert_cmpint (h->hashes[i], ==, h->hash_func (h->keys[i]));
1102 check_consistency (GHashTable *h)
1108 count_keys (h, &unused, &occupied, &tombstones);
1110 g_assert_cmpint (occupied, ==, h->nnodes);
1111 g_assert_cmpint (occupied + tombstones, ==, h->noccupied);
1112 g_assert_cmpint (occupied + tombstones + unused, ==, h->size);
1118 check_counts (GHashTable *h, gint occupied, gint tombstones)
1120 g_assert_cmpint (occupied, ==, h->nnodes);
1121 g_assert_cmpint (occupied + tombstones, ==, h->noccupied);
1125 trivial_key_destroy (gpointer key)
1130 test_internal_consistency (void)
1134 h = g_hash_table_new_full (g_str_hash, g_str_equal, trivial_key_destroy, NULL);
1136 check_counts (h, 0, 0);
1137 check_consistency (h);
1139 g_hash_table_insert (h, "a", "A");
1140 g_hash_table_insert (h, "b", "B");
1141 g_hash_table_insert (h, "c", "C");
1142 g_hash_table_insert (h, "d", "D");
1143 g_hash_table_insert (h, "e", "E");
1144 g_hash_table_insert (h, "f", "F");
1146 check_counts (h, 6, 0);
1147 check_consistency (h);
1149 g_hash_table_remove (h, "a");
1150 check_counts (h, 5, 1);
1151 check_consistency (h);
1153 g_hash_table_remove (h, "b");
1154 check_counts (h, 4, 2);
1155 check_consistency (h);
1157 g_hash_table_insert (h, "c", "c");
1158 check_counts (h, 4, 2);
1159 check_consistency (h);
1161 g_hash_table_insert (h, "a", "A");
1162 check_counts (h, 5, 1);
1163 check_consistency (h);
1165 g_hash_table_remove_all (h);
1166 check_counts (h, 0, 0);
1167 check_consistency (h);
1169 g_hash_table_unref (h);
1173 my_key_free (gpointer v)
1176 g_assert (s[0] != 'x');
1182 my_value_free (gpointer v)
1185 g_assert (s[0] != 'y');
1191 test_iter_replace (void)
1194 GHashTableIter iter;
1198 g_test_bug ("662544");
1200 h = g_hash_table_new_full (g_str_hash, g_str_equal, my_key_free, my_value_free);
1202 g_hash_table_insert (h, g_strdup ("A"), g_strdup ("a"));
1203 g_hash_table_insert (h, g_strdup ("B"), g_strdup ("b"));
1204 g_hash_table_insert (h, g_strdup ("C"), g_strdup ("c"));
1206 g_hash_table_iter_init (&iter, h);
1208 while (g_hash_table_iter_next (&iter, &k, &v))
1211 g_assert (g_ascii_islower (s[0]));
1212 g_hash_table_iter_replace (&iter, g_strdup (k));
1215 g_hash_table_unref (h);
1219 replace_first_character (gchar *string)
1225 test_set_insert_corruption (void)
1227 GHashTable *hash_table =
1228 g_hash_table_new_full (g_str_hash, g_str_equal,
1229 (GDestroyNotify) replace_first_character, NULL);
1230 GHashTableIter iter;
1233 gpointer key, value;
1235 g_test_bug ("692815");
1237 g_hash_table_insert (hash_table, a, a);
1238 g_assert (g_hash_table_contains (hash_table, "foo"));
1240 g_hash_table_insert (hash_table, b, b);
1242 g_assert_cmpuint (g_hash_table_size (hash_table), ==, 1);
1243 g_hash_table_iter_init (&iter, hash_table);
1244 if (!g_hash_table_iter_next (&iter, &key, &value))
1245 g_assert_not_reached();
1247 /* per the documentation to g_hash_table_insert(), 'b' has now been freed,
1248 * and the sole key in 'hash_table' should be 'a'.
1250 g_assert (key != b);
1251 g_assert (key == a);
1253 g_assert_cmpstr (b, ==, "boo");
1255 /* g_hash_table_insert() also says that the value should now be 'b',
1256 * which is probably not what the caller intended but is precisely what they
1259 g_assert (value == b);
1261 /* even though the hash has now been de-set-ified: */
1262 g_assert (g_hash_table_contains (hash_table, "foo"));
1264 g_hash_table_unref (hash_table);
1268 test_set_to_strv (void)
1274 set = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1275 g_hash_table_add (set, g_strdup ("xyz"));
1276 g_hash_table_add (set, g_strdup ("xyz"));
1277 g_hash_table_add (set, g_strdup ("abc"));
1278 strv = (gchar **) g_hash_table_get_keys_as_array (set, &n);
1279 g_hash_table_steal_all (set);
1280 g_hash_table_unref (set);
1281 g_assert_cmpint (n, ==, 2);
1282 n = g_strv_length (strv);
1283 g_assert_cmpint (n, ==, 2);
1284 if (g_str_equal (strv[0], "abc"))
1285 g_assert_cmpstr (strv[1], ==, "xyz");
1288 g_assert_cmpstr (strv[0], ==, "xyz");
1289 g_assert_cmpstr (strv[1], ==, "abc");
1295 main (int argc, char *argv[])
1297 g_test_init (&argc, &argv, NULL);
1299 g_test_bug_base ("http://bugzilla.gnome.org/");
1301 g_test_add_func ("/hash/misc", test_hash_misc);
1302 g_test_add_data_func ("/hash/one", GINT_TO_POINTER (TRUE), second_hash_test);
1303 g_test_add_data_func ("/hash/honeyman", GINT_TO_POINTER (FALSE), second_hash_test);
1304 g_test_add_func ("/hash/direct", direct_hash_test);
1305 g_test_add_func ("/hash/int64", int64_hash_test);
1306 g_test_add_func ("/hash/double", double_hash_test);
1307 g_test_add_func ("/hash/string", string_hash_test);
1308 g_test_add_func ("/hash/set", set_hash_test);
1309 g_test_add_func ("/hash/set-ref", set_ref_hash_test);
1310 g_test_add_func ("/hash/ref", test_hash_ref);
1311 g_test_add_func ("/hash/remove-all", test_remove_all);
1312 g_test_add_func ("/hash/find", test_find);
1313 g_test_add_func ("/hash/foreach", test_foreach);
1315 /* tests for individual bugs */
1316 g_test_add_func ("/hash/lookup-null-key", test_lookup_null_key);
1317 g_test_add_func ("/hash/destroy-modify", test_destroy_modify);
1318 g_test_add_func ("/hash/consistency", test_internal_consistency);
1319 g_test_add_func ("/hash/iter-replace", test_iter_replace);
1320 g_test_add_func ("/hash/set-insert-corruption", test_set_insert_corruption);
1321 g_test_add_func ("/hash/set-to-strv", test_set_to_strv);
1323 return g_test_run ();