check: use private copy of check for libgstcheck
[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 #define __CHECK_VERSION_LATER_THAN(major,minor,micro) \
42     (CHECK_MAJOR_VERSION > major || \
43      (CHECK_MAJOR_VERSION == (major) && CHECK_MINOR_VERSION > (minor)) || \
44      (CHECK_MAJOR_VERSION == (major) && CHECK_MINOR_VERSION == (minor) && \
45       CHECK_MICRO_VERSION > (micro)))
46
47 /* logging function for tests
48  * a test uses g_message() to log a debug line
49  * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
50  * messages
51  */
52 extern gboolean _gst_check_threads_running;
53 extern gboolean _gst_check_raised_critical;
54 extern gboolean _gst_check_raised_warning;
55 extern gboolean _gst_check_expecting_log;
56
57 /* global variables used in test methods */
58 extern GList * buffers;
59
60 extern GMutex *check_mutex;
61 extern GCond *check_cond;
62
63 typedef struct
64 {
65   char *name;
66   int size;
67   int abi_size;
68 }
69 GstCheckABIStruct;
70
71 void gst_check_init (int *argc, char **argv[]);
72
73 GstFlowReturn gst_check_chain_func (GstPad * pad, GstBuffer * buffer);
74
75 void gst_check_message_error (GstMessage * message, GstMessageType type,
76     GQuark domain, gint code);
77
78 GstElement *gst_check_setup_element (const gchar * factory);
79 void gst_check_teardown_element (GstElement * element);
80 GstPad *gst_check_setup_src_pad (GstElement * element,
81     GstStaticPadTemplate * template, GstCaps * caps);
82 GstPad * gst_check_setup_src_pad_by_name (GstElement * element,
83           GstStaticPadTemplate * template, gchar *name);
84 GstPad * gst_check_setup_sink_pad_by_name (GstElement * element, 
85           GstStaticPadTemplate * template, gchar *name);
86 void gst_check_teardown_pad_by_name (GstElement * element, gchar *name);
87 void gst_check_teardown_src_pad (GstElement * element);
88 void gst_check_drop_buffers ();
89 void gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2);
90 void gst_check_element_push_buffer_list (const gchar * element_name,
91     GList * buffer_in, GList * buffer_out, GstFlowReturn last_flow_return);
92 void gst_check_element_push_buffer (const gchar * element_name,
93     GstBuffer * buffer_in, GstBuffer * buffer_out);
94 GstPad *gst_check_setup_sink_pad (GstElement * element,
95     GstStaticPadTemplate * template, GstCaps * caps);
96 void gst_check_teardown_sink_pad (GstElement * element);
97 void gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes);
98 gint gst_check_run_suite (Suite * suite, const gchar * name,
99     const gchar * fname);
100
101 #define fail_unless_message_error(msg, domain, code)            \
102 gst_check_message_error (msg, GST_MESSAGE_ERROR,                \
103   GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
104 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
105
106 /**
107  * GST_START_TEST:
108  * @__testname: test function name
109  *
110  * wrapper for checks START_TEST
111  */
112 /**
113  * GST_END_TEST:
114  *
115  * wrapper for checks END_TEST
116  */
117 #if __CHECK_VERSION_LATER_THAN(0,9,3)
118 #define GST_START_TEST(__testname) \
119 static void __testname (int __i__)\
120 {\
121   GST_DEBUG ("test start"); \
122   tcase_fn_start (""# __testname, __FILE__, __LINE__);
123
124 #define GST_END_TEST END_TEST
125 #else
126 #define GST_START_TEST(__testname) \
127 static void __testname ()\
128 {\
129   GST_DEBUG ("test start"); \
130   tcase_fn_start (""# __testname, __FILE__, __LINE__);
131
132 #define GST_END_TEST END_TEST
133 #endif
134
135
136 /* additional fail macros */
137 /**
138  * fail_unless_equals_int:
139  * @a: a #gint value or expression
140  * @b: a #gint value or expression
141  *
142  * This macro checks that @a and @b are equal and aborts if this is not the
143  * case, printing both expressions and the values they evaluated to. This
144  * macro is for use in unit tests.
145  */
146 #define fail_unless_equals_int(a, b)                                    \
147 G_STMT_START {                                                          \
148   int first = a;                                                        \
149   int second = b;                                                       \
150   fail_unless(first == second,                                          \
151     "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second);       \
152 } G_STMT_END;
153 /**
154  * assert_equals_int:
155  * @a: a #gint value or expression
156  * @b: a #gint value or expression
157  *
158  * This macro checks that @a and @b are equal and aborts if this is not the
159  * case, printing both expressions and the values they evaluated to. This
160  * macro is for use in unit tests.
161  */
162 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
163
164 /**
165  * fail_unless_equals_uint64:
166  * @a: a #guint64 value or expression
167  * @b: a #guint64 value or expression
168  *
169  * This macro checks that @a and @b are equal and aborts if this is not the
170  * case, printing both expressions and the values they evaluated to. This
171  * macro is for use in unit tests.
172  */
173 #define fail_unless_equals_uint64(a, b)                                 \
174 G_STMT_START {                                                          \
175   guint64 first = a;                                                    \
176   guint64 second = b;                                                   \
177   fail_unless(first == second,                                          \
178     "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%"       \
179     G_GUINT64_FORMAT ")", first, second);                               \
180 } G_STMT_END;
181 /**
182  * assert_equals_uint64:
183  * @a: a #guint64 value or expression
184  * @b: a #guint64 value or expression
185  *
186  * This macro checks that @a and @b are equal and aborts if this is not the
187  * case, printing both expressions and the values they evaluated to. This
188  * macro is for use in unit tests.
189  */
190 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
191
192 /**
193  * fail_unless_equals_string:
194  * @a: a string literal or expression
195  * @b: a string literal or expression
196  *
197  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
198  * this is not the case, printing both expressions and the values they
199  * evaluated to. This macro is for use in unit tests.
200  */
201 #define fail_unless_equals_string(a, b)                             \
202 G_STMT_START {                                                      \
203   const gchar * first = a;                                          \
204   const gchar * second = b;                                         \
205   fail_unless(strcmp (first, second) == 0,                          \
206     "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second);   \
207 } G_STMT_END;
208 /**
209  * assert_equals_string:
210  * @a: a string literal or expression
211  * @b: a string literal or expression
212  *
213  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
214  * this is not the case, printing both expressions and the values they
215  * evaluated to. This macro is for use in unit tests.
216  */
217 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
218
219 /**
220  * fail_unless_equals_float:
221  * @a: a #gdouble or #gfloat value or expression
222  * @b: a #gdouble or #gfloat value or expression
223  *
224  * This macro checks that @a and @b are (almost) equal and aborts if this
225  * is not the case, printing both expressions and the values they evaluated
226  * to. This macro is for use in unit tests.
227  *
228  * Since: 0.10.14
229  */
230 #define fail_unless_equals_float(a, b)                            \
231 G_STMT_START {                                                    \
232   double first = a;                                               \
233   double second = b;                                              \
234   /* This will only work for 'normal' values and values around 0, \
235    * which should be good enough for our purposes here */         \
236   fail_unless(fabs (first - second) < 0.0000001,                  \
237     "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
238 } G_STMT_END;
239
240 /**
241  * assert_equals_float:
242  * @a: a #gdouble or #gfloat value or expression
243  * @b: a #gdouble or #gfloat value or expression
244  *
245  * This macro checks that @a and @b are (almost) equal and aborts if this
246  * is not the case, printing both expressions and the values they evaluated
247  * to. This macro is for use in unit tests.
248  *
249  * Since: 0.10.14
250  */
251 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
252
253
254 /***
255  * thread test macros and variables
256  */
257 extern GList *thread_list;
258 extern GMutex *mutex;
259 extern GCond *start_cond;       /* used to notify main thread of thread startups */
260 extern GCond *sync_cond;        /* used to synchronize all threads and main thread */
261
262 #define MAIN_START_THREADS(count, function, data)               \
263 MAIN_INIT();                                                    \
264 MAIN_START_THREAD_FUNCTIONS(count, function, data);             \
265 MAIN_SYNCHRONIZE();
266
267 #define MAIN_INIT()                     \
268 G_STMT_START {                          \
269   _gst_check_threads_running = TRUE;    \
270                                         \
271   if (mutex == NULL) {                  \
272     mutex = g_mutex_new ();             \
273     start_cond = g_cond_new ();         \
274     sync_cond = g_cond_new ();          \
275   }                                     \
276 } G_STMT_END;
277
278 #define MAIN_START_THREAD_FUNCTIONS(count, function, data)      \
279 G_STMT_START {                                                  \
280   int i;                                                        \
281   for (i = 0; i < count; ++i) {                                 \
282     MAIN_START_THREAD_FUNCTION (i, function, data);             \
283   }                                                             \
284 } G_STMT_END;
285
286 #define MAIN_START_THREAD_FUNCTION(i, function, data)           \
287 G_STMT_START {                                                  \
288     GThread *thread = NULL;                                     \
289     GST_DEBUG ("MAIN: creating thread %d", i);                  \
290     g_mutex_lock (mutex);                                       \
291     thread = g_thread_create ((GThreadFunc) function, data,     \
292         TRUE, NULL);                                            \
293     /* wait for thread to signal us that it's ready */          \
294     GST_DEBUG ("MAIN: waiting for thread %d", i);               \
295     g_cond_wait (start_cond, mutex);                            \
296     g_mutex_unlock (mutex);                                     \
297                                                                 \
298     thread_list = g_list_append (thread_list, thread);          \
299 } G_STMT_END;
300
301
302 #define MAIN_SYNCHRONIZE()              \
303 G_STMT_START {                          \
304   GST_DEBUG ("MAIN: synchronizing");    \
305   g_cond_broadcast (sync_cond);         \
306   GST_DEBUG ("MAIN: synchronized");     \
307 } G_STMT_END;
308
309 #define MAIN_STOP_THREADS()                                     \
310 G_STMT_START {                                                  \
311   _gst_check_threads_running = FALSE;                           \
312                                                                 \
313   /* join all threads */                                        \
314   GST_DEBUG ("MAIN: joining");                                  \
315   g_list_foreach (thread_list, (GFunc) g_thread_join, NULL);    \
316   g_list_free (thread_list);                                    \
317   thread_list = NULL;                                           \
318   GST_DEBUG ("MAIN: joined");                                   \
319 } G_STMT_END;
320
321 #define THREAD_START()                                          \
322 THREAD_STARTED();                                               \
323 THREAD_SYNCHRONIZE();
324
325 #define THREAD_STARTED()                                        \
326 G_STMT_START {                                                  \
327   /* signal main thread that we started */                      \
328   GST_DEBUG ("THREAD %p: started", g_thread_self ());           \
329   g_mutex_lock (mutex);                                         \
330   g_cond_signal (start_cond);                                   \
331 } G_STMT_END;
332
333 #define THREAD_SYNCHRONIZE()                                    \
334 G_STMT_START {                                                  \
335   /* synchronize everyone */                                    \
336   GST_DEBUG ("THREAD %p: syncing", g_thread_self ());           \
337   g_cond_wait (sync_cond, mutex);                               \
338   GST_DEBUG ("THREAD %p: synced", g_thread_self ());            \
339   g_mutex_unlock (mutex);                                       \
340 } G_STMT_END;
341
342 #define THREAD_SWITCH()                                         \
343 G_STMT_START {                                                  \
344   /* a minimal sleep is a context switch */                     \
345   g_usleep (1);                                                 \
346 } G_STMT_END;
347
348 #define THREAD_TEST_RUNNING()   (_gst_check_threads_running == TRUE)
349
350 /* additional assertions */
351 #define ASSERT_CRITICAL(code)                                   \
352 G_STMT_START {                                                  \
353   _gst_check_expecting_log = TRUE;                              \
354   _gst_check_raised_critical = FALSE;                           \
355   code;                                                         \
356   _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
357                 "Expected g_critical, got nothing", NULL);      \
358   _gst_check_expecting_log = FALSE;                             \
359 } G_STMT_END
360
361 #define ASSERT_WARNING(code)                                    \
362 G_STMT_START {                                                  \
363   _gst_check_expecting_log = TRUE;                              \
364   _gst_check_raised_warning = FALSE;                            \
365   code;                                                         \
366   _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__,  \
367                 "Expected g_warning, got nothing", NULL);       \
368   _gst_check_expecting_log = FALSE;                             \
369 } G_STMT_END
370
371
372 #define ASSERT_OBJECT_REFCOUNT(object, name, value)             \
373 G_STMT_START {                                                  \
374   int rc;                                                       \
375   rc = GST_OBJECT_REFCOUNT_VALUE (object);                      \
376   fail_unless (rc == value,                                     \
377       "%s (%p) refcount is %d instead of %d",                   \
378       name, object, rc, value);                                 \
379 } G_STMT_END
380
381 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper)      \
382 G_STMT_START {                                                          \
383   int rc = GST_OBJECT_REFCOUNT_VALUE (object);                          \
384   int lo = lower;                                                       \
385   int hi = upper;                                                       \
386                                                                         \
387   fail_unless (rc >= lo,                                                \
388       "%s (%p) refcount %d is smaller than %d",                         \
389       name, object, rc, lo);                                            \
390   fail_unless (rc <= hi,                                                \
391       "%s (%p) refcount %d is bigger than %d",                          \
392       name, object, rc, hi);                                            \
393 } G_STMT_END
394
395
396 #define ASSERT_CAPS_REFCOUNT(caps, name, value)                 \
397         ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
398
399 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value)             \
400         ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
401
402 #define ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)          \
403 G_STMT_START {                                                  \
404   int rc;                                                       \
405   rc = GST_MINI_OBJECT_REFCOUNT_VALUE (caps);                   \
406   fail_unless (rc == value,                                     \
407                name " refcount is %d instead of %d", rc, value);\
408 } G_STMT_END
409
410 #define ASSERT_SET_STATE(element, state, ret)                   \
411 fail_unless (gst_element_set_state (element,                    \
412   state) == ret,                                                \
413   "could not change state to " #state);
414
415 #define GST_CHECK_MAIN(name)                                    \
416 int main (int argc, char **argv)                                \
417 {                                                               \
418   Suite *s;                                                     \
419   gst_check_init (&argc, &argv);                                \
420   s = name ## _suite ();                                        \
421   return gst_check_run_suite (s, # name, __FILE__);             \
422 }
423
424 /* Hack to allow run-time selection of unit tests to run via the
425  * GST_CHECKS environment variable (test function names, comma-separated) */
426
427 gboolean _gst_check_run_test_func (const gchar * func_name);
428
429 #if __CHECK_VERSION_LATER_THAN(0,9,6)
430 static inline void
431 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
432     int allowed_exit_value, int start, int end)
433 {
434   if (_gst_check_run_test_func (fname)) {
435     _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
436   }
437 }
438 #elif __CHECK_VERSION_LATER_THAN(0,9,3)
439 static inline void
440 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
441     int start, int end)
442 {
443   if (_gst_check_run_test_func (fname)) {
444     _tcase_add_test (tc, tf, fname, signal, start, end);
445   }
446 }
447 #else
448 static inline void
449 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal)
450 {
451   if (_gst_check_run_test_func (fname)) {
452     _tcase_add_test (tc, tf, fname, signal);
453   }
454 }
455 #endif
456
457 #define _tcase_add_test __gst_tcase_add_test
458
459 #undef __CHECK_VERSION_LATER_THAN
460
461 G_END_DECLS
462
463 #endif /* __GST_CHECK_H__ */