Increase the timeout for some GLib tests
[glib.git] / glib / tests / dataset.c
bloba92760554269a0e0064aaf1570bb8b07b917f5a1
1 #include <glib.h>
2 #include <stdlib.h>
4 static void
5 test_quark_basic (void)
7 GQuark quark;
8 const gchar *orig = "blargh";
9 gchar *copy;
10 const gchar *str;
12 quark = g_quark_try_string ("no-such-quark");
13 g_assert (quark == 0);
15 copy = g_strdup (orig);
16 quark = g_quark_from_static_string (orig);
17 g_assert (quark != 0);
18 g_assert (g_quark_from_string (orig) == quark);
19 g_assert (g_quark_from_string (copy) == quark);
20 g_assert (g_quark_try_string (orig) == quark);
22 str = g_quark_to_string (quark);
23 g_assert_cmpstr (str, ==, orig);
25 g_free (copy);
28 static void
29 test_quark_string (void)
31 const gchar *orig = "string1";
32 gchar *copy;
33 const gchar *str1;
34 const gchar *str2;
36 copy = g_strdup (orig);
38 str1 = g_intern_static_string (orig);
39 str2 = g_intern_string (copy);
40 g_assert (str1 == str2);
41 g_assert (str1 == orig);
43 g_free (copy);
46 static void
47 test_dataset_basic (void)
49 gpointer location = (gpointer)test_dataset_basic;
50 gpointer other = (gpointer)test_quark_basic;
51 gpointer data = "test1";
52 gpointer ret;
54 g_dataset_set_data (location, "test1", data);
56 ret = g_dataset_get_data (location, "test1");
57 g_assert (ret == data);
59 ret = g_dataset_get_data (location, "test2");
60 g_assert (ret == NULL);
62 ret = g_dataset_get_data (other, "test1");
63 g_assert (ret == NULL);
65 g_dataset_set_data (location, "test1", "new-value");
66 ret = g_dataset_get_data (location, "test1");
67 g_assert (ret != data);
69 g_dataset_remove_data (location, "test1");
70 ret = g_dataset_get_data (location, "test1");
71 g_assert (ret == NULL);
73 ret = g_dataset_get_data (location, NULL);
74 g_assert (ret == NULL);
77 static gint destroy_count;
79 static void
80 notify (gpointer data)
82 destroy_count++;
85 static void
86 test_dataset_full (void)
88 gpointer location = (gpointer)test_dataset_full;
90 g_dataset_set_data_full (location, "test1", "test1", notify);
92 destroy_count = 0;
93 g_dataset_set_data (location, "test1", NULL);
94 g_assert (destroy_count == 1);
96 g_dataset_set_data_full (location, "test1", "test1", notify);
98 destroy_count = 0;
99 g_dataset_remove_data (location, "test1");
100 g_assert (destroy_count == 1);
102 g_dataset_set_data_full (location, "test1", "test1", notify);
104 destroy_count = 0;
105 g_dataset_remove_no_notify (location, "test1");
106 g_assert (destroy_count == 0);
109 static void
110 foreach (GQuark id,
111 gpointer data,
112 gpointer user_data)
114 gint *counter = user_data;
116 *counter += 1;
119 static void
120 test_dataset_foreach (void)
122 gpointer location = (gpointer)test_dataset_foreach;
123 gint my_count;
125 my_count = 0;
126 g_dataset_set_data_full (location, "test1", "test1", notify);
127 g_dataset_set_data_full (location, "test2", "test2", notify);
128 g_dataset_set_data_full (location, "test3", "test3", notify);
129 g_dataset_foreach (location, foreach, &my_count);
130 g_assert (my_count == 3);
132 g_dataset_destroy (location);
135 static void
136 test_dataset_destroy (void)
138 gpointer location = (gpointer)test_dataset_destroy;
140 destroy_count = 0;
141 g_dataset_set_data_full (location, "test1", "test1", notify);
142 g_dataset_set_data_full (location, "test2", "test2", notify);
143 g_dataset_set_data_full (location, "test3", "test3", notify);
144 g_dataset_destroy (location);
145 g_assert (destroy_count == 3);
148 static void
149 test_dataset_id (void)
151 gpointer location = (gpointer)test_dataset_id;
152 gpointer other = (gpointer)test_quark_basic;
153 gpointer data = "test1";
154 gpointer ret;
155 GQuark quark;
157 quark = g_quark_from_string ("test1");
159 g_dataset_id_set_data (location, quark, data);
161 ret = g_dataset_id_get_data (location, quark);
162 g_assert (ret == data);
164 ret = g_dataset_id_get_data (location, g_quark_from_string ("test2"));
165 g_assert (ret == NULL);
167 ret = g_dataset_id_get_data (other, quark);
168 g_assert (ret == NULL);
170 g_dataset_id_set_data (location, quark, "new-value");
171 ret = g_dataset_id_get_data (location, quark);
172 g_assert (ret != data);
174 g_dataset_id_remove_data (location, quark);
175 ret = g_dataset_id_get_data (location, quark);
176 g_assert (ret == NULL);
178 ret = g_dataset_id_get_data (location, 0);
179 g_assert (ret == NULL);
182 static GData *list;
184 static void
185 free_one (gpointer data)
187 /* recurse */
188 g_datalist_clear (&list);
191 static void
192 test_datalist_clear (void)
194 /* Need to use a subprocess because it will deadlock if it fails */
195 if (g_test_subprocess ())
197 g_datalist_init (&list);
198 g_datalist_set_data_full (&list, "one", GINT_TO_POINTER (1), free_one);
199 g_datalist_set_data_full (&list, "two", GINT_TO_POINTER (2), NULL);
200 g_datalist_clear (&list);
201 g_assert (list == NULL);
202 return;
205 g_test_trap_subprocess (NULL, 500000, 0);
206 g_test_trap_assert_passed ();
209 static void
210 test_datalist_basic (void)
212 GData *list = NULL;
213 gpointer data;
214 gpointer ret;
216 g_datalist_init (&list);
217 data = "one";
218 g_datalist_set_data (&list, "one", data);
219 ret = g_datalist_get_data (&list, "one");
220 g_assert (ret == data);
222 ret = g_datalist_get_data (&list, "two");
223 g_assert (ret == NULL);
225 ret = g_datalist_get_data (&list, NULL);
226 g_assert (ret == NULL);
228 g_datalist_clear (&list);
231 static void
232 test_datalist_id (void)
234 GData *list = NULL;
235 gpointer data;
236 gpointer ret;
238 g_datalist_init (&list);
239 data = "one";
240 g_datalist_id_set_data (&list, g_quark_from_string ("one"), data);
241 ret = g_datalist_id_get_data (&list, g_quark_from_string ("one"));
242 g_assert (ret == data);
244 ret = g_datalist_id_get_data (&list, g_quark_from_string ("two"));
245 g_assert (ret == NULL);
247 ret = g_datalist_id_get_data (&list, 0);
248 g_assert (ret == NULL);
250 g_datalist_clear (&list);
254 main (int argc, char *argv[])
256 g_test_init (&argc, &argv, NULL);
258 g_test_add_func ("/quark/basic", test_quark_basic);
259 g_test_add_func ("/quark/string", test_quark_string);
260 g_test_add_func ("/dataset/basic", test_dataset_basic);
261 g_test_add_func ("/dataset/id", test_dataset_id);
262 g_test_add_func ("/dataset/full", test_dataset_full);
263 g_test_add_func ("/dataset/foreach", test_dataset_foreach);
264 g_test_add_func ("/dataset/destroy", test_dataset_destroy);
265 g_test_add_func ("/datalist/basic", test_datalist_basic);
266 g_test_add_func ("/datalist/id", test_datalist_id);
267 g_test_add_func ("/datalist/recursive-clear", test_datalist_clear);
269 return g_test_run ();