1 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
2 #define GLIB_DISABLE_DEPRECATION_WARNINGS
5 #include <glib-object.h>
7 typedef struct _MyBoxed MyBoxed;
16 my_boxed_copy (gpointer orig)
21 b = g_slice_new (MyBoxed);
22 b->ivalue = a->ivalue;
23 b->bla = g_strdup (a->bla);
28 static gint my_boxed_free_count;
31 my_boxed_free (gpointer orig)
36 g_slice_free (MyBoxed, a);
38 my_boxed_free_count++;
41 static GType my_boxed_get_type (void);
42 #define MY_TYPE_BOXED (my_boxed_get_type ())
44 G_DEFINE_BOXED_TYPE (MyBoxed, my_boxed, my_boxed_copy, my_boxed_free)
47 test_define_boxed (void)
53 a.bla = g_strdup ("bla");
55 b = g_boxed_copy (MY_TYPE_BOXED, &a);
57 g_assert_cmpint (b->ivalue, ==, 20);
58 g_assert_cmpstr (b->bla, ==, "bla");
60 g_boxed_free (MY_TYPE_BOXED, b);
66 test_boxed_ownership (void)
68 GValue value = G_VALUE_INIT;
69 static MyBoxed boxed = { 10, "bla" };
71 g_value_init (&value, MY_TYPE_BOXED);
73 my_boxed_free_count = 0;
75 g_value_set_static_boxed (&value, &boxed);
76 g_value_reset (&value);
78 g_assert_cmpint (my_boxed_free_count, ==, 0);
80 g_value_set_boxed_take_ownership (&value, g_boxed_copy (MY_TYPE_BOXED, &boxed));
81 g_value_reset (&value);
82 g_assert_cmpint (my_boxed_free_count, ==, 1);
84 g_value_take_boxed (&value, g_boxed_copy (MY_TYPE_BOXED, &boxed));
85 g_value_reset (&value);
86 g_assert_cmpint (my_boxed_free_count, ==, 2);
88 g_value_set_boxed (&value, &boxed);
89 g_value_reset (&value);
90 g_assert_cmpint (my_boxed_free_count, ==, 3);
94 my_callback (gpointer user_data)
98 static gint destroy_count;
101 my_closure_notify (gpointer user_data, GClosure *closure)
107 test_boxed_closure (void)
111 GValue value = G_VALUE_INIT;
113 g_value_init (&value, G_TYPE_CLOSURE);
114 g_assert (G_VALUE_HOLDS_BOXED (&value));
116 closure = g_cclosure_new (G_CALLBACK (my_callback), "bla", my_closure_notify);
117 g_value_take_boxed (&value, closure);
119 closure2 = g_value_get_boxed (&value);
120 g_assert (closure2 == closure);
122 closure2 = g_value_dup_boxed (&value);
123 g_assert (closure2 == closure); /* closures use ref/unref for copy/free */
124 g_closure_unref (closure2);
126 g_value_unset (&value);
127 g_assert_cmpint (destroy_count, ==, 1);
131 test_boxed_date (void)
135 GValue value = G_VALUE_INIT;
137 g_value_init (&value, G_TYPE_DATE);
138 g_assert (G_VALUE_HOLDS_BOXED (&value));
140 date = g_date_new_dmy (1, 3, 1970);
141 g_value_take_boxed (&value, date);
143 date2 = g_value_get_boxed (&value);
144 g_assert (date2 == date);
146 date2 = g_value_dup_boxed (&value);
147 g_assert (date2 != date);
148 g_assert (g_date_compare (date, date2) == 0);
151 g_value_unset (&value);
155 test_boxed_value (void)
157 GValue value1 = G_VALUE_INIT;
159 GValue value = G_VALUE_INIT;
161 g_value_init (&value, G_TYPE_VALUE);
162 g_assert (G_VALUE_HOLDS_BOXED (&value));
164 g_value_init (&value1, G_TYPE_INT);
165 g_value_set_int (&value1, 26);
167 g_value_set_static_boxed (&value, &value1);
169 value2 = g_value_get_boxed (&value);
170 g_assert (value2 == &value1);
172 value2 = g_value_dup_boxed (&value);
173 g_assert (value2 != &value1);
174 g_assert (G_VALUE_HOLDS_INT (value2));
175 g_assert_cmpint (g_value_get_int (value2), ==, 26);
176 g_boxed_free (G_TYPE_VALUE, value2);
178 g_value_unset (&value);
182 test_boxed_string (void)
186 GValue value = G_VALUE_INIT;
188 g_value_init (&value, G_TYPE_GSTRING);
189 g_assert (G_VALUE_HOLDS_BOXED (&value));
191 v = g_string_new ("bla");
192 g_value_take_boxed (&value, v);
194 v2 = g_value_get_boxed (&value);
197 v2 = g_value_dup_boxed (&value);
199 g_assert (g_string_equal (v, v2));
200 g_string_free (v2, TRUE);
202 g_value_unset (&value);
206 test_boxed_hashtable (void)
210 GValue value = G_VALUE_INIT;
212 g_value_init (&value, G_TYPE_HASH_TABLE);
213 g_assert (G_VALUE_HOLDS_BOXED (&value));
215 v = g_hash_table_new (g_str_hash, g_str_equal);
216 g_value_take_boxed (&value, v);
218 v2 = g_value_get_boxed (&value);
221 v2 = g_value_dup_boxed (&value);
222 g_assert (v2 == v); /* hash tables use ref/unref for copy/free */
223 g_hash_table_unref (v2);
225 g_value_unset (&value);
229 test_boxed_array (void)
233 GValue value = G_VALUE_INIT;
235 g_value_init (&value, G_TYPE_ARRAY);
236 g_assert (G_VALUE_HOLDS_BOXED (&value));
238 v = g_array_new (TRUE, FALSE, 1);
239 g_value_take_boxed (&value, v);
241 v2 = g_value_get_boxed (&value);
244 v2 = g_value_dup_boxed (&value);
245 g_assert (v2 == v); /* arrays use ref/unref for copy/free */
248 g_value_unset (&value);
252 test_boxed_ptrarray (void)
256 GValue value = G_VALUE_INIT;
258 g_value_init (&value, G_TYPE_PTR_ARRAY);
259 g_assert (G_VALUE_HOLDS_BOXED (&value));
261 v = g_ptr_array_new ();
262 g_value_take_boxed (&value, v);
264 v2 = g_value_get_boxed (&value);
267 v2 = g_value_dup_boxed (&value);
268 g_assert (v2 == v); /* ptr arrays use ref/unref for copy/free */
269 g_ptr_array_unref (v2);
271 g_value_unset (&value);
275 test_boxed_regex (void)
279 GValue value = G_VALUE_INIT;
281 g_value_init (&value, G_TYPE_REGEX);
282 g_assert (G_VALUE_HOLDS_BOXED (&value));
284 v = g_regex_new ("a+b+", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
285 g_value_take_boxed (&value, v);
287 v2 = g_value_get_boxed (&value);
290 v2 = g_value_dup_boxed (&value);
291 g_assert (v2 == v); /* regexes use ref/unref for copy/free */
294 g_value_unset (&value);
298 test_boxed_matchinfo (void)
301 GMatchInfo *info, *info2;
303 GValue value = G_VALUE_INIT;
305 g_value_init (&value, G_TYPE_MATCH_INFO);
306 g_assert (G_VALUE_HOLDS_BOXED (&value));
308 r = g_regex_new ("ab", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
309 ret = g_regex_match (r, "blabla abab bla", 0, &info);
311 g_value_take_boxed (&value, info);
313 info2 = g_value_get_boxed (&value);
314 g_assert (info == info2);
316 info2 = g_value_dup_boxed (&value);
317 g_assert (info == info2); /* matchinfo uses ref/unref for copy/free */
318 g_match_info_unref (info2);
320 g_value_unset (&value);
325 test_boxed_varianttype (void)
329 GValue value = G_VALUE_INIT;
331 g_value_init (&value, G_TYPE_VARIANT_TYPE);
332 g_assert (G_VALUE_HOLDS_BOXED (&value));
334 v = g_variant_type_new ("mas");
335 g_value_take_boxed (&value, v);
337 v2 = g_value_get_boxed (&value);
340 v2 = g_value_dup_boxed (&value);
342 g_assert_cmpstr (g_variant_type_peek_string (v), ==, g_variant_type_peek_string (v2));
343 g_variant_type_free (v2);
345 g_value_unset (&value);
349 test_boxed_datetime (void)
353 GValue value = G_VALUE_INIT;
355 g_value_init (&value, G_TYPE_DATE_TIME);
356 g_assert (G_VALUE_HOLDS_BOXED (&value));
358 v = g_date_time_new_now_local ();
359 g_value_take_boxed (&value, v);
361 v2 = g_value_get_boxed (&value);
364 v2 = g_value_dup_boxed (&value);
365 g_assert (v2 == v); /* datetime uses ref/unref for copy/free */
366 g_date_time_unref (v2);
368 g_value_unset (&value);
372 test_boxed_error (void)
376 GValue value = G_VALUE_INIT;
378 g_value_init (&value, G_TYPE_ERROR);
379 g_assert (G_VALUE_HOLDS_BOXED (&value));
381 v = g_error_new_literal (G_VARIANT_PARSE_ERROR,
382 G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG,
384 g_value_take_boxed (&value, v);
386 v2 = g_value_get_boxed (&value);
389 v2 = g_value_dup_boxed (&value);
391 g_assert_cmpint (v->domain, ==, v2->domain);
392 g_assert_cmpint (v->code, ==, v2->code);
393 g_assert_cmpstr (v->message, ==, v2->message);
396 g_value_unset (&value);
400 test_boxed_keyfile (void)
403 GValue value = G_VALUE_INIT;
405 g_value_init (&value, G_TYPE_KEY_FILE);
406 g_assert (G_VALUE_HOLDS_BOXED (&value));
408 k = g_key_file_new ();
409 g_value_take_boxed (&value, k);
411 k2 = g_value_get_boxed (&value);
414 k2 = g_value_dup_boxed (&value);
415 g_assert (k == k2); /* keyfile uses ref/unref for copy/free */
416 g_key_file_unref (k2);
418 g_value_unset (&value);
422 test_boxed_mainloop (void)
425 GValue value = G_VALUE_INIT;
427 g_value_init (&value, G_TYPE_MAIN_LOOP);
428 g_assert (G_VALUE_HOLDS_BOXED (&value));
430 l = g_main_loop_new (NULL, FALSE);
431 g_value_take_boxed (&value, l);
433 l2 = g_value_get_boxed (&value);
436 l2 = g_value_dup_boxed (&value);
437 g_assert (l == l2); /* mainloop uses ref/unref for copy/free */
438 g_main_loop_unref (l2);
440 g_value_unset (&value);
444 test_boxed_maincontext (void)
446 GMainContext *c, *c2;
447 GValue value = G_VALUE_INIT;
449 g_value_init (&value, G_TYPE_MAIN_CONTEXT);
450 g_assert (G_VALUE_HOLDS_BOXED (&value));
452 c = g_main_context_new ();
453 g_value_take_boxed (&value, c);
455 c2 = g_value_get_boxed (&value);
458 c2 = g_value_dup_boxed (&value);
459 g_assert (c == c2); /* maincontext uses ref/unref for copy/free */
460 g_main_context_unref (c2);
462 g_value_unset (&value);
466 test_boxed_source (void)
469 GValue value = G_VALUE_INIT;
471 g_value_init (&value, G_TYPE_SOURCE);
472 g_assert (G_VALUE_HOLDS_BOXED (&value));
474 s = g_idle_source_new ();
475 g_value_take_boxed (&value, s);
477 s2 = g_value_get_boxed (&value);
480 s2 = g_value_dup_boxed (&value);
481 g_assert (s == s2); /* source uses ref/unref for copy/free */
484 g_value_unset (&value);
488 test_boxed_variantbuilder (void)
490 GVariantBuilder *v, *v2;
491 GValue value = G_VALUE_INIT;
493 g_value_init (&value, G_TYPE_VARIANT_BUILDER);
494 g_assert (G_VALUE_HOLDS_BOXED (&value));
496 v = g_variant_builder_new (G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
497 g_value_take_boxed (&value, v);
499 v2 = g_value_get_boxed (&value);
502 v2 = g_value_dup_boxed (&value);
503 g_assert (v == v2); /* variantbuilder uses ref/unref for copy/free */
504 g_variant_builder_unref (v2);
506 g_value_unset (&value);
510 test_boxed_timezone (void)
513 GValue value = G_VALUE_INIT;
515 g_value_init (&value, G_TYPE_TIME_ZONE);
516 g_assert (G_VALUE_HOLDS_BOXED (&value));
518 z = g_time_zone_new_utc ();
519 g_value_take_boxed (&value, z);
521 z2 = g_value_get_boxed (&value);
524 z2 = g_value_dup_boxed (&value);
525 g_assert (z == z2); /* timezone uses ref/unref for copy/free */
526 g_time_zone_unref (z2);
528 g_value_unset (&value);
532 test_boxed_pollfd (void)
535 GValue value = G_VALUE_INIT;
537 g_value_init (&value, G_TYPE_POLLFD);
538 g_assert (G_VALUE_HOLDS_BOXED (&value));
540 p = g_new (GPollFD, 1);
541 g_value_take_boxed (&value, p);
543 p2 = g_value_get_boxed (&value);
546 p2 = g_value_dup_boxed (&value);
550 g_value_unset (&value);
554 test_boxed_markup (void)
556 GMarkupParseContext *c, *c2;
557 const GMarkupParser parser = { 0 };
558 GValue value = G_VALUE_INIT;
560 g_value_init (&value, G_TYPE_MARKUP_PARSE_CONTEXT);
561 g_assert (G_VALUE_HOLDS_BOXED (&value));
563 c = g_markup_parse_context_new (&parser, G_MARKUP_DEFAULT_FLAGS, NULL, NULL);
564 g_value_take_boxed (&value, c);
566 c2 = g_value_get_boxed (&value);
569 c2 = g_value_dup_boxed (&value);
571 g_markup_parse_context_unref (c2);
573 g_value_unset (&value);
577 test_boxed_thread (void)
580 GValue value = G_VALUE_INIT;
582 g_value_init (&value, G_TYPE_THREAD);
583 g_assert (G_VALUE_HOLDS_BOXED (&value));
585 t = g_thread_self ();
586 g_value_set_boxed (&value, t);
588 t2 = g_value_get_boxed (&value);
591 t2 = g_value_dup_boxed (&value);
595 g_value_unset (&value);
599 test_boxed_checksum (void)
602 GValue value = G_VALUE_INIT;
604 g_value_init (&value, G_TYPE_CHECKSUM);
605 g_assert (G_VALUE_HOLDS_BOXED (&value));
607 c = g_checksum_new (G_CHECKSUM_SHA512);
608 g_value_take_boxed (&value, c);
610 c2 = g_value_get_boxed (&value);
613 c2 = g_value_dup_boxed (&value);
615 g_checksum_free (c2);
617 g_value_unset (&value);
621 treecmp (gconstpointer a, gconstpointer b)
623 return (a < b) ? -1 : (a > b);
627 test_boxed_tree (void)
630 GValue value = G_VALUE_INIT;
632 g_value_init (&value, G_TYPE_TREE);
633 g_assert_true (G_VALUE_HOLDS_BOXED (&value));
635 t = g_tree_new (treecmp);
636 g_value_take_boxed (&value, t);
638 t2 = g_value_get_boxed (&value);
639 g_assert_true (t == t2);
641 t2 = g_value_dup_boxed (&value);
642 g_assert_true (t == t2); /* trees use ref/unref for copy/free */
645 g_value_unset (&value);
649 test_boxed_pattern_spec (void)
651 GPatternSpec *ps, *ps2;
652 GValue value = G_VALUE_INIT;
654 g_value_init (&value, G_TYPE_PATTERN_SPEC);
655 g_assert_true (G_VALUE_HOLDS_BOXED (&value));
657 ps = g_pattern_spec_new ("*abc*?cde");
658 g_value_take_boxed (&value, ps);
660 ps2 = g_value_get_boxed (&value);
661 g_assert_true (ps == ps2);
663 ps2 = g_value_dup_boxed (&value);
664 g_assert_true (ps != ps2);
665 g_assert_true (g_pattern_spec_equal (ps, ps2));
666 g_pattern_spec_free (ps2);
668 g_value_unset (&value);
672 test_boxed_rand (void)
675 GValue value = G_VALUE_INIT;
677 g_value_init (&value, G_TYPE_RAND);
678 g_assert_true (G_VALUE_HOLDS_BOXED (&value));
681 g_value_take_boxed (&value, r);
683 r2 = g_value_get_boxed (&value);
684 g_assert_true (r == r2);
686 r2 = g_value_dup_boxed (&value);
687 g_assert_true (r != r2);
690 g_value_unset (&value);
694 main (int argc, char *argv[])
696 g_test_init (&argc, &argv, NULL);
698 g_test_add_func ("/boxed/define", test_define_boxed);
699 g_test_add_func ("/boxed/ownership", test_boxed_ownership);
700 g_test_add_func ("/boxed/closure", test_boxed_closure);
701 g_test_add_func ("/boxed/date", test_boxed_date);
702 g_test_add_func ("/boxed/value", test_boxed_value);
703 g_test_add_func ("/boxed/string", test_boxed_string);
704 g_test_add_func ("/boxed/hashtable", test_boxed_hashtable);
705 g_test_add_func ("/boxed/array", test_boxed_array);
706 g_test_add_func ("/boxed/ptrarray", test_boxed_ptrarray);
707 g_test_add_func ("/boxed/regex", test_boxed_regex);
708 g_test_add_func ("/boxed/varianttype", test_boxed_varianttype);
709 g_test_add_func ("/boxed/error", test_boxed_error);
710 g_test_add_func ("/boxed/datetime", test_boxed_datetime);
711 g_test_add_func ("/boxed/matchinfo", test_boxed_matchinfo);
712 g_test_add_func ("/boxed/keyfile", test_boxed_keyfile);
713 g_test_add_func ("/boxed/mainloop", test_boxed_mainloop);
714 g_test_add_func ("/boxed/maincontext", test_boxed_maincontext);
715 g_test_add_func ("/boxed/source", test_boxed_source);
716 g_test_add_func ("/boxed/variantbuilder", test_boxed_variantbuilder);
717 g_test_add_func ("/boxed/timezone", test_boxed_timezone);
718 g_test_add_func ("/boxed/pollfd", test_boxed_pollfd);
719 g_test_add_func ("/boxed/markup", test_boxed_markup);
720 g_test_add_func ("/boxed/thread", test_boxed_thread);
721 g_test_add_func ("/boxed/checksum", test_boxed_checksum);
722 g_test_add_func ("/boxed/tree", test_boxed_tree);
723 g_test_add_func ("/boxed/patternspec", test_boxed_pattern_spec);
724 g_test_add_func ("/boxed/rand", test_boxed_rand);
726 return g_test_run ();