And correct even more valid sparse warnings.
[platform/upstream/gstreamer.git] / tests / check / gst / gstobject.c
1 /* GStreamer
2  *
3  * unit test for GstObject
4  *
5  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <gst/check/gstcheck.h>
24
25 /*
26   Create a fake subclass
27  */
28 typedef struct _GstFakeObjectClass GstFakeObjectClass;
29 typedef struct _GstFakeObject GstFakeObject;
30
31 struct _GstFakeObject
32 {
33   GstObject object;
34 };
35
36 struct _GstFakeObjectClass
37 {
38   GstObjectClass parent_class;
39 };
40
41 static GType _gst_fake_object_type = 0;
42
43 //static GstObjectClass *parent_class = NULL;
44 //static guint gst_fake_object_signals[LAST_SIGNAL] = { 0 };
45
46 static GType
47 gst_fake_object_get_type (void)
48 {
49   if (!_gst_fake_object_type) {
50     static const GTypeInfo fake_object_info = {
51       sizeof (GstFakeObjectClass),
52       NULL,                     //gst_fake_object_base_class_init,
53       NULL,                     //gst_fake_object_base_class_finalize,
54       NULL,                     //(GClassInitFunc) gst_fake_object_class_init,
55       NULL,
56       NULL,
57       sizeof (GstFakeObject),
58       0,
59       NULL,                     //(GInstanceInitFunc) gst_fake_object_init,
60       NULL
61     };
62
63     _gst_fake_object_type = g_type_register_static (GST_TYPE_OBJECT,
64         "GstFakeObject", &fake_object_info, 0);
65   }
66   return _gst_fake_object_type;
67 }
68
69 /* g_object_new on abstract GstObject should fail */
70 GST_START_TEST (test_fail_abstract_new)
71 {
72   GstObject *object;
73
74   ASSERT_CRITICAL (object = g_object_new (gst_object_get_type (), NULL));
75   fail_unless (object == NULL, "Created an instance of abstract GstObject");
76 }
77
78 GST_END_TEST;
79
80 /* g_object_new on GstFakeObject should succeed */
81 GST_START_TEST (test_fake_object_new)
82 {
83   GstObject *object;
84
85   object = g_object_new (gst_fake_object_get_type (), NULL);
86   fail_if (object == NULL, "Failed to create instance of GstFakeObject");
87   fail_unless (GST_IS_OBJECT (object),
88       "GstFakeObject instance is not a GstObject");
89   gst_object_unref (object);
90 }
91
92 GST_END_TEST;
93
94 /* GstFakeObject name tests */
95 GST_START_TEST (test_fake_object_name)
96 {
97   GstObject *object;
98   gchar *name;
99   gchar *name2;
100
101   object = g_object_new (gst_fake_object_get_type (), NULL);
102
103   name = gst_object_get_name (object);
104   fail_if (name == NULL, "Newly created object has no name");
105   fail_if (strncmp (name, "fakeobject", 10) != 0,
106       "Random name %s does not start with Gst", name);
107   g_free (name);
108
109   /* give a random name by setting with NULL;
110    * GstFakeObject class -> fakeobject%d */
111   gst_object_set_name (object, NULL);
112   name = gst_object_get_name (object);
113   fail_if (name == NULL, "Random name was not assigned");
114   fail_if (strncmp (name, "fakeobject", 10) != 0,
115       "Random name %s does not start with Gst", name);
116   g_free (name);
117
118   gst_object_set_name (object, "fake");
119   name = gst_object_get_name (object);
120   fail_if (name == NULL, "Failed to get name of GstFakeObject");
121   fail_if (strcmp (name, "fake") != 0, "Name of GstFakeObject is not 'fake'");
122
123   /* change the gotten name to see that it's a copy and not the original */
124   name[0] = 'm';
125   name2 = gst_object_get_name (object);
126   fail_if (strcmp (name2, "fake") != 0,
127       "Copy of object name affected actual object name");
128   g_free (name);
129   g_free (name2);
130
131   gst_object_unref (object);
132 }
133
134 GST_END_TEST;
135
136 /* thread function for threaded name change test */
137 static gpointer
138 thread_name_object (GstObject * object)
139 {
140   gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
141
142   THREAD_START ();
143
144   /* give main thread a head start */
145   g_usleep (100000);
146
147   /* write our name repeatedly */
148   g_message ("THREAD %s: starting loop\n", thread_id);
149   while (THREAD_TEST_RUNNING ()) {
150     gst_object_set_name (object, thread_id);
151     /* a minimal sleep invokes a thread switch */
152     THREAD_SWITCH ();
153   }
154
155   /* thread is done, so let's return */
156   g_message ("THREAD %s: set name\n", thread_id);
157   g_free (thread_id);
158
159   return NULL;
160 }
161
162 #if 0
163 GST_START_TEST (test_fake_object_name_threaded_wrong)
164 {
165   GstObject *object;
166   gchar *name;
167   gint i;
168   gboolean expected_failure = FALSE;
169
170   g_message ("\nTEST: set/get without lock\n");
171
172   object = g_object_new (gst_fake_object_get_type (), NULL);
173   gst_object_set_name (object, "main");
174
175   MAIN_START_THREADS (5, thread_name_object, object);
176
177   /* start looping and set/get name repeatedly */
178   for (i = 0; i < 1000; ++i) {
179     gst_object_set_name (object, "main");
180     THREAD_SWITCH ();
181     name = gst_object_get_name (object);
182     if (strcmp (name, "main") != 0) {
183       g_message ("MAIN: expected failure during run %d\n", i);
184       expected_failure = TRUE;
185       g_free (name);
186       break;
187     }
188     g_free (name);
189   }
190   MAIN_STOP_THREADS ();
191
192   gst_object_unref (object);
193
194   fail_unless (expected_failure, "name did not get changed");
195 }
196
197 GST_END_TEST;
198 #endif
199
200 /*
201  * main thread sets and gets name directly on struct inside the object lock
202  * succeed because lock is held during set/get, and threads are locked out
203  */
204 GST_START_TEST (test_fake_object_name_threaded_right)
205 {
206   GstObject *object;
207   gchar *name;
208   gint i;
209
210   g_message ("\nTEST: set/get inside lock\n");
211
212   object = g_object_new (gst_fake_object_get_type (), NULL);
213   gst_object_set_name (object, "main");
214
215   MAIN_START_THREADS (5, thread_name_object, object);
216
217   /* start looping and set/get name repeatedly */
218   for (i = 0; i < 1000; ++i) {
219     GST_OBJECT_LOCK (object);
220     g_free (GST_OBJECT_NAME (object));
221     GST_OBJECT_NAME (object) = g_strdup ("main");
222     THREAD_SWITCH ();
223     name = g_strdup (GST_OBJECT_NAME (object));
224     GST_OBJECT_UNLOCK (object);
225
226     fail_unless (strcmp (name, "main") == 0,
227         "Name got changed while lock held during run %d", i);
228     g_free (name);
229   }
230   MAIN_STOP_THREADS ();
231   gst_object_unref (object);
232 }
233
234 GST_END_TEST;
235 /*
236  * main thread creates lots of objects
237  * child threads sets default names on objects
238  * then main thread checks uniqueness of object names
239  */
240
241 static GList *object_list = NULL;
242 static gint num_objects = 1000;
243 static gint num_threads = 5;
244
245 /* thread function for threaded default name change test */
246 static gpointer
247 thread_name_object_default (int *i)
248 {
249   int j;
250
251   THREAD_START ();
252
253   for (j = *i; j < num_objects; j += num_threads) {
254     GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
255
256     /* g_message ("THREAD %p: setting default name on object %d\n",
257        g_thread_self (), j); */
258     gst_object_set_name (o, NULL);
259     THREAD_SWITCH ();
260   }
261
262   /* thread is done, so let's return */
263   g_message ("THREAD %p: set name\n", g_thread_self ());
264   g_free (i);
265
266   return NULL;
267 }
268
269 static gint
270 gst_object_name_compare (GstObject * o, GstObject * p)
271 {
272   gint result;
273
274   GST_OBJECT_LOCK (o);
275   GST_OBJECT_LOCK (p);
276
277   if (o->name == NULL && p->name == NULL) {
278     result = 0;
279   } else if (o->name == NULL) {
280     result = -1;
281   } else if (p->name == NULL) {
282     result = 1;
283   } else {
284     result = strcmp (o->name, p->name);
285   }
286
287   GST_OBJECT_UNLOCK (p);
288   GST_OBJECT_UNLOCK (o);
289
290   return result;
291 }
292
293 GST_START_TEST (test_fake_object_name_threaded_unique)
294 {
295   GstObject *object;
296   gint i;
297   gint *ip;
298   gchar *name1, *name2;
299   GList *l;
300
301   g_message ("\nTEST: uniqueness of default names\n");
302
303   for (i = 0; i < num_objects; ++i) {
304     object = g_object_new (gst_fake_object_get_type (), NULL);
305     object_list = g_list_append (object_list, object);
306   }
307
308   MAIN_INIT ();
309
310   mark_point ();
311   for (i = 0; i < num_threads; ++i) {
312     ip = g_new (gint, 1);
313     *ip = i;
314     MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
315   }
316
317   mark_point ();
318   MAIN_SYNCHRONIZE ();
319   mark_point ();
320   MAIN_STOP_THREADS ();
321
322   /* sort GList based on object name */
323   /* FIXME: sort and test */
324   object_list =
325       g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
326
327   name1 = gst_object_get_name (GST_OBJECT (object_list->data));
328   for (l = object_list->next; l->next; l = l->next) {
329     g_message ("object with name %s\n", name1);
330     name2 = gst_object_get_name (GST_OBJECT (l->data));
331     fail_if (strcmp (name1, name2) == 0, "Two objects with name %s", name2);
332     g_free (name1);
333     name1 = name2;
334   }
335   g_free (name1);
336
337   /* free stuff */
338   g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
339 }
340
341 GST_END_TEST;
342
343 /* parentage test on GstFakeObject */
344 GST_START_TEST (test_fake_object_parentage)
345 {
346   GstObject *object1, *object2;
347   GstObject *parent;
348   gboolean result;
349
350   /* create new object */
351   object1 = g_object_new (gst_fake_object_get_type (), NULL);
352   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
353   fail_unless (GST_IS_OBJECT (object1),
354       "GstFakeObject instance is not a GstObject");
355   fail_unless (GST_OBJECT_IS_FLOATING (object1),
356       "GstFakeObject instance is not floating");
357
358   /* check the parent */
359   parent = gst_object_get_parent (object1);
360   fail_if (parent != NULL, "GstFakeObject has parent");
361   /* try to set a NULL parent, this should give a warning */
362   ASSERT_CRITICAL (result = gst_object_set_parent (object1, NULL));
363   fail_if (result == TRUE, "GstFakeObject accepted NULL parent");
364   /* try to set itself as parent, we expect a warning here */
365   ASSERT_CRITICAL (result = gst_object_set_parent (object1, object1));
366   fail_if (result == TRUE, "GstFakeObject accepted itself as parent");
367
368   /* should still be floating */
369   fail_unless (GST_OBJECT_IS_FLOATING (object1),
370       "GstFakeObject instance is not floating");
371
372   /* create another object */
373   object2 = g_object_new (gst_fake_object_get_type (), NULL);
374   fail_if (object2 == NULL,
375       "Failed to create another instance of GstFakeObject");
376   fail_unless (GST_IS_OBJECT (object2),
377       "second GstFakeObject instance is not a GstObject");
378   fail_unless (GST_OBJECT_IS_FLOATING (object1),
379       "GstFakeObject instance is not floating");
380
381   /* try to set other object as parent */
382   result = gst_object_set_parent (object1, object2);
383   fail_if (result == FALSE,
384       "GstFakeObject could not accept other object as parent");
385
386   /* should not be floating anymore */
387   fail_if (GST_OBJECT_IS_FLOATING (object1),
388       "GstFakeObject instance is still floating");
389   /* parent should still be floating */
390   fail_unless (GST_OBJECT_IS_FLOATING (object2),
391       "GstFakeObject instance is not floating");
392
393   /* check the parent */
394   parent = gst_object_get_parent (object1);
395   fail_if (parent != object2, "GstFakeObject has wrong parent");
396   gst_object_unref (parent);
397   /* try to set other object as parent again */
398   result = gst_object_set_parent (object1, object2);
399   fail_if (result == TRUE, "GstFakeObject could set parent twice");
400
401   /* ref before unparenting */
402   gst_object_ref (object1);
403   /* clear parent of object */
404   gst_object_unparent (object1);
405
406   /* check the parent */
407   parent = gst_object_get_parent (object1);
408   fail_if (parent != NULL, "GstFakeObject has parent");
409
410   /* object should not be floating */
411   fail_if (GST_OBJECT_IS_FLOATING (object1),
412       "GstFakeObject instance is floating again");
413
414   gst_object_unref (object1);
415   gst_object_unref (object2);
416 }
417
418 GST_END_TEST;
419
420 /* parentage test dispose on GstFakeObject, since our testcase
421  * does not handle the parent relation completely, the parent does
422  * not hold a ref to the child, we cannot dispose the parent to
423  * dipose the child as well. This test needs to be run with DEBUG
424  * info to check if the finalize methods are called correctly. */
425 GST_START_TEST (test_fake_object_parentage_dispose)
426 {
427   GstObject *object1, *object2;
428   gboolean result;
429
430   object1 = g_object_new (gst_fake_object_get_type (), NULL);
431   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
432
433   object2 = g_object_new (gst_fake_object_get_type (), NULL);
434   fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
435
436   /* try to set other object as parent */
437   result = gst_object_set_parent (object1, object2);
438   fail_if (result == FALSE,
439       "GstFakeObject could not accept other object as parent");
440
441   /* clear parent of object */
442   gst_object_unparent (object1);
443
444   /* now dispose parent */
445   gst_object_unref (object2);
446 }
447
448 GST_END_TEST;
449
450 /* test: try renaming a parented object, make sure it fails */
451
452 static Suite *
453 gst_object_suite (void)
454 {
455   Suite *s = suite_create ("GstObject");
456   TCase *tc_chain = tcase_create ("general");
457
458   /* turn off timeout */
459   tcase_set_timeout (tc_chain, 60);
460
461   suite_add_tcase (s, tc_chain);
462   tcase_add_test (tc_chain, test_fake_object_new);
463   tcase_add_test (tc_chain, test_fake_object_name);
464 #if 0
465   tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
466 #endif
467   tcase_add_test (tc_chain, test_fake_object_name_threaded_right);
468   tcase_add_test (tc_chain, test_fake_object_name_threaded_unique);
469   tcase_add_test (tc_chain, test_fake_object_parentage);
470   tcase_add_test (tc_chain, test_fake_object_parentage_dispose);
471   //tcase_add_checked_fixture (tc_chain, setup, teardown);
472
473   /* SEGV tests go last so we can debug the others */
474   tcase_add_test_raise_signal (tc_chain, test_fail_abstract_new, SIGSEGV);
475   return s;
476 }
477
478 GST_CHECK_MAIN (gst_object);