[rx] add missing project file generator helper files.
[mono-project.git] / eglib / test / hashtable.c
blob8eb98852fd161ff7b457cec2a120c1845a4661e1
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 void foreach (gpointer key, gpointer value, gpointer user_data)
11 foreach_count++;
12 if (GPOINTER_TO_INT (user_data) != 'a')
13 foreach_fail = 1;
16 RESULT hash_t1 (void)
18 GHashTable *t = g_hash_table_new (g_str_hash, g_str_equal);
20 foreach_count = 0;
21 foreach_fail = 0;
22 g_hash_table_insert (t, "hello", "world");
23 g_hash_table_insert (t, "my", "god");
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");
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");
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);
45 return OK;
48 RESULT hash_t2 (void)
50 return OK;
53 RESULT hash_default (void)
55 GHashTable *hash = g_hash_table_new (NULL, NULL);
57 if (hash == NULL)
58 return FAILED ("g_hash_table_new should return a valid hash");
60 g_hash_table_destroy (hash);
61 return NULL;
64 RESULT
65 hash_null_lookup (void)
67 GHashTable *hash = g_hash_table_new (NULL, NULL);
68 gpointer ok, ov;
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));
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);
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);
87 g_hash_table_destroy (hash);
89 return NULL;
92 static void
93 counter (gpointer key, gpointer value, gpointer user_data)
95 int *counter = (int *) user_data;
97 (*counter)++;
100 RESULT hash_grow (void)
102 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
103 int i, count = 0;
105 for (i = 0; i < 1000; i++)
106 g_hash_table_insert (hash, g_strdup_printf ("%d", i), g_strdup_printf ("x-%d", i));
108 for (i = 0; i < 1000; i++){
109 char buffer [30];
110 gpointer value;
112 sprintf (buffer, "%d", i);
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);
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));
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");
130 g_hash_table_destroy (hash);
131 return NULL;
134 RESULT hash_iter (void)
136 #if !defined(GLIB_MAJOR_VERSION) || GLIB_CHECK_VERSION(2, 16, 0)
137 GHashTable *hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
138 GHashTableIter iter;
139 int i, sum, keys_sum, values_sum;
140 gpointer key, value;
142 sum = 0;
143 for (i = 0; i < 1000; i++) {
144 sum += i;
145 g_hash_table_insert (hash, GUINT_TO_POINTER (i), GUINT_TO_POINTER (i));
148 keys_sum = values_sum = 0;
149 g_hash_table_iter_init (&iter, hash);
150 while (g_hash_table_iter_next (&iter, &key, &value)) {
151 if (key != value)
152 return FAILED ("key != value");
153 keys_sum += GPOINTER_TO_UINT (key);
154 values_sum += GPOINTER_TO_UINT (value);
156 if (keys_sum != sum || values_sum != sum)
157 return FAILED ("Did not find all key-value pairs");
158 g_hash_table_destroy (hash);
159 return NULL;
160 #else
161 /* GHashTableIter was added in glib 2.16 */
162 return NULL;
163 #endif
166 static Test hashtable_tests [] = {
167 {"t1", hash_t1},
168 {"t2", hash_t2},
169 {"grow", hash_grow},
170 {"default", hash_default},
171 {"null_lookup", hash_null_lookup},
172 {"iter", hash_iter},
173 {NULL, NULL}
176 DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)