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>
30 #include <check.h>
32 #include <gst/gst.h>
34 GST_DEBUG_CATEGORY_EXTERN (check_debug);
35 #define GST_CAT_DEFAULT check_debug
37 /* logging function for tests
38 * a test uses g_message() to log a debug line
39 * a gst unit test can be run with GST_TEST_DEBUG env var set to see the
40 * messages
41 */
42 extern gboolean _gst_check_threads_running;
43 extern gboolean _gst_check_raised_critical;
44 extern gboolean _gst_check_raised_warning;
45 extern gboolean _gst_check_expecting_log;
47 /* global variables used in test methods */
48 GList * buffers;
50 GMutex *check_mutex;
51 GCond *check_cond;
53 void gst_check_init (int *argc, char **argv[]);
55 GstFlowReturn gst_check_chain_func (GstPad *pad, GstBuffer *buffer);
57 void gst_check_message_error (GstMessage *message, GstMessageType type, GQuark domain, gint code);
59 GstElement * gst_check_setup_element (const gchar *factory);
60 void gst_check_teardown_element (GstElement *element);
61 GstPad * gst_check_setup_src_pad (GstElement *element,
62 GstStaticPadTemplate *template, GstCaps *caps);
63 void gst_check_teardown_src_pad (GstElement *element);
64 GstPad * gst_check_setup_sink_pad (GstElement *element,
65 GstStaticPadTemplate *template, GstCaps *caps);
66 void gst_check_teardown_sink_pad (GstElement *element);
69 #define fail_unless_message_error(msg, domain, code) \
70 gst_check_message_error (msg, GST_MESSAGE_ERROR, \
71 GST_ ## domain ## _ERROR, GST_ ## domain ## _ERROR_ ## code)
73 /***
74 * wrappers for START_TEST and END_TEST
75 */
76 #define GST_START_TEST(__testname) \
77 static void __testname (void)\
78 {\
79 GST_DEBUG ("test start"); \
80 tcase_fn_start (""# __testname, __FILE__, __LINE__);
82 #define GST_END_TEST END_TEST
84 /* additional fail macros */
85 #define fail_unless_equals_int(a, b) \
86 G_STMT_START { \
87 int first = a; \
88 int second = b; \
89 fail_unless(first == second, \
90 "'" #a "' (%d) is not equal to '" #b"' (%d)", first, second); \
91 } G_STMT_END;
93 #define fail_unless_equals_uint64(a, b) \
94 G_STMT_START { \
95 guint64 first = a; \
96 guint64 second = b; \
97 fail_unless(first == second, \
98 "'" #a "' (%" G_GUINT64_FORMAT ") is not equal to '" #b"' (%" \
99 G_GUINT64_FORMAT ")", first, second); \
100 } G_STMT_END;
102 #define fail_unless_equals_string(a, b) \
103 G_STMT_START { \
104 gchar * first = a; \
105 gchar * second = b; \
106 fail_unless(strcmp (first, second) == 0, \
107 "'" #a "' (%s) is not equal to '" #b"' (%s)", first, second); \
108 } G_STMT_END;
111 /***
112 * thread test macros and variables
113 */
114 extern GList *thread_list;
115 extern GMutex *mutex;
116 extern GCond *start_cond; /* used to notify main thread of thread startups */
117 extern GCond *sync_cond; /* used to synchronize all threads and main thread */
119 #define MAIN_START_THREADS(count, function, data) \
120 MAIN_INIT(); \
121 MAIN_START_THREAD_FUNCTIONS(count, function, data); \
122 MAIN_SYNCHRONIZE();
124 #define MAIN_INIT() \
125 G_STMT_START { \
126 _gst_check_threads_running = TRUE; \
127 \
128 mutex = g_mutex_new (); \
129 start_cond = g_cond_new (); \
130 sync_cond = g_cond_new (); \
131 } G_STMT_END;
133 #define MAIN_START_THREAD_FUNCTIONS(count, function, data) \
134 G_STMT_START { \
135 int i; \
136 for (i = 0; i < count; ++i) { \
137 MAIN_START_THREAD_FUNCTION (i, function, data); \
138 } \
139 } G_STMT_END;
141 #define MAIN_START_THREAD_FUNCTION(i, function, data) \
142 G_STMT_START { \
143 GThread *thread = NULL; \
144 GST_DEBUG ("MAIN: creating thread %d", i); \
145 g_mutex_lock (mutex); \
146 thread = g_thread_create ((GThreadFunc) function, data, \
147 TRUE, NULL); \
148 /* wait for thread to signal us that it's ready */ \
149 GST_DEBUG ("MAIN: waiting for thread %d", i); \
150 g_cond_wait (start_cond, mutex); \
151 g_mutex_unlock (mutex); \
152 \
153 thread_list = g_list_append (thread_list, thread); \
154 } G_STMT_END;
157 #define MAIN_SYNCHRONIZE() \
158 G_STMT_START { \
159 GST_DEBUG ("MAIN: synchronizing"); \
160 g_cond_broadcast (sync_cond); \
161 GST_DEBUG ("MAIN: synchronized"); \
162 } G_STMT_END;
164 #define MAIN_STOP_THREADS() \
165 G_STMT_START { \
166 _gst_check_threads_running = FALSE; \
167 \
168 /* join all threads */ \
169 GST_DEBUG ("MAIN: joining"); \
170 g_list_foreach (thread_list, (GFunc) g_thread_join, NULL); \
171 GST_DEBUG ("MAIN: joined"); \
172 } G_STMT_END;
174 #define THREAD_START() \
175 THREAD_STARTED(); \
176 THREAD_SYNCHRONIZE();
178 #define THREAD_STARTED() \
179 G_STMT_START { \
180 /* signal main thread that we started */ \
181 GST_DEBUG ("THREAD %p: started", g_thread_self ()); \
182 g_mutex_lock (mutex); \
183 g_cond_signal (start_cond); \
184 } G_STMT_END;
186 #define THREAD_SYNCHRONIZE() \
187 G_STMT_START { \
188 /* synchronize everyone */ \
189 GST_DEBUG ("THREAD %p: syncing", g_thread_self ()); \
190 g_cond_wait (sync_cond, mutex); \
191 GST_DEBUG ("THREAD %p: synced", g_thread_self ()); \
192 g_mutex_unlock (mutex); \
193 } G_STMT_END;
195 #define THREAD_SWITCH() \
196 G_STMT_START { \
197 /* a minimal sleep is a context switch */ \
198 g_usleep (1); \
199 } G_STMT_END;
201 #define THREAD_TEST_RUNNING() (_gst_check_threads_running == TRUE)
203 /* additional assertions */
204 #define ASSERT_CRITICAL(code) \
205 G_STMT_START { \
206 _gst_check_expecting_log = TRUE; \
207 _gst_check_raised_critical = FALSE; \
208 code; \
209 _fail_unless (_gst_check_raised_critical, __FILE__, __LINE__, \
210 "Expected g_critical, got nothing"); \
211 _gst_check_expecting_log = FALSE; \
212 } G_STMT_END
214 #define ASSERT_WARNING(code) \
215 G_STMT_START { \
216 _gst_check_expecting_log = TRUE; \
217 _gst_check_raised_warning = FALSE; \
218 code; \
219 _fail_unless (_gst_check_raised_warning, __FILE__, __LINE__, \
220 "Expected g_warning, got nothing"); \
221 _gst_check_expecting_log = FALSE; \
222 } G_STMT_END
225 #define ASSERT_OBJECT_REFCOUNT(object, name, value) \
226 G_STMT_START { \
227 int rc; \
228 rc = GST_OBJECT_REFCOUNT_VALUE (object); \
229 fail_unless (rc == value, \
230 "%s (%p) refcount is %d instead of %d", \
231 name, object, rc, value); \
232 } G_STMT_END
234 #define ASSERT_OBJECT_REFCOUNT_BETWEEN(object, name, lower, upper) \
235 G_STMT_START { \
236 int rc = GST_OBJECT_REFCOUNT_VALUE (object); \
237 int lo = lower; \
238 int hi = upper; \
239 \
240 fail_unless (rc >= lo, \
241 "%s (%p) refcount %d is smaller than %d", \
242 name, object, rc, lo); \
243 fail_unless (rc <= hi, \
244 "%s (%p) refcount %d is bigger than %d", \
245 name, object, rc, hi); \
246 } G_STMT_END
249 #define ASSERT_CAPS_REFCOUNT(caps, name, value) \
250 ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value)
252 #define ASSERT_BUFFER_REFCOUNT(buffer, name, value) \
253 ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
255 #define ASSERT_MINI_OBJECT_REFCOUNT(caps, name, value) \
256 G_STMT_START { \
257 int rc; \
258 rc = GST_MINI_OBJECT_REFCOUNT_VALUE (caps); \
259 fail_unless (rc == value, \
260 name " refcount is %d instead of %d", rc, value);\
261 } G_STMT_END
263 #define ASSERT_SET_STATE(element, state, ret) \
264 fail_unless (gst_element_set_state (element, \
265 state) == ret, \
266 "could not change state to " #state);
268 #endif /* __GST_CHECK_H__ */