7e380c78568f11504a9889c329fd4c8208f0bd03
[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 #include <gst/check/gstbufferstraw.h>
37 #include <gst/check/gstconsistencychecker.h>
38 #include <gst/check/gsttestclock.h>
39
40 G_BEGIN_DECLS
41
42 GST_DEBUG_CATEGORY_EXTERN (check_debug);
43 #define GST_CAT_DEFAULT check_debug
44
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
48  * messages
49  */
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;
54
55 /* global variables used in test methods */
56 extern GList * buffers;
57
58 extern GMutex check_mutex;
59 extern GCond check_cond;
60
61 typedef struct
62 {
63   const char *name;
64   int size;
65   int abi_size;
66 }
67 GstCheckABIStruct;
68
69 void gst_check_init (int *argc, char **argv[]);
70
71 GstFlowReturn gst_check_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer);
72
73 void gst_check_message_error (GstMessage * message, GstMessageType type,
74     GQuark domain, gint code);
75
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,
94     GstCaps *caps_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);
101
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)
106
107 /**
108  * GST_START_TEST:
109  * @__testname: test function name
110  *
111  * wrapper for checks START_TEST
112  */
113 /**
114  * GST_END_TEST:
115  *
116  * wrapper for checks END_TEST
117  */
118 #define GST_START_TEST(__testname) \
119 static void __testname (int __i__)\
120 {\
121   GST_DEBUG ("test start"); \
122   tcase_fn_start (""# __testname, __FILE__, __LINE__);
123
124 #define GST_END_TEST GST_LOG ("cleaning up tasks"); \
125                      gst_task_cleanup_all (); \
126                      END_TEST
127
128 /* additional fail macros */
129 /**
130  * fail_unless_equals_int:
131  * @a: a #gint value or expression
132  * @b: a #gint value or expression
133  *
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.
137  */
138 #define fail_unless_equals_int(a, b)                                    \
139 G_STMT_START {                                                          \
140   int first = a;                                                        \
141   int second = b;                                                       \
142   fail_unless(first == second,                                          \
143     "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second);       \
144 } G_STMT_END;
145 /**
146  * assert_equals_int:
147  * @a: a #gint value or expression
148  * @b: a #gint value or expression
149  *
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.
153  */
154 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
155
156 /**
157  * fail_unless_equals_int64:
158  * @a: a #gint64 value or expression
159  * @b: a #gint64 value or expression
160  *
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.
164  */
165 #define fail_unless_equals_int64(a, b)                                  \
166 G_STMT_START {                                                          \
167   gint64 first = a;                                                     \
168   gint64 second = b;                                                    \
169   fail_unless(first == second,                                          \
170     "'" #a "' (%" G_GINT64_FORMAT") is not equal to '" #b"' (%"         \
171     G_GINT64_FORMAT")", first, second);                                 \
172 } G_STMT_END;
173 /**
174  * assert_equals_int64:
175  * @a: a #gint64 value or expression
176  * @b: a #gint64 value or expression
177  *
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.
181  */
182 #define assert_equals_int64(a, b) fail_unless_equals_int64(a, b)
183
184 /**
185  * fail_unless_equals_uint64:
186  * @a: a #guint64 value or expression
187  * @b: a #guint64 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_uint64(a, b)                                 \
194 G_STMT_START {                                                          \
195   guint64 first = a;                                                    \
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);                               \
200 } G_STMT_END;
201 /**
202  * assert_equals_uint64:
203  * @a: a #guint64 value or expression
204  * @b: a #guint64 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_uint64(a, b) fail_unless_equals_uint64(a, b)
211
212 /**
213  * fail_unless_equals_string:
214  * @a: a string literal or expression
215  * @b: a string literal or expression
216  *
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.
220  */
221 #define fail_unless_equals_string(a, b)                             \
222 G_STMT_START {                                                      \
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);   \
227 } G_STMT_END;
228 /**
229  * assert_equals_string:
230  * @a: a string literal or expression
231  * @b: a string literal or expression
232  *
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.
236  */
237 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
238
239 /**
240  * fail_unless_equals_float:
241  * @a: a #gdouble or #gfloat value or expression
242  * @b: a #gdouble or #gfloat value or expression
243  *
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.
247  */
248 #define fail_unless_equals_float(a, b)                            \
249 G_STMT_START {                                                    \
250   double first = a;                                               \
251   double second = 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);\
256 } G_STMT_END;
257
258 /**
259  * assert_equals_float:
260  * @a: a #gdouble or #gfloat value or expression
261  * @b: a #gdouble or #gfloat value or expression
262  *
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.
266  */
267 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
268
269
270 /***
271  * thread test macros and variables
272  */
273 extern GList *thread_list;
274 extern GMutex mutex;
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 */
277
278 #define MAIN_START_THREADS(count, function, data)               \
279 MAIN_INIT();                                                    \
280 MAIN_START_THREAD_FUNCTIONS(count, function, data);             \
281 MAIN_SYNCHRONIZE();
282
283 #define MAIN_INIT()                     \
284 G_STMT_START {                          \
285   _gst_check_threads_running = TRUE;    \
286 } G_STMT_END;
287
288 #define MAIN_START_THREAD_FUNCTIONS(count, function, data)      \
289 G_STMT_START {                                                  \
290   int i;                                                        \
291   for (i = 0; i < count; ++i) {                                 \
292     MAIN_START_THREAD_FUNCTION (i, function, data);             \
293   }                                                             \
294 } G_STMT_END;
295
296 #define MAIN_START_THREAD_FUNCTION(i, function, data)           \
297 G_STMT_START {                                                  \
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);                                    \
307                                                                 \
308     thread_list = g_list_append (thread_list, thread);          \
309 } G_STMT_END;
310
311
312 #define MAIN_SYNCHRONIZE()              \
313 G_STMT_START {                          \
314   GST_DEBUG ("MAIN: synchronizing");    \
315   g_cond_broadcast (&sync_cond);        \
316   GST_DEBUG ("MAIN: synchronized");     \
317 } G_STMT_END;
318
319 #define MAIN_STOP_THREADS()                                     \
320 G_STMT_START {                                                  \
321   _gst_check_threads_running = FALSE;                           \
322                                                                 \
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");                                   \
329 } G_STMT_END;
330
331 #define THREAD_START()                                          \
332 THREAD_STARTED();                                               \
333 THREAD_SYNCHRONIZE();
334
335 #define THREAD_STARTED()                                        \
336 G_STMT_START {                                                  \
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);                                  \
341 } G_STMT_END;
342
343 #define THREAD_SYNCHRONIZE()                                    \
344 G_STMT_START {                                                  \
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);                                      \
350 } G_STMT_END;
351
352 #define THREAD_SWITCH()                                         \
353 G_STMT_START {                                                  \
354   /* a minimal sleep is a context switch */                     \
355   g_usleep (1);                                                 \
356 } G_STMT_END;
357
358 #define THREAD_TEST_RUNNING()   (_gst_check_threads_running == TRUE)
359
360 /* additional assertions */
361 #define ASSERT_CRITICAL(code)                                   \
362 G_STMT_START {                                                  \
363   _gst_check_expecting_log = TRUE;                              \
364   _gst_check_raised_critical = FALSE;                           \
365   code;                                                         \
366   _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
367                 "Expected g_critical, got nothing", NULL);      \
368   _gst_check_expecting_log = FALSE;                             \
369 } G_STMT_END
370
371 #define ASSERT_WARNING(code)                                    \
372 G_STMT_START {                                                  \
373   _gst_check_expecting_log = TRUE;                              \
374   _gst_check_raised_warning = FALSE;                            \
375   code;                                                         \
376   _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__,  \
377                 "Expected g_warning, got nothing", NULL);       \
378   _gst_check_expecting_log = FALSE;                             \
379 } G_STMT_END
380
381
382 #define ASSERT_OBJECT_REFCOUNT(object, name, value)             \
383 G_STMT_START {                                                  \
384   int rc;                                                       \
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);                                 \
389 } G_STMT_END
390
391 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper)      \
392 G_STMT_START {                                                          \
393   int rc = GST_OBJECT_REFCOUNT_VALUE (object);                          \
394   int lo = lower;                                                       \
395   int hi = upper;                                                       \
396                                                                         \
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);                                            \
403 } G_STMT_END
404
405
406 #define ASSERT_CAPS_REFCOUNT(caps, name, value)                 \
407         ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
408
409 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value)             \
410         ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
411
412 #define ASSERT_MINI_OBJECT_REFCOUNT(miniobj, name, value)       \
413 G_STMT_START {                                                  \
414   int rc;                                                       \
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); \
418 } G_STMT_END
419
420 #define ASSERT_SET_STATE(element, state, ret)                   \
421 fail_unless (gst_element_set_state (element,                    \
422   state) == ret,                                                \
423   "could not change state to " #state);
424
425 #define GST_CHECK_MAIN(name)                                    \
426 int main (int argc, char **argv)                                \
427 {                                                               \
428   Suite *s;                                                     \
429   gst_check_init (&argc, &argv);                                \
430   s = name ## _suite ();                                        \
431   return gst_check_run_suite (s, # name, __FILE__);             \
432 }
433
434 /* Hack to allow run-time selection of unit tests to run via the
435  * GST_CHECKS environment variable (test function names, comma-separated) */
436
437 gboolean _gst_check_run_test_func (const gchar * func_name);
438
439 static inline void
440 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
441     int allowed_exit_value, int start, int end)
442 {
443   if (_gst_check_run_test_func (fname)) {
444     _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
445   }
446 }
447
448 #define _tcase_add_test __gst_tcase_add_test
449
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)); \
454   }
455
456 G_END_DECLS
457
458 #endif /* __GST_CHECK_H__ */