1 /* GLib testing framework examples and tests
2 * Copyright (C) 2018 Canonical Ltd
3 * Authors: Marco Trevisan <marco@ubuntu.com>
5 * SPDX-License-Identifier: LicenseRef-old-glib-tests
7 * This work is provided "as is"; redistribution and modification
8 * in whole or in part, in any medium, physical or electronic is
9 * permitted without restriction.
11 * This work is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * In no event shall the authors or contributors be liable for any
16 * direct, indirect, incidental, special, exemplary, or consequential
17 * damages (including, but not limited to, procurement of substitute
18 * goods or services; loss of use, data, or profits; or business
19 * interruption) however caused and on any theory of liability, whether
20 * in contract, strict liability, or tort (including negligence or
21 * otherwise) arising in any way out of the use of this software, even
22 * if advised of the possibility of such damage.
25 #include <glib-object.h>
28 G_DECLARE_DERIVABLE_TYPE (TestAutoCleanupBase, test_base_auto_cleanup, TEST, BASE_AUTO_CLEANUP, GObject)
30 struct _TestAutoCleanupBaseClass {
31 GObjectClass parent_class;
34 G_DEFINE_TYPE (TestAutoCleanupBase, test_base_auto_cleanup, G_TYPE_OBJECT)
37 test_base_auto_cleanup_class_init (TestAutoCleanupBaseClass *class)
42 test_base_auto_cleanup_init (TestAutoCleanupBase *tac)
46 G_DECLARE_FINAL_TYPE (TestAutoCleanup, test_auto_cleanup, TEST, AUTO_CLEANUP, TestAutoCleanupBase)
48 struct _TestAutoCleanup
50 TestAutoCleanupBase parent_instance;
53 G_DEFINE_TYPE (TestAutoCleanup, test_auto_cleanup, G_TYPE_OBJECT)
56 test_auto_cleanup_class_init (TestAutoCleanupClass *class)
61 test_auto_cleanup_init (TestAutoCleanup *tac)
65 static TestAutoCleanup *
66 test_auto_cleanup_new (void)
68 return g_object_new (test_auto_cleanup_get_type (), NULL);
71 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
72 * autocleanup functions, defined using the ones of the base type (defined with
73 * G_DECLARE_DERIVABLE_TYPE) and so that it can be used with g_autoptr */
77 TestAutoCleanup *tac_ptr = test_auto_cleanup_new ();
78 g_object_add_weak_pointer (G_OBJECT (tac_ptr), (gpointer *) &tac_ptr);
81 g_autoptr (TestAutoCleanup) tac = tac_ptr;
82 g_assert_nonnull (tac);
85 g_assert_null (tac_ptr);
89 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
90 * autocleanup functions, defined using the ones of the base type (defined with
91 * G_DECLARE_DERIVABLE_TYPE) and that stealing an autopointer works properly */
93 test_autoptr_steal (void)
95 g_autoptr (TestAutoCleanup) tac1 = test_auto_cleanup_new ();
96 TestAutoCleanup *tac_ptr = tac1;
98 g_object_add_weak_pointer (G_OBJECT (tac_ptr), (gpointer *) &tac_ptr);
101 g_autoptr (TestAutoCleanup) tac2 = g_steal_pointer (&tac1);
102 g_assert_nonnull (tac_ptr);
103 g_assert_null (tac1);
104 g_assert_true (tac2 == tac_ptr);
107 g_assert_null (tac_ptr);
111 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
112 * autolist cleanup functions defined using the ones of the parent type
113 * and so that can be used with g_autolist, and that freeing the list correctly
114 * unrefs the object too */
118 TestAutoCleanup *tac1 = test_auto_cleanup_new ();
119 TestAutoCleanup *tac2 = test_auto_cleanup_new ();
120 g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
122 g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
123 g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
124 g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
127 g_autolist (TestAutoCleanup) l = NULL;
129 l = g_list_prepend (l, tac1);
130 l = g_list_prepend (l, tac2);
132 /* Squash warnings about dead stores */
136 /* Only assert if autoptr works */
138 g_assert_null (tac1);
139 g_assert_null (tac2);
141 g_assert_nonnull (tac3);
143 g_clear_object (&tac3);
144 g_assert_null (tac3);
147 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
148 * autoslist cleanup functions (defined using the ones of the base type declared
149 * with G_DECLARE_DERIVABLE_TYPE) and so that can be used with g_autoslist, and
150 * that freeing the slist correctly unrefs the object too */
152 test_autoslist (void)
154 TestAutoCleanup *tac1 = test_auto_cleanup_new ();
155 TestAutoCleanup *tac2 = test_auto_cleanup_new ();
156 g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
158 g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
159 g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
160 g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
163 g_autoslist (TestAutoCleanup) l = NULL;
165 l = g_slist_prepend (l, tac1);
166 l = g_slist_prepend (l, tac2);
169 /* Only assert if autoptr works */
171 g_assert_null (tac1);
172 g_assert_null (tac2);
174 g_assert_nonnull (tac3);
176 g_clear_object (&tac3);
177 g_assert_null (tac3);
180 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
181 * autoqueue cleanup functions (defined using the ones of the base type declared
182 * with G_DECLARE_DERIVABLE_TYPE) and so that can be used with g_autoqueue, and
183 * that freeing the queue correctly unrefs the object too */
185 test_autoqueue (void)
187 TestAutoCleanup *tac1 = test_auto_cleanup_new ();
188 TestAutoCleanup *tac2 = test_auto_cleanup_new ();
189 g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
191 g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
192 g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
193 g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
196 g_autoqueue (TestAutoCleanup) q = g_queue_new ();
198 g_queue_push_head (q, tac1);
199 g_queue_push_tail (q, tac2);
202 /* Only assert if autoptr works */
204 g_assert_null (tac1);
205 g_assert_null (tac2);
207 g_assert_nonnull (tac3);
209 g_clear_object (&tac3);
210 g_assert_null (tac3);
214 test_autoclass (void)
216 g_autoptr (TestAutoCleanupBaseClass) base_class_ptr = NULL;
217 g_autoptr (TestAutoCleanupClass) class_ptr = NULL;
219 base_class_ptr = g_type_class_ref (test_base_auto_cleanup_get_type ());
220 class_ptr = g_type_class_ref (test_auto_cleanup_get_type ());
222 g_assert_nonnull (base_class_ptr);
223 g_assert_nonnull (class_ptr);
227 main (int argc, gchar *argv[])
229 g_test_init (&argc, &argv, NULL);
231 g_test_add_func ("/autoptr/autoptr", test_autoptr);
232 g_test_add_func ("/autoptr/autoptr_steal", test_autoptr_steal);
233 g_test_add_func ("/autoptr/autolist", test_autolist);
234 g_test_add_func ("/autoptr/autoslist", test_autoslist);
235 g_test_add_func ("/autoptr/autoqueue", test_autoqueue);
236 g_test_add_func ("/autoptr/autoclass", test_autoclass);
238 return g_test_run ();