check: Change stream_id parameter name to match GtkDoc
[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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, 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, GstCaps * caps_in, GList * buffer_out,
87     GstCaps * caps_out, GstFlowReturn last_flow_return);
88 void gst_check_element_push_buffer (const gchar * element_name,
89     GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
90     GstCaps *caps_out);
91 GstPad *gst_check_setup_sink_pad (GstElement * element,
92     GstStaticPadTemplate * tmpl);
93 void gst_check_teardown_sink_pad (GstElement * element);
94 void gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes);
95 gint gst_check_run_suite (Suite * suite, const gchar * name,
96     const gchar * fname);
97 void gst_check_setup_events (GstPad * srcpad, GstElement * element,
98     GstCaps * caps, GstFormat format);
99 void gst_check_setup_events_with_stream_id (GstPad * srcpad,
100     GstElement * element, GstCaps * caps, GstFormat format,
101     const gchar * stream_id);
102
103 #define fail_unless_message_error(msg, domain, code)            \
104 gst_check_message_error (msg, GST_MESSAGE_ERROR,                \
105   GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
106 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
107
108 /**
109  * GST_START_TEST:
110  * @__testname: test function name
111  *
112  * wrapper for checks START_TEST
113  */
114 /**
115  * GST_END_TEST:
116  *
117  * wrapper for checks END_TEST
118  */
119 #define GST_START_TEST(__testname) \
120 static void __testname (int __i__)\
121 {\
122   GST_DEBUG ("test start"); \
123   tcase_fn_start (""# __testname, __FILE__, __LINE__);
124
125 #define GST_END_TEST GST_LOG ("cleaning up tasks"); \
126                      gst_task_cleanup_all (); \
127                      END_TEST
128
129 /* additional fail macros */
130 /**
131  * fail_unless_equals_int:
132  * @a: a #gint value or expression
133  * @b: a #gint value or expression
134  *
135  * This macro checks that @a and @b are equal and aborts if this is not the
136  * case, printing both expressions and the values they evaluated to. This
137  * macro is for use in unit tests.
138  */
139 #define fail_unless_equals_int(a, b)                                    \
140 G_STMT_START {                                                          \
141   int first = a;                                                        \
142   int second = b;                                                       \
143   fail_unless(first == second,                                          \
144     "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second);       \
145 } G_STMT_END;
146 /**
147  * assert_equals_int:
148  * @a: a #gint value or expression
149  * @b: a #gint value or expression
150  *
151  * This macro checks that @a and @b are equal and aborts if this is not the
152  * case, printing both expressions and the values they evaluated to. This
153  * macro is for use in unit tests.
154  */
155 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
156
157 /**
158  * fail_unless_equals_int_hex:
159  * @a: a #gint value or expression
160  * @b: a #gint value or expression
161  *
162  * This macro checks that @a and @b are equal and aborts if this is not the
163  * case, printing both expressions and the values they evaluated to in
164  * hexadecimal format. This macro is for use in unit tests.
165  *
166  * Since: 1.2
167  */
168 #define fail_unless_equals_int_hex(a, b)                                \
169 G_STMT_START {                                                          \
170   int first = a;                                                        \
171   int second = b;                                                       \
172   fail_unless(first == second,                                          \
173     "'" #a "' (0x%08x) is not equal to '" #b"' (0x%08x)", first, second);\
174 } G_STMT_END;
175
176 /**
177  * assert_equals_int_hex:
178  * @a: a #gint value or expression
179  * @b: a #gint value or expression
180  *
181  * This macro checks that @a and @b are equal and aborts if this is not the
182  * case, printing both expressions and the values they evaluated to in
183  * hexadecimal format. This macro is for use in unit tests.
184  *
185  * Since: 1.2
186  */
187 #define assert_equals_int_hex(a, b) fail_unless_equals_int_hex(a, b)
188
189 /**
190  * fail_unless_equals_int64:
191  * @a: a #gint64 value or expression
192  * @b: a #gint64 value or expression
193  *
194  * This macro checks that @a and @b are equal and aborts if this is not the
195  * case, printing both expressions and the values they evaluated to. This
196  * macro is for use in unit tests.
197  */
198 #define fail_unless_equals_int64(a, b)                                  \
199 G_STMT_START {                                                          \
200   gint64 first = a;                                                     \
201   gint64 second = b;                                                    \
202   fail_unless(first == second,                                          \
203     "'" #a "' (%" G_GINT64_FORMAT") is not equal to '" #b"' (%"         \
204     G_GINT64_FORMAT")", first, second);                                 \
205 } G_STMT_END;
206 /**
207  * assert_equals_int64:
208  * @a: a #gint64 value or expression
209  * @b: a #gint64 value or expression
210  *
211  * This macro checks that @a and @b are equal and aborts if this is not the
212  * case, printing both expressions and the values they evaluated to. This
213  * macro is for use in unit tests.
214  */
215 #define assert_equals_int64(a, b) fail_unless_equals_int64(a, b)
216
217 /**
218  * fail_unless_equals_int64_hex:
219  * @a: a #gint64 value or expression
220  * @b: a #gint64 value or expression
221  *
222  * This macro checks that @a and @b are equal and aborts if this is not the
223  * case, printing both expressions and the values they evaluated to in
224  * hexadecimal format. This macro is for use in unit tests.
225  *
226  * Since: 1.2
227  */
228 #define fail_unless_equals_int64_hex(a, b)                              \
229 G_STMT_START {                                                          \
230   gint64 first = a;                                                     \
231   gint64 second = b;                                                    \
232   fail_unless(first == second,                                          \
233     "'" #a "' (0x%016x) is not equal to '" #b"' (0x%016x)", first, second);\
234 } G_STMT_END;
235 /**
236  * assert_equals_int64_hex:
237  * @a: a #gint64 value or expression
238  * @b: a #gint64 value or expression
239  *
240  * This macro checks that @a and @b are equal and aborts if this is not the
241  * case, printing both expressions and the values they evaluated to in
242  * hexadecimal format. This macro is for use in unit tests.
243  *
244  * Since: 1.2
245  */
246 #define assert_equals_int64_hex(a,b) fail_unless_equals_int64_hex(a,b)
247
248 /**
249  * fail_unless_equals_uint64:
250  * @a: a #guint64 value or expression
251  * @b: a #guint64 value or expression
252  *
253  * This macro checks that @a and @b are equal and aborts if this is not the
254  * case, printing both expressions and the values they evaluated to. This
255  * macro is for use in unit tests.
256  */
257 #define fail_unless_equals_uint64(a, b)                                 \
258 G_STMT_START {                                                          \
259   guint64 first = a;                                                    \
260   guint64 second = b;                                                   \
261   fail_unless(first == second,                                          \
262     "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%"       \
263     G_GUINT64_FORMAT ")", first, second);                               \
264 } G_STMT_END;
265 /**
266  * assert_equals_uint64:
267  * @a: a #guint64 value or expression
268  * @b: a #guint64 value or expression
269  *
270  * This macro checks that @a and @b are equal and aborts if this is not the
271  * case, printing both expressions and the values they evaluated to. This
272  * macro is for use in unit tests.
273  */
274 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
275
276 /**
277  * fail_unless_equals_uint64_hex:
278  * @a: a #gint64 value or expression
279  * @b: a #gint64 value or expression
280  *
281  * This macro checks that @a and @b are equal and aborts if this is not the
282  * case, printing both expressions and the values they evaluated to in
283  * hexadecimal format. This macro is for use in unit tests.
284  *
285  * Since: 1.2
286  */
287 #define fail_unless_equals_uint64_hex(a, b)                             \
288 G_STMT_START {                                                          \
289   guint64 first = a;                                                    \
290   guint64 second = b;                                                   \
291   fail_unless(first == second,                                          \
292     "'" #a "' (0x%016x) is not equal to '" #b"' (0x%016x)", first, second);\
293 } G_STMT_END;
294 /**
295  * assert_equals_uint64_hex:
296  * @a: a #guint64 value or expression
297  * @b: a #guint64 value or expression
298  *
299  * This macro checks that @a and @b are equal and aborts if this is not the
300  * case, printing both expressions and the values they evaluated to in
301  * hexadecimal format. This macro is for use in unit tests.
302  *
303  * Since: 1.2
304  */
305 #define assert_equals_uint64_hex(a,b) fail_unless_equals_uint64_hex(a,b)
306
307 /**
308  * fail_unless_equals_string:
309  * @a: a string literal or expression
310  * @b: a string literal or expression
311  *
312  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
313  * this is not the case, printing both expressions and the values they
314  * evaluated to. This macro is for use in unit tests.
315  */
316 #define fail_unless_equals_string(a, b)                             \
317 G_STMT_START {                                                      \
318   const gchar * first = a;                                          \
319   const gchar * second = b;                                         \
320   fail_unless(g_strcmp0 (first, second) == 0,                          \
321     "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second);   \
322 } G_STMT_END;
323 /**
324  * assert_equals_string:
325  * @a: a string literal or expression
326  * @b: a string literal or expression
327  *
328  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
329  * this is not the case, printing both expressions and the values they
330  * evaluated to. This macro is for use in unit tests.
331  */
332 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
333
334 /**
335  * fail_unless_equals_float:
336  * @a: a #gdouble or #gfloat value or expression
337  * @b: a #gdouble or #gfloat value or expression
338  *
339  * This macro checks that @a and @b are (almost) equal and aborts if this
340  * is not the case, printing both expressions and the values they evaluated
341  * to. This macro is for use in unit tests.
342  */
343 #define fail_unless_equals_float(a, b)                            \
344 G_STMT_START {                                                    \
345   double first = a;                                               \
346   double second = b;                                              \
347   /* This will only work for 'normal' values and values around 0, \
348    * which should be good enough for our purposes here */         \
349   fail_unless(fabs (first - second) < 0.0000001,                  \
350     "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
351 } G_STMT_END;
352
353 /**
354  * assert_equals_float:
355  * @a: a #gdouble or #gfloat value or expression
356  * @b: a #gdouble or #gfloat value or expression
357  *
358  * This macro checks that @a and @b are (almost) equal and aborts if this
359  * is not the case, printing both expressions and the values they evaluated
360  * to. This macro is for use in unit tests.
361  */
362 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
363
364 /**
365  * fail_unless_equals_pointer:
366  * @a: a pointer value or expression
367  * @b: a pointer value or expression
368  *
369  * This macro checks that @a and @b are equal and aborts if this
370  * is not the case, printing both expressions and the values they
371  * evaluated to. This macro is for use in unit tests.
372  *
373  * Since: 1.2
374  */
375 #define fail_unless_equals_pointer(a, b)                          \
376 G_STMT_START {                                                    \
377   gpointer first = a;                                             \
378   gpointer second = b;                                            \
379   fail_unless(first == second,                                    \
380     "'" #a "' (%p) is not equal to '" #b "' (%p)", first, second);\
381 } G_STMT_END;
382
383 /**
384  * assert_equals_pointer:
385  * @a: a pointer value or expression
386  * @b: a pointer value or expression
387  *
388  * This macro checks that @a and @b are equal and aborts if this
389  * is not the case, printing both expressions and the values they
390  * evaluated to. This macro is for use in unit tests.
391  *
392  * Since: 1.2
393  */
394 #define assert_equals_pointer(a, b) fail_unless_equals_pointer(a, b)
395
396 /***
397  * thread test macros and variables
398  */
399 extern GList *thread_list;
400 extern GMutex mutex;
401 extern GCond start_cond;       /* used to notify main thread of thread startups */
402 extern GCond sync_cond;        /* used to synchronize all threads and main thread */
403
404 #define MAIN_START_THREADS(count, function, data)               \
405 MAIN_INIT();                                                    \
406 MAIN_START_THREAD_FUNCTIONS(count, function, data);             \
407 MAIN_SYNCHRONIZE();
408
409 #define MAIN_INIT()                     \
410 G_STMT_START {                          \
411   _gst_check_threads_running = TRUE;    \
412 } G_STMT_END;
413
414 #define MAIN_START_THREAD_FUNCTIONS(count, function, data)      \
415 G_STMT_START {                                                  \
416   int i;                                                        \
417   for (i = 0; i < count; ++i) {                                 \
418     MAIN_START_THREAD_FUNCTION (i, function, data);             \
419   }                                                             \
420 } G_STMT_END;
421
422 #define MAIN_START_THREAD_FUNCTION(i, function, data)           \
423 G_STMT_START {                                                  \
424     GThread *thread = NULL;                                     \
425     GST_DEBUG ("MAIN: creating thread %d", i);                  \
426     g_mutex_lock (&mutex);                                      \
427     thread = g_thread_try_new ("gst-check",                     \
428         (GThreadFunc) function, data, NULL);                    \
429     /* wait for thread to signal us that it's ready */          \
430     GST_DEBUG ("MAIN: waiting for thread %d", i);               \
431     g_cond_wait (&start_cond, &mutex);                          \
432     g_mutex_unlock (&mutex);                                    \
433                                                                 \
434     thread_list = g_list_append (thread_list, thread);          \
435 } G_STMT_END;
436
437
438 #define MAIN_SYNCHRONIZE()              \
439 G_STMT_START {                          \
440   GST_DEBUG ("MAIN: synchronizing");    \
441   g_cond_broadcast (&sync_cond);        \
442   GST_DEBUG ("MAIN: synchronized");     \
443 } G_STMT_END;
444
445 #define MAIN_STOP_THREADS()                                     \
446 G_STMT_START {                                                  \
447   _gst_check_threads_running = FALSE;                           \
448                                                                 \
449   /* join all threads */                                        \
450   GST_DEBUG ("MAIN: joining");                                  \
451   g_list_foreach (thread_list, (GFunc) g_thread_join, NULL);    \
452   g_list_free (thread_list);                                    \
453   thread_list = NULL;                                           \
454   GST_DEBUG ("MAIN: joined");                                   \
455 } G_STMT_END;
456
457 #define THREAD_START()                                          \
458 THREAD_STARTED();                                               \
459 THREAD_SYNCHRONIZE();
460
461 #define THREAD_STARTED()                                        \
462 G_STMT_START {                                                  \
463   /* signal main thread that we started */                      \
464   GST_DEBUG ("THREAD %p: started", g_thread_self ());           \
465   g_mutex_lock (&mutex);                                        \
466   g_cond_signal (&start_cond);                                  \
467 } G_STMT_END;
468
469 #define THREAD_SYNCHRONIZE()                                    \
470 G_STMT_START {                                                  \
471   /* synchronize everyone */                                    \
472   GST_DEBUG ("THREAD %p: syncing", g_thread_self ());           \
473   g_cond_wait (&sync_cond, &mutex);                             \
474   GST_DEBUG ("THREAD %p: synced", g_thread_self ());            \
475   g_mutex_unlock (&mutex);                                      \
476 } G_STMT_END;
477
478 #define THREAD_SWITCH()                                         \
479 G_STMT_START {                                                  \
480   /* a minimal sleep is a context switch */                     \
481   g_usleep (1);                                                 \
482 } G_STMT_END;
483
484 #define THREAD_TEST_RUNNING()   (_gst_check_threads_running == TRUE)
485
486 /* additional assertions */
487 #define ASSERT_CRITICAL(code)                                   \
488 G_STMT_START {                                                  \
489   _gst_check_expecting_log = TRUE;                              \
490   _gst_check_raised_critical = FALSE;                           \
491   code;                                                         \
492   _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
493                 "Expected g_critical, got nothing", NULL);      \
494   _gst_check_expecting_log = FALSE;                             \
495 } G_STMT_END
496
497 #define ASSERT_WARNING(code)                                    \
498 G_STMT_START {                                                  \
499   _gst_check_expecting_log = TRUE;                              \
500   _gst_check_raised_warning = FALSE;                            \
501   code;                                                         \
502   _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__,  \
503                 "Expected g_warning, got nothing", NULL);       \
504   _gst_check_expecting_log = FALSE;                             \
505 } G_STMT_END
506
507
508 #define ASSERT_OBJECT_REFCOUNT(object, name, value)             \
509 G_STMT_START {                                                  \
510   int rc;                                                       \
511   rc = GST_OBJECT_REFCOUNT_VALUE (object);                      \
512   fail_unless (rc == value,                                     \
513       "%s (%p) refcount is %d instead of %d",                   \
514       name, object, rc, value);                                 \
515 } G_STMT_END
516
517 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper)      \
518 G_STMT_START {                                                          \
519   int rc = GST_OBJECT_REFCOUNT_VALUE (object);                          \
520   int lo = lower;                                                       \
521   int hi = upper;                                                       \
522                                                                         \
523   fail_unless (rc >= lo,                                                \
524       "%s (%p) refcount %d is smaller than %d",                         \
525       name, object, rc, lo);                                            \
526   fail_unless (rc <= hi,                                                \
527       "%s (%p) refcount %d is bigger than %d",                          \
528       name, object, rc, hi);                                            \
529 } G_STMT_END
530
531
532 #define ASSERT_CAPS_REFCOUNT(caps, name, value)                 \
533         ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
534
535 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value)             \
536         ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
537
538 #define ASSERT_MINI_OBJECT_REFCOUNT(miniobj, name, value)       \
539 G_STMT_START {                                                  \
540   int rc;                                                       \
541   rc = GST_MINI_OBJECT_REFCOUNT_VALUE (miniobj);                \
542   fail_unless (rc == value,                                     \
543                name " (%p) refcount is %d instead of %d", miniobj, rc, value); \
544 } G_STMT_END
545
546 #define ASSERT_SET_STATE(element, state, ret)                   \
547 fail_unless (gst_element_set_state (element,                    \
548   state) == ret,                                                \
549   "could not change state to " #state);
550
551 #define GST_CHECK_MAIN(name)                                    \
552 int main (int argc, char **argv)                                \
553 {                                                               \
554   Suite *s;                                                     \
555   gst_check_init (&argc, &argv);                                \
556   s = name ## _suite ();                                        \
557   return gst_check_run_suite (s, # name, __FILE__);             \
558 }
559
560 /* Hack to allow run-time selection of unit tests to run via the
561  * GST_CHECKS environment variable (test function names, comma-separated) */
562
563 gboolean _gst_check_run_test_func (const gchar * func_name);
564
565 static inline void
566 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
567     int allowed_exit_value, int start, int end)
568 {
569   if (_gst_check_run_test_func (fname)) {
570     _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
571   }
572 }
573
574 #define _tcase_add_test __gst_tcase_add_test
575
576 /* add define to skip broken tests */
577 #define tcase_skip_broken_test(chain,test_func) \
578 G_STMT_START {                                                  \
579   const char *env = g_getenv ("GST_CHECKS");                    \
580                                                                 \
581   if (env != NULL && strstr (env, G_STRINGIFY (test_func))) {   \
582     tcase_add_test(chain,test_func);                            \
583   } else {                                                      \
584     g_printerr ("FIXME: skipping test %s because it's broken\n", G_STRINGIFY (test_func)); \
585   } \
586 } G_STMT_END
587
588 G_END_DECLS
589
590 #endif /* __GST_CHECK_H__ */