3 * Common code for GStreamer unittests
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6 * Copyright (C) <2008> Thijs Vermeir <thijsvermeir@gmail.com>
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.
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.
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.
24 #ifndef __GST_CHECK_H__
25 #define __GST_CHECK_H__
32 #include <gst/check/internal-check.h>
38 GST_DEBUG_CATEGORY_EXTERN (check_debug);
39 #define GST_CAT_DEFAULT check_debug
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
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;
51 /* global variables used in test methods */
52 extern GList * buffers;
54 extern GMutex check_mutex;
55 extern GCond check_cond;
65 void gst_check_init (int *argc, char **argv[]);
67 GstFlowReturn gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer);
69 void gst_check_message_error (GstMessage * message, GstMessageType type,
70 GQuark domain, gint code);
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_from_template (GstElement * element,
77 GstPadTemplate * tmpl);
78 GstPad * gst_check_setup_src_pad_by_name (GstElement * element,
79 GstStaticPadTemplate * tmpl, const gchar *name);
80 GstPad * gst_check_setup_src_pad_by_name_from_template (GstElement * element,
81 GstPadTemplate * tmpl, const gchar *name);
82 GstPad *gst_check_setup_sink_pad (GstElement * element,
83 GstStaticPadTemplate * tmpl);
84 GstPad *gst_check_setup_sink_pad_from_template (GstElement * element,
85 GstPadTemplate * tmpl);
86 GstPad * gst_check_setup_sink_pad_by_name (GstElement * element,
87 GstStaticPadTemplate * tmpl, const gchar *name);
88 GstPad * gst_check_setup_sink_pad_by_name_from_template (GstElement * element,
89 GstPadTemplate * tmpl, const gchar *name);
90 void gst_check_teardown_pad_by_name (GstElement * element, const gchar *name);
91 void gst_check_teardown_src_pad (GstElement * element);
92 void gst_check_drop_buffers (void);
93 void gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2);
94 void gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size);
95 void gst_check_element_push_buffer_list (const gchar * element_name,
96 GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
97 GstCaps * caps_out, GstFlowReturn last_flow_return);
98 void gst_check_element_push_buffer (const gchar * element_name,
99 GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
101 void gst_check_teardown_sink_pad (GstElement * element);
102 void gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes);
103 gint gst_check_run_suite (Suite * suite, const gchar * name,
104 const gchar * fname);
105 void gst_check_setup_events (GstPad * srcpad, GstElement * element,
106 GstCaps * caps, GstFormat format);
107 void gst_check_setup_events_with_stream_id (GstPad * srcpad,
108 GstElement * element, GstCaps * caps, GstFormat format,
109 const gchar * stream_id);
110 void gst_check_objects_destroyed_on_unref (gpointer object_to_unref, gpointer first_object, ...)
111 G_GNUC_NULL_TERMINATED;
112 void gst_check_object_destroyed_on_unref (gpointer object_to_unref);
114 #define fail_unless_message_error(msg, domain, code) \
115 gst_check_message_error (msg, GST_MESSAGE_ERROR, \
116 GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
117 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
119 #ifdef GST_CHECK_TEST_ENVIRONMENT_BEACON
120 #define GST_DO_CHECK_TEST_ENVIRONMENT \
122 if (g_getenv (GST_CHECK_TEST_ENVIRONMENT_BEACON) == NULL) \
123 fail ("Test environment not set up correctly! Expected environment " \
124 "variable '%s' to be set.", GST_CHECK_TEST_ENVIRONMENT_BEACON); \
128 #define GST_DO_CHECK_TEST_ENVIRONMENT /* nothing to check */
133 * @__testname: test function name
135 * wrapper for checks START_TEST
140 * wrapper for checks END_TEST
142 #define GST_START_TEST(__testname) \
143 static void __testname (int __i__)\
145 GST_DEBUG ("test start"); \
146 GST_DO_CHECK_TEST_ENVIRONMENT; \
147 tcase_fn_start (""# __testname, __FILE__, __LINE__);
149 #define GST_END_TEST GST_LOG ("cleaning up tasks"); \
150 gst_task_cleanup_all (); \
153 /* additional fail macros */
155 * fail_unless_equals_int:
156 * @a: a #gint value or expression
157 * @b: a #gint value or expression
159 * This macro checks that @a and @b are equal and aborts if this is not the
160 * case, printing both expressions and the values they evaluated to. This
161 * macro is for use in unit tests.
163 #define fail_unless_equals_int(a, b) \
167 fail_unless(first == second, \
168 "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second); \
172 * @a: a #gint value or expression
173 * @b: a #gint value or expression
175 * This macro checks that @a and @b are equal and aborts if this is not the
176 * case, printing both expressions and the values they evaluated to. This
177 * macro is for use in unit tests.
179 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
182 * fail_unless_equals_int_hex:
183 * @a: a #gint value or expression
184 * @b: a #gint value or expression
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 in
188 * hexadecimal format. This macro is for use in unit tests.
192 #define fail_unless_equals_int_hex(a, b) \
196 fail_unless(first == second, \
197 "'" #a "' (0x%08x) is not equal to '" #b"' (0x%08x)", first, second);\
201 * assert_equals_int_hex:
202 * @a: a #gint value or expression
203 * @b: a #gint value or expression
205 * This macro checks that @a and @b are equal and aborts if this is not the
206 * case, printing both expressions and the values they evaluated to in
207 * hexadecimal format. This macro is for use in unit tests.
211 #define assert_equals_int_hex(a, b) fail_unless_equals_int_hex(a, b)
214 * fail_unless_equals_int64:
215 * @a: a #gint64 value or expression
216 * @b: a #gint64 value or expression
218 * This macro checks that @a and @b are equal and aborts if this is not the
219 * case, printing both expressions and the values they evaluated to. This
220 * macro is for use in unit tests.
222 #define fail_unless_equals_int64(a, b) \
226 fail_unless(first == second, \
227 "'" #a "' (%" G_GINT64_FORMAT") is not equal to '" #b"' (%" \
228 G_GINT64_FORMAT")", first, second); \
231 * assert_equals_int64:
232 * @a: a #gint64 value or expression
233 * @b: a #gint64 value or expression
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. This
237 * macro is for use in unit tests.
239 #define assert_equals_int64(a, b) fail_unless_equals_int64(a, b)
242 * fail_unless_equals_int64_hex:
243 * @a: a #gint64 value or expression
244 * @b: a #gint64 value or expression
246 * This macro checks that @a and @b are equal and aborts if this is not the
247 * case, printing both expressions and the values they evaluated to in
248 * hexadecimal format. This macro is for use in unit tests.
252 #define fail_unless_equals_int64_hex(a, b) \
256 fail_unless(first == second, \
257 "'" #a "' (0x%016x) is not equal to '" #b"' (0x%016x)", first, second);\
260 * assert_equals_int64_hex:
261 * @a: a #gint64 value or expression
262 * @b: a #gint64 value or expression
264 * This macro checks that @a and @b are equal and aborts if this is not the
265 * case, printing both expressions and the values they evaluated to in
266 * hexadecimal format. This macro is for use in unit tests.
270 #define assert_equals_int64_hex(a,b) fail_unless_equals_int64_hex(a,b)
273 * fail_unless_equals_uint64:
274 * @a: a #guint64 value or expression
275 * @b: a #guint64 value or expression
277 * This macro checks that @a and @b are equal and aborts if this is not the
278 * case, printing both expressions and the values they evaluated to. This
279 * macro is for use in unit tests.
281 #define fail_unless_equals_uint64(a, b) \
284 guint64 second = b; \
285 fail_unless(first == second, \
286 "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%" \
287 G_GUINT64_FORMAT ")", first, second); \
290 * assert_equals_uint64:
291 * @a: a #guint64 value or expression
292 * @b: a #guint64 value or expression
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. This
296 * macro is for use in unit tests.
298 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
301 * fail_unless_equals_uint64_hex:
302 * @a: a #gint64 value or expression
303 * @b: a #gint64 value or expression
305 * This macro checks that @a and @b are equal and aborts if this is not the
306 * case, printing both expressions and the values they evaluated to in
307 * hexadecimal format. This macro is for use in unit tests.
311 #define fail_unless_equals_uint64_hex(a, b) \
314 guint64 second = b; \
315 fail_unless(first == second, \
316 "'" #a "' (0x%016x) is not equal to '" #b"' (0x%016x)", first, second);\
319 * assert_equals_uint64_hex:
320 * @a: a #guint64 value or expression
321 * @b: a #guint64 value or expression
323 * This macro checks that @a and @b are equal and aborts if this is not the
324 * case, printing both expressions and the values they evaluated to in
325 * hexadecimal format. This macro is for use in unit tests.
329 #define assert_equals_uint64_hex(a,b) fail_unless_equals_uint64_hex(a,b)
332 * fail_unless_equals_string:
333 * @a: a string literal or expression
334 * @b: a string literal or expression
336 * This macro checks that @a and @b are equal (as per strcmp) and aborts if
337 * this is not the case, printing both expressions and the values they
338 * evaluated to. This macro is for use in unit tests.
340 #define fail_unless_equals_string(a, b) \
342 const gchar * first = a; \
343 const gchar * second = b; \
344 fail_unless(g_strcmp0 (first, second) == 0, \
345 "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second); \
348 * assert_equals_string:
349 * @a: a string literal or expression
350 * @b: a string literal or expression
352 * This macro checks that @a and @b are equal (as per strcmp) and aborts if
353 * this is not the case, printing both expressions and the values they
354 * evaluated to. This macro is for use in unit tests.
356 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
359 * fail_unless_equals_float:
360 * @a: a #gdouble or #gfloat value or expression
361 * @b: a #gdouble or #gfloat value or expression
363 * This macro checks that @a and @b are (almost) equal and aborts if this
364 * is not the case, printing both expressions and the values they evaluated
365 * to. This macro is for use in unit tests.
367 #define fail_unless_equals_float(a, b) \
371 /* This will only work for 'normal' values and values around 0, \
372 * which should be good enough for our purposes here */ \
373 fail_unless(fabs (first - second) < 0.0000001, \
374 "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
378 * assert_equals_float:
379 * @a: a #gdouble or #gfloat value or expression
380 * @b: a #gdouble or #gfloat value or expression
382 * This macro checks that @a and @b are (almost) equal and aborts if this
383 * is not the case, printing both expressions and the values they evaluated
384 * to. This macro is for use in unit tests.
386 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
389 * fail_unless_equals_pointer:
390 * @a: a pointer value or expression
391 * @b: a pointer value or expression
393 * This macro checks that @a and @b are equal and aborts if this
394 * is not the case, printing both expressions and the values they
395 * evaluated to. This macro is for use in unit tests.
399 #define fail_unless_equals_pointer(a, b) \
401 gpointer first = a; \
402 gpointer second = b; \
403 fail_unless(first == second, \
404 "'" #a "' (%p) is not equal to '" #b "' (%p)", first, second);\
408 * assert_equals_pointer:
409 * @a: a pointer value or expression
410 * @b: a pointer value or expression
412 * This macro checks that @a and @b are equal and aborts if this
413 * is not the case, printing both expressions and the values they
414 * evaluated to. This macro is for use in unit tests.
418 #define assert_equals_pointer(a, b) fail_unless_equals_pointer(a, b)
421 * thread test macros and variables
423 extern GList *thread_list;
425 extern GCond start_cond; /* used to notify main thread of thread startups */
426 extern GCond sync_cond; /* used to synchronize all threads and main thread */
428 #define MAIN_START_THREADS(count, function, data) \
430 MAIN_START_THREAD_FUNCTIONS(count, function, data); \
433 #define MAIN_INIT() \
435 g_mutex_init (&mutex); \
436 g_cond_init (&start_cond); \
437 g_cond_init (&sync_cond); \
438 _gst_check_threads_running = TRUE; \
441 #define MAIN_START_THREAD_FUNCTIONS(count, function, data) \
444 for (i = 0; i < count; ++i) { \
445 MAIN_START_THREAD_FUNCTION (i, function, data); \
449 #define MAIN_START_THREAD_FUNCTION(i, function, data) \
451 GThread *thread = NULL; \
452 GST_DEBUG ("MAIN: creating thread %d", i); \
453 g_mutex_lock (&mutex); \
454 thread = g_thread_try_new ("gst-check", \
455 (GThreadFunc) function, data, NULL); \
456 /* wait for thread to signal us that it's ready */ \
457 GST_DEBUG ("MAIN: waiting for thread %d", i); \
458 g_cond_wait (&start_cond, &mutex); \
459 g_mutex_unlock (&mutex); \
461 thread_list = g_list_append (thread_list, thread); \
465 #define MAIN_SYNCHRONIZE() \
467 GST_DEBUG ("MAIN: synchronizing"); \
468 g_cond_broadcast (&sync_cond); \
469 GST_DEBUG ("MAIN: synchronized"); \
472 #define MAIN_STOP_THREADS() \
474 _gst_check_threads_running = FALSE; \
476 /* join all threads */ \
477 GST_DEBUG ("MAIN: joining"); \
478 g_list_foreach (thread_list, (GFunc) g_thread_join, NULL); \
479 g_list_free (thread_list); \
480 thread_list = NULL; \
481 g_mutex_clear (&mutex); \
482 g_cond_clear (&start_cond); \
483 g_cond_clear (&sync_cond); \
484 GST_DEBUG ("MAIN: joined"); \
487 #define THREAD_START() \
489 THREAD_SYNCHRONIZE();
491 #define THREAD_STARTED() \
493 /* signal main thread that we started */ \
494 GST_DEBUG ("THREAD %p: started", g_thread_self ()); \
495 g_mutex_lock (&mutex); \
496 g_cond_signal (&start_cond); \
499 #define THREAD_SYNCHRONIZE() \
501 /* synchronize everyone */ \
502 GST_DEBUG ("THREAD %p: syncing", g_thread_self ()); \
503 fail_if (g_mutex_trylock (&mutex), \
504 "bug in unit test, mutex should be locked at this point");\
505 g_cond_wait (&sync_cond, &mutex); \
506 GST_DEBUG ("THREAD %p: synced", g_thread_self ()); \
507 g_mutex_unlock (&mutex); \
510 #define THREAD_SWITCH() \
512 /* a minimal sleep is a context switch */ \
516 #define THREAD_TEST_RUNNING() (!!_gst_check_threads_running)
518 /* additional assertions */
519 #define ASSERT_CRITICAL(code) \
521 _gst_check_expecting_log = TRUE; \
522 _gst_check_raised_critical = FALSE; \
524 if (!_gst_check_raised_critical) \
525 _ck_assert_failed (__FILE__, __LINE__, \
526 "Expected g_critical, got nothing", NULL); \
527 _gst_check_expecting_log = FALSE; \
530 #define ASSERT_WARNING(code) \
532 _gst_check_expecting_log = TRUE; \
533 _gst_check_raised_warning = FALSE; \
535 if (!_gst_check_raised_warning) \
536 _ck_assert_failed (__FILE__, __LINE__, \
537 "Expected g_warning, got nothing", NULL); \
538 _gst_check_expecting_log = FALSE; \
542 #define ASSERT_OBJECT_REFCOUNT(object, name, value) \
545 rc = GST_OBJECT_REFCOUNT_VALUE (object); \
546 fail_unless (rc == value, \
547 "%s (%p) refcount is %d instead of %d", \
548 name, object, rc, value); \
551 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper) \
553 int rc = GST_OBJECT_REFCOUNT_VALUE (object); \
557 fail_unless (rc >= lo, \
558 "%s (%p) refcount %d is smaller than %d", \
559 name, object, rc, lo); \
560 fail_unless (rc <= hi, \
561 "%s (%p) refcount %d is bigger than %d", \
562 name, object, rc, hi); \
566 #define ASSERT_CAPS_REFCOUNT(caps, name, value) \
567 ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
569 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value) \
570 ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
572 #define ASSERT_MINI_OBJECT_REFCOUNT(miniobj, name, value) \
575 rc = GST_MINI_OBJECT_REFCOUNT_VALUE (miniobj); \
576 fail_unless (rc == value, \
577 name " (%p) refcount is %d instead of %d", miniobj, rc, value); \
580 #define ASSERT_SET_STATE(element, state, ret) \
581 fail_unless (gst_element_set_state (GST_ELEMENT(element), \
583 "could not change state to " #state);
585 #define GST_CHECK_MAIN(name) \
586 int main (int argc, char **argv) \
589 gst_check_init (&argc, &argv); \
590 s = name ## _suite (); \
591 return gst_check_run_suite (s, # name, __FILE__); \
594 /* Hack to allow run-time selection of unit tests to run via the
595 * GST_CHECKS environment variable (test function names globs, comma
596 * separated), or GST_CHECKS_IGNORE with the same semantics */
598 gboolean _gst_check_run_test_func (const gchar * func_name);
601 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
602 int allowed_exit_value, int start, int end)
604 if (_gst_check_run_test_func (fname)) {
605 _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
609 #define _tcase_add_test __gst_tcase_add_test
611 /* A special variant to add broken tests. These are normally skipped, but can be
612 * forced to run via GST_CHECKS */
613 #define tcase_skip_broken_test(chain,test_func) \
615 const char *env = g_getenv ("GST_CHECKS"); \
617 if (env != NULL && g_pattern_match_simple (env, G_STRINGIFY (test_func))) { \
618 tcase_add_test(chain,test_func); \
620 g_printerr ("FIXME: skipping test %s because it's broken\n", G_STRINGIFY (test_func)); \
626 #endif /* __GST_CHECK_H__ */