hash test: avoid leaking various keys and values

Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=666115
Reviewed-by: Matthias Clasen <mclasen@redhat.com>
This commit is contained in:
Simon McVittie 2011-12-13 14:46:02 +00:00
parent 5dca72fe67
commit f6b2847e57

View File

@ -861,6 +861,33 @@ set_ref_hash_test (void)
GHashTable *h;
typedef struct {
gchar *string;
gboolean freed;
} FakeFreeData;
GPtrArray *fake_free_data;
static void
fake_free (gpointer dead)
{
guint i;
for (i = 0; i < fake_free_data->len; i++)
{
FakeFreeData *ffd = g_ptr_array_index (fake_free_data, i);
if (ffd->string == (gchar *) dead)
{
g_assert (!ffd->freed);
ffd->freed = TRUE;
return;
}
}
g_assert_not_reached ();
}
static void
value_destroy_insert (gpointer value)
{
@ -870,18 +897,59 @@ value_destroy_insert (gpointer value)
static void
test_destroy_modify (void)
{
FakeFreeData *ffd;
guint i;
g_test_bug ("650459");
h = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, value_destroy_insert);
fake_free_data = g_ptr_array_new ();
g_hash_table_insert (h, g_strdup ("a"), g_strdup ("b"));
g_hash_table_insert (h, g_strdup ("c"), g_strdup ("d"));
g_hash_table_insert (h, g_strdup ("e"), g_strdup ("f"));
g_hash_table_insert (h, g_strdup ("g"), g_strdup ("h"));
g_hash_table_insert (h, g_strdup ("h"), g_strdup ("k"));
g_hash_table_insert (h, g_strdup ("a"), g_strdup ("c"));
h = g_hash_table_new_full (g_str_hash, g_str_equal, fake_free, value_destroy_insert);
ffd = g_new0 (FakeFreeData, 1);
ffd->string = g_strdup ("a");
g_ptr_array_add (fake_free_data, ffd);
g_hash_table_insert (h, ffd->string, "b");
ffd = g_new0 (FakeFreeData, 1);
ffd->string = g_strdup ("c");
g_ptr_array_add (fake_free_data, ffd);
g_hash_table_insert (h, ffd->string, "d");
ffd = g_new0 (FakeFreeData, 1);
ffd->string = g_strdup ("e");
g_ptr_array_add (fake_free_data, ffd);
g_hash_table_insert (h, ffd->string, "f");
ffd = g_new0 (FakeFreeData, 1);
ffd->string = g_strdup ("g");
g_ptr_array_add (fake_free_data, ffd);
g_hash_table_insert (h, ffd->string, "h");
ffd = g_new0 (FakeFreeData, 1);
ffd->string = g_strdup ("h");
g_ptr_array_add (fake_free_data, ffd);
g_hash_table_insert (h, ffd->string, "k");
ffd = g_new0 (FakeFreeData, 1);
ffd->string = g_strdup ("a");
g_ptr_array_add (fake_free_data, ffd);
g_hash_table_insert (h, ffd->string, "c");
g_hash_table_remove (h, "c");
/* that removed everything... */
for (i = 0; i < fake_free_data->len; i++)
{
FakeFreeData *ffd = g_ptr_array_index (fake_free_data, i);
g_assert (ffd->freed);
g_free (ffd);
}
g_ptr_array_unref (fake_free_data);
/* ... so this is a no-op */
g_hash_table_remove (h, "e");
g_hash_table_unref (h);