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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, 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 #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)))
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
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;
57 /* global variables used in test methods */
58 extern GList * buffers;
60 extern GMutex *check_mutex;
61 extern GCond *check_cond;
71 void gst_check_init (int *argc, char **argv[]);
73 GstFlowReturn gst_check_chain_func (GstPad * pad, GstBuffer * buffer);
75 void gst_check_message_error (GstMessage * message, GstMessageType type,
76 GQuark domain, gint code);
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,
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)
108 * @__testname: test function name
110 * wrapper for checks START_TEST
115 * wrapper for checks END_TEST
117 #if __CHECK_VERSION_LATER_THAN(0,9,3)
118 #define GST_START_TEST(__testname) \
119 static void __testname (int __i__)\
121 GST_DEBUG ("test start"); \
122 tcase_fn_start (""# __testname, __FILE__, __LINE__);
124 #define GST_END_TEST END_TEST
126 #define GST_START_TEST(__testname) \
127 static void __testname ()\
129 GST_DEBUG ("test start"); \
130 tcase_fn_start (""# __testname, __FILE__, __LINE__);
132 #define GST_END_TEST END_TEST
136 /* additional fail macros */
138 * fail_unless_equals_int:
139 * @a: a #gint value or expression
140 * @b: a #gint value or expression
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.
146 #define fail_unless_equals_int(a, b) \
150 fail_unless(first == second, \
151 "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second); \
155 * @a: a #gint value or expression
156 * @b: a #gint value or expression
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.
162 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
165 * fail_unless_equals_uint64:
166 * @a: a #guint64 value or expression
167 * @b: a #guint64 value or expression
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.
173 #define fail_unless_equals_uint64(a, b) \
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); \
182 * assert_equals_uint64:
183 * @a: a #guint64 value or expression
184 * @b: a #guint64 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. This
188 * macro is for use in unit tests.
190 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
193 * fail_unless_equals_string:
194 * @a: a string literal or expression
195 * @b: a string literal or expression
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.
201 #define fail_unless_equals_string(a, b) \
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); \
209 * assert_equals_string:
210 * @a: a string literal or expression
211 * @b: a string literal or expression
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.
217 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
220 * fail_unless_equals_float:
221 * @a: a #gdouble or #gfloat value or expression
222 * @b: a #gdouble or #gfloat value or expression
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.
230 #define fail_unless_equals_float(a, 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);\
241 * assert_equals_float:
242 * @a: a #gdouble or #gfloat value or expression
243 * @b: a #gdouble or #gfloat value or expression
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.
251 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
255 * thread test macros and variables
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 */
262 #define MAIN_START_THREADS(count, function, data) \
264 MAIN_START_THREAD_FUNCTIONS(count, function, data); \
267 #define MAIN_INIT() \
269 _gst_check_threads_running = TRUE; \
271 if (mutex == NULL) { \
272 mutex = g_mutex_new (); \
273 start_cond = g_cond_new (); \
274 sync_cond = g_cond_new (); \
278 #define MAIN_START_THREAD_FUNCTIONS(count, function, data) \
281 for (i = 0; i < count; ++i) { \
282 MAIN_START_THREAD_FUNCTION (i, function, data); \
286 #define MAIN_START_THREAD_FUNCTION(i, function, data) \
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, \
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); \
298 thread_list = g_list_append (thread_list, thread); \
302 #define MAIN_SYNCHRONIZE() \
304 GST_DEBUG ("MAIN: synchronizing"); \
305 g_cond_broadcast (sync_cond); \
306 GST_DEBUG ("MAIN: synchronized"); \
309 #define MAIN_STOP_THREADS() \
311 _gst_check_threads_running = FALSE; \
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"); \
321 #define THREAD_START() \
323 THREAD_SYNCHRONIZE();
325 #define THREAD_STARTED() \
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); \
333 #define THREAD_SYNCHRONIZE() \
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); \
342 #define THREAD_SWITCH() \
344 /* a minimal sleep is a context switch */ \
348 #define THREAD_TEST_RUNNING() (_gst_check_threads_running == TRUE)
350 /* additional assertions */
351 #define ASSERT_CRITICAL(code) \
353 _gst_check_expecting_log = TRUE; \
354 _gst_check_raised_critical = FALSE; \
356 _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
357 "Expected g_critical, got nothing", NULL); \
358 _gst_check_expecting_log = FALSE; \
361 #define ASSERT_WARNING(code) \
363 _gst_check_expecting_log = TRUE; \
364 _gst_check_raised_warning = FALSE; \
366 _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__, \
367 "Expected g_warning, got nothing", NULL); \
368 _gst_check_expecting_log = FALSE; \
372 #define ASSERT_OBJECT_REFCOUNT(object, name, value) \
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); \
381 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper) \
383 int rc = GST_OBJECT_REFCOUNT_VALUE (object); \
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); \
396 #define ASSERT_CAPS_REFCOUNT(caps, name, value) \
397 ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
399 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value) \
400 ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
402 #define ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value) \
405 rc = GST_MINI_OBJECT_REFCOUNT_VALUE (caps); \
406 fail_unless (rc == value, \
407 name " refcount is %d instead of %d", rc, value);\
410 #define ASSERT_SET_STATE(element, state, ret) \
411 fail_unless (gst_element_set_state (element, \
413 "could not change state to " #state);
415 #define GST_CHECK_MAIN(name) \
416 int main (int argc, char **argv) \
419 gst_check_init (&argc, &argv); \
420 s = name ## _suite (); \
421 return gst_check_run_suite (s, # name, __FILE__); \
424 /* Hack to allow run-time selection of unit tests to run via the
425 * GST_CHECKS environment variable (test function names, comma-separated) */
427 gboolean _gst_check_run_test_func (const gchar * func_name);
429 #if __CHECK_VERSION_LATER_THAN(0,9,6)
431 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
432 int allowed_exit_value, int start, int end)
434 if (_gst_check_run_test_func (fname)) {
435 _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
438 #elif __CHECK_VERSION_LATER_THAN(0,9,3)
440 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
443 if (_gst_check_run_test_func (fname)) {
444 _tcase_add_test (tc, tf, fname, signal, start, end);
449 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal)
451 if (_gst_check_run_test_func (fname)) {
452 _tcase_add_test (tc, tf, fname, signal);
457 #define _tcase_add_test __gst_tcase_add_test
459 #undef __CHECK_VERSION_LATER_THAN
463 #endif /* __GST_CHECK_H__ */