0b2f20ca5dfd4c28271334d4a0a7132fafa5e9c2
[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         g_hash_table_destroy (hash);
61         return NULL;
62 }
63
64 RESULT
65 hash_null_lookup (void)
66 {
67         GHashTable *hash = g_hash_table_new (NULL, NULL);
68         gpointer ok, ov;
69                 
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));
72
73         if (!g_hash_table_lookup_extended (hash, NULL, &ok, &ov))
74                 return FAILED ("Did not find the NULL");
75         if (ok != NULL)
76                 return FAILED ("Incorrect key found");
77         if (ov != GINT_TO_POINTER (1))
78                 return FAILED ("Got wrong value %p\n", ov);
79
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);
86         
87         g_hash_table_destroy (hash);
88
89         return NULL;
90 }
91
92 static void
93 counter (gpointer key, gpointer value, gpointer user_data)
94 {
95         int *counter = (int *) user_data;
96
97         (*counter)++;
98 }
99
100 RESULT hash_grow (void)
101 {
102         GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
103         int i, count = 0;
104         
105         for (i = 0; i < 1000; i++)
106                 g_hash_table_insert (hash, g_strdup_printf ("%d", i), g_strdup_printf ("x-%d", i));
107
108         for (i = 0; i < 1000; i++){
109                 char buffer [30];
110                 gpointer value;
111                 
112                 sprintf (buffer, "%d", i);
113
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);
118                 }
119         }
120
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));
123
124         /* Now do the manual count, lets not trust the internals */
125         g_hash_table_foreach (hash, counter, &count);
126         if (count != 1000){
127                 return FAILED ("Foreach count is not 1000");
128         }
129
130         g_hash_table_destroy (hash);
131         return NULL;
132 }
133
134 static Test hashtable_tests [] = {
135         {"t1", hash_t1},
136         {"t2", hash_t2},
137         {"grow", hash_grow},
138         {"default", hash_default},
139         {"null_lookup", hash_null_lookup},
140         {NULL, NULL}
141 };
142
143 DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)
144