10 foreach (gpointer key
, gpointer value
, gpointer user_data
)
13 if (GPOINTER_TO_INT (user_data
) != 'a')
20 GHashTable
*t
= g_hash_table_new (g_str_hash
, g_str_equal
);
24 g_hash_table_insert (t
, (char*)"hello", (char*)"world");
25 g_hash_table_insert (t
, (char*)"my", (char*)"god");
27 g_hash_table_foreach (t
, foreach
, GINT_TO_POINTER('a'));
28 if (foreach_count
!= 2)
29 return FAILED ("did not find all keys, got %d expected 2", foreach_count
);
31 return FAILED("failed to pass the user-data to foreach");
33 if (!g_hash_table_remove (t
, (char*)"my"))
34 return FAILED ("did not find known key");
35 if (g_hash_table_size (t
) != 1)
36 return FAILED ("unexpected size");
37 g_hash_table_insert(t
, (char*)"hello", (char*)"moon");
38 if (strcmp (g_hash_table_lookup (t
, (char*)"hello"), (char*)"moon") != 0)
39 return FAILED ("did not replace world with moon");
41 if (!g_hash_table_remove (t
, (char*)"hello"))
42 return FAILED ("did not find known key");
43 if (g_hash_table_size (t
) != 0)
44 return FAILED ("unexpected size");
45 g_hash_table_destroy (t
);
59 GHashTable
*hash
= g_hash_table_new (NULL
, NULL
);
62 return FAILED ("g_hash_table_new should return a valid hash");
64 g_hash_table_destroy (hash
);
69 hash_null_lookup (void)
71 GHashTable
*hash
= g_hash_table_new (NULL
, NULL
);
74 g_hash_table_insert (hash
, NULL
, GINT_TO_POINTER (1));
75 g_hash_table_insert (hash
, GINT_TO_POINTER(1), GINT_TO_POINTER(2));
77 if (!g_hash_table_lookup_extended (hash
, NULL
, &ok
, &ov
))
78 return FAILED ("Did not find the NULL");
80 return FAILED ("Incorrect key found");
81 if (ov
!= GINT_TO_POINTER (1))
82 return FAILED ("Got wrong value %p\n", ov
);
84 if (!g_hash_table_lookup_extended (hash
, GINT_TO_POINTER(1), &ok
, &ov
))
85 return FAILED ("Did not find the 1");
86 if (ok
!= GINT_TO_POINTER(1))
87 return FAILED ("Incorrect key found");
88 if (ov
!= GINT_TO_POINTER (2))
89 return FAILED ("Got wrong value %p\n", ov
);
91 g_hash_table_destroy (hash
);
97 counter (gpointer key
, gpointer value
, gpointer user_data
)
99 int *counter
= (int *) user_data
;
107 GHashTable
*hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
110 for (i
= 0; i
< 1000; i
++)
111 g_hash_table_insert (hash
, g_strdup_printf ("%d", i
), g_strdup_printf ("x-%d", i
));
113 for (i
= 0; i
< 1000; i
++){
117 sprintf (buffer
, "%d", i
);
119 value
= g_hash_table_lookup (hash
, buffer
);
120 sprintf (buffer
, "x-%d", i
);
121 if (strcmp (value
, buffer
) != 0){
122 return FAILED ("Failed to lookup the key %d, the value was %s\n", i
, value
);
126 if (g_hash_table_size (hash
) != 1000)
127 return FAILED ("Did not find 1000 elements on the hash, found %d\n", g_hash_table_size (hash
));
129 /* Now do the manual count, lets not trust the internals */
130 g_hash_table_foreach (hash
, counter
, &count
);
132 return FAILED ("Foreach count is not 1000");
135 g_hash_table_destroy (hash
);
142 #if !defined(GLIB_MAJOR_VERSION) || GLIB_CHECK_VERSION(2, 16, 0)
143 GHashTable
*hash
= g_hash_table_new_full (g_direct_hash
, g_direct_equal
, NULL
, NULL
);
145 int i
, sum
, keys_sum
, values_sum
;
149 for (i
= 0; i
< 1000; i
++) {
151 g_hash_table_insert (hash
, GUINT_TO_POINTER (i
), GUINT_TO_POINTER (i
));
154 keys_sum
= values_sum
= 0;
155 g_hash_table_iter_init (&iter
, hash
);
156 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
158 return FAILED ("key != value");
159 keys_sum
+= GPOINTER_TO_UINT (key
);
160 values_sum
+= GPOINTER_TO_UINT (value
);
162 if (keys_sum
!= sum
|| values_sum
!= sum
)
163 return FAILED ("Did not find all key-value pairs");
164 g_hash_table_destroy (hash
);
167 /* GHashTableIter was added in glib 2.16 */
172 static Test hashtable_tests
[] = {
176 {"default", hash_default
},
177 {"null_lookup", hash_null_lookup
},
182 DEFINE_TEST_GROUP_INIT(hashtable_tests_init
, hashtable_tests
)