tests/check/: use the new macro
[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 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 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 /* g_object_new on GstFakeObject should succeed */
80 GST_START_TEST (test_fake_object_new)
81 {
82   GstObject *object;
83
84   object = g_object_new (gst_fake_object_get_type (), NULL);
85   fail_if (object == NULL, "Failed to create instance of GstFakeObject");
86   fail_unless (GST_IS_OBJECT (object),
87       "GstFakeObject instance is not a GstObject");
88 }
89
90 GST_END_TEST
91 /* GstFakeObject name tests */
92 GST_START_TEST (test_fake_object_name)
93 {
94   GstObject *object;
95   gchar *name;
96   gchar *name2;
97
98   object = g_object_new (gst_fake_object_get_type (), NULL);
99
100   name = gst_object_get_name (object);
101   fail_if (name == NULL, "Newly created object has no name");
102   fail_if (strncmp (name, "fakeobject", 10) != 0,
103       "Random name %s does not start with Gst", name);
104
105   /* give a random name by setting with NULL;
106    * GstFakeObject class -> fakeobject%d */
107   gst_object_set_name (object, NULL);
108   name = gst_object_get_name (object);
109   fail_if (name == NULL, "Random name was not assigned");
110   fail_if (strncmp (name, "fakeobject", 10) != 0,
111       "Random name %s does not start with Gst", name);
112   g_free (name);
113
114   gst_object_set_name (object, "fake");
115   name = gst_object_get_name (object);
116   fail_if (name == NULL, "Failed to get name of GstFakeObject");
117   fail_if (strcmp (name, "fake") != 0, "Name of GstFakeObject is not 'fake'");
118
119   /* change the gotten name to see that it's a copy and not the original */
120   name[0] = 'm';
121   name2 = gst_object_get_name (object);
122   fail_if (strcmp (name2, "fake") != 0,
123       "Copy of object name affected actual object name");
124   g_free (name);
125   g_free (name2);
126 }
127
128 GST_END_TEST
129 /* thread function for threaded name change test */
130     gpointer thread_name_object (GstObject * object)
131 {
132   gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
133
134   THREAD_START ();
135
136   /* give main thread a head start */
137   g_usleep (100000);
138
139   /* write our name repeatedly */
140   g_message ("THREAD %s: starting loop\n", thread_id);
141   while (THREAD_TEST_RUNNING ()) {
142     gst_object_set_name (object, thread_id);
143     /* a minimal sleep invokes a thread switch */
144     THREAD_SWITCH ();
145   }
146
147   /* thread is done, so let's return */
148   g_message ("THREAD %s: set name\n", thread_id);
149   g_free (thread_id);
150
151   return NULL;
152 }
153
154 /*
155  * main thread sets and gets name while other threads set the name
156  * constantly; fails because lock is released inbetween set and get
157  */
158
159 GST_START_TEST (test_fake_object_name_threaded_wrong)
160 {
161   GstObject *object;
162   gchar *name;
163   gint i;
164   gboolean expected_failure = FALSE;
165
166   g_message ("\nTEST: set/get without lock\n");
167
168   object = g_object_new (gst_fake_object_get_type (), NULL);
169   gst_object_set_name (object, "main");
170
171   MAIN_START_THREADS (5, thread_name_object, object);
172
173   /* start looping and set/get name repeatedly */
174   for (i = 0; i < 1000; ++i) {
175     gst_object_set_name (object, "main");
176     THREAD_SWITCH ();
177     name = gst_object_get_name (object);
178     if (strcmp (name, "main") != 0) {
179       g_message ("MAIN: expected failure during run %d\n", i);
180       expected_failure = TRUE;
181       g_free (name);
182       break;
183     }
184     g_free (name);
185   }
186   MAIN_STOP_THREADS ();
187
188   fail_unless (expected_failure, "name did not get changed");
189 }
190
191 GST_END_TEST
192 /*
193  * main thread sets and gets name directly on struct inside the object lock
194  * succeed because lock is held during set/get, and threads are locked out
195  */
196 GST_START_TEST (test_fake_object_name_threaded_right)
197 {
198   GstObject *object;
199   gchar *name;
200   gint i;
201
202   g_message ("\nTEST: set/get inside lock\n");
203
204   object = g_object_new (gst_fake_object_get_type (), NULL);
205   gst_object_set_name (object, "main");
206
207   MAIN_START_THREADS (5, thread_name_object, object);
208
209   /* start looping and set/get name repeatedly */
210   for (i = 0; i < 1000; ++i) {
211     GST_OBJECT_LOCK (object);
212     g_free (GST_OBJECT_NAME (object));
213     GST_OBJECT_NAME (object) = g_strdup ("main");
214     THREAD_SWITCH ();
215     name = g_strdup (GST_OBJECT_NAME (object));
216     GST_OBJECT_UNLOCK (object);
217
218     fail_unless (strcmp (name, "main") == 0,
219         "Name got changed while lock held during run %d", i);
220     g_free (name);
221   }
222   MAIN_STOP_THREADS ();
223 }
224
225 GST_END_TEST
226 /*
227  * main thread creates lots of objects
228  * child threads sets default names on objects
229  * then main thread checks uniqueness of object names
230  */
231     GList * object_list = NULL;
232 gint num_objects = 1000;
233 gint num_threads = 5;
234
235 /* thread function for threaded default name change test */
236 gpointer
237 thread_name_object_default (int *i)
238 {
239   int j;
240
241   THREAD_START ();
242
243   for (j = *i; j < num_objects; j += num_threads) {
244     GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
245
246     /* g_message ("THREAD %p: setting default name on object %d\n",
247        g_thread_self (), j); */
248     gst_object_set_name (o, NULL);
249     THREAD_SWITCH ();
250   }
251
252   /* thread is done, so let's return */
253   g_message ("THREAD %p: set name\n", g_thread_self ());
254   g_free (i);
255
256   return NULL;
257 }
258
259 static gint
260 gst_object_name_compare (GstObject * o, GstObject * p)
261 {
262   gint result;
263
264   GST_OBJECT_LOCK (o);
265   GST_OBJECT_LOCK (p);
266
267   if (o->name == NULL && p->name == NULL) {
268     result = 0;
269   } else if (o->name == NULL) {
270     result = -1;
271   } else if (p->name == NULL) {
272     result = 1;
273   } else {
274     result = strcmp (o->name, p->name);
275   }
276
277   GST_OBJECT_UNLOCK (p);
278   GST_OBJECT_UNLOCK (o);
279
280   return result;
281 }
282
283 GST_START_TEST (test_fake_object_name_threaded_unique)
284 {
285   GstObject *object;
286   gint i;
287   gint *ip;
288   gchar *name1, *name2;
289   GList *l;
290
291   g_message ("\nTEST: uniqueness of default names\n");
292
293   for (i = 0; i < num_objects; ++i) {
294     object = g_object_new (gst_fake_object_get_type (), NULL);
295     object_list = g_list_append (object_list, object);
296   }
297
298   MAIN_INIT ();
299
300   mark_point ();
301   for (i = 0; i < num_threads; ++i) {
302     ip = g_new (gint, 1);
303     *ip = i;
304     MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
305   }
306
307   mark_point ();
308   MAIN_SYNCHRONIZE ();
309   mark_point ();
310   MAIN_STOP_THREADS ();
311
312   /* sort GList based on object name */
313   /* FIXME: sort and test */
314   object_list =
315       g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
316
317   name1 = gst_object_get_name (GST_OBJECT (object_list->data));
318   for (l = object_list->next; l->next; l = l->next) {
319     g_message ("object with name %s\n", name1);
320     name2 = gst_object_get_name (GST_OBJECT (l->data));
321     fail_if (strcmp (name1, name2) == 0, "Two objects with name %s", name2);
322     g_free (name1);
323     name1 = name2;
324   }
325
326   /* free stuff */
327   g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
328 }
329
330 GST_END_TEST
331 /* parentage test on GstFakeObject */
332 GST_START_TEST (test_fake_object_parentage)
333 {
334   GstObject *object1, *object2;
335   GstObject *parent;
336   gboolean result;
337
338   /* create new object */
339   object1 = g_object_new (gst_fake_object_get_type (), NULL);
340   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
341   fail_unless (GST_IS_OBJECT (object1),
342       "GstFakeObject instance is not a GstObject");
343   fail_unless (GST_OBJECT_IS_FLOATING (object1),
344       "GstFakeObject instance is not floating");
345
346   /* check the parent */
347   parent = gst_object_get_parent (object1);
348   fail_if (parent != NULL, "GstFakeObject has parent");
349   /* try to set a NULL parent, this should give a warning */
350   ASSERT_CRITICAL (result = gst_object_set_parent (object1, NULL));
351   fail_if (result == TRUE, "GstFakeObject accepted NULL parent");
352   /* try to set itself as parent, we expect a warning here */
353   ASSERT_CRITICAL (result = gst_object_set_parent (object1, object1));
354   fail_if (result == TRUE, "GstFakeObject accepted itself as parent");
355
356   /* should still be floating */
357   fail_unless (GST_OBJECT_IS_FLOATING (object1),
358       "GstFakeObject instance is not floating");
359
360   /* create another object */
361   object2 = g_object_new (gst_fake_object_get_type (), NULL);
362   fail_if (object2 == NULL,
363       "Failed to create another instance of GstFakeObject");
364   fail_unless (GST_IS_OBJECT (object2),
365       "second GstFakeObject instance is not a GstObject");
366   fail_unless (GST_OBJECT_IS_FLOATING (object1),
367       "GstFakeObject instance is not floating");
368
369   /* try to set other object as parent */
370   result = gst_object_set_parent (object1, object2);
371   fail_if (result == FALSE,
372       "GstFakeObject could not accept other object as parent");
373
374   /* should not be floating anymore */
375   fail_if (GST_OBJECT_IS_FLOATING (object1),
376       "GstFakeObject instance is still floating");
377   /* parent should still be floating */
378   fail_unless (GST_OBJECT_IS_FLOATING (object2),
379       "GstFakeObject instance is not floating");
380
381   /* check the parent */
382   parent = gst_object_get_parent (object1);
383   fail_if (parent != object2, "GstFakeObject has wrong parent");
384   gst_object_unref (parent);
385   /* try to set other object as parent again */
386   result = gst_object_set_parent (object1, object2);
387   fail_if (result == TRUE, "GstFakeObject could set parent twice");
388
389   /* ref before unparenting */
390   gst_object_ref (object1);
391   /* clear parent of object */
392   gst_object_unparent (object1);
393
394   /* check the parent */
395   parent = gst_object_get_parent (object1);
396   fail_if (parent != NULL, "GstFakeObject has parent");
397
398   /* object should not be floating */
399   fail_if (GST_OBJECT_IS_FLOATING (object1),
400       "GstFakeObject instance is floating again");
401
402   gst_object_unref (object1);
403   gst_object_unref (object2);
404 }
405
406 GST_END_TEST
407 /* parentage test dispose on GstFakeObject, since our testcase
408  * does not handle the parent relation completely, the parent does
409  * not hold a ref to the child, we cannot dispose the parent to
410  * dipose the child as well. This test needs to be run with DEBUG
411  * info to check if the finalize methods are called correctly. */
412 GST_START_TEST (test_fake_object_parentage_dispose)
413 {
414   GstObject *object1, *object2;
415   gboolean result;
416
417   object1 = g_object_new (gst_fake_object_get_type (), NULL);
418   fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
419
420   object2 = g_object_new (gst_fake_object_get_type (), NULL);
421   fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
422
423   /* try to set other object as parent */
424   result = gst_object_set_parent (object1, object2);
425   fail_if (result == FALSE,
426       "GstFakeObject could not accept other object as parent");
427
428   /* clear parent of object */
429   gst_object_unparent (object1);
430
431   /* now dispose parent */
432   gst_object_unref (object2);
433 }
434
435 GST_END_TEST
436 /* test: try renaming a parented object, make sure it fails */
437     Suite * gst_object_suite (void)
438 {
439   Suite *s = suite_create ("GstObject");
440   TCase *tc_chain = tcase_create ("general");
441
442   /* turn off timeout */
443   tcase_set_timeout (tc_chain, 60);
444
445   suite_add_tcase (s, tc_chain);
446   tcase_add_test (tc_chain, test_fake_object_new);
447   tcase_add_test (tc_chain, test_fake_object_name);
448   tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
449   tcase_add_test (tc_chain, test_fake_object_name_threaded_right);
450   tcase_add_test (tc_chain, test_fake_object_name_threaded_unique);
451   tcase_add_test (tc_chain, test_fake_object_parentage);
452   tcase_add_test (tc_chain, test_fake_object_parentage_dispose);
453   //tcase_add_checked_fixture (tc_chain, setup, teardown);
454
455   /* SEGV tests go last so we can debug the others */
456   tcase_add_test_raise_signal (tc_chain, test_fail_abstract_new, SIGSEGV);
457   return s;
458 }
459
460 GST_CHECK_MAIN (gst_object);