[bcl] Update BCL Linked Size
[mono-project.git] / mono / eglib / test / hashtable.c
blob94878f3fa8e1c7c8865a658685eb09db0de84857
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
6 int foreach_count = 0;
7 int foreach_fail = 0;
9 static void
10 foreach (gpointer key, gpointer value, gpointer user_data)
12 foreach_count++;
13 if (GPOINTER_TO_INT (user_data) != 'a')
14 foreach_fail = 1;
17 static RESULT
18 hash_t1 (void)
20 GHashTable *t = g_hash_table_new (g_str_hash, g_str_equal);
22 foreach_count = 0;
23 foreach_fail = 0;
24 g_hash_table_insert (t, (char*)"hello", (char*)"world");
25 g_hash_table_insert (t, (char*)"my", (char*)"god");
27 g_hash_table_foreach (t, foreach, GINT_TO_POINTER('a'));
28 if (foreach_count != 2)
29 return FAILED ("did not find all keys, got %d expected 2", foreach_count);
30 if (foreach_fail)
31 return FAILED("failed to pass the user-data to foreach");
33 if (!g_hash_table_remove (t, (char*)"my"))
34 return FAILED ("did not find known key");
35 if (g_hash_table_size (t) != 1)
36 return FAILED ("unexpected size");
37 g_hash_table_insert(t, (char*)"hello", (char*)"moon");
38 if (strcmp (g_hash_table_lookup (t, (char*)"hello"), (char*)"moon") != 0)
39 return FAILED ("did not replace world with moon");
41 if (!g_hash_table_remove (t, (char*)"hello"))
42 return FAILED ("did not find known key");
43 if (g_hash_table_size (t) != 0)
44 return FAILED ("unexpected size");
45 g_hash_table_destroy (t);
47 return OK;
50 static RESULT
51 hash_t2 (void)
53 return OK;
56 static RESULT
57 hash_default (void)
59 GHashTable *hash = g_hash_table_new (NULL, NULL);
61 if (hash == NULL)
62 return FAILED ("g_hash_table_new should return a valid hash");
64 g_hash_table_destroy (hash);
65 return NULL;
68 static RESULT
69 hash_null_lookup (void)
71 GHashTable *hash = g_hash_table_new (NULL, NULL);
72 gpointer ok, ov;
74 g_hash_table_insert (hash, NULL, GINT_TO_POINTER (1));
75 g_hash_table_insert (hash, GINT_TO_POINTER(1), GINT_TO_POINTER(2));
77 if (!g_hash_table_lookup_extended (hash, NULL, &ok, &ov))
78 return FAILED ("Did not find the NULL");
79 if (ok != NULL)
80 return FAILED ("Incorrect key found");
81 if (ov != GINT_TO_POINTER (1))
82 return FAILED ("Got wrong value %p\n", ov);
84 if (!g_hash_table_lookup_extended (hash, GINT_TO_POINTER(1), &ok, &ov))
85 return FAILED ("Did not find the 1");
86 if (ok != GINT_TO_POINTER(1))
87 return FAILED ("Incorrect key found");
88 if (ov != GINT_TO_POINTER (2))
89 return FAILED ("Got wrong value %p\n", ov);
91 g_hash_table_destroy (hash);
93 return NULL;
96 static void
97 counter (gpointer key, gpointer value, gpointer user_data)
99 int *counter = (int *) user_data;
101 (*counter)++;
104 static RESULT
105 hash_grow (void)
107 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
108 int i, count = 0;
110 for (i = 0; i < 1000; i++)
111 g_hash_table_insert (hash, g_strdup_printf ("%d", i), g_strdup_printf ("x-%d", i));
113 for (i = 0; i < 1000; i++){
114 char buffer [30];
115 gpointer value;
117 sprintf (buffer, "%d", i);
119 value = g_hash_table_lookup (hash, buffer);
120 sprintf (buffer, "x-%d", i);
121 if (strcmp (value, buffer) != 0){
122 return FAILED ("Failed to lookup the key %d, the value was %s\n", i, value);
126 if (g_hash_table_size (hash) != 1000)
127 return FAILED ("Did not find 1000 elements on the hash, found %d\n", g_hash_table_size (hash));
129 /* Now do the manual count, lets not trust the internals */
130 g_hash_table_foreach (hash, counter, &count);
131 if (count != 1000){
132 return FAILED ("Foreach count is not 1000");
135 g_hash_table_destroy (hash);
136 return NULL;
139 static RESULT
140 hash_iter (void)
142 #if !defined(GLIB_MAJOR_VERSION) || GLIB_CHECK_VERSION(2, 16, 0)
143 GHashTable *hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
144 GHashTableIter iter;
145 int i, sum, keys_sum, values_sum;
146 gpointer key, value;
148 sum = 0;
149 for (i = 0; i < 1000; i++) {
150 sum += i;
151 g_hash_table_insert (hash, GUINT_TO_POINTER (i), GUINT_TO_POINTER (i));
154 keys_sum = values_sum = 0;
155 g_hash_table_iter_init (&iter, hash);
156 while (g_hash_table_iter_next (&iter, &key, &value)) {
157 if (key != value)
158 return FAILED ("key != value");
159 keys_sum += GPOINTER_TO_UINT (key);
160 values_sum += GPOINTER_TO_UINT (value);
162 if (keys_sum != sum || values_sum != sum)
163 return FAILED ("Did not find all key-value pairs");
164 g_hash_table_destroy (hash);
165 return NULL;
166 #else
167 /* GHashTableIter was added in glib 2.16 */
168 return NULL;
169 #endif
172 static Test hashtable_tests [] = {
173 {"t1", hash_t1},
174 {"t2", hash_t2},
175 {"grow", hash_grow},
176 {"default", hash_default},
177 {"null_lookup", hash_null_lookup},
178 {"iter", hash_iter},
179 {NULL, NULL}
182 DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)