2006-08-18 Aaron Bockover <abockover@novell.com>
[mono.git] / eglib / test / hashtable.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
5
6 int foreach_count = 0;
7 int foreach_fail = 0;
8
9 void foreach (gpointer key, gpointer value, gpointer user_data)
10 {
11         foreach_count++;
12         if (GPOINTER_TO_INT (user_data) != 'a')
13                 foreach_fail = 1;
14 }
15
16 RESULT hash_t1 (void)
17 {
18         GHashTable *t = g_hash_table_new (g_str_hash, g_str_equal);
19
20         g_hash_table_insert (t, "hello", "world");
21         g_hash_table_insert (t, "my", "god");
22
23         g_hash_table_foreach (t, foreach, GINT_TO_POINTER('a'));
24         if (foreach_count != 2)
25                 return "did not find all keys";
26         if (foreach_fail)
27                 return "failed to pass the user-data to foreach";
28         
29         if (!g_hash_table_remove (t, "my"))
30                 return "did not find known key";
31         if (g_hash_table_size (t) != 1)
32                 return "unexpected size";
33         g_hash_table_insert(t, "hello", "moon");
34         if (strcmp (g_hash_table_lookup (t, "hello"), "moon") != 0)
35                 return "did not replace world with moon";
36                 
37         if (!g_hash_table_remove (t, "hello"))
38                 return "did not find known key";
39         if (g_hash_table_size (t) != 0)
40                 return "unexpected size";
41         g_hash_table_destroy (t);
42
43         return OK;
44 }
45
46 RESULT hash_t2 (void)
47 {
48         return OK;
49 }
50
51 static Test hashtable_tests [] = {
52         {"hash_t1", hash_t1},
53         {"hash_t2", hash_t2},
54         {NULL, NULL}
55 };
56
57 DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)
58