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/gst.h>
33 #include <gst/check/check-prelude.h>
34
35 #define CK_DLL_EXP GST_CHECK_API
36 #include <gst/check/internal-check.h>
37
38 G_BEGIN_DECLS
39
40 GST_CHECK_API GstDebugCategory *check_debug;
41 #define GST_CAT_DEFAULT check_debug
42
43 /* logging function for tests
44 * a test uses g_message() to log a debug line
45 * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
46 * messages
47 */
48 GST_CHECK_API gboolean _gst_check_threads_running;
49 GST_CHECK_API gboolean _gst_check_raised_critical;
50 GST_CHECK_API gboolean _gst_check_raised_warning;
51 GST_CHECK_API gboolean _gst_check_expecting_log;
52 GST_CHECK_API gboolean _gst_check_list_tests;
53
54 /* global variables used in test methods */
55 GST_CHECK_API GList * buffers;
56
57 GST_CHECK_API GMutex check_mutex;
58 GST_CHECK_API GCond check_cond;
59
60 typedef struct
61 {
62 const char *name;
63 int size;
64 int abi_size;
65 }
66 GstCheckABIStruct;
67
68 typedef struct _GstCheckLogFilter GstCheckLogFilter;
69
70 /**
71 * GstCheckLogFilterFunc:
72 * @log_domain: the log domain of the message
73 * @log_level: the log level of the message
74 * @message: the message that has occurred
75 * @user_data: user data
76 *
77 * A function that is called for messages matching the filter added by
78 * @gst_check_add_log_filter.
79 *
80 * Returns: %TRUE if message should be discarded by GstCheck.
81 *
82 * Since: 1.12
83 */
84 typedef gboolean (*GstCheckLogFilterFunc) (const gchar * log_domain,
85 GLogLevelFlags log_level, const gchar * message, gpointer user_data);
86
87 GST_CHECK_API
88 void gst_check_init (int *argc, char **argv[]);
89
90 GST_CHECK_API
91 GstCheckLogFilter * gst_check_add_log_filter (const gchar * log_domain,
92 GLogLevelFlags log_level, GRegex * regex, GstCheckLogFilterFunc func,
93 gpointer user_data, GDestroyNotify destroy_data);
94
95 GST_CHECK_API
96 void gst_check_remove_log_filter (GstCheckLogFilter * filter);
97
98 GST_CHECK_API
99 void gst_check_clear_log_filter (void);
100
101 GST_CHECK_API
102 GstFlowReturn gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer);
103
104 GST_CHECK_API
105 void gst_check_message_error (GstMessage * message, GstMessageType type,
106 GQuark domain, gint code);
107
108 GST_CHECK_API
109 GstElement *gst_check_setup_element (const gchar * factory);
110
111 GST_CHECK_API
112 void gst_check_teardown_element (GstElement * element);
113
114 GST_CHECK_API
115 GstPad *gst_check_setup_src_pad (GstElement * element,
116 GstStaticPadTemplate * tmpl);
117
118 GST_CHECK_API
119 GstPad *gst_check_setup_src_pad_from_template (GstElement * element,
120 GstPadTemplate * tmpl);
121
122 GST_CHECK_API
123 GstPad * gst_check_setup_src_pad_by_name (GstElement * element,
124 GstStaticPadTemplate * tmpl, const gchar *name);
125
126 GST_CHECK_API
127 GstPad * gst_check_setup_src_pad_by_name_from_template (GstElement * element,
128 GstPadTemplate * tmpl, const gchar *name);
129
130 GST_CHECK_API
131 GstPad *gst_check_setup_sink_pad (GstElement * element,
132 GstStaticPadTemplate * tmpl);
133
134 GST_CHECK_API
135 GstPad *gst_check_setup_sink_pad_from_template (GstElement * element,
136 GstPadTemplate * tmpl);
137
138 GST_CHECK_API
139 GstPad * gst_check_setup_sink_pad_by_name (GstElement * element,
140 GstStaticPadTemplate * tmpl, const gchar *name);
141
142 GST_CHECK_API
143 GstPad * gst_check_setup_sink_pad_by_name_from_template (GstElement * element,
144 GstPadTemplate * tmpl, const gchar *name);
145
146 GST_CHECK_API
147 void gst_check_teardown_pad_by_name (GstElement * element, const gchar *name);
148
149 GST_CHECK_API
150 void gst_check_teardown_src_pad (GstElement * element);
151
152 GST_CHECK_API
153 void gst_check_drop_buffers (void);
154
155 GST_CHECK_API
156 void gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2);
157
158 GST_CHECK_API
159 void gst_check_buffer_data (GstBuffer * buffer, gconstpointer data, gsize size);
160
161 GST_CHECK_API
162 void gst_check_element_push_buffer_list (const gchar * element_name,
163 GList * buffer_in, GstCaps * caps_in, GList * buffer_out,
164 GstCaps * caps_out, GstFlowReturn last_flow_return);
165
166 GST_CHECK_API
167 void gst_check_element_push_buffer (const gchar * element_name,
168 GstBuffer * buffer_in, GstCaps * caps_in, GstBuffer * buffer_out,
169 GstCaps *caps_out);
170
171 GST_CHECK_API
172 void gst_check_teardown_sink_pad (GstElement * element);
173
174 GST_CHECK_API
175 void gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes);
176
177 GST_CHECK_API
178 gint gst_check_run_suite (Suite * suite, const gchar * name,
179 const gchar * fname);
180
181 GST_CHECK_API
182 void gst_check_setup_events (GstPad * srcpad, GstElement * element,
183 GstCaps * caps, GstFormat format);
184
185 GST_CHECK_API
186 void gst_check_setup_events_with_stream_id (GstPad * srcpad,
187 GstElement * element, GstCaps * caps, GstFormat format,
188 const gchar * stream_id);
189
190 GST_CHECK_API
191 void gst_check_objects_destroyed_on_unref (gpointer object_to_unref, gpointer first_object, ...)
192 G_GNUC_NULL_TERMINATED;
193
194 GST_CHECK_API
195 void gst_check_object_destroyed_on_unref (gpointer object_to_unref);
196
197 #define fail_unless_message_error(msg, domain, code) \
198 gst_check_message_error (msg, GST_MESSAGE_ERROR, \
199 GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
200 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
201
202 #ifdef GST_CHECK_TEST_ENVIRONMENT_BEACON
203 #define GST_DO_CHECK_TEST_ENVIRONMENT \
204 G_STMT_START { \
205 if (g_getenv (GST_CHECK_TEST_ENVIRONMENT_BEACON) == NULL) \
206 fail ("Test environment not set up correctly! Expected environment " \
207 "variable '%s' to be set.", GST_CHECK_TEST_ENVIRONMENT_BEACON); \
208 } G_STMT_END
209
210 #else
211 #define GST_DO_CHECK_TEST_ENVIRONMENT /* nothing to check */
212 #endif
213
214 /**
215 * GST_START_TEST:
216 * @__testname: test function name
217 *
218 * wrapper for checks START_TEST
219 */
220 /**
221 * GST_END_TEST:
222 *
223 * wrapper for checks END_TEST
224 */
225 #define GST_START_TEST(__testname) \
226 static void __testname (int G_GNUC_UNUSED __i__) \
227 {\
228 GST_DEBUG ("test start"); \
229 GST_DO_CHECK_TEST_ENVIRONMENT; \
230 tcase_fn_start (""# __testname, __FILE__, __LINE__);
231
232 #define GST_END_TEST GST_LOG ("cleaning up tasks"); \
233 gst_task_cleanup_all (); \
234 END_TEST
235
236 /* additional fail macros */
237 /**
238 * fail_unless_equals_int:
239 * @a: a #gint value or expression
240 * @b: a #gint value or expression
241 *
242 * This macro checks that @a and @b are equal and aborts if this is not the
243 * case, printing both expressions and the values they evaluated to. This
244 * macro is for use in unit tests.
245 */
246 #define fail_unless_equals_int(a, b) \
247 G_STMT_START { \
248 int first = a; \
249 int second = b; \
250 fail_unless(first == second, \
251 "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second); \
252 } G_STMT_END;
253 /**
254 * assert_equals_int:
255 * @a: a #gint value or expression
256 * @b: a #gint value or expression
257 *
258 * This macro checks that @a and @b are equal and aborts if this is not the
259 * case, printing both expressions and the values they evaluated to. This
260 * macro is for use in unit tests.
261 */
262 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
263
264 /**
265 * fail_unless_equals_int_hex:
266 * @a: a #gint value or expression
267 * @b: a #gint value or expression
268 *
269 * This macro checks that @a and @b are equal and aborts if this is not the
270 * case, printing both expressions and the values they evaluated to in
271 * hexadecimal format. This macro is for use in unit tests.
272 *
273 * Since: 1.2
274 */
275 #define fail_unless_equals_int_hex(a, b) \
276 G_STMT_START { \
277 int first = a; \
278 int second = b; \
279 fail_unless(first == second, \
280 "'" #a "' (0x%08x) is not equal to '" #b"' (0x%08x)", first, second);\
281 } G_STMT_END;
282
283 /**
284 * assert_equals_int_hex:
285 * @a: a #gint value or expression
286 * @b: a #gint value or expression
287 *
288 * This macro checks that @a and @b are equal and aborts if this is not the
289 * case, printing both expressions and the values they evaluated to in
290 * hexadecimal format. This macro is for use in unit tests.
291 *
292 * Since: 1.2
293 */
294 #define assert_equals_int_hex(a, b) fail_unless_equals_int_hex(a, b)
295
296 /**
297 * fail_unless_equals_int64:
298 * @a: a #gint64 value or expression
299 * @b: a #gint64 value or expression
300 *
301 * This macro checks that @a and @b are equal and aborts if this is not the
302 * case, printing both expressions and the values they evaluated to. This
303 * macro is for use in unit tests.
304 */
305 #define fail_unless_equals_int64(a, b) \
306 G_STMT_START { \
307 gint64 first = a; \
308 gint64 second = b; \
309 fail_unless(first == second, \
310 "'" #a "' (%" G_GINT64_FORMAT") is not equal to '" #b"' (%" \
311 G_GINT64_FORMAT")", first, second); \
312 } G_STMT_END;
313 /**
314 * assert_equals_int64:
315 * @a: a #gint64 value or expression
316 * @b: a #gint64 value or expression
317 *
318 * This macro checks that @a and @b are equal and aborts if this is not the
319 * case, printing both expressions and the values they evaluated to. This
320 * macro is for use in unit tests.
321 */
322 #define assert_equals_int64(a, b) fail_unless_equals_int64(a, b)
323
324 /**
325 * fail_unless_equals_int64_hex:
326 * @a: a #gint64 value or expression
327 * @b: a #gint64 value or expression
328 *
329 * This macro checks that @a and @b are equal and aborts if this is not the
330 * case, printing both expressions and the values they evaluated to in
331 * hexadecimal format. This macro is for use in unit tests.
332 *
333 * Since: 1.2
334 */
335 #define fail_unless_equals_int64_hex(a, b) \
336 G_STMT_START { \
337 gint64 first = a; \
338 gint64 second = b; \
339 fail_unless(first == second, \
340 "'" #a "' (0x%016x) is not equal to '" #b"' (0x%016x)", first, second);\
341 } G_STMT_END;
342 /**
343 * assert_equals_int64_hex:
344 * @a: a #gint64 value or expression
345 * @b: a #gint64 value or expression
346 *
347 * This macro checks that @a and @b are equal and aborts if this is not the
348 * case, printing both expressions and the values they evaluated to in
349 * hexadecimal format. This macro is for use in unit tests.
350 *
351 * Since: 1.2
352 */
353 #define assert_equals_int64_hex(a,b) fail_unless_equals_int64_hex(a,b)
354
355 /**
356 * fail_unless_equals_uint64:
357 * @a: a #guint64 value or expression
358 * @b: a #guint64 value or expression
359 *
360 * This macro checks that @a and @b are equal and aborts if this is not the
361 * case, printing both expressions and the values they evaluated to. This
362 * macro is for use in unit tests.
363 */
364 #define fail_unless_equals_uint64(a, b) \
365 G_STMT_START { \
366 guint64 first = a; \
367 guint64 second = b; \
368 fail_unless(first == second, \
369 "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%" \
370 G_GUINT64_FORMAT ")", first, second); \
371 } G_STMT_END;
372 /**
373 * assert_equals_uint64:
374 * @a: a #guint64 value or expression
375 * @b: a #guint64 value or expression
376 *
377 * This macro checks that @a and @b are equal and aborts if this is not the
378 * case, printing both expressions and the values they evaluated to. This
379 * macro is for use in unit tests.
380 */
381 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
382
383 /**
384 * fail_unless_equals_uint64_hex:
385 * @a: a #gint64 value or expression
386 * @b: a #gint64 value or expression
387 *
388 * This macro checks that @a and @b are equal and aborts if this is not the
389 * case, printing both expressions and the values they evaluated to in
390 * hexadecimal format. This macro is for use in unit tests.
391 *
392 * Since: 1.2
393 */
394 #define fail_unless_equals_uint64_hex(a, b) \
395 G_STMT_START { \
396 guint64 first = a; \
397 guint64 second = b; \
398 fail_unless(first == second, \
399 "'" #a "' (0x%016x) is not equal to '" #b"' (0x%016x)", first, second);\
400 } G_STMT_END;
401 /**
402 * assert_equals_uint64_hex:
403 * @a: a #guint64 value or expression
404 * @b: a #guint64 value or expression
405 *
406 * This macro checks that @a and @b are equal and aborts if this is not the
407 * case, printing both expressions and the values they evaluated to in
408 * hexadecimal format. This macro is for use in unit tests.
409 *
410 * Since: 1.2
411 */
412 #define assert_equals_uint64_hex(a,b) fail_unless_equals_uint64_hex(a,b)
413
414 /**
415 * fail_unless_equals_string:
416 * @a: a string literal or expression
417 * @b: a string literal or expression
418 *
419 * This macro checks that @a and @b are equal (as per strcmp) and aborts if
420 * this is not the case, printing both expressions and the values they
421 * evaluated to. This macro is for use in unit tests.
422 */
423 #define fail_unless_equals_string(a, b) \
424 G_STMT_START { \
425 const gchar * first = a; \
426 const gchar * second = b; \
427 fail_unless(g_strcmp0 (first, second) == 0, \
428 "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second); \
429 } G_STMT_END;
430 /**
431 * assert_equals_string:
432 * @a: a string literal or expression
433 * @b: a string literal or expression
434 *
435 * This macro checks that @a and @b are equal (as per strcmp) and aborts if
436 * this is not the case, printing both expressions and the values they
437 * evaluated to. This macro is for use in unit tests.
438 */
439 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
440
441 /**
442 * fail_unless_equals_float:
443 * @a: a #gdouble or #gfloat value or expression
444 * @b: a #gdouble or #gfloat value or expression
445 *
446 * This macro checks that @a and @b are (almost) equal and aborts if this
447 * is not the case, printing both expressions and the values they evaluated
448 * to. This macro is for use in unit tests.
449 */
450 #define fail_unless_equals_float(a, b) \
451 G_STMT_START { \
452 double first = a; \
453 double second = b; \
454 /* This will only work for 'normal' values and values around 0, \
455 * which should be good enough for our purposes here */ \
456 fail_unless(fabs (first - second) < 0.0000001, \
457 "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
458 } G_STMT_END;
459
460 /**
461 * assert_equals_float:
462 * @a: a #gdouble or #gfloat value or expression
463 * @b: a #gdouble or #gfloat value or expression
464 *
465 * This macro checks that @a and @b are (almost) equal and aborts if this
466 * is not the case, printing both expressions and the values they evaluated
467 * to. This macro is for use in unit tests.
468 */
469 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
470
471 /**
472 * fail_unless_equals_pointer:
473 * @a: a pointer value or expression
474 * @b: a pointer value or expression
475 *
476 * This macro checks that @a and @b are equal and aborts if this
477 * is not the case, printing both expressions and the values they
478 * evaluated to. This macro is for use in unit tests.
479 *
480 * Since: 1.2
481 */
482 #define fail_unless_equals_pointer(a, b) \
483 G_STMT_START { \
484 gpointer first = a; \
485 gpointer second = b; \
486 fail_unless(first == second, \
487 "'" #a "' (%p) is not equal to '" #b "' (%p)", first, second);\
488 } G_STMT_END;
489
490 /**
491 * assert_equals_pointer:
492 * @a: a pointer value or expression
493 * @b: a pointer value or expression
494 *
495 * This macro checks that @a and @b are equal and aborts if this
496 * is not the case, printing both expressions and the values they
497 * evaluated to. This macro is for use in unit tests.
498 *
499 * Since: 1.2
500 */
501 #define assert_equals_pointer(a, b) fail_unless_equals_pointer(a, b)
502
503 /**
504 * fail_unless_equals_clocktime:
505 * @a: a #GstClockTime value or expression
506 * @b: a #GstClockTime value or expression
507 *
508 * This macro checks that @a and @b are equal and aborts if this is not the
509 * case, printing both expressions and the values they evaluated to. This
510 * macro is for use in unit tests.
511 */
512 #define fail_unless_equals_clocktime(a, b) \
513 G_STMT_START { \
514 GstClockTime first = a; \
515 GstClockTime second = b; \
516 fail_unless(first == second, \
517 "'" #a "' (%" GST_TIME_FORMAT") is not equal to '" #b"' (%" GST_TIME_FORMAT")", \
518 GST_TIME_ARGS (first), GST_TIME_ARGS (second)); \
519 } G_STMT_END;
520
521 /***
522 * thread test macros and variables
523 */
524 GST_CHECK_API GList *thread_list;
525 GST_CHECK_API GMutex mutex;
526 GST_CHECK_API GCond start_cond; /* used to notify main thread of thread startups */
527 GST_CHECK_API GCond sync_cond; /* used to synchronize all threads and main thread */
528
529 #define MAIN_START_THREADS(count, function, data) \
530 MAIN_INIT(); \
531 MAIN_START_THREAD_FUNCTIONS(count, function, data); \
532 MAIN_SYNCHRONIZE();
533
534 #define MAIN_INIT() \
535 G_STMT_START { \
536 g_mutex_init (&mutex); \
537 g_cond_init (&start_cond); \
538 g_cond_init (&sync_cond); \
539 _gst_check_threads_running = TRUE; \
540 } G_STMT_END;
541
542 #define MAIN_START_THREAD_FUNCTIONS(count, function, data) \
543 G_STMT_START { \
544 int i; \
545 for (i = 0; i < count; ++i) { \
546 MAIN_START_THREAD_FUNCTION (i, function, data); \
547 } \
548 } G_STMT_END;
549
550 #define MAIN_START_THREAD_FUNCTION(i, function, data) \
551 G_STMT_START { \
552 GThread *thread = NULL; \
553 GST_DEBUG ("MAIN: creating thread %d", i); \
554 g_mutex_lock (&mutex); \
555 thread = g_thread_try_new ("gst-check", \
556 (GThreadFunc) function, data, NULL); \
557 /* wait for thread to signal us that it's ready */ \
558 GST_DEBUG ("MAIN: waiting for thread %d", i); \
559 g_cond_wait (&start_cond, &mutex); \
560 g_mutex_unlock (&mutex); \
561 \
562 thread_list = g_list_append (thread_list, thread); \
563 } G_STMT_END;
564
565
566 #define MAIN_SYNCHRONIZE() \
567 G_STMT_START { \
568 GST_DEBUG ("MAIN: synchronizing"); \
569 g_cond_broadcast (&sync_cond); \
570 GST_DEBUG ("MAIN: synchronized"); \
571 } G_STMT_END;
572
573 #define MAIN_STOP_THREADS() \
574 G_STMT_START { \
575 _gst_check_threads_running = FALSE; \
576 \
577 /* join all threads */ \
578 GST_DEBUG ("MAIN: joining"); \
579 g_list_foreach (thread_list, (GFunc) g_thread_join, NULL); \
580 g_list_free (thread_list); \
581 thread_list = NULL; \
582 g_mutex_clear (&mutex); \
583 g_cond_clear (&start_cond); \
584 g_cond_clear (&sync_cond); \
585 GST_DEBUG ("MAIN: joined"); \
586 } G_STMT_END;
587
588 #define THREAD_START() \
589 THREAD_STARTED(); \
590 THREAD_SYNCHRONIZE();
591
592 #define THREAD_STARTED() \
593 G_STMT_START { \
594 /* signal main thread that we started */ \
595 GST_DEBUG ("THREAD %p: started", g_thread_self ()); \
596 g_mutex_lock (&mutex); \
597 g_cond_signal (&start_cond); \
598 } G_STMT_END;
599
600 #define THREAD_SYNCHRONIZE() \
601 G_STMT_START { \
602 /* synchronize everyone */ \
603 GST_DEBUG ("THREAD %p: syncing", g_thread_self ()); \
604 fail_if (g_mutex_trylock (&mutex), \
605 "bug in unit test, mutex should be locked at this point");\
606 g_cond_wait (&sync_cond, &mutex); \
607 GST_DEBUG ("THREAD %p: synced", g_thread_self ()); \
608 g_mutex_unlock (&mutex); \
609 } G_STMT_END;
610
611 #define THREAD_SWITCH() \
612 G_STMT_START { \
613 /* a minimal sleep is a context switch */ \
614 g_usleep (1); \
615 } G_STMT_END;
616
617 #define THREAD_TEST_RUNNING() (!!_gst_check_threads_running)
618
619 /* additional assertions */
620
621 #if GST_DISABLE_GLIB_CHECKS
622 #define ASSERT_CRITICAL(code)
623 #else
624 #define ASSERT_CRITICAL(code) \
625 G_STMT_START { \
626 _gst_check_expecting_log = TRUE; \
627 _gst_check_raised_critical = FALSE; \
628 code; \
629 if (!_gst_check_raised_critical) \
630 _ck_assert_failed (__FILE__, __LINE__, \
631 "Expected g_critical, got nothing", NULL); \
632 _gst_check_expecting_log = FALSE; \
633 } G_STMT_END
634 #endif /* GST_DISABLE_GLIB_CHECKS */
635
636 #define ASSERT_WARNING(code) \
637 G_STMT_START { \
638 _gst_check_expecting_log = TRUE; \
639 _gst_check_raised_warning = FALSE; \
640 code; \
641 if (!_gst_check_raised_warning) \
642 _ck_assert_failed (__FILE__, __LINE__, \
643 "Expected g_warning, got nothing", NULL); \
644 _gst_check_expecting_log = FALSE; \
645 } G_STMT_END
646
647
648 #define ASSERT_OBJECT_REFCOUNT(object, name, value) \
649 G_STMT_START { \
650 int rc; \
651 rc = GST_OBJECT_REFCOUNT_VALUE (object); \
652 fail_unless (rc == value, \
653 "%s (%p) refcount is %d instead of %d", \
654 name, object, rc, value); \
655 } G_STMT_END
656
657 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper) \
658 G_STMT_START { \
659 int rc = GST_OBJECT_REFCOUNT_VALUE (object); \
660 int lo = lower; \
661 int hi = upper; \
662 \
663 fail_unless (rc >= lo, \
664 "%s (%p) refcount %d is smaller than %d", \
665 name, object, rc, lo); \
666 fail_unless (rc <= hi, \
667 "%s (%p) refcount %d is bigger than %d", \
668 name, object, rc, hi); \
669 } G_STMT_END
670
671
672 #define ASSERT_CAPS_REFCOUNT(caps, name, value) \
673 ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
674
675 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value) \
676 ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
677
678 #define ASSERT_MINI_OBJECT_REFCOUNT(miniobj, name, value) \
679 G_STMT_START { \
680 int rc; \
681 rc = GST_MINI_OBJECT_REFCOUNT_VALUE (miniobj); \
682 fail_unless (rc == value, \
683 name " (%p) refcount is %d instead of %d", miniobj, rc, value); \
684 } G_STMT_END
685
686 #define ASSERT_SET_STATE(element, state, ret) \
687 fail_unless (gst_element_set_state (GST_ELEMENT(element), \
688 state) == ret, \
689 "could not change state to " #state);
690
691 #define GST_CHECK_MAIN(name) \
692 int main (int argc, char **argv) \
693 { \
694 Suite *s; \
695 gst_check_init (&argc, &argv); \
696 s = name ## _suite (); \
697 return gst_check_run_suite (s, # name, __FILE__); \
698 }
699
700 /* Hack to allow run-time selection of unit tests to run via the
701 * GST_CHECKS environment variable (test function names globs, comma
702 * separated), or GST_CHECKS_IGNORE with the same semantics */
703
704 GST_CHECK_API
705 gboolean _gst_check_run_test_func (const gchar * func_name);
706
707 static inline void
__gst_tcase_add_test(TCase * tc,TFun tf,const char * fname,int signal,int allowed_exit_value,int start,int end)708 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
709 int allowed_exit_value, int start, int end)
710 {
711 if (_gst_check_list_tests) {
712 g_print ("Test: %s\n", fname);
713 return;
714 }
715
716 if (_gst_check_run_test_func (fname)) {
717 _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
718 }
719 }
720
721 #define _tcase_add_test __gst_tcase_add_test
722
723 /* A special variant to add broken tests. These are normally skipped, but can be
724 * forced to run via GST_CHECKS */
725 #define tcase_skip_broken_test(chain,test_func) \
726 G_STMT_START { \
727 const char *env = g_getenv ("GST_CHECKS"); \
728 \
729 if (env != NULL && g_pattern_match_simple (env, G_STRINGIFY (test_func))) { \
730 tcase_add_test(chain,test_func); \
731 } else { \
732 g_printerr ("FIXME: skipping test %s because it's broken\n", G_STRINGIFY (test_func)); \
733 } \
734 } G_STMT_END
735
736 #define tcase_skip_broken_loop_test(chain,test_func,a,b) \
737 tcase_skip_broken_test (chain, test_func)
738
739 G_END_DECLS
740
741 #endif /* __GST_CHECK_H__ */
742