Merge branch 'macosx-ci' into 'master'
[glib.git] / glib / gtestutils.h
blob1d05b97f451f73455eb98d41d031fda3bcbecb79
1 /* GLib testing utilities
2 * Copyright (C) 2007 Imendio AB
3 * Authors: Tim Janik
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #ifndef __G_TEST_UTILS_H__
20 #define __G_TEST_UTILS_H__
22 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23 #error "Only <glib.h> can be included directly."
24 #endif
26 #include <glib/gmessages.h>
27 #include <glib/gstring.h>
28 #include <glib/gerror.h>
29 #include <glib/gslist.h>
30 #include <string.h>
32 G_BEGIN_DECLS
34 typedef struct GTestCase GTestCase;
35 typedef struct GTestSuite GTestSuite;
36 typedef void (*GTestFunc) (void);
37 typedef void (*GTestDataFunc) (gconstpointer user_data);
38 typedef void (*GTestFixtureFunc) (gpointer fixture,
39 gconstpointer user_data);
41 /* assertion API */
42 #define g_assert_cmpstr(s1, cmp, s2) G_STMT_START { \
43 const char *__s1 = (s1), *__s2 = (s2); \
44 if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
45 g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
46 #s1 " " #cmp " " #s2, __s1, #cmp, __s2); \
47 } G_STMT_END
48 #define g_assert_cmpint(n1, cmp, n2) G_STMT_START { \
49 gint64 __n1 = (n1), __n2 = (n2); \
50 if (__n1 cmp __n2) ; else \
51 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
52 #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'i'); \
53 } G_STMT_END
54 #define g_assert_cmpuint(n1, cmp, n2) G_STMT_START { \
55 guint64 __n1 = (n1), __n2 = (n2); \
56 if (__n1 cmp __n2) ; else \
57 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
58 #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'i'); \
59 } G_STMT_END
60 #define g_assert_cmphex(n1, cmp, n2) G_STMT_START {\
61 guint64 __n1 = (n1), __n2 = (n2); \
62 if (__n1 cmp __n2) ; else \
63 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
64 #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'x'); \
65 } G_STMT_END
66 #define g_assert_cmpfloat(n1,cmp,n2) G_STMT_START { \
67 long double __n1 = (n1), __n2 = (n2); \
68 if (__n1 cmp __n2) ; else \
69 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
70 #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'f'); \
71 } G_STMT_END
72 #define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
73 gconstpointer __m1 = m1, __m2 = m2; \
74 int __l1 = l1, __l2 = l2; \
75 if (__l1 != __l2) \
76 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
77 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", \
78 (long double) __l1, "==", (long double) __l2, 'i'); \
79 else if (__l1 != 0 && memcmp (__m1, __m2, __l1) != 0) \
80 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
81 "assertion failed (" #m1 " == " #m2 ")"); \
82 } G_STMT_END
83 #define g_assert_no_error(err) G_STMT_START { \
84 if (err) \
85 g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
86 #err, err, 0, 0); \
87 } G_STMT_END
88 #define g_assert_error(err, dom, c) G_STMT_START { \
89 if (!err || (err)->domain != dom || (err)->code != c) \
90 g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
91 #err, err, dom, c); \
92 } G_STMT_END
93 #define g_assert_true(expr) G_STMT_START { \
94 if G_LIKELY (expr) ; else \
95 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
96 "'" #expr "' should be TRUE"); \
97 } G_STMT_END
98 #define g_assert_false(expr) G_STMT_START { \
99 if G_LIKELY (!(expr)) ; else \
100 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
101 "'" #expr "' should be FALSE"); \
102 } G_STMT_END
103 #define g_assert_null(expr) G_STMT_START { if G_LIKELY ((expr) == NULL) ; else \
104 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
105 "'" #expr "' should be NULL"); \
106 } G_STMT_END
107 #define g_assert_nonnull(expr) G_STMT_START { \
108 if G_LIKELY ((expr) != NULL) ; else \
109 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
110 "'" #expr "' should not be NULL"); \
111 } G_STMT_END
112 #ifdef G_DISABLE_ASSERT
113 #define g_assert_not_reached() G_STMT_START { (void) 0; } G_STMT_END
114 #define g_assert(expr) G_STMT_START { (void) 0; } G_STMT_END
115 #else /* !G_DISABLE_ASSERT */
116 #define g_assert_not_reached() G_STMT_START { g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } G_STMT_END
117 #define g_assert(expr) G_STMT_START { \
118 if G_LIKELY (expr) ; else \
119 g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
120 #expr); \
121 } G_STMT_END
122 #endif /* !G_DISABLE_ASSERT */
124 GLIB_AVAILABLE_IN_ALL
125 int g_strcmp0 (const char *str1,
126 const char *str2);
128 /* report performance results */
129 GLIB_AVAILABLE_IN_ALL
130 void g_test_minimized_result (double minimized_quantity,
131 const char *format,
132 ...) G_GNUC_PRINTF (2, 3);
133 GLIB_AVAILABLE_IN_ALL
134 void g_test_maximized_result (double maximized_quantity,
135 const char *format,
136 ...) G_GNUC_PRINTF (2, 3);
138 /* initialize testing framework */
139 GLIB_AVAILABLE_IN_ALL
140 void g_test_init (int *argc,
141 char ***argv,
142 ...) G_GNUC_NULL_TERMINATED;
143 /* query testing framework config */
144 #define g_test_initialized() (g_test_config_vars->test_initialized)
145 #define g_test_quick() (g_test_config_vars->test_quick)
146 #define g_test_slow() (!g_test_config_vars->test_quick)
147 #define g_test_thorough() (!g_test_config_vars->test_quick)
148 #define g_test_perf() (g_test_config_vars->test_perf)
149 #define g_test_verbose() (g_test_config_vars->test_verbose)
150 #define g_test_quiet() (g_test_config_vars->test_quiet)
151 #define g_test_undefined() (g_test_config_vars->test_undefined)
152 GLIB_AVAILABLE_IN_2_38
153 gboolean g_test_subprocess (void);
155 /* run all tests under toplevel suite (path: /) */
156 GLIB_AVAILABLE_IN_ALL
157 int g_test_run (void);
158 /* hook up a test functions under test path */
159 GLIB_AVAILABLE_IN_ALL
160 void g_test_add_func (const char *testpath,
161 GTestFunc test_func);
163 GLIB_AVAILABLE_IN_ALL
164 void g_test_add_data_func (const char *testpath,
165 gconstpointer test_data,
166 GTestDataFunc test_func);
168 GLIB_AVAILABLE_IN_2_34
169 void g_test_add_data_func_full (const char *testpath,
170 gpointer test_data,
171 GTestDataFunc test_func,
172 GDestroyNotify data_free_func);
174 /* tell about failure */
175 GLIB_AVAILABLE_IN_2_30
176 void g_test_fail (void);
177 GLIB_AVAILABLE_IN_2_38
178 void g_test_incomplete (const gchar *msg);
179 GLIB_AVAILABLE_IN_2_38
180 void g_test_skip (const gchar *msg);
181 GLIB_AVAILABLE_IN_2_38
182 gboolean g_test_failed (void);
183 GLIB_AVAILABLE_IN_2_38
184 void g_test_set_nonfatal_assertions (void);
186 /* hook up a test with fixture under test path */
187 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
188 G_STMT_START { \
189 void (*add_vtable) (const char*, \
190 gsize, \
191 gconstpointer, \
192 void (*) (Fixture*, gconstpointer), \
193 void (*) (Fixture*, gconstpointer), \
194 void (*) (Fixture*, gconstpointer)) = (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
195 add_vtable \
196 (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
197 } G_STMT_END
199 /* add test messages to the test report */
200 GLIB_AVAILABLE_IN_ALL
201 void g_test_message (const char *format,
202 ...) G_GNUC_PRINTF (1, 2);
203 GLIB_AVAILABLE_IN_ALL
204 void g_test_bug_base (const char *uri_pattern);
205 GLIB_AVAILABLE_IN_ALL
206 void g_test_bug (const char *bug_uri_snippet);
207 /* measure test timings */
208 GLIB_AVAILABLE_IN_ALL
209 void g_test_timer_start (void);
210 GLIB_AVAILABLE_IN_ALL
211 double g_test_timer_elapsed (void); /* elapsed seconds */
212 GLIB_AVAILABLE_IN_ALL
213 double g_test_timer_last (void); /* repeat last elapsed() result */
215 /* automatically g_free or g_object_unref upon teardown */
216 GLIB_AVAILABLE_IN_ALL
217 void g_test_queue_free (gpointer gfree_pointer);
218 GLIB_AVAILABLE_IN_ALL
219 void g_test_queue_destroy (GDestroyNotify destroy_func,
220 gpointer destroy_data);
221 #define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject)
223 typedef enum {
224 G_TEST_TRAP_SILENCE_STDOUT = 1 << 7,
225 G_TEST_TRAP_SILENCE_STDERR = 1 << 8,
226 G_TEST_TRAP_INHERIT_STDIN = 1 << 9
227 } GTestTrapFlags;
229 GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess)
230 gboolean g_test_trap_fork (guint64 usec_timeout,
231 GTestTrapFlags test_trap_flags);
233 typedef enum {
234 G_TEST_SUBPROCESS_INHERIT_STDIN = 1 << 0,
235 G_TEST_SUBPROCESS_INHERIT_STDOUT = 1 << 1,
236 G_TEST_SUBPROCESS_INHERIT_STDERR = 1 << 2
237 } GTestSubprocessFlags;
239 GLIB_AVAILABLE_IN_2_38
240 void g_test_trap_subprocess (const char *test_path,
241 guint64 usec_timeout,
242 GTestSubprocessFlags test_flags);
244 GLIB_AVAILABLE_IN_ALL
245 gboolean g_test_trap_has_passed (void);
246 GLIB_AVAILABLE_IN_ALL
247 gboolean g_test_trap_reached_timeout (void);
248 #define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
249 #define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
250 #define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
251 #define g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
252 #define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
253 #define g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
255 /* provide seed-able random numbers for tests */
256 #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
257 GLIB_AVAILABLE_IN_ALL
258 gint32 g_test_rand_int (void);
259 GLIB_AVAILABLE_IN_ALL
260 gint32 g_test_rand_int_range (gint32 begin,
261 gint32 end);
262 GLIB_AVAILABLE_IN_ALL
263 double g_test_rand_double (void);
264 GLIB_AVAILABLE_IN_ALL
265 double g_test_rand_double_range (double range_start,
266 double range_end);
269 * semi-internal API: non-documented symbols with stable ABI. You
270 * should use the non-internal helper macros instead. However, for
271 * compatibility reason, you may use this semi-internal API.
273 GLIB_AVAILABLE_IN_ALL
274 GTestCase* g_test_create_case (const char *test_name,
275 gsize data_size,
276 gconstpointer test_data,
277 GTestFixtureFunc data_setup,
278 GTestFixtureFunc data_test,
279 GTestFixtureFunc data_teardown);
280 GLIB_AVAILABLE_IN_ALL
281 GTestSuite* g_test_create_suite (const char *suite_name);
282 GLIB_AVAILABLE_IN_ALL
283 GTestSuite* g_test_get_root (void);
284 GLIB_AVAILABLE_IN_ALL
285 void g_test_suite_add (GTestSuite *suite,
286 GTestCase *test_case);
287 GLIB_AVAILABLE_IN_ALL
288 void g_test_suite_add_suite (GTestSuite *suite,
289 GTestSuite *nestedsuite);
290 GLIB_AVAILABLE_IN_ALL
291 int g_test_run_suite (GTestSuite *suite);
293 GLIB_AVAILABLE_IN_ALL
294 void g_test_trap_assertions (const char *domain,
295 const char *file,
296 int line,
297 const char *func,
298 guint64 assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
299 const char *pattern);
300 GLIB_AVAILABLE_IN_ALL
301 void g_assertion_message (const char *domain,
302 const char *file,
303 int line,
304 const char *func,
305 const char *message);
306 GLIB_AVAILABLE_IN_ALL
307 void g_assertion_message_expr (const char *domain,
308 const char *file,
309 int line,
310 const char *func,
311 const char *expr) G_GNUC_NORETURN;
312 GLIB_AVAILABLE_IN_ALL
313 void g_assertion_message_cmpstr (const char *domain,
314 const char *file,
315 int line,
316 const char *func,
317 const char *expr,
318 const char *arg1,
319 const char *cmp,
320 const char *arg2);
321 GLIB_AVAILABLE_IN_ALL
322 void g_assertion_message_cmpnum (const char *domain,
323 const char *file,
324 int line,
325 const char *func,
326 const char *expr,
327 long double arg1,
328 const char *cmp,
329 long double arg2,
330 char numtype);
331 GLIB_AVAILABLE_IN_ALL
332 void g_assertion_message_error (const char *domain,
333 const char *file,
334 int line,
335 const char *func,
336 const char *expr,
337 const GError *error,
338 GQuark error_domain,
339 int error_code);
340 GLIB_AVAILABLE_IN_ALL
341 void g_test_add_vtable (const char *testpath,
342 gsize data_size,
343 gconstpointer test_data,
344 GTestFixtureFunc data_setup,
345 GTestFixtureFunc data_test,
346 GTestFixtureFunc data_teardown);
347 typedef struct {
348 gboolean test_initialized;
349 gboolean test_quick; /* disable thorough tests */
350 gboolean test_perf; /* run performance tests */
351 gboolean test_verbose; /* extra info */
352 gboolean test_quiet; /* reduce output */
353 gboolean test_undefined; /* run tests that are meant to assert */
354 } GTestConfig;
355 GLIB_VAR const GTestConfig * const g_test_config_vars;
357 /* internal logging API */
358 typedef enum {
359 G_TEST_RUN_SUCCESS,
360 G_TEST_RUN_SKIPPED,
361 G_TEST_RUN_FAILURE,
362 G_TEST_RUN_INCOMPLETE
363 } GTestResult;
365 typedef enum {
366 G_TEST_LOG_NONE,
367 G_TEST_LOG_ERROR, /* s:msg */
368 G_TEST_LOG_START_BINARY, /* s:binaryname s:seed */
369 G_TEST_LOG_LIST_CASE, /* s:testpath */
370 G_TEST_LOG_SKIP_CASE, /* s:testpath */
371 G_TEST_LOG_START_CASE, /* s:testpath */
372 G_TEST_LOG_STOP_CASE, /* d:status d:nforks d:elapsed */
373 G_TEST_LOG_MIN_RESULT, /* s:blurb d:result */
374 G_TEST_LOG_MAX_RESULT, /* s:blurb d:result */
375 G_TEST_LOG_MESSAGE, /* s:blurb */
376 G_TEST_LOG_START_SUITE,
377 G_TEST_LOG_STOP_SUITE
378 } GTestLogType;
380 typedef struct {
381 GTestLogType log_type;
382 guint n_strings;
383 gchar **strings; /* NULL terminated */
384 guint n_nums;
385 long double *nums;
386 } GTestLogMsg;
387 typedef struct {
388 /*< private >*/
389 GString *data;
390 GSList *msgs;
391 } GTestLogBuffer;
393 GLIB_AVAILABLE_IN_ALL
394 const char* g_test_log_type_name (GTestLogType log_type);
395 GLIB_AVAILABLE_IN_ALL
396 GTestLogBuffer* g_test_log_buffer_new (void);
397 GLIB_AVAILABLE_IN_ALL
398 void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
399 GLIB_AVAILABLE_IN_ALL
400 void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
401 guint n_bytes,
402 const guint8 *bytes);
403 GLIB_AVAILABLE_IN_ALL
404 GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
405 GLIB_AVAILABLE_IN_ALL
406 void g_test_log_msg_free (GTestLogMsg *tmsg);
409 * GTestLogFatalFunc:
410 * @log_domain: the log domain of the message
411 * @log_level: the log level of the message (including the fatal and recursion flags)
412 * @message: the message to process
413 * @user_data: user data, set in g_test_log_set_fatal_handler()
415 * Specifies the prototype of fatal log handler functions.
417 * Returns: %TRUE if the program should abort, %FALSE otherwise
419 * Since: 2.22
421 typedef gboolean (*GTestLogFatalFunc) (const gchar *log_domain,
422 GLogLevelFlags log_level,
423 const gchar *message,
424 gpointer user_data);
425 GLIB_AVAILABLE_IN_ALL
426 void
427 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
428 gpointer user_data);
430 GLIB_AVAILABLE_IN_2_34
431 void g_test_expect_message (const gchar *log_domain,
432 GLogLevelFlags log_level,
433 const gchar *pattern);
434 GLIB_AVAILABLE_IN_2_34
435 void g_test_assert_expected_messages_internal (const char *domain,
436 const char *file,
437 int line,
438 const char *func);
440 typedef enum
442 G_TEST_DIST,
443 G_TEST_BUILT
444 } GTestFileType;
446 GLIB_AVAILABLE_IN_2_38
447 gchar * g_test_build_filename (GTestFileType file_type,
448 const gchar *first_path,
449 ...) G_GNUC_NULL_TERMINATED;
450 GLIB_AVAILABLE_IN_2_38
451 const gchar *g_test_get_dir (GTestFileType file_type);
452 GLIB_AVAILABLE_IN_2_38
453 const gchar *g_test_get_filename (GTestFileType file_type,
454 const gchar *first_path,
455 ...) G_GNUC_NULL_TERMINATED;
457 #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
459 G_END_DECLS
461 #endif /* __G_TEST_UTILS_H__ */