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>
36 #include <gst/check/gstbufferstraw.h>
37 #include <gst/check/gstconsistencychecker.h>
38 #include <gst/check/gsttestclock.h>
42 GST_DEBUG_CATEGORY_EXTERN (check_debug);
43 #define GST_CAT_DEFAULT check_debug
45 /* logging function for tests
46 * a test uses g_message() to log a debug line
47 * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
50 extern gboolean _gst_check_threads_running;
51 extern gboolean _gst_check_raised_critical;
52 extern gboolean _gst_check_raised_warning;
53 extern gboolean _gst_check_expecting_log;
55 /* global variables used in test methods */
56 extern GList * buffers;
58 extern GMutex check_mutex;
59 extern GCond check_cond;
69 void gst_check_init (int *argc, char **argv[]);
71 GstFlowReturn gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer);
73 void gst_check_message_error (GstMessage * message, GstMessageType type,
74 GQuark domain, gint code);
76 GstElement *gst_check_setup_element (const gchar * factory);
77 void gst_check_teardown_element (GstElement * element);
78 GstPad *gst_check_setup_src_pad (GstElement * element,
79 GstStaticPadTemplate * tmpl);
80 GstPad * gst_check_setup_src_pad_by_name (GstElement * element,
81 GstStaticPadTemplate * tmpl, const gchar *name);
82 GstPad * gst_check_setup_sink_pad_by_name (GstElement * element,
83 GstStaticPadTemplate * tmpl, const gchar *name);
84 void gst_check_teardown_pad_by_name (GstElement * element, const gchar *name);
85 void gst_check_teardown_src_pad (GstElement * element);
86 void gst_check_drop_buffers (void);
87 void gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2);
88 void gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size);
89 void gst_check_element_push_buffer_list (const gchar * element_name,
90 GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
91 GstCaps * caps_out, GstFlowReturn last_flow_return);
92 void gst_check_element_push_buffer (const gchar * element_name,
93 GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
95 GstPad *gst_check_setup_sink_pad (GstElement * element,
96 GstStaticPadTemplate * tmpl);
97 void gst_check_teardown_sink_pad (GstElement * element);
98 void gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes);
99 gint gst_check_run_suite (Suite * suite, const gchar * name,
100 const gchar * fname);
102 #define fail_unless_message_error(msg, domain, code) \
103 gst_check_message_error (msg, GST_MESSAGE_ERROR, \
104 GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
105 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
109 * @__testname: test function name
111 * wrapper for checks START_TEST
116 * wrapper for checks END_TEST
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 GST_LOG ("cleaning up tasks"); \
125 gst_task_cleanup_all (); \
128 /* additional fail macros */
130 * fail_unless_equals_int:
131 * @a: a #gint value or expression
132 * @b: a #gint value or expression
134 * This macro checks that @a and @b are equal and aborts if this is not the
135 * case, printing both expressions and the values they evaluated to. This
136 * macro is for use in unit tests.
138 #define fail_unless_equals_int(a, b) \
142 fail_unless(first == second, \
143 "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second); \
147 * @a: a #gint value or expression
148 * @b: a #gint value or expression
150 * This macro checks that @a and @b are equal and aborts if this is not the
151 * case, printing both expressions and the values they evaluated to. This
152 * macro is for use in unit tests.
154 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
157 * fail_unless_equals_int64:
158 * @a: a #gint64 value or expression
159 * @b: a #gint64 value or expression
161 * This macro checks that @a and @b are equal and aborts if this is not the
162 * case, printing both expressions and the values they evaluated to. This
163 * macro is for use in unit tests.
165 #define fail_unless_equals_int64(a, b) \
169 fail_unless(first == second, \
170 "'" #a "' (%" G_GINT64_FORMAT") is not equal to '" #b"' (%" \
171 G_GINT64_FORMAT")", first, second); \
174 * assert_equals_int64:
175 * @a: a #gint64 value or expression
176 * @b: a #gint64 value or expression
178 * This macro checks that @a and @b are equal and aborts if this is not the
179 * case, printing both expressions and the values they evaluated to. This
180 * macro is for use in unit tests.
182 #define assert_equals_int64(a, b) fail_unless_equals_int64(a, b)
185 * fail_unless_equals_uint64:
186 * @a: a #guint64 value or expression
187 * @b: a #guint64 value or expression
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.
193 #define fail_unless_equals_uint64(a, b) \
196 guint64 second = b; \
197 fail_unless(first == second, \
198 "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%" \
199 G_GUINT64_FORMAT ")", first, second); \
202 * assert_equals_uint64:
203 * @a: a #guint64 value or expression
204 * @b: a #guint64 value or expression
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.
210 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
213 * fail_unless_equals_string:
214 * @a: a string literal or expression
215 * @b: a string literal or expression
217 * This macro checks that @a and @b are equal (as per strcmp) and aborts if
218 * this is not the case, printing both expressions and the values they
219 * evaluated to. This macro is for use in unit tests.
221 #define fail_unless_equals_string(a, b) \
223 const gchar * first = a; \
224 const gchar * second = b; \
225 fail_unless(g_strcmp0 (first, second) == 0, \
226 "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second); \
229 * assert_equals_string:
230 * @a: a string literal or expression
231 * @b: a string literal or expression
233 * This macro checks that @a and @b are equal (as per strcmp) and aborts if
234 * this is not the case, printing both expressions and the values they
235 * evaluated to. This macro is for use in unit tests.
237 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
240 * fail_unless_equals_float:
241 * @a: a #gdouble or #gfloat value or expression
242 * @b: a #gdouble or #gfloat value or expression
244 * This macro checks that @a and @b are (almost) equal and aborts if this
245 * is not the case, printing both expressions and the values they evaluated
246 * to. This macro is for use in unit tests.
248 #define fail_unless_equals_float(a, b) \
252 /* This will only work for 'normal' values and values around 0, \
253 * which should be good enough for our purposes here */ \
254 fail_unless(fabs (first - second) < 0.0000001, \
255 "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
259 * assert_equals_float:
260 * @a: a #gdouble or #gfloat value or expression
261 * @b: a #gdouble or #gfloat value or expression
263 * This macro checks that @a and @b are (almost) equal and aborts if this
264 * is not the case, printing both expressions and the values they evaluated
265 * to. This macro is for use in unit tests.
267 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
271 * thread test macros and variables
273 extern GList *thread_list;
275 extern GCond start_cond; /* used to notify main thread of thread startups */
276 extern GCond sync_cond; /* used to synchronize all threads and main thread */
278 #define MAIN_START_THREADS(count, function, data) \
280 MAIN_START_THREAD_FUNCTIONS(count, function, data); \
283 #define MAIN_INIT() \
285 _gst_check_threads_running = TRUE; \
288 #define MAIN_START_THREAD_FUNCTIONS(count, function, data) \
291 for (i = 0; i < count; ++i) { \
292 MAIN_START_THREAD_FUNCTION (i, function, data); \
296 #define MAIN_START_THREAD_FUNCTION(i, function, data) \
298 GThread *thread = NULL; \
299 GST_DEBUG ("MAIN: creating thread %d", i); \
300 g_mutex_lock (&mutex); \
301 thread = g_thread_try_new ("gst-check", \
302 (GThreadFunc) function, data, NULL); \
303 /* wait for thread to signal us that it's ready */ \
304 GST_DEBUG ("MAIN: waiting for thread %d", i); \
305 g_cond_wait (&start_cond, &mutex); \
306 g_mutex_unlock (&mutex); \
308 thread_list = g_list_append (thread_list, thread); \
312 #define MAIN_SYNCHRONIZE() \
314 GST_DEBUG ("MAIN: synchronizing"); \
315 g_cond_broadcast (&sync_cond); \
316 GST_DEBUG ("MAIN: synchronized"); \
319 #define MAIN_STOP_THREADS() \
321 _gst_check_threads_running = FALSE; \
323 /* join all threads */ \
324 GST_DEBUG ("MAIN: joining"); \
325 g_list_foreach (thread_list, (GFunc) g_thread_join, NULL); \
326 g_list_free (thread_list); \
327 thread_list = NULL; \
328 GST_DEBUG ("MAIN: joined"); \
331 #define THREAD_START() \
333 THREAD_SYNCHRONIZE();
335 #define THREAD_STARTED() \
337 /* signal main thread that we started */ \
338 GST_DEBUG ("THREAD %p: started", g_thread_self ()); \
339 g_mutex_lock (&mutex); \
340 g_cond_signal (&start_cond); \
343 #define THREAD_SYNCHRONIZE() \
345 /* synchronize everyone */ \
346 GST_DEBUG ("THREAD %p: syncing", g_thread_self ()); \
347 g_cond_wait (&sync_cond, &mutex); \
348 GST_DEBUG ("THREAD %p: synced", g_thread_self ()); \
349 g_mutex_unlock (&mutex); \
352 #define THREAD_SWITCH() \
354 /* a minimal sleep is a context switch */ \
358 #define THREAD_TEST_RUNNING() (_gst_check_threads_running == TRUE)
360 /* additional assertions */
361 #define ASSERT_CRITICAL(code) \
363 _gst_check_expecting_log = TRUE; \
364 _gst_check_raised_critical = FALSE; \
366 _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
367 "Expected g_critical, got nothing", NULL); \
368 _gst_check_expecting_log = FALSE; \
371 #define ASSERT_WARNING(code) \
373 _gst_check_expecting_log = TRUE; \
374 _gst_check_raised_warning = FALSE; \
376 _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__, \
377 "Expected g_warning, got nothing", NULL); \
378 _gst_check_expecting_log = FALSE; \
382 #define ASSERT_OBJECT_REFCOUNT(object, name, value) \
385 rc = GST_OBJECT_REFCOUNT_VALUE (object); \
386 fail_unless (rc == value, \
387 "%s (%p) refcount is %d instead of %d", \
388 name, object, rc, value); \
391 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper) \
393 int rc = GST_OBJECT_REFCOUNT_VALUE (object); \
397 fail_unless (rc >= lo, \
398 "%s (%p) refcount %d is smaller than %d", \
399 name, object, rc, lo); \
400 fail_unless (rc <= hi, \
401 "%s (%p) refcount %d is bigger than %d", \
402 name, object, rc, hi); \
406 #define ASSERT_CAPS_REFCOUNT(caps, name, value) \
407 ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
409 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value) \
410 ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
412 #define ASSERT_MINI_OBJECT_REFCOUNT(miniobj, name, value) \
415 rc = GST_MINI_OBJECT_REFCOUNT_VALUE (miniobj); \
416 fail_unless (rc == value, \
417 name " (%p) refcount is %d instead of %d", miniobj, rc, value); \
420 #define ASSERT_SET_STATE(element, state, ret) \
421 fail_unless (gst_element_set_state (element, \
423 "could not change state to " #state);
425 #define GST_CHECK_MAIN(name) \
426 int main (int argc, char **argv) \
429 gst_check_init (&argc, &argv); \
430 s = name ## _suite (); \
431 return gst_check_run_suite (s, # name, __FILE__); \
434 /* Hack to allow run-time selection of unit tests to run via the
435 * GST_CHECKS environment variable (test function names, comma-separated) */
437 gboolean _gst_check_run_test_func (const gchar * func_name);
440 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
441 int allowed_exit_value, int start, int end)
443 if (_gst_check_run_test_func (fname)) {
444 _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
448 #define _tcase_add_test __gst_tcase_add_test
450 /* add define to skip broken tests */
451 #define tcase_skip_broken_test(chain,test_func) \
452 if (0) { tcase_add_test(chain,test_func); } else { \
453 g_printerr ("FIXME: skipping test %s because it's broken\n", G_STRINGIFY (test_func)); \
458 #endif /* __GST_CHECK_H__ */