• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2018 Canonical Ltd
3  * Authors: Marco Trevisan <marco@ubuntu.com>
4  *
5  * This work is provided "as is"; redistribution and modification
6  * in whole or in part, in any medium, physical or electronic is
7  * permitted without restriction.
8  *
9  * This work is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * In no event shall the authors or contributors be liable for any
14  * direct, indirect, incidental, special, exemplary, or consequential
15  * damages (including, but not limited to, procurement of substitute
16  * goods or services; loss of use, data, or profits; or business
17  * interruption) however caused and on any theory of liability, whether
18  * in contract, strict liability, or tort (including negligence or
19  * otherwise) arising in any way out of the use of this software, even
20  * if advised of the possibility of such damage.
21  */
22 
23 #include <glib-object.h>
24 #include <string.h>
25 
26 G_DECLARE_DERIVABLE_TYPE (TestAutoCleanupBase, test_base_auto_cleanup, TEST, BASE_AUTO_CLEANUP, GObject)
27 
28 struct _TestAutoCleanupBaseClass {
29   GObjectClass parent_class;
30 };
31 
32 G_DECLARE_FINAL_TYPE (TestAutoCleanup, test_auto_cleanup, TEST, AUTO_CLEANUP, TestAutoCleanupBase)
33 
34 struct _TestAutoCleanup
35 {
36   TestAutoCleanupBase parent_instance;
37 };
38 
G_DEFINE_TYPE(TestAutoCleanup,test_auto_cleanup,G_TYPE_OBJECT)39 G_DEFINE_TYPE (TestAutoCleanup, test_auto_cleanup, G_TYPE_OBJECT)
40 
41 static void
42 test_auto_cleanup_class_init (TestAutoCleanupClass *class)
43 {
44 }
45 
46 static void
test_auto_cleanup_init(TestAutoCleanup * tac)47 test_auto_cleanup_init (TestAutoCleanup *tac)
48 {
49 }
50 
51 static TestAutoCleanup *
test_auto_cleanup_new(void)52 test_auto_cleanup_new (void)
53 {
54   return g_object_new (test_auto_cleanup_get_type (), NULL);
55 }
56 
57 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
58  * autocleanup functions, defined using the ones of the base type (defined with
59  * G_DECLARE_DERIVABLE_TYPE) and so that it can be used with g_autoptr */
60 static void
test_autoptr(void)61 test_autoptr (void)
62 {
63   TestAutoCleanup *tac_ptr = test_auto_cleanup_new ();
64   g_object_add_weak_pointer (G_OBJECT (tac_ptr), (gpointer *) &tac_ptr);
65 
66   {
67     g_autoptr (TestAutoCleanup) tac = tac_ptr;
68   }
69 #ifdef __GNUC__
70   g_assert_null (tac_ptr);
71 #endif
72 }
73 
74 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
75  * autocleanup functions, defined using the ones of the base type (defined with
76  * G_DECLARE_DERIVABLE_TYPE) and that stealing an autopointer works properly */
77 static void
test_autoptr_steal(void)78 test_autoptr_steal (void)
79 {
80   g_autoptr (TestAutoCleanup) tac1 = test_auto_cleanup_new ();
81   TestAutoCleanup *tac_ptr = tac1;
82 
83   g_object_add_weak_pointer (G_OBJECT (tac_ptr), (gpointer *) &tac_ptr);
84 
85   {
86     g_autoptr (TestAutoCleanup) tac2 = g_steal_pointer (&tac1);
87     g_assert_nonnull (tac_ptr);
88     g_assert_null (tac1);
89     g_assert_true (tac2 == tac_ptr);
90   }
91 #ifdef __GNUC__
92   g_assert_null (tac_ptr);
93 #endif
94 }
95 
96 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
97  * autolist cleanup functions defined using the ones of the parent type
98  * and so that can be used with g_autolist, and that freeing the list correctly
99  * unrefs the object too */
100 static void
test_autolist(void)101 test_autolist (void)
102 {
103   TestAutoCleanup *tac1 = test_auto_cleanup_new ();
104   TestAutoCleanup *tac2 = test_auto_cleanup_new ();
105   g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
106 
107   g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
108   g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
109   g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
110 
111   {
112     g_autolist (TestAutoCleanup) l = NULL;
113 
114     l = g_list_prepend (l, tac1);
115     l = g_list_prepend (l, tac2);
116   }
117 
118   /* Only assert if autoptr works */
119 #ifdef __GNUC__
120   g_assert_null (tac1);
121   g_assert_null (tac2);
122 #endif
123   g_assert_nonnull (tac3);
124 
125   g_clear_object (&tac3);
126   g_assert_null (tac3);
127 }
128 
129 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
130  * autoslist cleanup functions (defined using the ones of the base type declared
131  * with G_DECLARE_DERIVABLE_TYPE) and so that can be used with g_autoslist, and
132  * that freeing the slist correctly unrefs the object too */
133 static void
test_autoslist(void)134 test_autoslist (void)
135 {
136   TestAutoCleanup *tac1 = test_auto_cleanup_new ();
137   TestAutoCleanup *tac2 = test_auto_cleanup_new ();
138   g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
139 
140   g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
141   g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
142   g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
143 
144   {
145     g_autoslist (TestAutoCleanup) l = NULL;
146 
147     l = g_slist_prepend (l, tac1);
148     l = g_slist_prepend (l, tac2);
149   }
150 
151   /* Only assert if autoptr works */
152 #ifdef __GNUC__
153   g_assert_null (tac1);
154   g_assert_null (tac2);
155 #endif
156   g_assert_nonnull (tac3);
157 
158   g_clear_object (&tac3);
159   g_assert_null (tac3);
160 }
161 
162 /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
163  * autoqueue cleanup functions (defined using the ones of the base type declared
164  * with G_DECLARE_DERIVABLE_TYPE) and so that can be used with g_autoqueue, and
165  * that freeing the queue correctly unrefs the object too */
166 static void
test_autoqueue(void)167 test_autoqueue (void)
168 {
169   TestAutoCleanup *tac1 = test_auto_cleanup_new ();
170   TestAutoCleanup *tac2 = test_auto_cleanup_new ();
171   g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
172 
173   g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
174   g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
175   g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
176 
177   {
178     g_autoqueue (TestAutoCleanup) q = g_queue_new ();
179 
180     g_queue_push_head (q, tac1);
181     g_queue_push_tail (q, tac2);
182   }
183 
184   /* Only assert if autoptr works */
185 #ifdef __GNUC__
186   g_assert_null (tac1);
187   g_assert_null (tac2);
188 #endif
189   g_assert_nonnull (tac3);
190 
191   g_clear_object (&tac3);
192   g_assert_null (tac3);
193 }
194 
195 int
main(int argc,gchar * argv[])196 main (int argc, gchar *argv[])
197 {
198   g_test_init (&argc, &argv, NULL);
199 
200   g_test_add_func ("/autoptr/autoptr", test_autoptr);
201   g_test_add_func ("/autoptr/autoptr_steal", test_autoptr_steal);
202   g_test_add_func ("/autoptr/autolist", test_autolist);
203   g_test_add_func ("/autoptr/autoslist", test_autoslist);
204   g_test_add_func ("/autoptr/autoqueue", test_autoqueue);
205 
206   return g_test_run ();
207 }
208