1 #define GLIB_DISABLE_DEPRECATION_WARNINGS
3 #include <glib-object.h>
5 typedef struct _MyBoxed MyBoxed
;
14 my_boxed_copy (gpointer orig
)
19 b
= g_slice_new (MyBoxed
);
20 b
->ivalue
= a
->ivalue
;
21 b
->bla
= g_strdup (a
->bla
);
26 static gint my_boxed_free_count
;
29 my_boxed_free (gpointer orig
)
34 g_slice_free (MyBoxed
, a
);
36 my_boxed_free_count
++;
39 static GType
my_boxed_get_type (void);
40 #define MY_TYPE_BOXED (my_boxed_get_type ())
42 G_DEFINE_BOXED_TYPE (MyBoxed
, my_boxed
, my_boxed_copy
, my_boxed_free
)
45 test_define_boxed (void)
51 a
.bla
= g_strdup ("bla");
53 b
= g_boxed_copy (MY_TYPE_BOXED
, &a
);
55 g_assert_cmpint (b
->ivalue
, ==, 20);
56 g_assert_cmpstr (b
->bla
, ==, "bla");
58 g_boxed_free (MY_TYPE_BOXED
, b
);
64 test_boxed_ownership (void)
66 GValue value
= G_VALUE_INIT
;
67 static MyBoxed boxed
= { 10, "bla" };
69 g_value_init (&value
, MY_TYPE_BOXED
);
71 my_boxed_free_count
= 0;
73 g_value_set_static_boxed (&value
, &boxed
);
74 g_value_reset (&value
);
76 g_assert_cmpint (my_boxed_free_count
, ==, 0);
78 g_value_set_boxed_take_ownership (&value
, g_boxed_copy (MY_TYPE_BOXED
, &boxed
));
79 g_value_reset (&value
);
80 g_assert_cmpint (my_boxed_free_count
, ==, 1);
82 g_value_take_boxed (&value
, g_boxed_copy (MY_TYPE_BOXED
, &boxed
));
83 g_value_reset (&value
);
84 g_assert_cmpint (my_boxed_free_count
, ==, 2);
86 g_value_set_boxed (&value
, &boxed
);
87 g_value_reset (&value
);
88 g_assert_cmpint (my_boxed_free_count
, ==, 3);
92 my_callback (gpointer user_data
)
96 static gint destroy_count
;
99 my_closure_notify (gpointer user_data
, GClosure
*closure
)
105 test_boxed_closure (void)
109 GValue value
= G_VALUE_INIT
;
111 g_value_init (&value
, G_TYPE_CLOSURE
);
112 g_assert (G_VALUE_HOLDS_BOXED (&value
));
114 closure
= g_cclosure_new (G_CALLBACK (my_callback
), "bla", my_closure_notify
);
115 g_value_take_boxed (&value
, closure
);
117 closure2
= g_value_get_boxed (&value
);
118 g_assert (closure2
== closure
);
120 closure2
= g_value_dup_boxed (&value
);
121 g_assert (closure2
== closure
); /* closures use ref/unref for copy/free */
122 g_closure_unref (closure2
);
124 g_value_unset (&value
);
125 g_assert_cmpint (destroy_count
, ==, 1);
129 test_boxed_date (void)
133 GValue value
= G_VALUE_INIT
;
135 g_value_init (&value
, G_TYPE_DATE
);
136 g_assert (G_VALUE_HOLDS_BOXED (&value
));
138 date
= g_date_new_dmy (1, 3, 1970);
139 g_value_take_boxed (&value
, date
);
141 date2
= g_value_get_boxed (&value
);
142 g_assert (date2
== date
);
144 date2
= g_value_dup_boxed (&value
);
145 g_assert (date2
!= date
);
146 g_assert (g_date_compare (date
, date2
) == 0);
149 g_value_unset (&value
);
153 test_boxed_value (void)
155 GValue value1
= G_VALUE_INIT
;
157 GValue value
= G_VALUE_INIT
;
159 g_value_init (&value
, G_TYPE_VALUE
);
160 g_assert (G_VALUE_HOLDS_BOXED (&value
));
162 g_value_init (&value1
, G_TYPE_INT
);
163 g_value_set_int (&value1
, 26);
165 g_value_set_static_boxed (&value
, &value1
);
167 value2
= g_value_get_boxed (&value
);
168 g_assert (value2
== &value1
);
170 value2
= g_value_dup_boxed (&value
);
171 g_assert (value2
!= &value1
);
172 g_assert (G_VALUE_HOLDS_INT (value2
));
173 g_assert_cmpint (g_value_get_int (value2
), ==, 26);
174 g_boxed_free (G_TYPE_VALUE
, value2
);
176 g_value_unset (&value
);
180 test_boxed_string (void)
184 GValue value
= G_VALUE_INIT
;
186 g_value_init (&value
, G_TYPE_GSTRING
);
187 g_assert (G_VALUE_HOLDS_BOXED (&value
));
189 v
= g_string_new ("bla");
190 g_value_take_boxed (&value
, v
);
192 v2
= g_value_get_boxed (&value
);
195 v2
= g_value_dup_boxed (&value
);
197 g_assert (g_string_equal (v
, v2
));
198 g_string_free (v2
, TRUE
);
200 g_value_unset (&value
);
204 test_boxed_hashtable (void)
208 GValue value
= G_VALUE_INIT
;
210 g_value_init (&value
, G_TYPE_HASH_TABLE
);
211 g_assert (G_VALUE_HOLDS_BOXED (&value
));
213 v
= g_hash_table_new (g_str_hash
, g_str_equal
);
214 g_value_take_boxed (&value
, v
);
216 v2
= g_value_get_boxed (&value
);
219 v2
= g_value_dup_boxed (&value
);
220 g_assert (v2
== v
); /* hash tables use ref/unref for copy/free */
221 g_hash_table_unref (v2
);
223 g_value_unset (&value
);
227 test_boxed_array (void)
231 GValue value
= G_VALUE_INIT
;
233 g_value_init (&value
, G_TYPE_ARRAY
);
234 g_assert (G_VALUE_HOLDS_BOXED (&value
));
236 v
= g_array_new (TRUE
, FALSE
, 1);
237 g_value_take_boxed (&value
, v
);
239 v2
= g_value_get_boxed (&value
);
242 v2
= g_value_dup_boxed (&value
);
243 g_assert (v2
== v
); /* arrays use ref/unref for copy/free */
246 g_value_unset (&value
);
250 test_boxed_ptrarray (void)
254 GValue value
= G_VALUE_INIT
;
256 g_value_init (&value
, G_TYPE_PTR_ARRAY
);
257 g_assert (G_VALUE_HOLDS_BOXED (&value
));
259 v
= g_ptr_array_new ();
260 g_value_take_boxed (&value
, v
);
262 v2
= g_value_get_boxed (&value
);
265 v2
= g_value_dup_boxed (&value
);
266 g_assert (v2
== v
); /* ptr arrays use ref/unref for copy/free */
267 g_ptr_array_unref (v2
);
269 g_value_unset (&value
);
273 test_boxed_regex (void)
277 GValue value
= G_VALUE_INIT
;
279 g_value_init (&value
, G_TYPE_REGEX
);
280 g_assert (G_VALUE_HOLDS_BOXED (&value
));
282 v
= g_regex_new ("a+b+", 0, 0, NULL
);
283 g_value_take_boxed (&value
, v
);
285 v2
= g_value_get_boxed (&value
);
288 v2
= g_value_dup_boxed (&value
);
289 g_assert (v2
== v
); /* regexes use ref/unref for copy/free */
292 g_value_unset (&value
);
296 test_boxed_matchinfo (void)
299 GMatchInfo
*info
, *info2
;
301 GValue value
= G_VALUE_INIT
;
303 g_value_init (&value
, G_TYPE_MATCH_INFO
);
304 g_assert (G_VALUE_HOLDS_BOXED (&value
));
306 r
= g_regex_new ("ab", 0, 0, NULL
);
307 ret
= g_regex_match (r
, "blabla abab bla", 0, &info
);
309 g_value_take_boxed (&value
, info
);
311 info2
= g_value_get_boxed (&value
);
312 g_assert (info
== info2
);
314 info2
= g_value_dup_boxed (&value
);
315 g_assert (info
== info2
); /* matchinfo uses ref/unref for copy/free */
316 g_match_info_unref (info2
);
318 g_value_unset (&value
);
323 test_boxed_varianttype (void)
327 GValue value
= G_VALUE_INIT
;
329 g_value_init (&value
, G_TYPE_VARIANT_TYPE
);
330 g_assert (G_VALUE_HOLDS_BOXED (&value
));
332 v
= g_variant_type_new ("mas");
333 g_value_take_boxed (&value
, v
);
335 v2
= g_value_get_boxed (&value
);
338 v2
= g_value_dup_boxed (&value
);
340 g_assert_cmpstr (g_variant_type_peek_string (v
), ==, g_variant_type_peek_string (v2
));
341 g_variant_type_free (v2
);
343 g_value_unset (&value
);
347 test_boxed_datetime (void)
351 GValue value
= G_VALUE_INIT
;
353 g_value_init (&value
, G_TYPE_DATE_TIME
);
354 g_assert (G_VALUE_HOLDS_BOXED (&value
));
356 v
= g_date_time_new_now_local ();
357 g_value_take_boxed (&value
, v
);
359 v2
= g_value_get_boxed (&value
);
362 v2
= g_value_dup_boxed (&value
);
363 g_assert (v2
== v
); /* datetime uses ref/unref for copy/free */
364 g_date_time_unref (v2
);
366 g_value_unset (&value
);
370 test_boxed_error (void)
374 GValue value
= G_VALUE_INIT
;
376 g_value_init (&value
, G_TYPE_ERROR
);
377 g_assert (G_VALUE_HOLDS_BOXED (&value
));
379 v
= g_error_new_literal (G_VARIANT_PARSE_ERROR
,
380 G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG
,
382 g_value_take_boxed (&value
, v
);
384 v2
= g_value_get_boxed (&value
);
387 v2
= g_value_dup_boxed (&value
);
389 g_assert_cmpint (v
->domain
, ==, v2
->domain
);
390 g_assert_cmpint (v
->code
, ==, v2
->code
);
391 g_assert_cmpstr (v
->message
, ==, v2
->message
);
394 g_value_unset (&value
);
398 test_boxed_keyfile (void)
401 GValue value
= G_VALUE_INIT
;
403 g_value_init (&value
, G_TYPE_KEY_FILE
);
404 g_assert (G_VALUE_HOLDS_BOXED (&value
));
406 k
= g_key_file_new ();
407 g_value_take_boxed (&value
, k
);
409 k2
= g_value_get_boxed (&value
);
412 k2
= g_value_dup_boxed (&value
);
413 g_assert (k
== k2
); /* keyfile uses ref/unref for copy/free */
414 g_key_file_unref (k2
);
416 g_value_unset (&value
);
420 test_boxed_mainloop (void)
423 GValue value
= G_VALUE_INIT
;
425 g_value_init (&value
, G_TYPE_MAIN_LOOP
);
426 g_assert (G_VALUE_HOLDS_BOXED (&value
));
428 l
= g_main_loop_new (NULL
, FALSE
);
429 g_value_take_boxed (&value
, l
);
431 l2
= g_value_get_boxed (&value
);
434 l2
= g_value_dup_boxed (&value
);
435 g_assert (l
== l2
); /* mainloop uses ref/unref for copy/free */
436 g_main_loop_unref (l2
);
438 g_value_unset (&value
);
442 test_boxed_maincontext (void)
444 GMainContext
*c
, *c2
;
445 GValue value
= G_VALUE_INIT
;
447 g_value_init (&value
, G_TYPE_MAIN_CONTEXT
);
448 g_assert (G_VALUE_HOLDS_BOXED (&value
));
450 c
= g_main_context_new ();
451 g_value_take_boxed (&value
, c
);
453 c2
= g_value_get_boxed (&value
);
456 c2
= g_value_dup_boxed (&value
);
457 g_assert (c
== c2
); /* maincontext uses ref/unref for copy/free */
458 g_main_context_unref (c2
);
460 g_value_unset (&value
);
464 test_boxed_source (void)
467 GValue value
= G_VALUE_INIT
;
469 g_value_init (&value
, G_TYPE_SOURCE
);
470 g_assert (G_VALUE_HOLDS_BOXED (&value
));
472 s
= g_idle_source_new ();
473 g_value_take_boxed (&value
, s
);
475 s2
= g_value_get_boxed (&value
);
478 s2
= g_value_dup_boxed (&value
);
479 g_assert (s
== s2
); /* source uses ref/unref for copy/free */
482 g_value_unset (&value
);
486 test_boxed_variantbuilder (void)
488 GVariantBuilder
*v
, *v2
;
489 GValue value
= G_VALUE_INIT
;
491 g_value_init (&value
, G_TYPE_VARIANT_BUILDER
);
492 g_assert (G_VALUE_HOLDS_BOXED (&value
));
494 v
= g_variant_builder_new (G_VARIANT_TYPE_OBJECT_PATH_ARRAY
);
495 g_value_take_boxed (&value
, v
);
497 v2
= g_value_get_boxed (&value
);
500 v2
= g_value_dup_boxed (&value
);
501 g_assert (v
== v2
); /* variantbuilder uses ref/unref for copy/free */
502 g_variant_builder_unref (v2
);
504 g_value_unset (&value
);
508 test_boxed_timezone (void)
511 GValue value
= G_VALUE_INIT
;
513 g_value_init (&value
, G_TYPE_TIME_ZONE
);
514 g_assert (G_VALUE_HOLDS_BOXED (&value
));
516 z
= g_time_zone_new_utc ();
517 g_value_take_boxed (&value
, z
);
519 z2
= g_value_get_boxed (&value
);
522 z2
= g_value_dup_boxed (&value
);
523 g_assert (z
== z2
); /* timezone uses ref/unref for copy/free */
524 g_time_zone_unref (z2
);
526 g_value_unset (&value
);
530 test_boxed_pollfd (void)
533 GValue value
= G_VALUE_INIT
;
535 g_value_init (&value
, G_TYPE_POLLFD
);
536 g_assert (G_VALUE_HOLDS_BOXED (&value
));
538 p
= g_new (GPollFD
, 1);
539 g_value_take_boxed (&value
, p
);
541 p2
= g_value_get_boxed (&value
);
544 p2
= g_value_dup_boxed (&value
);
548 g_value_unset (&value
);
552 test_boxed_markup (void)
554 GMarkupParseContext
*c
, *c2
;
555 const GMarkupParser parser
= { 0 };
556 GValue value
= G_VALUE_INIT
;
558 g_value_init (&value
, G_TYPE_MARKUP_PARSE_CONTEXT
);
559 g_assert (G_VALUE_HOLDS_BOXED (&value
));
561 c
= g_markup_parse_context_new (&parser
, 0, NULL
, NULL
);
562 g_value_take_boxed (&value
, c
);
564 c2
= g_value_get_boxed (&value
);
567 c2
= g_value_dup_boxed (&value
);
569 g_markup_parse_context_unref (c2
);
571 g_value_unset (&value
);
575 test_boxed_thread (void)
578 GValue value
= G_VALUE_INIT
;
580 g_value_init (&value
, G_TYPE_THREAD
);
581 g_assert (G_VALUE_HOLDS_BOXED (&value
));
583 t
= g_thread_self ();
584 g_value_set_boxed (&value
, t
);
586 t2
= g_value_get_boxed (&value
);
589 t2
= g_value_dup_boxed (&value
);
593 g_value_unset (&value
);
597 test_boxed_checksum (void)
600 GValue value
= G_VALUE_INIT
;
602 g_value_init (&value
, G_TYPE_CHECKSUM
);
603 g_assert (G_VALUE_HOLDS_BOXED (&value
));
605 c
= g_checksum_new (G_CHECKSUM_SHA512
);
606 g_value_take_boxed (&value
, c
);
608 c2
= g_value_get_boxed (&value
);
611 c2
= g_value_dup_boxed (&value
);
613 g_checksum_free (c2
);
615 g_value_unset (&value
);
619 main (int argc
, char *argv
[])
621 g_test_init (&argc
, &argv
, NULL
);
623 g_test_add_func ("/boxed/define", test_define_boxed
);
624 g_test_add_func ("/boxed/ownership", test_boxed_ownership
);
625 g_test_add_func ("/boxed/closure", test_boxed_closure
);
626 g_test_add_func ("/boxed/date", test_boxed_date
);
627 g_test_add_func ("/boxed/value", test_boxed_value
);
628 g_test_add_func ("/boxed/string", test_boxed_string
);
629 g_test_add_func ("/boxed/hashtable", test_boxed_hashtable
);
630 g_test_add_func ("/boxed/array", test_boxed_array
);
631 g_test_add_func ("/boxed/ptrarray", test_boxed_ptrarray
);
632 g_test_add_func ("/boxed/regex", test_boxed_regex
);
633 g_test_add_func ("/boxed/varianttype", test_boxed_varianttype
);
634 g_test_add_func ("/boxed/error", test_boxed_error
);
635 g_test_add_func ("/boxed/datetime", test_boxed_datetime
);
636 g_test_add_func ("/boxed/matchinfo", test_boxed_matchinfo
);
637 g_test_add_func ("/boxed/keyfile", test_boxed_keyfile
);
638 g_test_add_func ("/boxed/mainloop", test_boxed_mainloop
);
639 g_test_add_func ("/boxed/maincontext", test_boxed_maincontext
);
640 g_test_add_func ("/boxed/source", test_boxed_source
);
641 g_test_add_func ("/boxed/variantbuilder", test_boxed_variantbuilder
);
642 g_test_add_func ("/boxed/timezone", test_boxed_timezone
);
643 g_test_add_func ("/boxed/pollfd", test_boxed_pollfd
);
644 g_test_add_func ("/boxed/markup", test_boxed_markup
);
645 g_test_add_func ("/boxed/thread", test_boxed_thread
);
646 g_test_add_func ("/boxed/checksum", test_boxed_checksum
);
648 return g_test_run ();