summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2007-12-15 03:54:09 +0000
committerMatthias Clasen <matthiasc@src.gnome.org>2007-12-15 03:54:09 +0000
commitd741d3e7a344622e5a534c34e9e2c0488b2fea4d (patch)
tree32063874df7834245eb88f58a117fe5ce4e421ba /tests
parentf5063650794c987868830241a630993e624eabd9 (diff)
Add hash table iterators. (#500507, Jean-Yves Lefort)
2007-12-14 Matthias Clasen <mclasen@redhat.com> * glib/glib.symbols: * glib/ghash.[hc]: Add hash table iterators. (#500507, Jean-Yves Lefort) * tests/hash-test.c: Test iterators. svn path=/trunk/; revision=6130
Diffstat (limited to 'tests')
-rw-r--r--tests/hash-test.c84
1 files changed, 68 insertions, 16 deletions
diff --git a/tests/hash-test.c b/tests/hash-test.c
index 80c2ffb93..23411c354 100644
--- a/tests/hash-test.c
+++ b/tests/hash-test.c
@@ -44,7 +44,50 @@
int array[10000];
+static void
+fill_hash_table_and_array (GHashTable *hash_table)
+{
+ int i;
+ for (i = 0; i < 10000; i++)
+ {
+ array[i] = i;
+ g_hash_table_insert (hash_table, &array[i], &array[i]);
+ }
+}
+
+static void
+init_result_array (int result_array[10000])
+{
+ int i;
+
+ for (i = 0; i < 10000; i++)
+ result_array[i] = -1;
+}
+
+static void
+verify_result_array (int array[10000])
+{
+ int i;
+
+ for (i = 0; i < 10000; i++)
+ g_assert (array[i] == i);
+}
+
+static void
+handle_pair (gpointer key, gpointer value, int result_array[10000])
+{
+ int n;
+
+ g_assert (key == value);
+
+ n = *((int *) value);
+
+ g_assert (n >= 0 && n < 10000);
+ g_assert (result_array[n] == -1);
+
+ result_array[n] = n;
+}
static gboolean
my_hash_callback_remove (gpointer key,
@@ -75,8 +118,7 @@ my_hash_callback (gpointer key,
gpointer value,
gpointer user_data)
{
- int *d = value;
- *d = 1;
+ handle_pair (key, value, user_data);
}
static guint
@@ -347,13 +389,12 @@ main (int argc,
gint *pvalue;
GList *keys, *values;
gint keys_len, values_len;
+ GHashTableIter iter;
+ gpointer ikey, ivalue;
+ int result_array[10000];
hash_table = g_hash_table_new (my_hash, my_hash_equal);
- for (i = 0; i < 10000; i++)
- {
- array[i] = i;
- g_hash_table_insert (hash_table, &array[i], &array[i]);
- }
+ fill_hash_table_and_array (hash_table);
pvalue = g_hash_table_find (hash_table, find_first, &value);
if (!pvalue || *pvalue != value)
g_assert_not_reached();
@@ -373,21 +414,32 @@ main (int argc,
g_list_free (keys);
g_list_free (values);
-
- g_hash_table_foreach (hash_table, my_hash_callback, NULL);
+ init_result_array (result_array);
+ g_hash_table_iter_init (&iter, hash_table);
for (i = 0; i < 10000; i++)
- if (array[i] == 0)
- g_assert_not_reached();
+ {
+ g_assert (g_hash_table_iter_next (&iter, &ikey, &ivalue));
+
+ handle_pair (ikey, ivalue, result_array);
+
+ if (i % 2)
+ g_hash_table_iter_remove (&iter);
+ }
+ g_assert (! g_hash_table_iter_next (&iter, &ikey, &ivalue));
+ g_assert (g_hash_table_size (hash_table) == 5000);
+ verify_result_array (result_array);
+
+ fill_hash_table_and_array (hash_table);
+
+ init_result_array (result_array);
+ g_hash_table_foreach (hash_table, my_hash_callback, result_array);
+ verify_result_array (result_array);
for (i = 0; i < 10000; i++)
g_hash_table_remove (hash_table, &array[i]);
- for (i = 0; i < 10000; i++)
- {
- array[i] = i;
- g_hash_table_insert (hash_table, &array[i], &array[i]);
- }
+ fill_hash_table_and_array (hash_table);
if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
g_hash_table_size (hash_table) != 5000)