3 * unit test for GstObject
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
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.
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.
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.
23 #include <gst/check/gstcheck.h>
26 Create a fake subclass
28 typedef struct _GstFakeObjectClass GstFakeObjectClass;
29 typedef struct _GstFakeObject GstFakeObject;
36 struct _GstFakeObjectClass
38 GstObjectClass parent_class;
41 static GType _gst_fake_object_type = 0;
43 //static GstObjectClass *parent_class = NULL;
44 //static guint gst_fake_object_signals[LAST_SIGNAL] = { 0 };
47 gst_fake_object_get_type (void)
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,
57 sizeof (GstFakeObject),
59 NULL, //(GInstanceInitFunc) gst_fake_object_init,
63 _gst_fake_object_type = g_type_register_static (GST_TYPE_OBJECT,
64 "GstFakeObject", &fake_object_info, 0);
66 return _gst_fake_object_type;
69 /* g_object_new on abstract GstObject should fail */
70 GST_START_TEST (test_fail_abstract_new)
74 ASSERT_CRITICAL (object = g_object_new (gst_object_get_type (), NULL));
75 fail_unless (object == NULL, "Created an instance of abstract GstObject");
80 /* g_object_new on GstFakeObject should succeed */
81 GST_START_TEST (test_fake_object_new)
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);
94 /* GstFakeObject name tests */
95 GST_START_TEST (test_fake_object_name)
101 object = g_object_new (gst_fake_object_get_type (), NULL);
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);
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);
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'");
123 /* change the gotten name to see that it's a copy and not the original */
125 name2 = gst_object_get_name (object);
126 fail_if (strcmp (name2, "fake") != 0,
127 "Copy of object name affected actual object name");
131 gst_object_unref (object);
136 /* thread function for threaded name change test */
138 thread_name_object (GstObject * object)
140 gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
144 /* give main thread a head start */
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 */
155 /* thread is done, so let's return */
156 g_message ("THREAD %s: set name\n", thread_id);
163 GST_START_TEST (test_fake_object_name_threaded_wrong)
168 gboolean expected_failure = FALSE;
170 g_message ("\nTEST: set/get without lock\n");
172 object = g_object_new (gst_fake_object_get_type (), NULL);
173 gst_object_set_name (object, "main");
175 MAIN_START_THREADS (5, thread_name_object, object);
177 /* start looping and set/get name repeatedly */
178 for (i = 0; i < 1000; ++i) {
179 gst_object_set_name (object, "main");
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;
190 MAIN_STOP_THREADS ();
192 gst_object_unref (object);
194 fail_unless (expected_failure, "name did not get changed");
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
204 GST_START_TEST (test_fake_object_name_threaded_right)
210 g_message ("\nTEST: set/get inside lock\n");
212 object = g_object_new (gst_fake_object_get_type (), NULL);
213 gst_object_set_name (object, "main");
215 MAIN_START_THREADS (5, thread_name_object, object);
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");
223 name = g_strdup (GST_OBJECT_NAME (object));
224 GST_OBJECT_UNLOCK (object);
226 fail_unless (strcmp (name, "main") == 0,
227 "Name got changed while lock held during run %d", i);
230 MAIN_STOP_THREADS ();
231 gst_object_unref (object);
236 * main thread creates lots of objects
237 * child threads sets default names on objects
238 * then main thread checks uniqueness of object names
241 static GList *object_list = NULL;
242 static gint num_objects = 1000;
243 static gint num_threads = 5;
245 /* thread function for threaded default name change test */
247 thread_name_object_default (int *i)
253 for (j = *i; j < num_objects; j += num_threads) {
254 GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
256 /* g_message ("THREAD %p: setting default name on object %d\n",
257 g_thread_self (), j); */
258 gst_object_set_name (o, NULL);
262 /* thread is done, so let's return */
263 g_message ("THREAD %p: set name\n", g_thread_self ());
270 gst_object_name_compare (GstObject * o, GstObject * p)
277 if (o->name == NULL && p->name == NULL) {
279 } else if (o->name == NULL) {
281 } else if (p->name == NULL) {
284 result = strcmp (o->name, p->name);
287 GST_OBJECT_UNLOCK (p);
288 GST_OBJECT_UNLOCK (o);
293 GST_START_TEST (test_fake_object_name_threaded_unique)
298 gchar *name1, *name2;
301 g_message ("\nTEST: uniqueness of default names\n");
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);
311 for (i = 0; i < num_threads; ++i) {
312 ip = g_new (gint, 1);
314 MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
320 MAIN_STOP_THREADS ();
322 /* sort GList based on object name */
323 /* FIXME: sort and test */
325 g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
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);
338 g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
343 /* parentage test on GstFakeObject */
344 GST_START_TEST (test_fake_object_parentage)
346 GstObject *object1, *object2;
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");
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");
368 /* should still be floating */
369 fail_unless (GST_OBJECT_IS_FLOATING (object1),
370 "GstFakeObject instance is not floating");
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");
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");
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");
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");
401 /* ref before unparenting */
402 gst_object_ref (object1);
403 /* clear parent of object */
404 gst_object_unparent (object1);
406 /* check the parent */
407 parent = gst_object_get_parent (object1);
408 fail_if (parent != NULL, "GstFakeObject has parent");
410 /* object should not be floating */
411 fail_if (GST_OBJECT_IS_FLOATING (object1),
412 "GstFakeObject instance is floating again");
414 gst_object_unref (object1);
415 gst_object_unref (object2);
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)
427 GstObject *object1, *object2;
430 object1 = g_object_new (gst_fake_object_get_type (), NULL);
431 fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
433 object2 = g_object_new (gst_fake_object_get_type (), NULL);
434 fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
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");
441 /* clear parent of object */
442 gst_object_unparent (object1);
444 /* now dispose parent */
445 gst_object_unref (object2);
450 /* test: try renaming a parented object, make sure it fails */
453 gst_object_suite (void)
455 Suite *s = suite_create ("GstObject");
456 TCase *tc_chain = tcase_create ("general");
458 /* turn off timeout */
459 tcase_set_timeout (tc_chain, 60);
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);
465 tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
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);
473 /* SEGV tests go last so we can debug the others */
474 tcase_add_test_raise_signal (tc_chain, test_fail_abstract_new, SIGSEGV);
478 GST_CHECK_MAIN (gst_object);