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 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");
79 /* g_object_new on GstFakeObject should succeed */
80 GST_START_TEST (test_fake_object_new)
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");
91 /* GstFakeObject name tests */
92 GST_START_TEST (test_fake_object_name)
98 object = g_object_new (gst_fake_object_get_type (), NULL);
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);
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);
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'");
119 /* change the gotten name to see that it's a copy and not the original */
121 name2 = gst_object_get_name (object);
122 fail_if (strcmp (name2, "fake") != 0,
123 "Copy of object name affected actual object name");
129 /* thread function for threaded name change test */
130 gpointer thread_name_object (GstObject * object)
132 gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
136 /* give main thread a head start */
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 */
147 /* thread is done, so let's return */
148 g_message ("THREAD %s: set name\n", thread_id);
155 * main thread sets and gets name while other threads set the name
156 * constantly; fails because lock is released inbetween set and get
159 GST_START_TEST (test_fake_object_name_threaded_wrong)
164 gboolean expected_failure = FALSE;
166 g_message ("\nTEST: set/get without lock\n");
168 object = g_object_new (gst_fake_object_get_type (), NULL);
169 gst_object_set_name (object, "main");
171 MAIN_START_THREADS (5, thread_name_object, object);
173 /* start looping and set/get name repeatedly */
174 for (i = 0; i < 1000; ++i) {
175 gst_object_set_name (object, "main");
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;
186 MAIN_STOP_THREADS ();
188 fail_unless (expected_failure, "name did not get changed");
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
196 GST_START_TEST (test_fake_object_name_threaded_right)
202 g_message ("\nTEST: set/get inside lock\n");
204 object = g_object_new (gst_fake_object_get_type (), NULL);
205 gst_object_set_name (object, "main");
207 MAIN_START_THREADS (5, thread_name_object, object);
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");
215 name = g_strdup (GST_OBJECT_NAME (object));
216 GST_OBJECT_UNLOCK (object);
218 fail_unless (strcmp (name, "main") == 0,
219 "Name got changed while lock held during run %d", i);
222 MAIN_STOP_THREADS ();
227 * main thread creates lots of objects
228 * child threads sets default names on objects
229 * then main thread checks uniqueness of object names
231 GList * object_list = NULL;
232 gint num_objects = 1000;
233 gint num_threads = 5;
235 /* thread function for threaded default name change test */
237 thread_name_object_default (int *i)
243 for (j = *i; j < num_objects; j += num_threads) {
244 GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
246 /* g_message ("THREAD %p: setting default name on object %d\n",
247 g_thread_self (), j); */
248 gst_object_set_name (o, NULL);
252 /* thread is done, so let's return */
253 g_message ("THREAD %p: set name\n", g_thread_self ());
260 gst_object_name_compare (GstObject * o, GstObject * p)
267 if (o->name == NULL && p->name == NULL) {
269 } else if (o->name == NULL) {
271 } else if (p->name == NULL) {
274 result = strcmp (o->name, p->name);
277 GST_OBJECT_UNLOCK (p);
278 GST_OBJECT_UNLOCK (o);
283 GST_START_TEST (test_fake_object_name_threaded_unique)
288 gchar *name1, *name2;
291 g_message ("\nTEST: uniqueness of default names\n");
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);
301 for (i = 0; i < num_threads; ++i) {
302 ip = g_new (gint, 1);
304 MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
310 MAIN_STOP_THREADS ();
312 /* sort GList based on object name */
313 /* FIXME: sort and test */
315 g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
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);
327 g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
331 /* parentage test on GstFakeObject */
332 GST_START_TEST (test_fake_object_parentage)
334 GstObject *object1, *object2;
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");
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");
356 /* should still be floating */
357 fail_unless (GST_OBJECT_IS_FLOATING (object1),
358 "GstFakeObject instance is not floating");
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");
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");
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");
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");
389 /* ref before unparenting */
390 gst_object_ref (object1);
391 /* clear parent of object */
392 gst_object_unparent (object1);
394 /* check the parent */
395 parent = gst_object_get_parent (object1);
396 fail_if (parent != NULL, "GstFakeObject has parent");
398 /* object should not be floating */
399 fail_if (GST_OBJECT_IS_FLOATING (object1),
400 "GstFakeObject instance is floating again");
402 gst_object_unref (object1);
403 gst_object_unref (object2);
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)
414 GstObject *object1, *object2;
417 object1 = g_object_new (gst_fake_object_get_type (), NULL);
418 fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
420 object2 = g_object_new (gst_fake_object_get_type (), NULL);
421 fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
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");
428 /* clear parent of object */
429 gst_object_unparent (object1);
431 /* now dispose parent */
432 gst_object_unref (object2);
436 /* test: try renaming a parented object, make sure it fails */
437 Suite * gst_object_suite (void)
439 Suite *s = suite_create ("GstObject");
440 TCase *tc_chain = tcase_create ("general");
442 /* turn off timeout */
443 tcase_set_timeout (tc_chain, 60);
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);
455 /* SEGV tests go last so we can debug the others */
456 tcase_add_test_raise_signal (tc_chain, test_fail_abstract_new, SIGSEGV);
460 GST_CHECK_MAIN (gst_object);