3 * unit test for GstMiniObject
5 * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
6 * Copyright (C) <2005> Tim-Philipp Müller <tim centricular net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include <gst/check/gstcheck.h>
26 GST_START_TEST (test_copy)
28 GstBuffer *buffer, *copy;
30 buffer = gst_buffer_new_and_alloc (4);
32 copy = GST_BUFFER (gst_mini_object_copy (GST_MINI_OBJECT (buffer)));
34 fail_if (copy == NULL, "Copy of buffer returned NULL");
35 fail_unless (GST_BUFFER_SIZE (copy) == 4,
36 "Copy of buffer has different size");
41 GST_START_TEST (test_is_writable)
46 buffer = gst_buffer_new_and_alloc (4);
47 mobj = GST_MINI_OBJECT (buffer);
49 fail_unless (gst_mini_object_is_writable (mobj),
50 "A buffer with one ref should be writable");
52 GST_MINI_OBJECT_FLAG_SET (mobj, GST_MINI_OBJECT_FLAG_READONLY);
53 fail_if (gst_mini_object_is_writable (mobj),
54 "A buffer with READONLY set should not be writable");
55 GST_MINI_OBJECT_FLAG_UNSET (mobj, GST_MINI_OBJECT_FLAG_READONLY);
56 fail_unless (gst_mini_object_is_writable (mobj),
57 "A buffer with one ref and READONLY not set should be writable");
59 fail_if (gst_mini_object_ref (mobj) == NULL, "Could not ref the mobj");
61 fail_if (gst_mini_object_is_writable (mobj),
62 "A buffer with two refs should not be writable");
67 GST_START_TEST (test_make_writable)
70 GstMiniObject *mobj, *mobj2, *mobj3;
72 buffer = gst_buffer_new_and_alloc (4);
73 mobj = GST_MINI_OBJECT (buffer);
75 mobj2 = gst_mini_object_make_writable (mobj);
76 fail_unless (GST_IS_BUFFER (mobj2), "make_writable did not return a buffer");
77 fail_unless (mobj == mobj2,
78 "make_writable returned a copy for a buffer with refcount 1");
80 mobj2 = gst_mini_object_ref (mobj);
81 mobj3 = gst_mini_object_make_writable (mobj);
82 fail_unless (GST_IS_BUFFER (mobj3), "make_writable did not return a buffer");
83 fail_if (mobj == mobj3,
84 "make_writable returned same object for a buffer with refcount > 1");
86 fail_unless (GST_MINI_OBJECT_REFCOUNT_VALUE (mobj) == 1,
87 "refcount of original mobj object should be back to 1");
89 mobj2 = gst_mini_object_make_writable (mobj);
90 fail_unless (GST_IS_BUFFER (mobj2), "make_writable did not return a buffer");
91 fail_unless (mobj == mobj2,
92 "make_writable returned a copy for a buffer with refcount 1");
98 gint num_threads = 10;
99 gint refs_per_thread = 10000;
101 /* test thread-safe refcounting of GstMiniObject */
103 thread_ref (GstMiniObject * mobj)
109 for (j = 0; j < refs_per_thread; ++j) {
110 gst_mini_object_ref (mobj);
112 if (j % num_threads == 0)
115 GST_DEBUG ("thread stopped");
118 GST_START_TEST (test_ref_threaded)
124 buffer = gst_buffer_new_and_alloc (4);
126 mobj = GST_MINI_OBJECT (buffer);
128 MAIN_START_THREADS (num_threads, thread_ref, mobj);
130 MAIN_STOP_THREADS ();
132 expected = num_threads * refs_per_thread + 1;
133 ASSERT_MINI_OBJECT_REFCOUNT (mobj, "miniobject", expected);
139 thread_unref (GstMiniObject * mobj)
145 for (j = 0; j < refs_per_thread; ++j) {
146 gst_mini_object_unref (mobj);
148 if (j % num_threads == 0)
153 GST_START_TEST (test_unref_threaded)
159 buffer = gst_buffer_new_and_alloc (4);
161 mobj = GST_MINI_OBJECT (buffer);
163 for (i = 0; i < num_threads * refs_per_thread; ++i)
164 gst_mini_object_ref (mobj);
166 MAIN_START_THREADS (num_threads, thread_unref, mobj);
168 MAIN_STOP_THREADS ();
170 ASSERT_MINI_OBJECT_REFCOUNT (mobj, "miniobject", 1);
173 gst_mini_object_unref (mobj);
178 /* ======== value collection test ======== */
179 typedef struct _MyFoo
184 typedef struct _MyFooClass
186 GObjectClass gobject_class;
194 G_DEFINE_TYPE (MyFoo, my_foo, G_TYPE_OBJECT)
196 static void my_foo_init (MyFoo * foo)
201 my_foo_get_property (GObject * object, guint prop_id, GValue * value,
206 g_assert (prop_id == PROP_BUFFER);
208 new_buf = gst_buffer_new_and_alloc (1024);
209 gst_value_set_mini_object (value, GST_MINI_OBJECT (new_buf));
210 gst_buffer_unref (new_buf);
214 my_foo_set_property (GObject * object, guint prop_id, const GValue * value,
217 GstMiniObject *mini_obj;
219 g_assert (prop_id == PROP_BUFFER);
221 mini_obj = gst_value_get_mini_object (value);
222 g_assert (GST_IS_MINI_OBJECT (mini_obj));
223 g_assert (GST_IS_BUFFER (mini_obj));
226 /* gst_value_dup_mini_object() does not exist yet */
227 mini_obj = gst_value_dup_mini_object (value);
228 g_assert (GST_IS_MINI_OBJECT (mini_obj));
229 g_assert (GST_IS_BUFFER (mini_obj));
230 gst_mini_object_unref (mini_obj);
236 my_foo_class_init (MyFooClass * klass)
238 GObjectClass *gobject_klass = G_OBJECT_CLASS (klass);
240 gobject_klass->get_property = my_foo_get_property;
241 gobject_klass->set_property = my_foo_set_property;
243 g_object_class_install_property (gobject_klass, PROP_BUFFER,
244 gst_param_spec_mini_object ("buffer", "Buffer",
245 "a newly created GstBuffer", GST_TYPE_BUFFER, G_PARAM_READWRITE));
248 GST_START_TEST (test_value_collection)
250 GstBuffer *buf = NULL;
253 foo = (MyFoo *) g_object_new (my_foo_get_type (), NULL);
255 /* test g_object_get() refcounting */
256 g_object_get (foo, "buffer", &buf, NULL);
257 g_assert (GST_IS_BUFFER (buf));
258 g_assert (GST_MINI_OBJECT_REFCOUNT_VALUE (GST_MINI_OBJECT_CAST (buf)) == 1);
259 gst_buffer_unref (buf);
261 /* test g_object_set() refcounting */
262 buf = gst_buffer_new_and_alloc (1024);
263 g_object_set (foo, "buffer", buf, NULL);
264 g_assert (GST_MINI_OBJECT_REFCOUNT_VALUE (GST_MINI_OBJECT_CAST (buf)) == 1);
265 gst_buffer_unref (buf);
267 g_object_unref (foo);
274 gst_mini_object_suite (void)
276 Suite *s = suite_create ("GstMiniObject");
277 TCase *tc_chain = tcase_create ("general");
279 /* turn off timeout */
280 tcase_set_timeout (tc_chain, 60);
282 suite_add_tcase (s, tc_chain);
283 tcase_add_test (tc_chain, test_copy);
284 tcase_add_test (tc_chain, test_is_writable);
285 tcase_add_test (tc_chain, test_make_writable);
286 tcase_add_test (tc_chain, test_ref_threaded);
287 tcase_add_test (tc_chain, test_unref_threaded);
288 tcase_add_test (tc_chain, test_value_collection);
292 GST_CHECK_MAIN (gst_mini_object);