9 void foreach (gpointer key
, gpointer value
, gpointer user_data
)
12 if (GPOINTER_TO_INT (user_data
) != 'a')
18 GHashTable
*t
= g_hash_table_new (g_str_hash
, g_str_equal
);
22 g_hash_table_insert (t
, "hello", "world");
23 g_hash_table_insert (t
, "my", "god");
25 g_hash_table_foreach (t
, foreach
, GINT_TO_POINTER('a'));
26 if (foreach_count
!= 2)
27 return FAILED ("did not find all keys, got %d expected 2", foreach_count
);
29 return FAILED("failed to pass the user-data to foreach");
31 if (!g_hash_table_remove (t
, "my"))
32 return FAILED ("did not find known key");
33 if (g_hash_table_size (t
) != 1)
34 return FAILED ("unexpected size");
35 g_hash_table_insert(t
, "hello", "moon");
36 if (strcmp (g_hash_table_lookup (t
, "hello"), "moon") != 0)
37 return FAILED ("did not replace world with moon");
39 if (!g_hash_table_remove (t
, "hello"))
40 return FAILED ("did not find known key");
41 if (g_hash_table_size (t
) != 0)
42 return FAILED ("unexpected size");
43 g_hash_table_destroy (t
);
53 RESULT
hash_default (void)
55 GHashTable
*hash
= g_hash_table_new (NULL
, NULL
);
58 return FAILED ("g_hash_table_new should return a valid hash");
60 g_hash_table_destroy (hash
);
65 hash_null_lookup (void)
67 GHashTable
*hash
= g_hash_table_new (NULL
, NULL
);
70 g_hash_table_insert (hash
, NULL
, GINT_TO_POINTER (1));
71 g_hash_table_insert (hash
, GINT_TO_POINTER(1), GINT_TO_POINTER(2));
73 if (!g_hash_table_lookup_extended (hash
, NULL
, &ok
, &ov
))
74 return FAILED ("Did not find the NULL");
76 return FAILED ("Incorrect key found");
77 if (ov
!= GINT_TO_POINTER (1))
78 return FAILED ("Got wrong value %p\n", ov
);
80 if (!g_hash_table_lookup_extended (hash
, GINT_TO_POINTER(1), &ok
, &ov
))
81 return FAILED ("Did not find the 1");
82 if (ok
!= GINT_TO_POINTER(1))
83 return FAILED ("Incorrect key found");
84 if (ov
!= GINT_TO_POINTER (2))
85 return FAILED ("Got wrong value %p\n", ov
);
87 g_hash_table_destroy (hash
);
93 counter (gpointer key
, gpointer value
, gpointer user_data
)
95 int *counter
= (int *) user_data
;
100 RESULT
hash_grow (void)
102 GHashTable
*hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
105 for (i
= 0; i
< 1000; i
++)
106 g_hash_table_insert (hash
, g_strdup_printf ("%d", i
), g_strdup_printf ("x-%d", i
));
108 for (i
= 0; i
< 1000; i
++){
112 sprintf (buffer
, "%d", i
);
114 value
= g_hash_table_lookup (hash
, buffer
);
115 sprintf (buffer
, "x-%d", i
);
116 if (strcmp (value
, buffer
) != 0){
117 return FAILED ("Failed to lookup the key %d, the value was %s\n", i
, value
);
121 if (g_hash_table_size (hash
) != 1000)
122 return FAILED ("Did not find 1000 elements on the hash, found %d\n", g_hash_table_size (hash
));
124 /* Now do the manual count, lets not trust the internals */
125 g_hash_table_foreach (hash
, counter
, &count
);
127 return FAILED ("Foreach count is not 1000");
130 g_hash_table_destroy (hash
);
134 static Test hashtable_tests
[] = {
138 {"default", hash_default
},
139 {"null_lookup", hash_null_lookup
},
143 DEFINE_TEST_GROUP_INIT(hashtable_tests_init
, hashtable_tests
)