libs: Use foo/foo.h as single-include header consistently everywhere
[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
98 #define fail_unless_message_error(msg, domain, code)            \
99 gst_check_message_error (msg, GST_MESSAGE_ERROR,                \
100   GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
101 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
102
103 /**
104  * GST_START_TEST:
105  * @__testname: test function name
106  *
107  * wrapper for checks START_TEST
108  */
109 /**
110  * GST_END_TEST:
111  *
112  * wrapper for checks END_TEST
113  */
114 #define GST_START_TEST(__testname) \
115 static void __testname (int __i__)\
116 {\
117   GST_DEBUG ("test start"); \
118   tcase_fn_start (""# __testname, __FILE__, __LINE__);
119
120 #define GST_END_TEST GST_LOG ("cleaning up tasks"); \
121                      gst_task_cleanup_all (); \
122                      END_TEST
123
124 /* additional fail macros */
125 /**
126  * fail_unless_equals_int:
127  * @a: a #gint value or expression
128  * @b: a #gint value or expression
129  *
130  * This macro checks that @a and @b are equal and aborts if this is not the
131  * case, printing both expressions and the values they evaluated to. This
132  * macro is for use in unit tests.
133  */
134 #define fail_unless_equals_int(a, b)                                    \
135 G_STMT_START {                                                          \
136   int first = a;                                                        \
137   int second = b;                                                       \
138   fail_unless(first == second,                                          \
139     "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second);       \
140 } G_STMT_END;
141 /**
142  * assert_equals_int:
143  * @a: a #gint value or expression
144  * @b: a #gint value or expression
145  *
146  * This macro checks that @a and @b are equal and aborts if this is not the
147  * case, printing both expressions and the values they evaluated to. This
148  * macro is for use in unit tests.
149  */
150 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
151
152 /**
153  * fail_unless_equals_int64:
154  * @a: a #gint64 value or expression
155  * @b: a #gint64 value or expression
156  *
157  * This macro checks that @a and @b are equal and aborts if this is not the
158  * case, printing both expressions and the values they evaluated to. This
159  * macro is for use in unit tests.
160  */
161 #define fail_unless_equals_int64(a, b)                                  \
162 G_STMT_START {                                                          \
163   gint64 first = a;                                                     \
164   gint64 second = b;                                                    \
165   fail_unless(first == second,                                          \
166     "'" #a "' (%" G_GINT64_FORMAT") is not equal to '" #b"' (%"         \
167     G_GINT64_FORMAT")", first, second);                                 \
168 } G_STMT_END;
169 /**
170  * assert_equals_int64:
171  * @a: a #gint64 value or expression
172  * @b: a #gint64 value or expression
173  *
174  * This macro checks that @a and @b are equal and aborts if this is not the
175  * case, printing both expressions and the values they evaluated to. This
176  * macro is for use in unit tests.
177  */
178 #define assert_equals_int64(a, b) fail_unless_equals_int64(a, b)
179
180 /**
181  * fail_unless_equals_uint64:
182  * @a: a #guint64 value or expression
183  * @b: a #guint64 value or expression
184  *
185  * This macro checks that @a and @b are equal and aborts if this is not the
186  * case, printing both expressions and the values they evaluated to. This
187  * macro is for use in unit tests.
188  */
189 #define fail_unless_equals_uint64(a, b)                                 \
190 G_STMT_START {                                                          \
191   guint64 first = a;                                                    \
192   guint64 second = b;                                                   \
193   fail_unless(first == second,                                          \
194     "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%"       \
195     G_GUINT64_FORMAT ")", first, second);                               \
196 } G_STMT_END;
197 /**
198  * assert_equals_uint64:
199  * @a: a #guint64 value or expression
200  * @b: a #guint64 value or expression
201  *
202  * This macro checks that @a and @b are equal and aborts if this is not the
203  * case, printing both expressions and the values they evaluated to. This
204  * macro is for use in unit tests.
205  */
206 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
207
208 /**
209  * fail_unless_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 fail_unless_equals_string(a, b)                             \
218 G_STMT_START {                                                      \
219   const gchar * first = a;                                          \
220   const gchar * second = b;                                         \
221   fail_unless(g_strcmp0 (first, second) == 0,                          \
222     "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second);   \
223 } G_STMT_END;
224 /**
225  * assert_equals_string:
226  * @a: a string literal or expression
227  * @b: a string literal or expression
228  *
229  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
230  * this is not the case, printing both expressions and the values they
231  * evaluated to. This macro is for use in unit tests.
232  */
233 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
234
235 /**
236  * fail_unless_equals_float:
237  * @a: a #gdouble or #gfloat value or expression
238  * @b: a #gdouble or #gfloat value or expression
239  *
240  * This macro checks that @a and @b are (almost) equal and aborts if this
241  * is not the case, printing both expressions and the values they evaluated
242  * to. This macro is for use in unit tests.
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 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
264
265
266 /***
267  * thread test macros and variables
268  */
269 extern GList *thread_list;
270 extern GMutex mutex;
271 extern GCond start_cond;       /* used to notify main thread of thread startups */
272 extern GCond sync_cond;        /* used to synchronize all threads and main thread */
273
274 #define MAIN_START_THREADS(count, function, data)               \
275 MAIN_INIT();                                                    \
276 MAIN_START_THREAD_FUNCTIONS(count, function, data);             \
277 MAIN_SYNCHRONIZE();
278
279 #define MAIN_INIT()                     \
280 G_STMT_START {                          \
281   _gst_check_threads_running = TRUE;    \
282 } G_STMT_END;
283
284 #define MAIN_START_THREAD_FUNCTIONS(count, function, data)      \
285 G_STMT_START {                                                  \
286   int i;                                                        \
287   for (i = 0; i < count; ++i) {                                 \
288     MAIN_START_THREAD_FUNCTION (i, function, data);             \
289   }                                                             \
290 } G_STMT_END;
291
292 #define MAIN_START_THREAD_FUNCTION(i, function, data)           \
293 G_STMT_START {                                                  \
294     GThread *thread = NULL;                                     \
295     GST_DEBUG ("MAIN: creating thread %d", i);                  \
296     g_mutex_lock (&mutex);                                      \
297     thread = g_thread_try_new ("gst-check",                     \
298         (GThreadFunc) function, data, NULL);                    \
299     /* wait for thread to signal us that it's ready */          \
300     GST_DEBUG ("MAIN: waiting for thread %d", i);               \
301     g_cond_wait (&start_cond, &mutex);                          \
302     g_mutex_unlock (&mutex);                                    \
303                                                                 \
304     thread_list = g_list_append (thread_list, thread);          \
305 } G_STMT_END;
306
307
308 #define MAIN_SYNCHRONIZE()              \
309 G_STMT_START {                          \
310   GST_DEBUG ("MAIN: synchronizing");    \
311   g_cond_broadcast (&sync_cond);        \
312   GST_DEBUG ("MAIN: synchronized");     \
313 } G_STMT_END;
314
315 #define MAIN_STOP_THREADS()                                     \
316 G_STMT_START {                                                  \
317   _gst_check_threads_running = FALSE;                           \
318                                                                 \
319   /* join all threads */                                        \
320   GST_DEBUG ("MAIN: joining");                                  \
321   g_list_foreach (thread_list, (GFunc) g_thread_join, NULL);    \
322   g_list_free (thread_list);                                    \
323   thread_list = NULL;                                           \
324   GST_DEBUG ("MAIN: joined");                                   \
325 } G_STMT_END;
326
327 #define THREAD_START()                                          \
328 THREAD_STARTED();                                               \
329 THREAD_SYNCHRONIZE();
330
331 #define THREAD_STARTED()                                        \
332 G_STMT_START {                                                  \
333   /* signal main thread that we started */                      \
334   GST_DEBUG ("THREAD %p: started", g_thread_self ());           \
335   g_mutex_lock (&mutex);                                        \
336   g_cond_signal (&start_cond);                                  \
337 } G_STMT_END;
338
339 #define THREAD_SYNCHRONIZE()                                    \
340 G_STMT_START {                                                  \
341   /* synchronize everyone */                                    \
342   GST_DEBUG ("THREAD %p: syncing", g_thread_self ());           \
343   g_cond_wait (&sync_cond, &mutex);                             \
344   GST_DEBUG ("THREAD %p: synced", g_thread_self ());            \
345   g_mutex_unlock (&mutex);                                      \
346 } G_STMT_END;
347
348 #define THREAD_SWITCH()                                         \
349 G_STMT_START {                                                  \
350   /* a minimal sleep is a context switch */                     \
351   g_usleep (1);                                                 \
352 } G_STMT_END;
353
354 #define THREAD_TEST_RUNNING()   (_gst_check_threads_running == TRUE)
355
356 /* additional assertions */
357 #define ASSERT_CRITICAL(code)                                   \
358 G_STMT_START {                                                  \
359   _gst_check_expecting_log = TRUE;                              \
360   _gst_check_raised_critical = FALSE;                           \
361   code;                                                         \
362   _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
363                 "Expected g_critical, got nothing", NULL);      \
364   _gst_check_expecting_log = FALSE;                             \
365 } G_STMT_END
366
367 #define ASSERT_WARNING(code)                                    \
368 G_STMT_START {                                                  \
369   _gst_check_expecting_log = TRUE;                              \
370   _gst_check_raised_warning = FALSE;                            \
371   code;                                                         \
372   _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__,  \
373                 "Expected g_warning, got nothing", NULL);       \
374   _gst_check_expecting_log = FALSE;                             \
375 } G_STMT_END
376
377
378 #define ASSERT_OBJECT_REFCOUNT(object, name, value)             \
379 G_STMT_START {                                                  \
380   int rc;                                                       \
381   rc = GST_OBJECT_REFCOUNT_VALUE (object);                      \
382   fail_unless (rc == value,                                     \
383       "%s (%p) refcount is %d instead of %d",                   \
384       name, object, rc, value);                                 \
385 } G_STMT_END
386
387 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper)      \
388 G_STMT_START {                                                          \
389   int rc = GST_OBJECT_REFCOUNT_VALUE (object);                          \
390   int lo = lower;                                                       \
391   int hi = upper;                                                       \
392                                                                         \
393   fail_unless (rc >= lo,                                                \
394       "%s (%p) refcount %d is smaller than %d",                         \
395       name, object, rc, lo);                                            \
396   fail_unless (rc <= hi,                                                \
397       "%s (%p) refcount %d is bigger than %d",                          \
398       name, object, rc, hi);                                            \
399 } G_STMT_END
400
401
402 #define ASSERT_CAPS_REFCOUNT(caps, name, value)                 \
403         ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
404
405 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value)             \
406         ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
407
408 #define ASSERT_MINI_OBJECT_REFCOUNT(miniobj, name, value)       \
409 G_STMT_START {                                                  \
410   int rc;                                                       \
411   rc = GST_MINI_OBJECT_REFCOUNT_VALUE (miniobj);                \
412   fail_unless (rc == value,                                     \
413                name " (%p) refcount is %d instead of %d", miniobj, rc, value); \
414 } G_STMT_END
415
416 #define ASSERT_SET_STATE(element, state, ret)                   \
417 fail_unless (gst_element_set_state (element,                    \
418   state) == ret,                                                \
419   "could not change state to " #state);
420
421 #define GST_CHECK_MAIN(name)                                    \
422 int main (int argc, char **argv)                                \
423 {                                                               \
424   Suite *s;                                                     \
425   gst_check_init (&argc, &argv);                                \
426   s = name ## _suite ();                                        \
427   return gst_check_run_suite (s, # name, __FILE__);             \
428 }
429
430 /* Hack to allow run-time selection of unit tests to run via the
431  * GST_CHECKS environment variable (test function names, comma-separated) */
432
433 gboolean _gst_check_run_test_func (const gchar * func_name);
434
435 static inline void
436 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
437     int allowed_exit_value, int start, int end)
438 {
439   if (_gst_check_run_test_func (fname)) {
440     _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
441   }
442 }
443
444 #define _tcase_add_test __gst_tcase_add_test
445
446 /* add define to skip broken tests */
447 #define tcase_skip_broken_test(chain,test_func) \
448   if (0) { tcase_add_test(chain,test_func); } else { \
449     g_printerr ("FIXME: skipping test %s because it's broken\n", G_STRINGIFY (test_func)); \
450   }
451
452 G_END_DECLS
453
454 #endif /* __GST_CHECK_H__ */