2006-08-21 Miguel de Icaza <miguel@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         foreach_count = 0;
21         foreach_fail = 0;
22         g_hash_table_insert (t, "hello", "world");
23         g_hash_table_insert (t, "my", "god");
24
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);
28         if (foreach_fail)
29                 return FAILED("failed to pass the user-data to foreach");
30         
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");
38                 
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);
44
45         return OK;
46 }
47
48 RESULT hash_t2 (void)
49 {
50         return OK;
51 }
52
53 RESULT hash_default (void)
54 {
55         GHashTable *hash = g_hash_table_new (NULL, NULL);
56
57         if (hash == NULL)
58                 return FAILED ("g_hash_table_new should return a valid hash");
59
60         return NULL;
61 }
62
63 RESULT
64 hash_null_lookup (void)
65 {
66         GHashTable *hash = g_hash_table_new (NULL, NULL);
67         gpointer ok, ov;
68                 
69         g_hash_table_insert (hash, NULL, GINT_TO_POINTER (1));
70         g_hash_table_insert (hash, GINT_TO_POINTER(1), GINT_TO_POINTER(2));
71
72         if (!g_hash_table_lookup_extended (hash, NULL, &ok, &ov))
73                 return FAILED ("Did not find the NULL");
74         if (ok != NULL)
75                 return FAILED ("Incorrect key found");
76         if (ov != GINT_TO_POINTER (1))
77                 return FAILED ("Got wrong value %p\n", ov);
78
79         if (!g_hash_table_lookup_extended (hash, GINT_TO_POINTER(1), &ok, &ov))
80                 return FAILED ("Did not find the 1");
81         if (ok != GINT_TO_POINTER(1))
82                 return FAILED ("Incorrect key found");
83         if (ov != GINT_TO_POINTER (2))
84                 return FAILED ("Got wrong value %p\n", ov);
85         
86         g_hash_table_destroy (hash);
87
88         return NULL;
89 }
90
91 static void
92 counter (gpointer key, gpointer value, gpointer user_data)
93 {
94         int *counter = (int *) user_data;
95
96         (*counter)++;
97 }
98
99 RESULT hash_grow (void)
100 {
101         GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
102         int i, count = 0;
103         
104         for (i = 0; i < 1000; i++)
105                 g_hash_table_insert (hash, g_strdup_printf ("%d", i), g_strdup_printf ("x-%d", i));
106
107         for (i = 0; i < 1000; i++){
108                 char buffer [30];
109                 gpointer value;
110                 
111                 sprintf (buffer, "%d", i);
112
113                 value = g_hash_table_lookup (hash, buffer);
114                 sprintf (buffer, "x-%d", i);
115                 if (strcmp (value, buffer) != 0){
116                         return FAILED ("Failed to lookup the key %d, the value was %s\n", i, value);
117                 }
118         }
119
120         if (g_hash_table_size (hash) != 1000)
121                 return FAILED ("Did not find 1000 elements on the hash, found %d\n", g_hash_table_size (hash));
122
123         /* Now do the manual count, lets not trust the internals */
124         g_hash_table_foreach (hash, counter, &count);
125         if (count != 1000){
126                 return FAILED ("Foreach count is not 1000");
127         }
128
129         g_hash_table_destroy (hash);
130         return NULL;
131 }
132
133 static Test hashtable_tests [] = {
134         {"hash_t1", hash_t1},
135         {"hash_t2", hash_t2},
136         {"hash_grow", hash_grow},
137         {"hash_default", hash_default},
138         {"hash_null_lookup", hash_null_lookup},
139         {NULL, NULL}
140 };
141
142 DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)
143