Branch data Line data Source code
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 : : */
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 : : G_BEGIN_DECLS
37 : :
38 : : GST_DEBUG_CATEGORY_EXTERN (check_debug);
39 : : #define GST_CAT_DEFAULT check_debug
40 : :
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;
50 : :
51 : : /* global variables used in test methods */
52 : : extern GList * buffers;
53 : :
54 : : extern GMutex *check_mutex;
55 : : extern GCond *check_cond;
56 : :
57 : : typedef struct
58 : : {
59 : : const char *name;
60 : : int size;
61 : : int abi_size;
62 : : }
63 : : GstCheckABIStruct;
64 : :
65 : : void gst_check_init (int *argc, char **argv[]);
66 : :
67 : : GstFlowReturn gst_check_chain_func (GstPad * pad, GstBuffer * buffer);
68 : :
69 : : void gst_check_message_error (GstMessage * message, GstMessageType type,
70 : : GQuark domain, gint code);
71 : :
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);
94 : :
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)
99 : :
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__);
116 : :
117 : : #define GST_END_TEST GST_LOG ("cleaning up tasks"); \
118 : : gst_task_cleanup_all (); \
119 : : END_TEST
120 : :
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)
148 : :
149 : : /**
150 : : * fail_unless_equals_uint64:
151 : : * @a: a #guint64 value or expression
152 : : * @b: a #guint64 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_uint64(a, b) \
159 : : G_STMT_START { \
160 : : guint64 first = a; \
161 : : guint64 second = b; \
162 : : fail_unless(first == second, \
163 : : "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%" \
164 : : G_GUINT64_FORMAT ")", first, second); \
165 : : } G_STMT_END;
166 : : /**
167 : : * assert_equals_uint64:
168 : : * @a: a #guint64 value or expression
169 : : * @b: a #guint64 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_uint64(a, b) fail_unless_equals_uint64(a, b)
176 : :
177 : : /**
178 : : * fail_unless_equals_string:
179 : : * @a: a string literal or expression
180 : : * @b: a string literal or expression
181 : : *
182 : : * This macro checks that @a and @b are equal (as per strcmp) and aborts if
183 : : * this is not the case, printing both expressions and the values they
184 : : * evaluated to. This macro is for use in unit tests.
185 : : */
186 : : #define fail_unless_equals_string(a, b) \
187 : : G_STMT_START { \
188 : : const gchar * first = a; \
189 : : const gchar * second = b; \
190 : : fail_unless(g_strcmp0 (first, second) == 0, \
191 : : "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second); \
192 : : } G_STMT_END;
193 : : /**
194 : : * assert_equals_string:
195 : : * @a: a string literal or expression
196 : : * @b: a string literal or expression
197 : : *
198 : : * This macro checks that @a and @b are equal (as per strcmp) and aborts if
199 : : * this is not the case, printing both expressions and the values they
200 : : * evaluated to. This macro is for use in unit tests.
201 : : */
202 : : #define assert_equals_string(a, b) fail_unless_equals_string(a, b)
203 : :
204 : : /**
205 : : * fail_unless_equals_float:
206 : : * @a: a #gdouble or #gfloat value or expression
207 : : * @b: a #gdouble or #gfloat value or expression
208 : : *
209 : : * This macro checks that @a and @b are (almost) equal and aborts if this
210 : : * is not the case, printing both expressions and the values they evaluated
211 : : * to. This macro is for use in unit tests.
212 : : *
213 : : * Since: 0.10.14
214 : : */
215 : : #define fail_unless_equals_float(a, b) \
216 : : G_STMT_START { \
217 : : double first = a; \
218 : : double second = b; \
219 : : /* This will only work for 'normal' values and values around 0, \
220 : : * which should be good enough for our purposes here */ \
221 : : fail_unless(fabs (first - second) < 0.0000001, \
222 : : "'" #a "' (%g) is not equal to '" #b "' (%g)", first, second);\
223 : : } G_STMT_END;
224 : :
225 : : /**
226 : : * assert_equals_float:
227 : : * @a: a #gdouble or #gfloat value or expression
228 : : * @b: a #gdouble or #gfloat value or expression
229 : : *
230 : : * This macro checks that @a and @b are (almost) equal and aborts if this
231 : : * is not the case, printing both expressions and the values they evaluated
232 : : * to. This macro is for use in unit tests.
233 : : *
234 : : * Since: 0.10.14
235 : : */
236 : : #define assert_equals_float(a, b) fail_unless_equals_float(a, b)
237 : :
238 : :
239 : : /***
240 : : * thread test macros and variables
241 : : */
242 : : extern GList *thread_list;
243 : : extern GMutex *mutex;
244 : : extern GCond *start_cond; /* used to notify main thread of thread startups */
245 : : extern GCond *sync_cond; /* used to synchronize all threads and main thread */
246 : :
247 : : #define MAIN_START_THREADS(count, function, data) \
248 : : MAIN_INIT(); \
249 : : MAIN_START_THREAD_FUNCTIONS(count, function, data); \
250 : : MAIN_SYNCHRONIZE();
251 : :
252 : : #define MAIN_INIT() \
253 : : G_STMT_START { \
254 : : _gst_check_threads_running = TRUE; \
255 : : \
256 : : if (mutex == NULL) { \
257 : : mutex = g_mutex_new (); \
258 : : start_cond = g_cond_new (); \
259 : : sync_cond = g_cond_new (); \
260 : : } \
261 : : } G_STMT_END;
262 : :
263 : : #define MAIN_START_THREAD_FUNCTIONS(count, function, data) \
264 : : G_STMT_START { \
265 : : int i; \
266 : : for (i = 0; i < count; ++i) { \
267 : : MAIN_START_THREAD_FUNCTION (i, function, data); \
268 : : } \
269 : : } G_STMT_END;
270 : :
271 : : #define MAIN_START_THREAD_FUNCTION(i, function, data) \
272 : : G_STMT_START { \
273 : : GThread *thread = NULL; \
274 : : GST_DEBUG ("MAIN: creating thread %d", i); \
275 : : g_mutex_lock (mutex); \
276 : : thread = g_thread_create ((GThreadFunc) function, data, \
277 : : TRUE, NULL); \
278 : : /* wait for thread to signal us that it's ready */ \
279 : : GST_DEBUG ("MAIN: waiting for thread %d", i); \
280 : : g_cond_wait (start_cond, mutex); \
281 : : g_mutex_unlock (mutex); \
282 : : \
283 : : thread_list = g_list_append (thread_list, thread); \
284 : : } G_STMT_END;
285 : :
286 : :
287 : : #define MAIN_SYNCHRONIZE() \
288 : : G_STMT_START { \
289 : : GST_DEBUG ("MAIN: synchronizing"); \
290 : : g_cond_broadcast (sync_cond); \
291 : : GST_DEBUG ("MAIN: synchronized"); \
292 : : } G_STMT_END;
293 : :
294 : : #define MAIN_STOP_THREADS() \
295 : : G_STMT_START { \
296 : : _gst_check_threads_running = FALSE; \
297 : : \
298 : : /* join all threads */ \
299 : : GST_DEBUG ("MAIN: joining"); \
300 : : g_list_foreach (thread_list, (GFunc) g_thread_join, NULL); \
301 : : g_list_free (thread_list); \
302 : : thread_list = NULL; \
303 : : GST_DEBUG ("MAIN: joined"); \
304 : : } G_STMT_END;
305 : :
306 : : #define THREAD_START() \
307 : : THREAD_STARTED(); \
308 : : THREAD_SYNCHRONIZE();
309 : :
310 : : #define THREAD_STARTED() \
311 : : G_STMT_START { \
312 : : /* signal main thread that we started */ \
313 : : GST_DEBUG ("THREAD %p: started", g_thread_self ()); \
314 : : g_mutex_lock (mutex); \
315 : : g_cond_signal (start_cond); \
316 : : } G_STMT_END;
317 : :
318 : : #define THREAD_SYNCHRONIZE() \
319 : : G_STMT_START { \
320 : : /* synchronize everyone */ \
321 : : GST_DEBUG ("THREAD %p: syncing", g_thread_self ()); \
322 : : g_cond_wait (sync_cond, mutex); \
323 : : GST_DEBUG ("THREAD %p: synced", g_thread_self ()); \
324 : : g_mutex_unlock (mutex); \
325 : : } G_STMT_END;
326 : :
327 : : #define THREAD_SWITCH() \
328 : : G_STMT_START { \
329 : : /* a minimal sleep is a context switch */ \
330 : : g_usleep (1); \
331 : : } G_STMT_END;
332 : :
333 : : #define THREAD_TEST_RUNNING() (_gst_check_threads_running == TRUE)
334 : :
335 : : /* additional assertions */
336 : : #define ASSERT_CRITICAL(code) \
337 : : G_STMT_START { \
338 : : _gst_check_expecting_log = TRUE; \
339 : : _gst_check_raised_critical = FALSE; \
340 : : code; \
341 : : _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
342 : : "Expected g_critical, got nothing", NULL); \
343 : : _gst_check_expecting_log = FALSE; \
344 : : } G_STMT_END
345 : :
346 : : #define ASSERT_WARNING(code) \
347 : : G_STMT_START { \
348 : : _gst_check_expecting_log = TRUE; \
349 : : _gst_check_raised_warning = FALSE; \
350 : : code; \
351 : : _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__, \
352 : : "Expected g_warning, got nothing", NULL); \
353 : : _gst_check_expecting_log = FALSE; \
354 : : } G_STMT_END
355 : :
356 : :
357 : : #define ASSERT_OBJECT_REFCOUNT(object, name, value) \
358 : : G_STMT_START { \
359 : : int rc; \
360 : : rc = GST_OBJECT_REFCOUNT_VALUE (object); \
361 : : fail_unless (rc == value, \
362 : : "%s (%p) refcount is %d instead of %d", \
363 : : name, object, rc, value); \
364 : : } G_STMT_END
365 : :
366 : : #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper) \
367 : : G_STMT_START { \
368 : : int rc = GST_OBJECT_REFCOUNT_VALUE (object); \
369 : : int lo = lower; \
370 : : int hi = upper; \
371 : : \
372 : : fail_unless (rc >= lo, \
373 : : "%s (%p) refcount %d is smaller than %d", \
374 : : name, object, rc, lo); \
375 : : fail_unless (rc <= hi, \
376 : : "%s (%p) refcount %d is bigger than %d", \
377 : : name, object, rc, hi); \
378 : : } G_STMT_END
379 : :
380 : :
381 : : #define ASSERT_CAPS_REFCOUNT(caps, name, value) \
382 : : ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
383 : :
384 : : #define ASSERT_BUFFER_REFCOUNT(buffer, name, value) \
385 : : ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
386 : :
387 : : #define ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value) \
388 : : G_STMT_START { \
389 : : int rc; \
390 : : rc = GST_MINI_OBJECT_REFCOUNT_VALUE (caps); \
391 : : fail_unless (rc == value, \
392 : : name " refcount is %d instead of %d", rc, value);\
393 : : } G_STMT_END
394 : :
395 : : #define ASSERT_SET_STATE(element, state, ret) \
396 : : fail_unless (gst_element_set_state (element, \
397 : : state) == ret, \
398 : : "could not change state to " #state);
399 : :
400 : : #define GST_CHECK_MAIN(name) \
401 : : int main (int argc, char **argv) \
402 : : { \
403 : : Suite *s; \
404 : : gst_check_init (&argc, &argv); \
405 : : s = name ## _suite (); \
406 : : return gst_check_run_suite (s, # name, __FILE__); \
407 : : }
408 : :
409 : : /* Hack to allow run-time selection of unit tests to run via the
410 : : * GST_CHECKS environment variable (test function names, comma-separated) */
411 : :
412 : : gboolean _gst_check_run_test_func (const gchar * func_name);
413 : :
414 : : static inline void
415 : 6830 : __gst_tcase_add_test (TCase * tc, TFun tf, const char * fname, int signal,
416 : : int allowed_exit_value, int start, int end)
417 : : {
418 [ + - ]: 6830 : if (_gst_check_run_test_func (fname)) {
419 : 6830 : _tcase_add_test (tc, tf, fname, signal, allowed_exit_value, start, end);
420 : : }
421 : 6830 : }
422 : :
423 : : #define _tcase_add_test __gst_tcase_add_test
424 : :
425 : : G_END_DECLS
426 : :
427 : : #endif /* __GST_CHECK_H__ */
|