gstreamer_core/tsrc/check/gst/gstminiobject/src/gstminiobject.c
changeset 0 0e761a78d257
child 8 4a7fac7dd34a
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  *
       
     3  * unit test for GstMiniObject
       
     4  *
       
     5  * Copyright (C) <2005> Thomas Vander Stichele <thomas at apestaart dot org>
       
     6  * Copyright (C) <2005> Tim-Philipp Müller <tim centricular net>
       
     7  *
       
     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.
       
    12  *
       
    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.
       
    17  *
       
    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.
       
    22  */
       
    23 
       
    24 
       
    25 #include <gst/gst_global.h>
       
    26 #include <gst/check/gstcheck.h>
       
    27 #include <glib_global.h>
       
    28 
       
    29 #define LOG_FILE "c:\\logs\\gstminiobject_log1.txt" 
       
    30 #include "std_log_result.h" 
       
    31 #define LOG_FILENAME_LINE __FILE__, __LINE__
       
    32 
       
    33 void create_xml(int result)
       
    34 {
       
    35     if(result)
       
    36         assert_failed = 1;
       
    37     
       
    38     testResultXml(xmlfile);
       
    39     close_log_file();
       
    40 }
       
    41 
       
    42 #if EMULATOR
       
    43 static GET_GLOBAL_VAR_FROM_TLS(raised_critical,gstcheck,gboolean)
       
    44 #define _gst_check_raised_critical (*GET_GSTREAMER_WSD_VAR_NAME(raised_critical,gstcheck,g)())
       
    45 #else 
       
    46 extern gboolean _gst_check_raised_critical = FALSE;
       
    47 #endif
       
    48 //gboolean _gst_check_raised_warning = FALSE;
       
    49 #if EMULATOR
       
    50 static GET_GLOBAL_VAR_FROM_TLS(raised_warning,gstcheck,gboolean)
       
    51 #define _gst_check_raised_warning (*GET_GSTREAMER_WSD_VAR_NAME(raised_warning,gstcheck,g)())
       
    52 #else 
       
    53 extern gboolean _gst_check_raised_warning = FALSE;
       
    54 #endif
       
    55 //gboolean _gst_check_expecting_log = FALSE;
       
    56 #if EMULATOR
       
    57 static GET_GLOBAL_VAR_FROM_TLS(expecting_log,gstcheck,gboolean)
       
    58 #define _gst_check_expecting_log (*GET_GSTREAMER_WSD_VAR_NAME(expecting_log,gstcheck,g)())
       
    59 #else 
       
    60 extern gboolean _gst_check_expecting_log = FALSE;
       
    61 #endif
       
    62 
       
    63 //gboolean _gst_check_expecting_log = FALSE;
       
    64 #if EMULATOR
       
    65 static GET_GLOBAL_VAR_FROM_TLS(threads_running,gstcheck,gboolean)
       
    66 #define _gst_check_threads_running (*GET_GSTREAMER_WSD_VAR_NAME(threads_running,gstcheck,g)())
       
    67 #else 
       
    68 extern gboolean _gst_check_threads_running = FALSE;
       
    69 #endif
       
    70 
       
    71 
       
    72 
       
    73 #if EMULATOR
       
    74 GET_GLOBAL_VAR_FROM_TLS(thread_list,gstcheck,GList*)
       
    75 #define thread_list (*GET_GSTREAMER_WSD_VAR_NAME(thread_list,gstcheck,g)())
       
    76 #else 
       
    77 extern GList *thread_list = NULL;
       
    78 #endif
       
    79 
       
    80 //GMutex *mutex;
       
    81 #if EMULATOR
       
    82 GET_GLOBAL_VAR_FROM_TLS(mutex,gstcheck,GMutex*)
       
    83 #define mutex (*GET_GSTREAMER_WSD_VAR_NAME(mutex,gstcheck,g)())
       
    84 #else 
       
    85 extern GMutex *mutex = NULL;
       
    86 #endif
       
    87 
       
    88 //GCond *start_cond;              /* used to notify main thread of thread startups */
       
    89 #if EMULATOR
       
    90 GET_GLOBAL_VAR_FROM_TLS(start_cond,gstcheck,GCond*)
       
    91 #define start_cond (*GET_GSTREAMER_WSD_VAR_NAME(start_cond,gstcheck,g)())
       
    92 #else 
       
    93 extern GCond *start_cond = NULL;
       
    94 #endif
       
    95 
       
    96 //GCond *sync_cond;               /* used to synchronize all threads and main thread */
       
    97 #if EMULATOR
       
    98 GET_GLOBAL_VAR_FROM_TLS(sync_cond,gstcheck,GCond*)
       
    99 #define sync_cond (*GET_GSTREAMER_WSD_VAR_NAME(sync_cond,gstcheck,g)())
       
   100 #else 
       
   101 extern GCond *sync_cond = NULL;
       
   102 #endif
       
   103 
       
   104 void test_copy()
       
   105 {
       
   106   GstBuffer *buffer, *copy;
       
   107   
       
   108   xmlfile = "gstminiobject_test_copy";
       
   109   std_log(LOG_FILENAME_LINE, "Test Started test_copy");
       
   110 
       
   111   buffer = gst_buffer_new_and_alloc (4);
       
   112 
       
   113   copy = GST_BUFFER (gst_mini_object_copy (GST_MINI_OBJECT (buffer)));
       
   114 
       
   115   fail_if (copy == NULL, "Copy of buffer returned NULL");
       
   116   fail_unless (GST_BUFFER_SIZE (copy) == 4,
       
   117       "Copy of buffer has different size");
       
   118   
       
   119   std_log(LOG_FILENAME_LINE, "Test Successful");
       
   120     create_xml(0);
       
   121 }
       
   122 
       
   123 
       
   124 
       
   125 void test_is_writable()
       
   126 {
       
   127   GstBuffer *buffer;
       
   128   GstMiniObject *mobj;
       
   129   
       
   130   xmlfile = "gstminiobject_test_is_writable";
       
   131     std_log(LOG_FILENAME_LINE, "Test Started test_is_writable");
       
   132 
       
   133   buffer = gst_buffer_new_and_alloc (4);
       
   134   mobj = GST_MINI_OBJECT (buffer);
       
   135 
       
   136   fail_unless (gst_mini_object_is_writable (mobj),
       
   137       "A buffer with one ref should be writable");
       
   138 
       
   139   GST_MINI_OBJECT_FLAG_SET (mobj, GST_MINI_OBJECT_FLAG_READONLY);
       
   140   fail_if (gst_mini_object_is_writable (mobj),
       
   141       "A buffer with READONLY set should not be writable");
       
   142   GST_MINI_OBJECT_FLAG_UNSET (mobj, GST_MINI_OBJECT_FLAG_READONLY);
       
   143   fail_unless (gst_mini_object_is_writable (mobj),
       
   144       "A buffer with one ref and READONLY not set should be writable");
       
   145 
       
   146   fail_if (gst_mini_object_ref (mobj) == NULL, "Could not ref the mobj");
       
   147 
       
   148   fail_if (gst_mini_object_is_writable (mobj),
       
   149       "A buffer with two refs should not be writable");
       
   150   
       
   151   std_log(LOG_FILENAME_LINE, "Test Successful");
       
   152     create_xml(0);
       
   153 }
       
   154 
       
   155 
       
   156 
       
   157 void test_make_writable()
       
   158 {
       
   159   GstBuffer *buffer;
       
   160   GstMiniObject *mobj, *mobj2, *mobj3;
       
   161   
       
   162   xmlfile = "gstminiobject_test_make_writable";
       
   163       std_log(LOG_FILENAME_LINE, "Test Started test_make_writable");
       
   164 
       
   165   buffer = gst_buffer_new_and_alloc (4);
       
   166   mobj = GST_MINI_OBJECT (buffer);
       
   167 
       
   168   mobj2 = gst_mini_object_make_writable (mobj);
       
   169   fail_unless (GST_IS_BUFFER (mobj2), "make_writable did not return a buffer");
       
   170   fail_unless (mobj == mobj2,
       
   171       "make_writable returned a copy for a buffer with refcount 1");
       
   172 
       
   173   mobj2 = gst_mini_object_ref (mobj);
       
   174   mobj3 = gst_mini_object_make_writable (mobj);
       
   175   fail_unless (GST_IS_BUFFER (mobj3), "make_writable did not return a buffer");
       
   176   fail_if (mobj == mobj3,
       
   177       "make_writable returned same object for a buffer with refcount > 1");
       
   178 
       
   179   fail_unless (GST_MINI_OBJECT_REFCOUNT_VALUE (mobj) == 1,
       
   180       "refcount of original mobj object should be back to 1");
       
   181 
       
   182   mobj2 = gst_mini_object_make_writable (mobj);
       
   183   fail_unless (GST_IS_BUFFER (mobj2), "make_writable did not return a buffer");
       
   184   fail_unless (mobj == mobj2,
       
   185       "make_writable returned a copy for a buffer with refcount 1");
       
   186   
       
   187   std_log(LOG_FILENAME_LINE, "Test Successful");
       
   188     create_xml(0);
       
   189 }
       
   190 
       
   191 
       
   192 
       
   193 static gint num_threads = 10;
       
   194 static gint refs_per_thread = 10000;
       
   195 
       
   196 /* test thread-safe refcounting of GstMiniObject */
       
   197 static void
       
   198 thread_ref (GstMiniObject * mobj)
       
   199 {
       
   200   int j;
       
   201 
       
   202   THREAD_START ();
       
   203 
       
   204   for (j = 0; j < refs_per_thread; ++j) {
       
   205     gst_mini_object_ref (mobj);
       
   206 
       
   207     if (j % num_threads == 0)
       
   208       THREAD_SWITCH ();
       
   209   }
       
   210   GST_DEBUG ("thread stopped");
       
   211 }
       
   212 
       
   213 void test_ref_threaded()
       
   214 {
       
   215   GstBuffer *buffer;
       
   216   GstMiniObject *mobj;
       
   217   gint expected;
       
   218   
       
   219   xmlfile = "gstminiobject_test_ref_threaded";
       
   220   std_log(LOG_FILENAME_LINE, "Test Started test_ref_threaded");
       
   221 
       
   222   buffer = gst_buffer_new_and_alloc (4);
       
   223 
       
   224   mobj = GST_MINI_OBJECT (buffer);
       
   225 
       
   226   MAIN_START_THREADS (num_threads, thread_ref, mobj);
       
   227 
       
   228   MAIN_STOP_THREADS ();
       
   229 
       
   230   expected = num_threads * refs_per_thread + 1;
       
   231   ASSERT_MINI_OBJECT_REFCOUNT (mobj, "miniobject", expected);
       
   232   
       
   233   std_log(LOG_FILENAME_LINE, "Test Successful");
       
   234     create_xml(0);
       
   235 }
       
   236 
       
   237 
       
   238 
       
   239 static void
       
   240 thread_unref (GstMiniObject * mobj)
       
   241 {
       
   242   int j;
       
   243 
       
   244   THREAD_START ();
       
   245 
       
   246   for (j = 0; j < refs_per_thread; ++j) {
       
   247     gst_mini_object_unref (mobj);
       
   248 
       
   249     if (j % num_threads == 0)
       
   250       THREAD_SWITCH ();
       
   251   }
       
   252 }
       
   253 
       
   254 void test_unref_threaded()
       
   255 {
       
   256   GstBuffer *buffer;
       
   257   GstMiniObject *mobj;
       
   258   int i;
       
   259   
       
   260   xmlfile = "gstminiobject_test_unref_threaded";
       
   261     std_log(LOG_FILENAME_LINE, "Test Started test_unref_threaded");
       
   262 
       
   263   buffer = gst_buffer_new_and_alloc (4);
       
   264 
       
   265   mobj = GST_MINI_OBJECT (buffer);
       
   266 
       
   267   for (i = 0; i < num_threads * refs_per_thread; ++i)
       
   268     gst_mini_object_ref (mobj);
       
   269 
       
   270   MAIN_START_THREADS (num_threads, thread_unref, mobj);
       
   271 
       
   272   MAIN_STOP_THREADS ();
       
   273 
       
   274   ASSERT_MINI_OBJECT_REFCOUNT (mobj, "miniobject", 1);
       
   275 
       
   276   /* final unref */
       
   277   gst_mini_object_unref (mobj);
       
   278   
       
   279   std_log(LOG_FILENAME_LINE, "Test Successful");
       
   280     create_xml(0);
       
   281 }
       
   282 
       
   283 
       
   284 
       
   285 /* ======== value collection test ======== */
       
   286 typedef struct _MyFoo
       
   287 {
       
   288   GObject object;
       
   289 } MyFoo;
       
   290 
       
   291 typedef struct _MyFooClass
       
   292 {
       
   293   GObjectClass gobject_class;
       
   294 } MyFooClass;
       
   295 
       
   296 enum
       
   297 {
       
   298   PROP_BUFFER = 1
       
   299 };
       
   300 
       
   301 G_DEFINE_TYPE (MyFoo, my_foo, G_TYPE_OBJECT);
       
   302 
       
   303 static void
       
   304 my_foo_init (MyFoo * foo)
       
   305 {
       
   306 }
       
   307 
       
   308 static void
       
   309 my_foo_get_property (GObject * object, guint prop_id, GValue * value,
       
   310     GParamSpec * pspec)
       
   311 {
       
   312   GstBuffer *new_buf;
       
   313 
       
   314   g_assert (prop_id == PROP_BUFFER);
       
   315 
       
   316   new_buf = gst_buffer_new_and_alloc (1024);
       
   317   gst_value_set_mini_object (value, GST_MINI_OBJECT (new_buf));
       
   318   gst_buffer_unref (new_buf);
       
   319 }
       
   320 
       
   321 static void
       
   322 my_foo_set_property (GObject * object, guint prop_id, const GValue * value,
       
   323     GParamSpec * pspec)
       
   324 {
       
   325   GstMiniObject *mini_obj;
       
   326 
       
   327   g_assert (prop_id == PROP_BUFFER);
       
   328 
       
   329   mini_obj = gst_value_get_mini_object (value);
       
   330   g_assert (GST_IS_MINI_OBJECT (mini_obj));
       
   331   g_assert (GST_IS_BUFFER (mini_obj));
       
   332 
       
   333 #if 0
       
   334   /* gst_value_dup_mini_object() does not exist yet */
       
   335   mini_obj = gst_value_dup_mini_object (value);
       
   336   g_assert (GST_IS_MINI_OBJECT (mini_obj));
       
   337   g_assert (GST_IS_BUFFER (mini_obj));
       
   338   gst_mini_object_unref (mini_obj);
       
   339 #endif
       
   340 }
       
   341 
       
   342 
       
   343 static void
       
   344 my_foo_class_init (MyFooClass * klass)
       
   345 {
       
   346   GObjectClass *gobject_klass = G_OBJECT_CLASS (klass);
       
   347 
       
   348   gobject_klass->get_property = my_foo_get_property;
       
   349   gobject_klass->set_property = my_foo_set_property;
       
   350 
       
   351   g_object_class_install_property (gobject_klass, PROP_BUFFER,
       
   352       gst_param_spec_mini_object ("buffer", "Buffer",
       
   353           "a newly created GstBuffer", GST_TYPE_BUFFER, G_PARAM_READWRITE));
       
   354 }
       
   355 
       
   356 void test_value_collection()
       
   357 {
       
   358   GstBuffer *buf = NULL;
       
   359   MyFoo *foo;
       
   360   
       
   361   xmlfile = "gstminiobject_test_value_collection";
       
   362   std_log(LOG_FILENAME_LINE, "Test Started test_value_collection");
       
   363 
       
   364   foo = (MyFoo *) g_object_new (my_foo_get_type (), NULL);
       
   365 
       
   366   /* test g_object_get() refcounting */
       
   367   g_object_get (foo, "buffer", &buf, NULL);
       
   368   g_assert (GST_IS_BUFFER (buf));
       
   369   g_assert (GST_MINI_OBJECT_REFCOUNT_VALUE (GST_MINI_OBJECT_CAST (buf)) == 1);
       
   370   gst_buffer_unref (buf);
       
   371 
       
   372   /* test g_object_set() refcounting */
       
   373   buf = gst_buffer_new_and_alloc (1024);
       
   374   g_object_set (foo, "buffer", buf, NULL);
       
   375   g_assert (GST_MINI_OBJECT_REFCOUNT_VALUE (GST_MINI_OBJECT_CAST (buf)) == 1);
       
   376   gst_buffer_unref (buf);
       
   377 
       
   378   g_object_unref (foo);
       
   379   
       
   380   std_log(LOG_FILENAME_LINE, "Test Successful");
       
   381   create_xml(0);
       
   382 }
       
   383 
       
   384 
       
   385 void (*fn[]) (void) = {
       
   386 test_copy,
       
   387 test_is_writable,
       
   388 test_make_writable,
       
   389 test_ref_threaded,
       
   390 test_unref_threaded,
       
   391 test_value_collection
       
   392 };
       
   393 
       
   394 char *args[] = {
       
   395 "test_copy",
       
   396 "test_is_writable",
       
   397 "test_make_writable",
       
   398 "test_ref_threaded",
       
   399 "test_unref_threaded",
       
   400 "test_value_collection"
       
   401 };
       
   402 
       
   403 GST_CHECK_MAIN (gst_mini_object);