5 test_quark_basic (void)
8 const gchar
*orig
= "blargh";
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
);
29 test_quark_string (void)
31 const gchar
*orig
= "string1";
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
);
47 test_dataset_basic (void)
49 gpointer location
= (gpointer
)test_dataset_basic
;
50 gpointer other
= (gpointer
)test_quark_basic
;
51 gpointer data
= "test1";
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
;
80 notify (gpointer data
)
86 test_dataset_full (void)
88 gpointer location
= (gpointer
)test_dataset_full
;
90 g_dataset_set_data_full (location
, "test1", "test1", notify
);
93 g_dataset_set_data (location
, "test1", NULL
);
94 g_assert (destroy_count
== 1);
96 g_dataset_set_data_full (location
, "test1", "test1", notify
);
99 g_dataset_remove_data (location
, "test1");
100 g_assert (destroy_count
== 1);
102 g_dataset_set_data_full (location
, "test1", "test1", notify
);
105 g_dataset_remove_no_notify (location
, "test1");
106 g_assert (destroy_count
== 0);
114 gint
*counter
= user_data
;
120 test_dataset_foreach (void)
122 gpointer location
= (gpointer
)test_dataset_foreach
;
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
);
136 test_dataset_destroy (void)
138 gpointer location
= (gpointer
)test_dataset_destroy
;
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);
149 test_dataset_id (void)
151 gpointer location
= (gpointer
)test_dataset_id
;
152 gpointer other
= (gpointer
)test_quark_basic
;
153 gpointer data
= "test1";
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
);
185 free_one (gpointer data
)
188 g_datalist_clear (&list
);
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
);
205 g_test_trap_subprocess (NULL
, 500000, 0);
206 g_test_trap_assert_passed ();
210 test_datalist_basic (void)
216 g_datalist_init (&list
);
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
);
232 test_datalist_id (void)
238 g_datalist_init (&list
);
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 ();