index: remove GstIndex and GstIndexFactory for now
[platform/upstream/gstreamer.git] / libs / gst / check / gstcheck.h
1 /* GStreamer
2  *
3  * Common code for GStreamer unittests
4  *
5  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6  * Copyright (C) <2008> Thijs Vermeir <thijsvermeir@gmail.com>
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 #ifndef __GST_CHECK_H__
25 #define __GST_CHECK_H__
26
27 #include <signal.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <math.h>
31
32 #include <gst/check/internal-check.h>
33
34 #include <gst/gst.h>
35
36 G_BEGIN_DECLS
37
38 GST_DEBUG_CATEGORY_EXTERN (check_debug);
39 #define GST_CAT_DEFAULT check_debug
40
41 /* logging function for tests
42  * a test uses g_message() to log a debug line
43  * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
44  * messages
45  */
46 extern gboolean _gst_check_threads_running;
47 extern gboolean _gst_check_raised_critical;
48 extern gboolean _gst_check_raised_warning;
49 extern gboolean _gst_check_expecting_log;
50
51 /* global variables used in test methods */
52 extern GList * buffers;
53
54 extern GMutex *check_mutex;
55 extern GCond *check_cond;
56
57 typedef struct
58 {
59   const char *name;
60   int size;
61   int abi_size;
62 }
63 GstCheckABIStruct;
64
65 void gst_check_init (int *argc, char **argv[]);
66
67 GstFlowReturn gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer);
68
69 void gst_check_message_error (GstMessage * message, GstMessageType type,
70     GQuark domain, gint code);
71
72 GstElement *gst_check_setup_element (const gchar * factory);
73 void gst_check_teardown_element (GstElement * element);
74 GstPad *gst_check_setup_src_pad (GstElement * element,
75     GstStaticPadTemplate * tmpl);
76 GstPad * gst_check_setup_src_pad_by_name (GstElement * element,
77           GstStaticPadTemplate * tmpl, const gchar *name);
78 GstPad * gst_check_setup_sink_pad_by_name (GstElement * element, 
79           GstStaticPadTemplate * tmpl, const gchar *name);
80 void gst_check_teardown_pad_by_name (GstElement * element, const gchar *name);
81 void gst_check_teardown_src_pad (GstElement * element);
82 void gst_check_drop_buffers (void);
83 void gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2);
84 void gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size);
85 void gst_check_element_push_buffer_list (const gchar * element_name,
86     GList * buffer_in, GList * buffer_out, GstFlowReturn last_flow_return);
87 void gst_check_element_push_buffer (const gchar * element_name,
88     GstBuffer * buffer_in, GstBuffer * buffer_out);
89 GstPad *gst_check_setup_sink_pad (GstElement * element,
90     GstStaticPadTemplate * tmpl);
91 void gst_check_teardown_sink_pad (GstElement * element);
92 void gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes);
93 gint gst_check_run_suite (Suite * suite, const gchar * name,
94     const gchar * fname);
95
96 #define fail_unless_message_error(msg, domain, code)            \
97 gst_check_message_error (msg, GST_MESSAGE_ERROR,                \
98   GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
99 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
100
101 /**
102  * GST_START_TEST:
103  * @__testname: test function name
104  *
105  * wrapper for checks START_TEST
106  */
107 /**
108  * GST_END_TEST:
109  *
110  * wrapper for checks END_TEST
111  */
112 #define GST_START_TEST(__testname) \
113 static void __testname (int __i__)\
114 {\
115   GST_DEBUG ("test start"); \
116   tcase_fn_start (""# __testname, __FILE__, __LINE__);
117
118 #define GST_END_TEST GST_LOG ("cleaning up tasks"); \
119                      gst_task_cleanup_all (); \
120                      END_TEST
121
122 /* additional fail macros */
123 /**
124  * fail_unless_equals_int:
125  * @a: a #gint value or expression
126  * @b: a #gint value or expression
127  *
128  * This macro checks that @a and @b are equal and aborts if this is not the
129  * case, printing both expressions and the values they evaluated to. This
130  * macro is for use in unit tests.
131  */
132 #define fail_unless_equals_int(a, b)                                    \
133 G_STMT_START {                                                          \
134   int first = a;                                                        \
135   int second = b;                                                       \
136   fail_unless(first == second,                                          \
137     "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second);       \
138 } G_STMT_END;
139 /**
140  * assert_equals_int:
141  * @a: a #gint value or expression
142  * @b: a #gint value or expression
143  *
144  * This macro checks that @a and @b are equal and aborts if this is not the
145  * case, printing both expressions and the values they evaluated to. This
146  * macro is for use in unit tests.
147  */
148 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
149
150 /**
151  * fail_unless_equals_int64:
152  * @a: a #gint64 value or expression
153  * @b: a #gint64 value or expression
154  *
155  * This macro checks that @a and @b are equal and aborts if this is not the
156  * case, printing both expressions and the values they evaluated to. This
157  * macro is for use in unit tests.
158  */
159 #define fail_unless_equals_int64(a, b)                                  \
160 G_STMT_START {                                                          \
161   gint64 first = a;                                                     \
162   gint64 second = b;                                                    \
163   fail_unless(first == second,                                          \
164     "'" #a "' (%" G_GINT64_FORMAT") is not equal to '" #b"' (%"         \
165     G_GINT64_FORMAT")", first, second);                                 \
166 } G_STMT_END;
167 /**
168  * assert_equals_int64:
169  * @a: a #gint64 value or expression
170  * @b: a #gint64 value or expression
171  *
172  * This macro checks that @a and @b are equal and aborts if this is not the
173  * case, printing both expressions and the values they evaluated to. This
174  * macro is for use in unit tests.
175  */
176 #define assert_equals_int64(a, b) fail_unless_equals_int64(a, b)
177
178 /**
179  * fail_unless_equals_uint64:
180  * @a: a #guint64 value or expression
181  * @b: a #guint64 value or expression
182  *
183  * This macro checks that @a and @b are equal and aborts if this is not the
184  * case, printing both expressions and the values they evaluated to. This
185  * macro is for use in unit tests.
186  */
187 #define fail_unless_equals_uint64(a, b)                                 \
188 G_STMT_START {                                                          \
189   guint64 first = a;                                                    \
190   guint64 second = b;                                                   \
191   fail_unless(first == second,                                          \
192     "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%"       \
193     G_GUINT64_FORMAT ")", first, second);                               \
194 } G_STMT_END;
195 /**
196  * assert_equals_uint64:
197  * @a: a #guint64 value or expression
198  * @b: a #guint64 value or expression
199  *
200  * This macro checks that @a and @b are equal and aborts if this is not the
201  * case, printing both expressions and the values they evaluated to. This
202  * macro is for use in unit tests.
203  */
204 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
205
206 /**
207  * fail_unless_equals_string:
208  * @a: a string literal or expression
209  * @b: a string literal or expression
210  *
211  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
212  * this is not the case, printing both expressions and the values they
213  * evaluated to. This macro is for use in unit tests.
214  */
215 #define fail_unless_equals_string(a, b)                             \
216 G_STMT_START {                                                      \
217   const gchar * first = a;                                          \
218   const gchar * second = b;                                         \
219   fail_unless(g_strcmp0 (first, second) == 0,                          \
220     "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second);   \
221 } G_STMT_END;
222 /**
223  * assert_equals_string:
224  * @a: a string literal or expression
225  * @b: a string literal or expression
226  *
227  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
228  * this is not the case, printing both expressions and the values they
229  * evaluated to. This macro is for use in unit tests.
230  */
231 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
232
233 /**
234  * fail_unless_equals_float:
235  * @a: a #gdouble or #gfloat value or expression
236  * @b: a #gdouble or #gfloat value or expression
237  *
238  * This macro checks that @a and @b are (almost) equal and aborts if this
239  * is not the case, printing both expressions and the values they evaluated
240  * to. This macro is for use in unit tests.
241  *
242  * Since: 0.10.14
243  */
244 #define fail_unless_equals_float(a, b)                            \
245 G_STMT_START {                                                    \
246   double first = a;                                               \
247   double second = b;                                              \
248   /* This will only work for 'normal' values and values around 0, \
249    * which should be good enough for our purposes here */         \
250   fail_unless(fabs (first - second) < 0.0000001,                  \
251     "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
252 } G_STMT_END;
253
254 /**
255  * assert_equals_float:
256  * @a: a #gdouble or #gfloat value or expression
257  * @b: a #gdouble or #gfloat value or expression
258  *
259  * This macro checks that @a and @b are (almost) equal and aborts if this
260  * is not the case, printing both expressions and the values they evaluated
261  * to. This macro is for use in unit tests.
262  *
263  * Since: 0.10.14
264  */
265 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
266
267
268 /***
269  * thread test macros and variables
270  */
271 extern GList *thread_list;
272 extern GMutex *mutex;
273 extern GCond *start_cond;       /* used to notify main thread of thread startups */
274 extern GCond *sync_cond;        /* used to synchronize all threads and main thread */
275
276 #define MAIN_START_THREADS(count, function, data)               \
277 MAIN_INIT();                                                    \
278 MAIN_START_THREAD_FUNCTIONS(count, function, data);             \
279 MAIN_SYNCHRONIZE();
280
281 #if GLIB_CHECK_VERSION (2, 31, 0)
282 #define g_thread_create gst_g_thread_create
283 static inline GThread *
284 gst_g_thread_create (GThreadFunc func, gpointer data, gboolean joinable,
285     GError **error)
286 {
287   GThread *thread = g_thread_try_new ("gst-check", func, data, error);
288   if (!joinable)
289     g_thread_unref (thread);
290   return thread;
291 }
292 #define g_mutex_new gst_g_mutex_new
293 static inline GMutex *
294 gst_g_mutex_new (void)
295 {
296   GMutex *mutex = g_slice_new (GMutex);
297   g_mutex_init (mutex);
298   return mutex;
299 }
300 #define g_mutex_free gst_g_mutex_free
301 static inline void
302 gst_g_mutex_free (GMutex *mutex)
303 {
304   g_mutex_clear (mutex);
305   g_slice_free (GMutex, mutex);
306 }
307 #define g_static_rec_mutex_init gst_g_static_rec_mutex_init
308 static inline void
309 gst_g_static_rec_mutex_init (GStaticRecMutex *mutex)
310 {
311   static const GStaticRecMutex init_mutex = G_STATIC_REC_MUTEX_INIT;
312
313   *mutex = init_mutex;
314 }
315 #define g_cond_new gst_g_cond_new
316 static inline GCond *
317 gst_g_cond_new (void)
318 {
319   GCond *cond = g_slice_new (GCond);
320   g_cond_init (cond);
321   return cond;
322 }
323 #define g_cond_free gst_g_cond_free
324 static inline void
325 gst_g_cond_free (GCond *cond)
326 {
327   g_cond_clear (cond);
328   g_slice_free (GCond, cond);
329 }
330 #define g_cond_timed_wait gst_g_cond_timed_wait
331 static inline gboolean
332 gst_g_cond_timed_wait (GCond *cond, GMutex *mutex, GTimeVal *abs_time)
333 {
334   gint64 end_time;
335
336   if (abs_time == NULL) {
337     g_cond_wait (cond, mutex);
338     return TRUE;
339   }
340
341   end_time = abs_time->tv_sec;
342   end_time *= 1000000;
343   end_time += abs_time->tv_usec;
344
345   /* would be nice if we had clock_rtoffset, but that didn't seem to
346    * make it into the kernel yet...
347    */
348   /* if CLOCK_MONOTONIC is not defined then g_get_montonic_time() and
349    * g_get_real_time() are returning the same clock and we'd add ~0
350    */
351   end_time += g_get_monotonic_time () - g_get_real_time ();
352   return g_cond_wait_until (cond, mutex, end_time);
353 }
354 #endif
355
356 #define MAIN_INIT()                     \
357 G_STMT_START {                          \
358   _gst_check_threads_running = TRUE;    \
359                                         \
360   if (mutex == NULL) {                  \
361     mutex = g_mutex_new ();             \
362     start_cond = g_cond_new ();         \
363     sync_cond = g_cond_new ();          \
364   }                                     \
365 } G_STMT_END;
366
367 #define MAIN_START_THREAD_FUNCTIONS(count, function, data)      \
368 G_STMT_START {                                                  \
369   int i;                                                        \
370   for (i = 0; i < count; ++i) {                                 \
371     MAIN_START_THREAD_FUNCTION (i, function, data);             \
372   }                                                             \
373 } G_STMT_END;
374
375 #define MAIN_START_THREAD_FUNCTION(i, function, data)           \
376 G_STMT_START {                                                  \
377     GThread *thread = NULL;                                     \
378     GST_DEBUG ("MAIN: creating thread %d", i);                  \
379     g_mutex_lock (mutex);                                       \
380     thread = g_thread_create ((GThreadFunc) function, data,     \
381         TRUE, NULL);                                            \
382     /* wait for thread to signal us that it's ready */          \
383     GST_DEBUG ("MAIN: waiting for thread %d", i);               \
384     g_cond_wait (start_cond, mutex);                            \
385     g_mutex_unlock (mutex);                                     \
386                                                                 \
387     thread_list = g_list_append (thread_list, thread);          \
388 } G_STMT_END;
389
390
391 #define MAIN_SYNCHRONIZE()              \
392 G_STMT_START {                          \
393   GST_DEBUG ("MAIN: synchronizing");    \
394   g_cond_broadcast (sync_cond);         \
395   GST_DEBUG ("MAIN: synchronized");     \
396 } G_STMT_END;
397
398 #define MAIN_STOP_THREADS()                                     \
399 G_STMT_START {                                                  \
400   _gst_check_threads_running = FALSE;                           \
401                                                                 \
402   /* join all threads */                                        \
403   GST_DEBUG ("MAIN: joining");                                  \
404   g_list_foreach (thread_list, (GFunc) g_thread_join, NULL);    \
405   g_list_free (thread_list);                                    \
406   thread_list = NULL;                                           \
407   GST_DEBUG ("MAIN: joined");                                   \
408 } G_STMT_END;
409
410 #define THREAD_START()                                          \
411 THREAD_STARTED();                                               \
412 THREAD_SYNCHRONIZE();
413
414 #define THREAD_STARTED()                                        \
415 G_STMT_START {                                                  \
416   /* signal main thread that we started */                      \
417   GST_DEBUG ("THREAD %p: started", g_thread_self ());           \
418   g_mutex_lock (mutex);                                         \
419   g_cond_signal (start_cond);                                   \
420 } G_STMT_END;
421
422 #define THREAD_SYNCHRONIZE()                                    \
423 G_STMT_START {                                                  \
424   /* synchronize everyone */                                    \
425   GST_DEBUG ("THREAD %p: syncing", g_thread_self ());           \
426   g_cond_wait (sync_cond, mutex);                               \
427   GST_DEBUG ("THREAD %p: synced", g_thread_self ());            \
428   g_mutex_unlock (mutex);                                       \
429 } G_STMT_END;
430
431 #define THREAD_SWITCH()                                         \
432 G_STMT_START {                                                  \
433   /* a minimal sleep is a context switch */                     \
434   g_usleep (1);                                                 \
435 } G_STMT_END;
436
437 #define THREAD_TEST_RUNNING()   (_gst_check_threads_running == TRUE)
438
439 /* additional assertions */
440 #define ASSERT_CRITICAL(code)                                   \
441 G_STMT_START {                                                  \
442   _gst_check_expecting_log = TRUE;                              \
443   _gst_check_raised_critical = FALSE;                           \
444   code;                                                         \
445   _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
446                 "Expected g_critical, got nothing", NULL);      \
447   _gst_check_expecting_log = FALSE;                             \
448 } G_STMT_END
449
450 #define ASSERT_WARNING(code)                                    \
451 G_STMT_START {                                                  \
452   _gst_check_expecting_log = TRUE;                              \
453   _gst_check_raised_warning = FALSE;                            \
454   code;                                                         \
455   _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__,  \
456                 "Expected g_warning, got nothing", NULL);       \
457   _gst_check_expecting_log = FALSE;                             \
458 } G_STMT_END
459
460
461 #define ASSERT_OBJECT_REFCOUNT(object, name, value)             \
462 G_STMT_START {                                                  \
463   int rc;                                                       \
464   rc = GST_OBJECT_REFCOUNT_VALUE (object);                      \
465   fail_unless (rc == value,                                     \
466       "%s (%p) refcount is %d instead of %d",                   \
467       name, object, rc, value);                                 \
468 } G_STMT_END
469
470 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper)      \
471 G_STMT_START {                                                          \
472   int rc = GST_OBJECT_REFCOUNT_VALUE (object);                          \
473   int lo = lower;                                                       \
474   int hi = upper;                                                       \
475                                                                         \
476   fail_unless (rc >= lo,                                                \
477       "%s (%p) refcount %d is smaller than %d",                         \
478       name, object, rc, lo);                                            \
479   fail_unless (rc <= hi,                                                \
480       "%s (%p) refcount %d is bigger than %d",                          \
481       name, object, rc, hi);                                            \
482 } G_STMT_END
483
484
485 #define ASSERT_CAPS_REFCOUNT(caps, name, value)                 \
486         ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
487
488 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value)             \
489         ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
490
491 #define ASSERT_MINI_OBJECT_REFCOUNT(miniobj, name, value)       \
492 G_STMT_START {                                                  \
493   int rc;                                                       \
494   rc = GST_MINI_OBJECT_REFCOUNT_VALUE (miniobj);                \
495   fail_unless (rc == value,                                     \
496                name " (%p) refcount is %d instead of %d", miniobj, rc, value); \
497 } G_STMT_END
498
499 #define ASSERT_SET_STATE(element, state, ret)                   \
500 fail_unless (gst_element_set_state (element,                    \
501   state) == ret,                                                \
502   "could not change state to " #state);
503
504 #define GST_CHECK_MAIN(name)                                    \
505 int main (int argc, char **argv)                                \
506 {                                                               \
507   Suite *s;                                                     \
508   gst_check_init (&argc, &argv);                                \
509   s = name ## _suite ();                                        \
510   return gst_check_run_suite (s, # name, __FILE__);             \
511 }
512
513 /* Hack to allow run-time selection of unit tests to run via the
514  * GST_CHECKS environment variable (test function names, comma-separated) */
515
516 gboolean _gst_check_run_test_func (const gchar * func_name);
517
518 static inline void
519 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
520     int allowed_exit_value, int start, int end)
521 {
522   if (_gst_check_run_test_func (fname)) {
523     _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
524   }
525 }
526
527 #define _tcase_add_test __gst_tcase_add_test
528
529 G_END_DECLS
530
531 #endif /* __GST_CHECK_H__ */