]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - libs/gst/check/gstcheck.h
Add more missing docs.
[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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
23 #ifndef __GST_CHECK_H__
24 #define __GST_CHECK_H__
26 #include <signal.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <math.h>
31 #include <check.h>
33 #include <gst/gst.h>
35 G_BEGIN_DECLS
37 GST_DEBUG_CATEGORY_EXTERN (check_debug);
38 #define GST_CAT_DEFAULT check_debug
40 /* logging function for tests
41  * a test uses g_message() to log a debug line
42  * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
43  * messages
44  */
45 extern gboolean _gst_check_threads_running;
46 extern gboolean _gst_check_raised_critical;
47 extern gboolean _gst_check_raised_warning;
48 extern gboolean _gst_check_expecting_log;
50 /* global variables used in test methods */
51 GList * buffers;
53 GMutex *check_mutex;
54 GCond *check_cond;
56 typedef struct
57 {
58   char *name;
59   int size;
60   int abi_size;
61 }
62 GstCheckABIStruct;
64 void gst_check_init (int *argc, char **argv[]);
66 GstFlowReturn gst_check_chain_func (GstPad *pad, GstBuffer *buffer);
68 void gst_check_message_error (GstMessage *message, GstMessageType type, GQuark domain, gint code);
70 GstElement * gst_check_setup_element (const gchar *factory);
71 void gst_check_teardown_element (GstElement *element);
72 GstPad * gst_check_setup_src_pad (GstElement *element,
73     GstStaticPadTemplate *template, GstCaps *caps);
74 void gst_check_teardown_src_pad (GstElement *element);
75 GstPad * gst_check_setup_sink_pad (GstElement *element,
76     GstStaticPadTemplate *template, GstCaps *caps);
77 void gst_check_teardown_sink_pad (GstElement *element);
78 void gst_check_abi_list (GstCheckABIStruct list[], gboolean have_abi_sizes);
79 gint gst_check_run_suite (Suite *suite, const gchar *name, const gchar *fname);
81 #define fail_unless_message_error(msg, domain, code)            \
82 gst_check_message_error (msg, GST_MESSAGE_ERROR,                \
83   GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
84 #define assert_message_error(m, d, c) fail_unless_message_error(m, d, c)
86 /**
87  * GST_START_TEST:
88  * @__testname: test function name
89  *
90  * wrapper for checks START_TEST
91  */
92 /**
93  * GST_END_TEST:
94  *
95  * wrapper for checks END_TEST
96  */
97 #if CHECK_MAJOR_VERSION >= 0 && CHECK_MINOR_VERSION >= 9 && CHECK_MICRO_VERSION >= 4
98 #define GST_START_TEST(__testname) \
99 static void __testname (int __i__)\
100 {\
101   GST_DEBUG ("test start"); \
102   tcase_fn_start (""# __testname, __FILE__, __LINE__);
104 #define GST_END_TEST END_TEST
105 #else
106 #define GST_START_TEST(__testname) \
107 static void __testname ()\
108 {\
109   GST_DEBUG ("test start"); \
110   tcase_fn_start (""# __testname, __FILE__, __LINE__);
112 #define GST_END_TEST END_TEST
113 #endif
116 /* additional fail macros */
117 /**
118  * fail_unless_equals_int:
119  * @a: a #gint value or expression
120  * @b: a #gint value or expression
121  *
122  * This macro checks that @a and @b are equal and aborts if this is not the
123  * case, printing both expressions and the values they evaluated to. This
124  * macro is for use in unit tests.
125  */
126 #define fail_unless_equals_int(a, b)                                    \
127 G_STMT_START {                                                          \
128   int first = a;                                                        \
129   int second = b;                                                       \
130   fail_unless(first == second,                                          \
131     "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second);       \
132 } G_STMT_END;
133 /**
134  * assert_equals_int:
135  * @a: a #gint value or expression
136  * @b: a #gint value or expression
137  *
138  * This macro checks that @a and @b are equal and aborts if this is not the
139  * case, printing both expressions and the values they evaluated to. This
140  * macro is for use in unit tests.
141  */
142 #define assert_equals_int(a, b) fail_unless_equals_int(a, b)
144 /**
145  * fail_unless_equals_uint64:
146  * @a: a #guint64 value or expression
147  * @b: a #guint64 value or expression
148  *
149  * This macro checks that @a and @b are equal and aborts if this is not the
150  * case, printing both expressions and the values they evaluated to. This
151  * macro is for use in unit tests.
152  */
153 #define fail_unless_equals_uint64(a, b)                                 \
154 G_STMT_START {                                                          \
155   guint64 first = a;                                                    \
156   guint64 second = b;                                                   \
157   fail_unless(first == second,                                          \
158     "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%"       \
159     G_GUINT64_FORMAT ")", first, second);                               \
160 } G_STMT_END;
161 /**
162  * assert_equals_uint64:
163  * @a: a #guint64 value or expression
164  * @b: a #guint64 value or expression
165  *
166  * This macro checks that @a and @b are equal and aborts if this is not the
167  * case, printing both expressions and the values they evaluated to. This
168  * macro is for use in unit tests.
169  */
170 #define assert_equals_uint64(a, b) fail_unless_equals_uint64(a, b)
172 /**
173  * fail_unless_equals_string:
174  * @a: a string literal or expression
175  * @b: a string literal or expression
176  *
177  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
178  * this is not the case, printing both expressions and the values they
179  * evaluated to. This macro is for use in unit tests.
180  */
181 #define fail_unless_equals_string(a, b)                             \
182 G_STMT_START {                                                      \
183   const gchar * first = a;                                          \
184   const gchar * second = b;                                         \
185   fail_unless(strcmp (first, second) == 0,                          \
186     "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second);   \
187 } G_STMT_END;
188 /**
189  * assert_equals_string:
190  * @a: a string literal or expression
191  * @b: a string literal or expression
192  *
193  * This macro checks that @a and @b are equal (as per strcmp) and aborts if
194  * this is not the case, printing both expressions and the values they
195  * evaluated to. This macro is for use in unit tests.
196  */
197 #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
199 /**
200  * fail_unless_equals_float:
201  * @a: a #gdouble or #gfloat value or expression
202  * @b: a #gdouble or #gfloat value or expression
203  *
204  * This macro checks that @a and @b are (almost) equal and aborts if this
205  * is not the case, printing both expressions and the values they evaluated
206  * to. This macro is for use in unit tests.
207  *
208  * Since: 0.10.14
209  */
210 #define fail_unless_equals_float(a, b)                            \
211 G_STMT_START {                                                    \
212   double first = a;                                               \
213   double second = b;                                              \
214   /* This will only work for 'normal' values and values around 0, \
215    * which should be good enough for our purposes here */         \
216   fail_unless(fabs (first - second) < 0.0000001,                  \
217     "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
218 } G_STMT_END;
220 /**
221  * assert_equals_float:
222  * @a: a #gdouble or #gfloat value or expression
223  * @b: a #gdouble or #gfloat value or expression
224  *
225  * This macro checks that @a and @b are (almost) equal and aborts if this
226  * is not the case, printing both expressions and the values they evaluated
227  * to. This macro is for use in unit tests.
228  *
229  * Since: 0.10.14
230  */
231 #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
234 /***
235  * thread test macros and variables
236  */
237 extern GList *thread_list;
238 extern GMutex *mutex;
239 extern GCond *start_cond;       /* used to notify main thread of thread startups */
240 extern GCond *sync_cond;        /* used to synchronize all threads and main thread */
242 #define MAIN_START_THREADS(count, function, data)               \
243 MAIN_INIT();                                                    \
244 MAIN_START_THREAD_FUNCTIONS(count, function, data);             \
245 MAIN_SYNCHRONIZE();
247 #define MAIN_INIT()                     \
248 G_STMT_START {                          \
249   _gst_check_threads_running = TRUE;    \
250                                         \
251   mutex = g_mutex_new ();               \
252   start_cond = g_cond_new ();           \
253   sync_cond = g_cond_new ();            \
254 } G_STMT_END;
256 #define MAIN_START_THREAD_FUNCTIONS(count, function, data)      \
257 G_STMT_START {                                                  \
258   int i;                                                        \
259   for (i = 0; i < count; ++i) {                                 \
260     MAIN_START_THREAD_FUNCTION (i, function, data);             \
261   }                                                             \
262 } G_STMT_END;
264 #define MAIN_START_THREAD_FUNCTION(i, function, data)           \
265 G_STMT_START {                                                  \
266     GThread *thread = NULL;                                     \
267     GST_DEBUG ("MAIN: creating thread %d", i);                  \
268     g_mutex_lock (mutex);                                       \
269     thread = g_thread_create ((GThreadFunc) function, data,     \
270         TRUE, NULL);                                            \
271     /* wait for thread to signal us that it's ready */          \
272     GST_DEBUG ("MAIN: waiting for thread %d", i);               \
273     g_cond_wait (start_cond, mutex);                            \
274     g_mutex_unlock (mutex);                                     \
275                                                                 \
276     thread_list = g_list_append (thread_list, thread);          \
277 } G_STMT_END;
280 #define MAIN_SYNCHRONIZE()              \
281 G_STMT_START {                          \
282   GST_DEBUG ("MAIN: synchronizing");    \
283   g_cond_broadcast (sync_cond);         \
284   GST_DEBUG ("MAIN: synchronized");     \
285 } G_STMT_END;
287 #define MAIN_STOP_THREADS()                                     \
288 G_STMT_START {                                                  \
289   _gst_check_threads_running = FALSE;                           \
290                                                                 \
291   /* join all threads */                                        \
292   GST_DEBUG ("MAIN: joining");                                  \
293   g_list_foreach (thread_list, (GFunc) g_thread_join, NULL);    \
294   GST_DEBUG ("MAIN: joined");                                   \
295 } G_STMT_END;
297 #define THREAD_START()                                          \
298 THREAD_STARTED();                                               \
299 THREAD_SYNCHRONIZE();
301 #define THREAD_STARTED()                                        \
302 G_STMT_START {                                                  \
303   /* signal main thread that we started */                      \
304   GST_DEBUG ("THREAD %p: started", g_thread_self ());           \
305   g_mutex_lock (mutex);                                         \
306   g_cond_signal (start_cond);                                   \
307 } G_STMT_END;
309 #define THREAD_SYNCHRONIZE()                                    \
310 G_STMT_START {                                                  \
311   /* synchronize everyone */                                    \
312   GST_DEBUG ("THREAD %p: syncing", g_thread_self ());           \
313   g_cond_wait (sync_cond, mutex);                               \
314   GST_DEBUG ("THREAD %p: synced", g_thread_self ());            \
315   g_mutex_unlock (mutex);                                       \
316 } G_STMT_END;
318 #define THREAD_SWITCH()                                         \
319 G_STMT_START {                                                  \
320   /* a minimal sleep is a context switch */                     \
321   g_usleep (1);                                                 \
322 } G_STMT_END;
324 #define THREAD_TEST_RUNNING()   (_gst_check_threads_running == TRUE)
326 /* additional assertions */
327 #define ASSERT_CRITICAL(code)                                   \
328 G_STMT_START {                                                  \
329   _gst_check_expecting_log = TRUE;                              \
330   _gst_check_raised_critical = FALSE;                           \
331   code;                                                         \
332   _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
333                 "Expected g_critical, got nothing", NULL);      \
334   _gst_check_expecting_log = FALSE;                             \
335 } G_STMT_END
337 #define ASSERT_WARNING(code)                                    \
338 G_STMT_START {                                                  \
339   _gst_check_expecting_log = TRUE;                              \
340   _gst_check_raised_warning = FALSE;                            \
341   code;                                                         \
342   _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__,  \
343                 "Expected g_warning, got nothing", NULL);       \
344   _gst_check_expecting_log = FALSE;                             \
345 } G_STMT_END
348 #define ASSERT_OBJECT_REFCOUNT(object, name, value)             \
349 G_STMT_START {                                                  \
350   int rc;                                                       \
351   rc = GST_OBJECT_REFCOUNT_VALUE (object);                      \
352   fail_unless (rc == value,                                     \
353       "%s (%p) refcount is %d instead of %d",                   \
354       name, object, rc, value);                                 \
355 } G_STMT_END
357 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper)      \
358 G_STMT_START {                                                          \
359   int rc = GST_OBJECT_REFCOUNT_VALUE (object);                          \
360   int lo = lower;                                                       \
361   int hi = upper;                                                       \
362                                                                         \
363   fail_unless (rc >= lo,                                                \
364       "%s (%p) refcount %d is smaller than %d",                         \
365       name, object, rc, lo);                                            \
366   fail_unless (rc <= hi,                                                \
367       "%s (%p) refcount %d is bigger than %d",                          \
368       name, object, rc, hi);                                            \
369 } G_STMT_END
372 #define ASSERT_CAPS_REFCOUNT(caps, name, value)                 \
373         ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
375 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value)             \
376         ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
378 #define ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)          \
379 G_STMT_START {                                                  \
380   int rc;                                                       \
381   rc = GST_MINI_OBJECT_REFCOUNT_VALUE (caps);                   \
382   fail_unless (rc == value,                                     \
383                name " refcount is %d instead of %d", rc, value);\
384 } G_STMT_END
386 #define ASSERT_SET_STATE(element, state, ret)                   \
387 fail_unless (gst_element_set_state (element,                    \
388   state) == ret,                                                \
389   "could not change state to " #state);
391 #define GST_CHECK_MAIN(name)                                    \
392 int main (int argc, char **argv)                                \
393 {                                                               \
394   Suite *s = name ## _suite ();                                 \
395   gst_check_init (&argc, &argv);                                \
396   return gst_check_run_suite (s, # name, __FILE__);             \
399 G_END_DECLS
401 #endif /* __GST_CHECK_H__ */