1 #include <glib-object.h>
4 test_param_value (void)
10 g_value_init (&value, G_TYPE_PARAM);
11 g_assert (G_VALUE_HOLDS_PARAM (&value));
13 p = g_param_spec_int ("my-int", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
15 g_value_take_param (&value, p);
16 p2 = g_value_get_param (&value);
19 pp = g_param_spec_uint ("my-uint", "My UInt", "Blurb", 0, 10, 5, G_PARAM_READWRITE);
20 g_value_set_param (&value, pp);
22 p2 = g_value_dup_param (&value);
23 g_assert (p2 == pp); /* param specs use ref/unref for copy/free */
24 g_param_spec_unref (p2);
26 g_value_unset (&value);
29 static gint destroy_count;
32 my_destroy (gpointer data)
38 test_param_qdata (void)
44 q = g_quark_from_string ("bla");
46 p = g_param_spec_int ("my-int", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
47 g_param_spec_set_qdata (p, q, "bla");
48 bla = g_param_spec_get_qdata (p, q);
49 g_assert_cmpstr (bla, ==, "bla");
51 g_assert_cmpint (destroy_count, ==, 0);
52 g_param_spec_set_qdata_full (p, q, "bla", my_destroy);
53 g_param_spec_set_qdata_full (p, q, "blabla", my_destroy);
54 g_assert_cmpint (destroy_count, ==, 1);
55 g_assert_cmpstr (g_param_spec_steal_qdata (p, q), ==, "blabla");
56 g_assert_cmpint (destroy_count, ==, 1);
57 g_assert (g_param_spec_get_qdata (p, q) == NULL);
59 g_param_spec_ref_sink (p);
61 g_param_spec_unref (p);
65 test_param_validate (void)
68 GValue value = { 0, };
70 p = g_param_spec_int ("my-int", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
72 g_value_init (&value, G_TYPE_INT);
73 g_value_set_int (&value, 100);
74 g_assert (!g_param_value_defaults (p, &value));
75 g_assert (g_param_value_validate (p, &value));
76 g_assert_cmpint (g_value_get_int (&value), ==, 20);
78 g_param_value_set_default (p, &value);
79 g_assert (g_param_value_defaults (p, &value));
80 g_assert_cmpint (g_value_get_int (&value), ==, 10);
82 g_param_spec_unref (p);
86 test_param_strings (void)
90 /* test canonicalization */
91 p = g_param_spec_int ("my_int:bla", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
93 g_assert_cmpstr (g_param_spec_get_name (p), ==, "my-int-bla");
94 g_assert_cmpstr (g_param_spec_get_nick (p), ==, "My Int");
95 g_assert_cmpstr (g_param_spec_get_blurb (p), ==, "Blurb");
97 g_param_spec_unref (p);
99 /* test nick defaults to name */
100 p = g_param_spec_int ("my-int", NULL, NULL, 0, 20, 10, G_PARAM_READWRITE);
102 g_assert_cmpstr (g_param_spec_get_name (p), ==, "my-int");
103 g_assert_cmpstr (g_param_spec_get_nick (p), ==, "my-int");
104 g_assert (g_param_spec_get_blurb (p) == NULL);
106 g_param_spec_unref (p);
110 test_param_convert (void)
116 p = g_param_spec_int ("my-int", "My Int", "Blurb", 0, 20, 10, G_PARAM_READWRITE);
117 g_value_init (&v1, G_TYPE_UINT);
118 g_value_set_uint (&v1, 43);
120 g_value_init (&v2, G_TYPE_INT);
121 g_value_set_int (&v2, -4);
123 g_assert (!g_param_value_convert (p, &v1, &v2, TRUE));
124 g_assert_cmpint (g_value_get_int (&v2), ==, -4);
126 g_assert (g_param_value_convert (p, &v1, &v2, FALSE));
127 g_assert_cmpint (g_value_get_int (&v2), ==, 20);
129 g_param_spec_unref (p);
133 test_value_transform (void)
136 GValue dest = { 0, };
138 #define CHECK_INT_CONVERSION(type, getter, value) \
139 g_assert (g_value_type_transformable (G_TYPE_INT, type)); \
140 g_value_init (&src, G_TYPE_INT); \
141 g_value_init (&dest, type); \
142 g_value_set_int (&src, value); \
143 g_assert (g_value_transform (&src, &dest)); \
144 g_assert_cmpint (g_value_get_##getter (&dest), ==, value); \
145 g_value_unset (&src); \
146 g_value_unset (&dest);
148 /* Keep a check for an integer in the range of 0-127 so we're
149 * still testing g_value_get_char(). See
150 * https://bugzilla.gnome.org/show_bug.cgi?id=659870
151 * for why it is broken.
153 CHECK_INT_CONVERSION(G_TYPE_CHAR, char, 124)
155 CHECK_INT_CONVERSION(G_TYPE_CHAR, schar, -124)
156 CHECK_INT_CONVERSION(G_TYPE_CHAR, schar, 124)
157 CHECK_INT_CONVERSION(G_TYPE_UCHAR, uchar, 0)
158 CHECK_INT_CONVERSION(G_TYPE_UCHAR, uchar, 255)
159 CHECK_INT_CONVERSION(G_TYPE_INT, int, -12345)
160 CHECK_INT_CONVERSION(G_TYPE_INT, int, 12345)
161 CHECK_INT_CONVERSION(G_TYPE_UINT, uint, 0)
162 CHECK_INT_CONVERSION(G_TYPE_UINT, uint, 12345)
163 CHECK_INT_CONVERSION(G_TYPE_LONG, long, -12345678)
164 CHECK_INT_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
165 CHECK_INT_CONVERSION(G_TYPE_INT64, int64, -12345678)
166 CHECK_INT_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
167 CHECK_INT_CONVERSION(G_TYPE_FLOAT, float, 12345678)
168 CHECK_INT_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
170 #define CHECK_UINT_CONVERSION(type, getter, value) \
171 g_assert (g_value_type_transformable (G_TYPE_UINT, type)); \
172 g_value_init (&src, G_TYPE_UINT); \
173 g_value_init (&dest, type); \
174 g_value_set_uint (&src, value); \
175 g_assert (g_value_transform (&src, &dest)); \
176 g_assert_cmpuint (g_value_get_##getter (&dest), ==, value); \
177 g_value_unset (&src); \
178 g_value_unset (&dest);
180 CHECK_UINT_CONVERSION(G_TYPE_CHAR, char, 124)
181 CHECK_UINT_CONVERSION(G_TYPE_CHAR, char, 124)
182 CHECK_UINT_CONVERSION(G_TYPE_UCHAR, uchar, 0)
183 CHECK_UINT_CONVERSION(G_TYPE_UCHAR, uchar, 255)
184 CHECK_UINT_CONVERSION(G_TYPE_INT, int, 12345)
185 CHECK_UINT_CONVERSION(G_TYPE_INT, int, 12345)
186 CHECK_UINT_CONVERSION(G_TYPE_UINT, uint, 0)
187 CHECK_UINT_CONVERSION(G_TYPE_UINT, uint, 12345)
188 CHECK_UINT_CONVERSION(G_TYPE_LONG, long, 12345678)
189 CHECK_UINT_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
190 CHECK_UINT_CONVERSION(G_TYPE_INT64, int64, 12345678)
191 CHECK_UINT_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
192 CHECK_UINT_CONVERSION(G_TYPE_FLOAT, float, 12345678)
193 CHECK_UINT_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
195 #define CHECK_LONG_CONVERSION(type, getter, value) \
196 g_assert (g_value_type_transformable (G_TYPE_LONG, type)); \
197 g_value_init (&src, G_TYPE_LONG); \
198 g_value_init (&dest, type); \
199 g_value_set_long (&src, value); \
200 g_assert (g_value_transform (&src, &dest)); \
201 g_assert_cmpint (g_value_get_##getter (&dest), ==, value); \
202 g_value_unset (&src); \
203 g_value_unset (&dest);
205 CHECK_LONG_CONVERSION(G_TYPE_CHAR, char, -124)
206 CHECK_LONG_CONVERSION(G_TYPE_CHAR, char, 124)
207 CHECK_LONG_CONVERSION(G_TYPE_UCHAR, uchar, 0)
208 CHECK_LONG_CONVERSION(G_TYPE_UCHAR, uchar, 255)
209 CHECK_LONG_CONVERSION(G_TYPE_INT, int, -12345)
210 CHECK_LONG_CONVERSION(G_TYPE_INT, int, 12345)
211 CHECK_LONG_CONVERSION(G_TYPE_UINT, uint, 0)
212 CHECK_LONG_CONVERSION(G_TYPE_UINT, uint, 12345)
213 CHECK_LONG_CONVERSION(G_TYPE_LONG, long, -12345678)
214 CHECK_LONG_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
215 CHECK_LONG_CONVERSION(G_TYPE_INT64, int64, -12345678)
216 CHECK_LONG_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
217 CHECK_LONG_CONVERSION(G_TYPE_FLOAT, float, 12345678)
218 CHECK_LONG_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
220 #define CHECK_ULONG_CONVERSION(type, getter, value) \
221 g_assert (g_value_type_transformable (G_TYPE_ULONG, type)); \
222 g_value_init (&src, G_TYPE_ULONG); \
223 g_value_init (&dest, type); \
224 g_value_set_ulong (&src, value); \
225 g_assert (g_value_transform (&src, &dest)); \
226 g_assert_cmpuint (g_value_get_##getter (&dest), ==, value); \
227 g_value_unset (&src); \
228 g_value_unset (&dest);
230 CHECK_ULONG_CONVERSION(G_TYPE_CHAR, char, 124)
231 CHECK_ULONG_CONVERSION(G_TYPE_CHAR, char, 124)
232 CHECK_ULONG_CONVERSION(G_TYPE_UCHAR, uchar, 0)
233 CHECK_ULONG_CONVERSION(G_TYPE_UCHAR, uchar, 255)
234 CHECK_ULONG_CONVERSION(G_TYPE_INT, int, -12345)
235 CHECK_ULONG_CONVERSION(G_TYPE_INT, int, 12345)
236 CHECK_ULONG_CONVERSION(G_TYPE_UINT, uint, 0)
237 CHECK_ULONG_CONVERSION(G_TYPE_UINT, uint, 12345)
238 CHECK_ULONG_CONVERSION(G_TYPE_LONG, long, 12345678)
239 CHECK_ULONG_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
240 CHECK_ULONG_CONVERSION(G_TYPE_INT64, int64, 12345678)
241 CHECK_ULONG_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
242 CHECK_ULONG_CONVERSION(G_TYPE_FLOAT, float, 12345678)
243 CHECK_ULONG_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
245 #define CHECK_INT64_CONVERSION(type, getter, value) \
246 g_assert (g_value_type_transformable (G_TYPE_INT64, type)); \
247 g_value_init (&src, G_TYPE_INT64); \
248 g_value_init (&dest, type); \
249 g_value_set_int64 (&src, value); \
250 g_assert (g_value_transform (&src, &dest)); \
251 g_assert_cmpint (g_value_get_##getter (&dest), ==, value); \
252 g_value_unset (&src); \
253 g_value_unset (&dest);
255 CHECK_INT64_CONVERSION(G_TYPE_CHAR, char, -124)
256 CHECK_INT64_CONVERSION(G_TYPE_CHAR, char, 124)
257 CHECK_INT64_CONVERSION(G_TYPE_UCHAR, uchar, 0)
258 CHECK_INT64_CONVERSION(G_TYPE_UCHAR, uchar, 255)
259 CHECK_INT64_CONVERSION(G_TYPE_INT, int, -12345)
260 CHECK_INT64_CONVERSION(G_TYPE_INT, int, 12345)
261 CHECK_INT64_CONVERSION(G_TYPE_UINT, uint, 0)
262 CHECK_INT64_CONVERSION(G_TYPE_UINT, uint, 12345)
263 CHECK_INT64_CONVERSION(G_TYPE_LONG, long, -12345678)
264 CHECK_INT64_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
265 CHECK_INT64_CONVERSION(G_TYPE_INT64, int64, -12345678)
266 CHECK_INT64_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
267 CHECK_INT64_CONVERSION(G_TYPE_FLOAT, float, 12345678)
268 CHECK_INT64_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
270 #define CHECK_UINT64_CONVERSION(type, getter, value) \
271 g_assert (g_value_type_transformable (G_TYPE_UINT64, type)); \
272 g_value_init (&src, G_TYPE_UINT64); \
273 g_value_init (&dest, type); \
274 g_value_set_uint64 (&src, value); \
275 g_assert (g_value_transform (&src, &dest)); \
276 g_assert_cmpuint (g_value_get_##getter (&dest), ==, value); \
277 g_value_unset (&src); \
278 g_value_unset (&dest);
280 CHECK_UINT64_CONVERSION(G_TYPE_CHAR, char, -124)
281 CHECK_UINT64_CONVERSION(G_TYPE_CHAR, char, 124)
282 CHECK_UINT64_CONVERSION(G_TYPE_UCHAR, uchar, 0)
283 CHECK_UINT64_CONVERSION(G_TYPE_UCHAR, uchar, 255)
284 CHECK_UINT64_CONVERSION(G_TYPE_INT, int, -12345)
285 CHECK_UINT64_CONVERSION(G_TYPE_INT, int, 12345)
286 CHECK_UINT64_CONVERSION(G_TYPE_UINT, uint, 0)
287 CHECK_UINT64_CONVERSION(G_TYPE_UINT, uint, 12345)
288 CHECK_UINT64_CONVERSION(G_TYPE_LONG, long, -12345678)
289 CHECK_UINT64_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
290 CHECK_UINT64_CONVERSION(G_TYPE_INT64, int64, -12345678)
291 CHECK_UINT64_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
292 CHECK_UINT64_CONVERSION(G_TYPE_FLOAT, float, 12345678)
293 CHECK_UINT64_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
295 #define CHECK_FLOAT_CONVERSION(type, getter, value) \
296 g_assert (g_value_type_transformable (G_TYPE_FLOAT, type)); \
297 g_value_init (&src, G_TYPE_FLOAT); \
298 g_value_init (&dest, type); \
299 g_value_set_float (&src, value); \
300 g_assert (g_value_transform (&src, &dest)); \
301 g_assert_cmpfloat (g_value_get_##getter (&dest), ==, value); \
302 g_value_unset (&src); \
303 g_value_unset (&dest);
305 CHECK_FLOAT_CONVERSION(G_TYPE_CHAR, char, -124)
306 CHECK_FLOAT_CONVERSION(G_TYPE_CHAR, char, 124)
307 CHECK_FLOAT_CONVERSION(G_TYPE_UCHAR, uchar, 0)
308 CHECK_FLOAT_CONVERSION(G_TYPE_UCHAR, uchar, 255)
309 CHECK_FLOAT_CONVERSION(G_TYPE_INT, int, -12345)
310 CHECK_FLOAT_CONVERSION(G_TYPE_INT, int, 12345)
311 CHECK_FLOAT_CONVERSION(G_TYPE_UINT, uint, 0)
312 CHECK_FLOAT_CONVERSION(G_TYPE_UINT, uint, 12345)
313 CHECK_FLOAT_CONVERSION(G_TYPE_LONG, long, -12345678)
314 CHECK_FLOAT_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
315 CHECK_FLOAT_CONVERSION(G_TYPE_INT64, int64, -12345678)
316 CHECK_FLOAT_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
317 CHECK_FLOAT_CONVERSION(G_TYPE_FLOAT, float, 12345678)
318 CHECK_FLOAT_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
320 #define CHECK_DOUBLE_CONVERSION(type, getter, value) \
321 g_assert (g_value_type_transformable (G_TYPE_DOUBLE, type)); \
322 g_value_init (&src, G_TYPE_DOUBLE); \
323 g_value_init (&dest, type); \
324 g_value_set_double (&src, value); \
325 g_assert (g_value_transform (&src, &dest)); \
326 g_assert_cmpfloat (g_value_get_##getter (&dest), ==, value); \
327 g_value_unset (&src); \
328 g_value_unset (&dest);
330 CHECK_DOUBLE_CONVERSION(G_TYPE_CHAR, char, -124)
331 CHECK_DOUBLE_CONVERSION(G_TYPE_CHAR, char, 124)
332 CHECK_DOUBLE_CONVERSION(G_TYPE_UCHAR, uchar, 0)
333 CHECK_DOUBLE_CONVERSION(G_TYPE_UCHAR, uchar, 255)
334 CHECK_DOUBLE_CONVERSION(G_TYPE_INT, int, -12345)
335 CHECK_DOUBLE_CONVERSION(G_TYPE_INT, int, 12345)
336 CHECK_DOUBLE_CONVERSION(G_TYPE_UINT, uint, 0)
337 CHECK_DOUBLE_CONVERSION(G_TYPE_UINT, uint, 12345)
338 CHECK_DOUBLE_CONVERSION(G_TYPE_LONG, long, -12345678)
339 CHECK_DOUBLE_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
340 CHECK_DOUBLE_CONVERSION(G_TYPE_INT64, int64, -12345678)
341 CHECK_DOUBLE_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
342 CHECK_DOUBLE_CONVERSION(G_TYPE_FLOAT, float, 12345678)
343 CHECK_DOUBLE_CONVERSION(G_TYPE_DOUBLE, double, 12345678)
345 #define CHECK_BOOLEAN_CONVERSION(type, setter, value) \
346 g_assert (g_value_type_transformable (type, G_TYPE_BOOLEAN)); \
347 g_value_init (&src, type); \
348 g_value_init (&dest, G_TYPE_BOOLEAN); \
349 g_value_set_##setter (&src, value); \
350 g_assert (g_value_transform (&src, &dest)); \
351 g_assert_cmpint (g_value_get_boolean (&dest), ==, TRUE); \
352 g_value_set_##setter (&src, 0); \
353 g_assert (g_value_transform (&src, &dest)); \
354 g_assert_cmpint (g_value_get_boolean (&dest), ==, FALSE); \
355 g_value_unset (&src); \
356 g_value_unset (&dest);
358 CHECK_BOOLEAN_CONVERSION(G_TYPE_INT, int, -12345)
359 CHECK_BOOLEAN_CONVERSION(G_TYPE_UINT, uint, 12345)
360 CHECK_BOOLEAN_CONVERSION(G_TYPE_LONG, long, -12345678)
361 CHECK_BOOLEAN_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
362 CHECK_BOOLEAN_CONVERSION(G_TYPE_INT64, int64, -12345678)
363 CHECK_BOOLEAN_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
365 #define CHECK_STRING_CONVERSION(int_type, setter, int_value) \
366 g_assert (g_value_type_transformable (int_type, G_TYPE_STRING)); \
367 g_value_init (&src, int_type); \
368 g_value_init (&dest, G_TYPE_STRING); \
369 g_value_set_##setter (&src, int_value); \
370 g_assert (g_value_transform (&src, &dest)); \
371 g_assert_cmpstr (g_value_get_string (&dest), ==, #int_value); \
372 g_value_unset (&src); \
373 g_value_unset (&dest);
375 CHECK_STRING_CONVERSION(G_TYPE_INT, int, -12345)
376 CHECK_STRING_CONVERSION(G_TYPE_UINT, uint, 12345)
377 CHECK_STRING_CONVERSION(G_TYPE_LONG, long, -12345678)
378 CHECK_STRING_CONVERSION(G_TYPE_ULONG, ulong, 12345678)
379 CHECK_STRING_CONVERSION(G_TYPE_INT64, int64, -12345678)
380 CHECK_STRING_CONVERSION(G_TYPE_UINT64, uint64, 12345678)
381 CHECK_STRING_CONVERSION(G_TYPE_FLOAT, float, 0.500000)
382 CHECK_STRING_CONVERSION(G_TYPE_DOUBLE, double, -1.234567)
384 g_assert (!g_value_type_transformable (G_TYPE_STRING, G_TYPE_CHAR));
385 g_value_init (&src, G_TYPE_STRING);
386 g_value_init (&dest, G_TYPE_CHAR);
387 g_value_set_static_string (&src, "bla");
388 g_value_set_schar (&dest, 'c');
389 g_assert (!g_value_transform (&src, &dest));
390 g_assert_cmpint (g_value_get_schar (&dest), ==, 'c');
391 g_value_unset (&src);
392 g_value_unset (&dest);
396 main (int argc, char *argv[])
399 g_test_init (&argc, &argv, NULL);
401 g_test_add_func ("/param/value", test_param_value);
402 g_test_add_func ("/param/strings", test_param_strings);
403 g_test_add_func ("/param/qdata", test_param_qdata);
404 g_test_add_func ("/param/validate", test_param_validate);
405 g_test_add_func ("/param/convert", test_param_convert);
406 g_test_add_func ("/value/transform", test_value_transform);
408 return g_test_run ();