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