]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - libs/gst/check/gstcheck.h
e77c007bce3783f370ff579e4aa31b4e23dfc7f6
[glsdk/gstreamer0-10.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., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
24 #ifndef __GST_CHECK_H__
25 #define __GST_CHECK_H__
27 #include <signal.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <math.h>
32 #include <gst/check/internal-check.h>
34 #include <gst/gst.h>
36 G_BEGIN_DECLS
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
44  * messages
45  */
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;
57 typedef struct
58 {
59   const char *name;
60   int size;
61   int abi_size;
62 }
63 GstCheckABIStruct;
65 void gst_check_init (int *argc, char **argv[]);
67 GstFlowReturn gst_check_chain_func (GstPad * pad, 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, GstCaps * caps);
76 GstPad * gst_check_setup_src_pad_by_name (GstElement * element,
77           GstStaticPadTemplate * tmpl, const gchar *name);
78 GstPad * gst_check_setup_sink_pad_by_name (GstElement * element, 
79           GstStaticPadTemplate * tmpl, const gchar *name);
80 void gst_check_teardown_pad_by_name (GstElement * element, const gchar *name);
81 void gst_check_teardown_src_pad (GstElement * element);
82 void gst_check_drop_buffers (void);
83 void gst_check_caps_equal (GstCaps * caps1, GstCaps * caps2);
84 void gst_check_element_push_buffer_list (const gchar * element_name,
85     GList * buffer_in, GList * buffer_out, GstFlowReturn last_flow_return);
86 void gst_check_element_push_buffer (const gchar * element_name,
87     GstBuffer * buffer_in, GstBuffer * buffer_out);
88 GstPad *gst_check_setup_sink_pad (GstElement * element,
89     GstStaticPadTemplate * tmpl, GstCaps * caps);
90 void gst_check_teardown_sink_pad (GstElement * element);
91 void gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes);
92 gint gst_check_run_suite (Suite * suite, const gchar * name,
93     const gchar * fname);
95 #define fail_unless_message_error(msg, domain, code)            \
96 gst_check_message_error (msg, GST_MESSAGE_ERROR,                \
97   GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
98 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
100 /**
101  * GST_START_TEST:
102  * @__testname: test function name
103  *
104  * wrapper for checks START_TEST
105  */
106 /**
107  * GST_END_TEST:
108  *
109  * wrapper for checks END_TEST
110  */
111 #define GST_START_TEST(__testname) \
112 static void __testname (int __i__)\
113 {\
114   GST_DEBUG ("test start"); \
115   tcase_fn_start (""# __testname, __FILE__, __LINE__);
117 #define GST_END_TEST GST_LOG ("cleaning up tasks"); \
118                      gst_task_cleanup_all (); \
119                      END_TEST
121 /* additional fail macros */
122 /**
123  * fail_unless_equals_int:
124  * @a: a #gint value or expression
125  * @b: a #gint value or expression
126  *
127  * This macro checks that @a and @b are equal and aborts if this is not the
128  * case, printing both expressions and the values they evaluated to. This
129  * macro is for use in unit tests.
130  */
131 #define fail_unless_equals_int(a, b)                                    \
132 G_STMT_START {                                                          \
133   int first = a;                                                        \
134   int second = b;                                                       \
135   fail_unless(first == second,                                          \
136     "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second);       \
137 } G_STMT_END;
138 /**
139  * assert_equals_int:
140  * @a: a #gint value or expression
141  * @b: a #gint value or expression
142  *
143  * This macro checks that @a and @b are equal and aborts if this is not the
144  * case, printing both expressions and the values they evaluated to. This
145  * macro is for use in unit tests.
146  */
147 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
149 /**
150  * fail_unless_equals_int64:
151  * @a: a #gint64 value or expression
152  * @b: a #gint64 value or expression
153  *
154  * This macro checks that @a and @b are equal and aborts if this is not the
155  * case, printing both expressions and the values they evaluated to. This
156  * macro is for use in unit tests.
157  */
158 #define fail_unless_equals_int64(a, b)                                  \
159 G_STMT_START {                                                          \
160   gint64 first = a;                                                     \
161   gint64 second = b;                                                    \
162   fail_unless(first == second,                                          \
163     "'" #a "' (%" G_GINT64_FORMAT") is not equal to '" #b"' (%"         \
164     G_GINT64_FORMAT")", first, second);                                 \
165 } G_STMT_END;
166 /**
167  * assert_equals_int64:
168  * @a: a #gint64 value or expression
169  * @b: a #gint64 value or expression
170  *
171  * This macro checks that @a and @b are equal and aborts if this is not the
172  * case, printing both expressions and the values they evaluated to. This
173  * macro is for use in unit tests.
174  */
175 #define assert_equals_int64(a, b) fail_unless_equals_int64(a, b)
177 /**
178  * fail_unless_equals_uint64:
179  * @a: a #guint64 value or expression
180  * @b: a #guint64 value or expression
181  *
182  * This macro checks that @a and @b are equal and aborts if this is not the
183  * case, printing both expressions and the values they evaluated to. This
184  * macro is for use in unit tests.
185  */
186 #define fail_unless_equals_uint64(a, b)                                 \
187 G_STMT_START {                                                          \
188   guint64 first = a;                                                    \
189   guint64 second = b;                                                   \
190   fail_unless(first == second,                                          \
191     "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%"       \
192     G_GUINT64_FORMAT ")", first, second);                               \
193 } G_STMT_END;
194 /**
195  * assert_equals_uint64:
196  * @a: a #guint64 value or expression
197  * @b: a #guint64 value or expression
198  *
199  * This macro checks that @a and @b are equal and aborts if this is not the
200  * case, printing both expressions and the values they evaluated to. This
201  * macro is for use in unit tests.
202  */
203 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
205 /**
206  * fail_unless_equals_string:
207  * @a: a string literal or expression
208  * @b: a string literal or expression
209  *
210  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
211  * this is not the case, printing both expressions and the values they
212  * evaluated to. This macro is for use in unit tests.
213  */
214 #define fail_unless_equals_string(a, b)                             \
215 G_STMT_START {                                                      \
216   const gchar * first = a;                                          \
217   const gchar * second = b;                                         \
218   fail_unless(g_strcmp0 (first, second) == 0,                          \
219     "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second);   \
220 } G_STMT_END;
221 /**
222  * assert_equals_string:
223  * @a: a string literal or expression
224  * @b: a string literal or expression
225  *
226  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
227  * this is not the case, printing both expressions and the values they
228  * evaluated to. This macro is for use in unit tests.
229  */
230 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
232 /**
233  * fail_unless_equals_float:
234  * @a: a #gdouble or #gfloat value or expression
235  * @b: a #gdouble or #gfloat value or expression
236  *
237  * This macro checks that @a and @b are (almost) equal and aborts if this
238  * is not the case, printing both expressions and the values they evaluated
239  * to. This macro is for use in unit tests.
240  *
241  * Since: 0.10.14
242  */
243 #define fail_unless_equals_float(a, b)                            \
244 G_STMT_START {                                                    \
245   double first = a;                                               \
246   double second = b;                                              \
247   /* This will only work for 'normal' values and values around 0, \
248    * which should be good enough for our purposes here */         \
249   fail_unless(fabs (first - second) < 0.0000001,                  \
250     "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
251 } G_STMT_END;
253 /**
254  * assert_equals_float:
255  * @a: a #gdouble or #gfloat value or expression
256  * @b: a #gdouble or #gfloat value or expression
257  *
258  * This macro checks that @a and @b are (almost) equal and aborts if this
259  * is not the case, printing both expressions and the values they evaluated
260  * to. This macro is for use in unit tests.
261  *
262  * Since: 0.10.14
263  */
264 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
267 /***
268  * thread test macros and variables
269  */
270 extern GList *thread_list;
271 extern GMutex *mutex;
272 extern GCond *start_cond;       /* used to notify main thread of thread startups */
273 extern GCond *sync_cond;        /* used to synchronize all threads and main thread */
275 #define MAIN_START_THREADS(count, function, data)               \
276 MAIN_INIT();                                                    \
277 MAIN_START_THREAD_FUNCTIONS(count, function, data);             \
278 MAIN_SYNCHRONIZE();
280 #if GLIB_CHECK_VERSION (2, 31, 0)
281 #define g_thread_create gst_g_thread_create
282 static inline GThread *
283 gst_g_thread_create (GThreadFunc func, gpointer data, gboolean joinable,
284     GError **error)
286   g_assert (joinable);
287   return g_thread_try_new ("gst-check", func, data, error);
289 #endif
291 #define MAIN_INIT()                     \
292 G_STMT_START {                          \
293   _gst_check_threads_running = TRUE;    \
294                                         \
295   if (mutex == NULL) {                  \
296     mutex = g_mutex_new ();             \
297     start_cond = g_cond_new ();         \
298     sync_cond = g_cond_new ();          \
299   }                                     \
300 } G_STMT_END;
302 #define MAIN_START_THREAD_FUNCTIONS(count, function, data)      \
303 G_STMT_START {                                                  \
304   int i;                                                        \
305   for (i = 0; i < count; ++i) {                                 \
306     MAIN_START_THREAD_FUNCTION (i, function, data);             \
307   }                                                             \
308 } G_STMT_END;
310 #define MAIN_START_THREAD_FUNCTION(i, function, data)           \
311 G_STMT_START {                                                  \
312     GThread *thread = NULL;                                     \
313     GST_DEBUG ("MAIN: creating thread %d", i);                  \
314     g_mutex_lock (mutex);                                       \
315     thread = g_thread_create ((GThreadFunc) function, data,     \
316         TRUE, NULL);                                            \
317     /* wait for thread to signal us that it's ready */          \
318     GST_DEBUG ("MAIN: waiting for thread %d", i);               \
319     g_cond_wait (start_cond, mutex);                            \
320     g_mutex_unlock (mutex);                                     \
321                                                                 \
322     thread_list = g_list_append (thread_list, thread);          \
323 } G_STMT_END;
326 #define MAIN_SYNCHRONIZE()              \
327 G_STMT_START {                          \
328   GST_DEBUG ("MAIN: synchronizing");    \
329   g_cond_broadcast (sync_cond);         \
330   GST_DEBUG ("MAIN: synchronized");     \
331 } G_STMT_END;
333 #define MAIN_STOP_THREADS()                                     \
334 G_STMT_START {                                                  \
335   _gst_check_threads_running = FALSE;                           \
336                                                                 \
337   /* join all threads */                                        \
338   GST_DEBUG ("MAIN: joining");                                  \
339   g_list_foreach (thread_list, (GFunc) g_thread_join, NULL);    \
340   g_list_free (thread_list);                                    \
341   thread_list = NULL;                                           \
342   GST_DEBUG ("MAIN: joined");                                   \
343 } G_STMT_END;
345 #define THREAD_START()                                          \
346 THREAD_STARTED();                                               \
347 THREAD_SYNCHRONIZE();
349 #define THREAD_STARTED()                                        \
350 G_STMT_START {                                                  \
351   /* signal main thread that we started */                      \
352   GST_DEBUG ("THREAD %p: started", g_thread_self ());           \
353   g_mutex_lock (mutex);                                         \
354   g_cond_signal (start_cond);                                   \
355 } G_STMT_END;
357 #define THREAD_SYNCHRONIZE()                                    \
358 G_STMT_START {                                                  \
359   /* synchronize everyone */                                    \
360   GST_DEBUG ("THREAD %p: syncing", g_thread_self ());           \
361   g_cond_wait (sync_cond, mutex);                               \
362   GST_DEBUG ("THREAD %p: synced", g_thread_self ());            \
363   g_mutex_unlock (mutex);                                       \
364 } G_STMT_END;
366 #define THREAD_SWITCH()                                         \
367 G_STMT_START {                                                  \
368   /* a minimal sleep is a context switch */                     \
369   g_usleep (1);                                                 \
370 } G_STMT_END;
372 #define THREAD_TEST_RUNNING()   (_gst_check_threads_running == TRUE)
374 /* additional assertions */
375 #define ASSERT_CRITICAL(code)                                   \
376 G_STMT_START {                                                  \
377   _gst_check_expecting_log = TRUE;                              \
378   _gst_check_raised_critical = FALSE;                           \
379   code;                                                         \
380   _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
381                 "Expected g_critical, got nothing", NULL);      \
382   _gst_check_expecting_log = FALSE;                             \
383 } G_STMT_END
385 #define ASSERT_WARNING(code)                                    \
386 G_STMT_START {                                                  \
387   _gst_check_expecting_log = TRUE;                              \
388   _gst_check_raised_warning = FALSE;                            \
389   code;                                                         \
390   _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__,  \
391                 "Expected g_warning, got nothing", NULL);       \
392   _gst_check_expecting_log = FALSE;                             \
393 } G_STMT_END
396 #define ASSERT_OBJECT_REFCOUNT(object, name, value)             \
397 G_STMT_START {                                                  \
398   int rc;                                                       \
399   rc = GST_OBJECT_REFCOUNT_VALUE (object);                      \
400   fail_unless (rc == value,                                     \
401       "%s (%p) refcount is %d instead of %d",                   \
402       name, object, rc, value);                                 \
403 } G_STMT_END
405 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper)      \
406 G_STMT_START {                                                          \
407   int rc = GST_OBJECT_REFCOUNT_VALUE (object);                          \
408   int lo = lower;                                                       \
409   int hi = upper;                                                       \
410                                                                         \
411   fail_unless (rc >= lo,                                                \
412       "%s (%p) refcount %d is smaller than %d",                         \
413       name, object, rc, lo);                                            \
414   fail_unless (rc <= hi,                                                \
415       "%s (%p) refcount %d is bigger than %d",                          \
416       name, object, rc, hi);                                            \
417 } G_STMT_END
420 #define ASSERT_CAPS_REFCOUNT(caps, name, value)                 \
421         ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
423 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value)             \
424         ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
426 #define ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)          \
427 G_STMT_START {                                                  \
428   int rc;                                                       \
429   rc = GST_MINI_OBJECT_REFCOUNT_VALUE (caps);                   \
430   fail_unless (rc == value,                                     \
431                name " refcount is %d instead of %d", rc, value);\
432 } G_STMT_END
434 #define ASSERT_SET_STATE(element, state, ret)                   \
435 fail_unless (gst_element_set_state (element,                    \
436   state) == ret,                                                \
437   "could not change state to " #state);
439 #define GST_CHECK_MAIN(name)                                    \
440 int main (int argc, char **argv)                                \
441 {                                                               \
442   Suite *s;                                                     \
443   gst_check_init (&argc, &argv);                                \
444   s = name ## _suite ();                                        \
445   return gst_check_run_suite (s, # name, __FILE__);             \
448 /* Hack to allow run-time selection of unit tests to run via the
449  * GST_CHECKS environment variable (test function names, comma-separated) */
451 gboolean _gst_check_run_test_func (const gchar * func_name);
453 static inline void
454 __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
455     int allowed_exit_value, int start, int end)
457   if (_gst_check_run_test_func (fname)) {
458     _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
459   }
462 #define _tcase_add_test __gst_tcase_add_test
464 G_END_DECLS
466 #endif /* __GST_CHECK_H__ */