tests: move /param/implement to -m slow
[platform/upstream/glib.git] / gobject / tests / private.c
1 #include <glib-object.h>
2
3 typedef struct {
4   GObject parent_instance;
5 } TestObject;
6
7 typedef struct {
8   int dummy_0;
9   float dummy_1;
10 } TestObjectPrivate;
11
12 typedef struct {
13   GObjectClass parent_class;
14 } TestObjectClass;
15
16 GType test_object_get_type (void);
17
18 G_DEFINE_TYPE_WITH_CODE (TestObject, test_object, G_TYPE_OBJECT,
19                          G_ADD_PRIVATE (TestObject))
20
21 static void
22 test_object_class_init (TestObjectClass *klass)
23 {
24 }
25
26 static void
27 test_object_init (TestObject *self)
28 {
29   TestObjectPrivate *priv = test_object_get_instance_private (self);
30
31   if (g_test_verbose ())
32     g_print ("Offset of %sPrivate for type '%s': %d\n",
33              G_OBJECT_TYPE_NAME (self),
34              G_OBJECT_TYPE_NAME (self),
35              TestObject_private_offset);
36
37   priv->dummy_0 = 42;
38   priv->dummy_1 = 3.14159f;
39 }
40
41 static int
42 test_object_get_dummy_0 (TestObject *self)
43 {
44   TestObjectPrivate *priv = test_object_get_instance_private (self);
45
46   return priv->dummy_0;
47 }
48
49 static float
50 test_object_get_dummy_1 (TestObject *self)
51 {
52   TestObjectPrivate *priv = test_object_get_instance_private (self);
53
54   return priv->dummy_1;
55 }
56
57 typedef struct {
58   TestObject parent_instance;
59 } TestDerived;
60
61 typedef struct {
62   char *dummy_2;
63 } TestDerivedPrivate;
64
65 typedef struct {
66   TestObjectClass parent_class;
67 } TestDerivedClass;
68
69 GType test_derived_get_type (void);
70
71 G_DEFINE_TYPE_WITH_CODE (TestDerived, test_derived, test_object_get_type (),
72                          G_ADD_PRIVATE (TestDerived))
73
74 static void
75 test_derived_finalize (GObject *obj)
76 {
77   TestDerivedPrivate *priv = test_derived_get_instance_private ((TestDerived *) obj);
78
79   g_free (priv->dummy_2);
80
81   G_OBJECT_CLASS (test_derived_parent_class)->finalize (obj);
82 }
83
84 static void
85 test_derived_class_init (TestDerivedClass *klass)
86 {
87   G_OBJECT_CLASS (klass)->finalize = test_derived_finalize;
88 }
89
90 static void
91 test_derived_init (TestDerived *self)
92 {
93   TestDerivedPrivate *priv = test_derived_get_instance_private (self);
94
95   if (g_test_verbose ())
96     g_print ("Offset of %sPrivate for type '%s': %d\n",
97              G_OBJECT_TYPE_NAME (self),
98              G_OBJECT_TYPE_NAME (self),
99              TestDerived_private_offset);
100
101   priv->dummy_2 = g_strdup ("Hello");
102 }
103
104 static const char *
105 test_derived_get_dummy_2 (TestDerived *self)
106 {
107   TestDerivedPrivate *priv = test_derived_get_instance_private (self);
108
109   return priv->dummy_2;
110 }
111
112 typedef struct {
113   TestObject parent_instance;
114 } TestMixed;
115
116 typedef struct {
117   gint dummy_3;
118 } TestMixedPrivate;
119
120 typedef struct {
121   TestObjectClass parent_class;
122 } TestMixedClass;
123
124 GType test_mixed_get_type (void);
125
126 G_DEFINE_TYPE (TestMixed, test_mixed, test_object_get_type ())
127
128 static void
129 test_mixed_class_init (TestMixedClass *klass)
130 {
131   g_type_class_add_private (klass, sizeof (TestMixedPrivate));
132 }
133
134 static void
135 test_mixed_init (TestMixed *self)
136 {
137   TestMixedPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, test_mixed_get_type (), TestMixedPrivate);
138
139   if (g_test_verbose ())
140     g_print ("Offset of %sPrivate for type '%s': %d\n",
141              G_OBJECT_TYPE_NAME (self),
142              G_OBJECT_TYPE_NAME (self),
143              TestMixed_private_offset);
144
145   priv->dummy_3 = 47;
146 }
147
148 static gint
149 test_mixed_get_dummy_3 (TestMixed *self)
150 {
151   TestMixedPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, test_mixed_get_type (), TestMixedPrivate);
152
153   return priv->dummy_3;
154 }
155
156 typedef struct {
157   TestMixed parent_instance;
158 } TestMixedDerived;
159
160 typedef struct {
161   gint64 dummy_4;
162 } TestMixedDerivedPrivate;
163
164 typedef struct {
165   TestMixedClass parent_class;
166 } TestMixedDerivedClass;
167
168 GType test_mixed_derived_get_type (void);
169
170 G_DEFINE_TYPE_WITH_CODE (TestMixedDerived, test_mixed_derived, test_mixed_get_type (),
171                          G_ADD_PRIVATE (TestMixedDerived))
172
173 static void
174 test_mixed_derived_class_init (TestMixedDerivedClass *klass)
175 {
176 }
177
178 static void
179 test_mixed_derived_init (TestMixedDerived *self)
180 {
181   TestMixedDerivedPrivate *priv = test_mixed_derived_get_instance_private (self);
182
183   if (g_test_verbose ())
184     g_print ("Offset of %sPrivate for type '%s': %d\n",
185              G_OBJECT_TYPE_NAME (self),
186              G_OBJECT_TYPE_NAME (self),
187              TestMixedDerived_private_offset);
188
189   priv->dummy_4 = g_get_monotonic_time ();
190 }
191
192 static gint64
193 test_mixed_derived_get_dummy_4 (TestMixedDerived *self)
194 {
195   TestMixedDerivedPrivate *priv = test_mixed_derived_get_instance_private (self);
196
197   return priv->dummy_4;
198 }
199
200 static void
201 private_instance (void)
202 {
203   TestObject *obj = g_object_new (test_object_get_type (), NULL);
204
205   g_assert_cmpint (test_object_get_dummy_0 (obj), ==, 42);
206   g_assert_cmpfloat (test_object_get_dummy_1 (obj), ==, 3.14159f);
207
208   g_object_unref (obj);
209 }
210
211 static void
212 private_derived_instance (void)
213 {
214   TestDerived *obj = g_object_new (test_derived_get_type (), NULL);
215
216   g_assert_cmpstr (test_derived_get_dummy_2 (obj), ==, "Hello");
217   g_assert_cmpint (test_object_get_dummy_0 ((TestObject *) obj), ==, 42);
218
219   g_object_unref (obj);
220 }
221
222 static void
223 private_mixed_derived_instance (void)
224 {
225   TestMixedDerived *derived = g_object_new (test_mixed_derived_get_type (), NULL);
226   TestMixed *mixed = g_object_new (test_mixed_get_type (), NULL);
227
228   g_assert_cmpint (test_mixed_get_dummy_3 (mixed), ==, 47);
229   g_assert (test_mixed_derived_get_dummy_4 (derived) <= g_get_monotonic_time ());
230
231   g_object_unref (derived);
232   g_object_unref (mixed);
233 }
234
235 int
236 main (int argc,
237       char *argv[])
238 {
239   g_test_init (&argc, &argv, NULL);
240
241   g_test_add_func ("/private/instance", private_instance);
242   g_test_add_func ("/private/derived-instance", private_derived_instance);
243   g_test_add_func ("/private/mixed-derived-instance", private_mixed_derived_instance);
244
245   return g_test_run ();
246 }