1 /* GLib testing utilities
2 * Copyright (C) 2007 Imendio AB
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."
26 #include <glib/gmessages.h>
27 #include <glib/gstring.h>
28 #include <glib/gerror.h>
29 #include <glib/gslist.h>
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);
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); \
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'); \
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'); \
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'); \
66 #define g_assert_cmpfloat(n1,cmp,n2) G_STMT_START { \
67 long double __n1 = (long double) (n1), __n2 = (long double) (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'); \
72 #define g_assert_cmpfloat_with_epsilon(n1,n2,epsilon) \
74 double __n1 = (n1), __n2 = (n2), __epsilon = (epsilon); \
75 if (G_APPROX_VALUE (__n1, __n2, __epsilon)) ; else \
76 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
77 #n1 " == " #n2 " (+/- " #epsilon ")", __n1, "==", __n2, 'f'); \
79 #define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
80 gconstpointer __m1 = m1, __m2 = m2; \
81 int __l1 = l1, __l2 = l2; \
83 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
84 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", \
85 (long double) __l1, "==", (long double) __l2, 'i'); \
86 else if (__l1 != 0 && memcmp (__m1, __m2, __l1) != 0) \
87 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
88 "assertion failed (" #m1 " == " #m2 ")"); \
90 #define g_assert_cmpvariant(v1, v2) \
93 GVariant *__v1 = (v1), *__v2 = (v2); \
94 if (!g_variant_equal (__v1, __v2)) \
96 gchar *__s1, *__s2, *__msg; \
97 __s1 = g_variant_print (__v1, TRUE); \
98 __s2 = g_variant_print (__v2, TRUE); \
99 __msg = g_strdup_printf ("assertion failed (" #v1 " == " #v2 "): %s does not equal %s", __s1, __s2); \
100 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
107 #define g_assert_no_error(err) G_STMT_START { \
109 g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
112 #define g_assert_error(err, dom, c) G_STMT_START { \
113 if (!err || (err)->domain != dom || (err)->code != c) \
114 g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
115 #err, err, dom, c); \
117 #define g_assert_true(expr) G_STMT_START { \
118 if G_LIKELY (expr) ; else \
119 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
120 "'" #expr "' should be TRUE"); \
122 #define g_assert_false(expr) G_STMT_START { \
123 if G_LIKELY (!(expr)) ; else \
124 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
125 "'" #expr "' should be FALSE"); \
127 #define g_assert_null(expr) G_STMT_START { if G_LIKELY ((expr) == NULL) ; else \
128 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
129 "'" #expr "' should be NULL"); \
131 #define g_assert_nonnull(expr) G_STMT_START { \
132 if G_LIKELY ((expr) != NULL) ; else \
133 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
134 "'" #expr "' should not be NULL"); \
136 #ifdef G_DISABLE_ASSERT
137 #define g_assert_not_reached() G_STMT_START { (void) 0; } G_STMT_END
138 #define g_assert(expr) G_STMT_START { (void) 0; } G_STMT_END
139 #else /* !G_DISABLE_ASSERT */
140 #define g_assert_not_reached() G_STMT_START { g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } G_STMT_END
141 #define g_assert(expr) G_STMT_START { \
142 if G_LIKELY (expr) ; else \
143 g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
146 #endif /* !G_DISABLE_ASSERT */
148 GLIB_AVAILABLE_IN_ALL
149 int g_strcmp0 (const char *str1,
152 /* report performance results */
153 GLIB_AVAILABLE_IN_ALL
154 void g_test_minimized_result (double minimized_quantity,
156 ...) G_GNUC_PRINTF (2, 3);
157 GLIB_AVAILABLE_IN_ALL
158 void g_test_maximized_result (double maximized_quantity,
160 ...) G_GNUC_PRINTF (2, 3);
162 /* initialize testing framework */
163 GLIB_AVAILABLE_IN_ALL
164 void g_test_init (int *argc,
166 ...) G_GNUC_NULL_TERMINATED;
169 * G_TEST_OPTION_ISOLATE_DIRS:
171 * Creates a unique temporary directory for each unit test and uses
172 * g_set_user_dirs() to set XDG directories to point into subdirectories of it
173 * for the duration of the unit test. The directory tree is cleaned up after the
174 * test finishes successfully. Note that this doesn’t take effect until
175 * g_test_run() is called, so calls to (for example) g_get_user_home_dir() will
176 * return the system-wide value when made in a test program’s main() function.
178 * The following functions will return subdirectories of the temporary directory
179 * when this option is used. The specific subdirectory paths in use are not
180 * guaranteed to be stable API — always use a getter function to retrieve them.
183 * - g_get_user_cache_dir()
184 * - g_get_system_config_dirs()
185 * - g_get_user_config_dir()
186 * - g_get_system_data_dirs()
187 * - g_get_user_data_dir()
188 * - g_get_user_runtime_dir()
190 * The subdirectories may not be created by the test harness; as with normal
191 * calls to functions like g_get_user_cache_dir(), the caller must be prepared
192 * to create the directory if it doesn’t exist.
196 #define G_TEST_OPTION_ISOLATE_DIRS "isolate_dirs"
198 /* While we discourage its use, g_assert() is often used in unit tests
199 * (especially in legacy code). g_assert_*() should really be used instead.
200 * g_assert() can be disabled at client program compile time, which can render
201 * tests useless. Highlight that to the user. */
202 #ifdef G_DISABLE_ASSERT
203 #if defined(G_HAVE_ISO_VARARGS)
204 #define g_test_init(argc, argv, ...) \
206 g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \
209 #elif defined(G_HAVE_GNUC_VARARGS)
210 #define g_test_init(argc, argv...) \
212 g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \
215 #else /* no varargs */
217 #endif /* varargs support */
218 #endif /* G_DISABLE_ASSERT */
220 /* query testing framework config */
221 #define g_test_initialized() (g_test_config_vars->test_initialized)
222 #define g_test_quick() (g_test_config_vars->test_quick)
223 #define g_test_slow() (!g_test_config_vars->test_quick)
224 #define g_test_thorough() (!g_test_config_vars->test_quick)
225 #define g_test_perf() (g_test_config_vars->test_perf)
226 #define g_test_verbose() (g_test_config_vars->test_verbose)
227 #define g_test_quiet() (g_test_config_vars->test_quiet)
228 #define g_test_undefined() (g_test_config_vars->test_undefined)
229 GLIB_AVAILABLE_IN_2_38
230 gboolean g_test_subprocess (void);
232 /* run all tests under toplevel suite (path: /) */
233 GLIB_AVAILABLE_IN_ALL
234 int g_test_run (void);
235 /* hook up a test functions under test path */
236 GLIB_AVAILABLE_IN_ALL
237 void g_test_add_func (const char *testpath,
238 GTestFunc test_func);
240 GLIB_AVAILABLE_IN_ALL
241 void g_test_add_data_func (const char *testpath,
242 gconstpointer test_data,
243 GTestDataFunc test_func);
245 GLIB_AVAILABLE_IN_2_34
246 void g_test_add_data_func_full (const char *testpath,
248 GTestDataFunc test_func,
249 GDestroyNotify data_free_func);
251 /* tell about failure */
252 GLIB_AVAILABLE_IN_2_30
253 void g_test_fail (void);
254 GLIB_AVAILABLE_IN_2_38
255 void g_test_incomplete (const gchar *msg);
256 GLIB_AVAILABLE_IN_2_38
257 void g_test_skip (const gchar *msg);
258 GLIB_AVAILABLE_IN_2_38
259 gboolean g_test_failed (void);
260 GLIB_AVAILABLE_IN_2_38
261 void g_test_set_nonfatal_assertions (void);
265 * @testpath: The test path for a new test case.
266 * @Fixture: The type of a fixture data structure.
267 * @tdata: Data argument for the test functions.
268 * @fsetup: The function to set up the fixture data.
269 * @ftest: The actual test function.
270 * @fteardown: The function to tear down the fixture data.
272 * Hook up a new test case at @testpath, similar to g_test_add_func().
273 * A fixture data structure with setup and teardown functions may be provided,
274 * similar to g_test_create_case().
276 * g_test_add() is implemented as a macro, so that the fsetup(), ftest() and
277 * fteardown() callbacks can expect a @Fixture pointer as their first argument
278 * in a type safe manner. They otherwise have type #GTestFixtureFunc.
282 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
284 void (*add_vtable) (const char*, \
287 void (*) (Fixture*, gconstpointer), \
288 void (*) (Fixture*, gconstpointer), \
289 void (*) (Fixture*, gconstpointer)) = (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
291 (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
294 /* add test messages to the test report */
295 GLIB_AVAILABLE_IN_ALL
296 void g_test_message (const char *format,
297 ...) G_GNUC_PRINTF (1, 2);
298 GLIB_AVAILABLE_IN_ALL
299 void g_test_bug_base (const char *uri_pattern);
300 GLIB_AVAILABLE_IN_ALL
301 void g_test_bug (const char *bug_uri_snippet);
302 /* measure test timings */
303 GLIB_AVAILABLE_IN_ALL
304 void g_test_timer_start (void);
305 GLIB_AVAILABLE_IN_ALL
306 double g_test_timer_elapsed (void); /* elapsed seconds */
307 GLIB_AVAILABLE_IN_ALL
308 double g_test_timer_last (void); /* repeat last elapsed() result */
310 /* automatically g_free or g_object_unref upon teardown */
311 GLIB_AVAILABLE_IN_ALL
312 void g_test_queue_free (gpointer gfree_pointer);
313 GLIB_AVAILABLE_IN_ALL
314 void g_test_queue_destroy (GDestroyNotify destroy_func,
315 gpointer destroy_data);
316 #define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject)
319 G_TEST_TRAP_SILENCE_STDOUT = 1 << 7,
320 G_TEST_TRAP_SILENCE_STDERR = 1 << 8,
321 G_TEST_TRAP_INHERIT_STDIN = 1 << 9
324 GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess)
325 gboolean g_test_trap_fork (guint64 usec_timeout,
326 GTestTrapFlags test_trap_flags);
329 G_TEST_SUBPROCESS_INHERIT_STDIN = 1 << 0,
330 G_TEST_SUBPROCESS_INHERIT_STDOUT = 1 << 1,
331 G_TEST_SUBPROCESS_INHERIT_STDERR = 1 << 2
332 } GTestSubprocessFlags;
334 GLIB_AVAILABLE_IN_2_38
335 void g_test_trap_subprocess (const char *test_path,
336 guint64 usec_timeout,
337 GTestSubprocessFlags test_flags);
339 GLIB_AVAILABLE_IN_ALL
340 gboolean g_test_trap_has_passed (void);
341 GLIB_AVAILABLE_IN_ALL
342 gboolean g_test_trap_reached_timeout (void);
343 #define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
344 #define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
345 #define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
346 #define g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
347 #define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
348 #define g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
350 /* provide seed-able random numbers for tests */
351 #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
352 GLIB_AVAILABLE_IN_ALL
353 gint32 g_test_rand_int (void);
354 GLIB_AVAILABLE_IN_ALL
355 gint32 g_test_rand_int_range (gint32 begin,
357 GLIB_AVAILABLE_IN_ALL
358 double g_test_rand_double (void);
359 GLIB_AVAILABLE_IN_ALL
360 double g_test_rand_double_range (double range_start,
364 * semi-internal API: non-documented symbols with stable ABI. You
365 * should use the non-internal helper macros instead. However, for
366 * compatibility reason, you may use this semi-internal API.
368 GLIB_AVAILABLE_IN_ALL
369 GTestCase* g_test_create_case (const char *test_name,
371 gconstpointer test_data,
372 GTestFixtureFunc data_setup,
373 GTestFixtureFunc data_test,
374 GTestFixtureFunc data_teardown);
375 GLIB_AVAILABLE_IN_ALL
376 GTestSuite* g_test_create_suite (const char *suite_name);
377 GLIB_AVAILABLE_IN_ALL
378 GTestSuite* g_test_get_root (void);
379 GLIB_AVAILABLE_IN_ALL
380 void g_test_suite_add (GTestSuite *suite,
381 GTestCase *test_case);
382 GLIB_AVAILABLE_IN_ALL
383 void g_test_suite_add_suite (GTestSuite *suite,
384 GTestSuite *nestedsuite);
385 GLIB_AVAILABLE_IN_ALL
386 int g_test_run_suite (GTestSuite *suite);
388 GLIB_AVAILABLE_IN_ALL
389 void g_test_trap_assertions (const char *domain,
393 guint64 assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
394 const char *pattern);
395 GLIB_AVAILABLE_IN_ALL
396 void g_assertion_message (const char *domain,
400 const char *message);
401 GLIB_AVAILABLE_IN_ALL
402 void g_assertion_message_expr (const char *domain,
406 const char *expr) G_GNUC_NORETURN;
407 GLIB_AVAILABLE_IN_ALL
408 void g_assertion_message_cmpstr (const char *domain,
416 GLIB_AVAILABLE_IN_ALL
417 void g_assertion_message_cmpnum (const char *domain,
426 GLIB_AVAILABLE_IN_ALL
427 void g_assertion_message_error (const char *domain,
435 GLIB_AVAILABLE_IN_ALL
436 void g_test_add_vtable (const char *testpath,
438 gconstpointer test_data,
439 GTestFixtureFunc data_setup,
440 GTestFixtureFunc data_test,
441 GTestFixtureFunc data_teardown);
443 gboolean test_initialized;
444 gboolean test_quick; /* disable thorough tests */
445 gboolean test_perf; /* run performance tests */
446 gboolean test_verbose; /* extra info */
447 gboolean test_quiet; /* reduce output */
448 gboolean test_undefined; /* run tests that are meant to assert */
450 GLIB_VAR const GTestConfig * const g_test_config_vars;
452 /* internal logging API */
457 G_TEST_RUN_INCOMPLETE
462 G_TEST_LOG_ERROR, /* s:msg */
463 G_TEST_LOG_START_BINARY, /* s:binaryname s:seed */
464 G_TEST_LOG_LIST_CASE, /* s:testpath */
465 G_TEST_LOG_SKIP_CASE, /* s:testpath */
466 G_TEST_LOG_START_CASE, /* s:testpath */
467 G_TEST_LOG_STOP_CASE, /* d:status d:nforks d:elapsed */
468 G_TEST_LOG_MIN_RESULT, /* s:blurb d:result */
469 G_TEST_LOG_MAX_RESULT, /* s:blurb d:result */
470 G_TEST_LOG_MESSAGE, /* s:blurb */
471 G_TEST_LOG_START_SUITE,
472 G_TEST_LOG_STOP_SUITE
476 GTestLogType log_type;
478 gchar **strings; /* NULL terminated */
488 GLIB_AVAILABLE_IN_ALL
489 const char* g_test_log_type_name (GTestLogType log_type);
490 GLIB_AVAILABLE_IN_ALL
491 GTestLogBuffer* g_test_log_buffer_new (void);
492 GLIB_AVAILABLE_IN_ALL
493 void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
494 GLIB_AVAILABLE_IN_ALL
495 void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
497 const guint8 *bytes);
498 GLIB_AVAILABLE_IN_ALL
499 GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
500 GLIB_AVAILABLE_IN_ALL
501 void g_test_log_msg_free (GTestLogMsg *tmsg);
505 * @log_domain: the log domain of the message
506 * @log_level: the log level of the message (including the fatal and recursion flags)
507 * @message: the message to process
508 * @user_data: user data, set in g_test_log_set_fatal_handler()
510 * Specifies the prototype of fatal log handler functions.
512 * Returns: %TRUE if the program should abort, %FALSE otherwise
516 typedef gboolean (*GTestLogFatalFunc) (const gchar *log_domain,
517 GLogLevelFlags log_level,
518 const gchar *message,
520 GLIB_AVAILABLE_IN_ALL
522 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
525 GLIB_AVAILABLE_IN_2_34
526 void g_test_expect_message (const gchar *log_domain,
527 GLogLevelFlags log_level,
528 const gchar *pattern);
529 GLIB_AVAILABLE_IN_2_34
530 void g_test_assert_expected_messages_internal (const char *domain,
541 GLIB_AVAILABLE_IN_2_38
542 gchar * g_test_build_filename (GTestFileType file_type,
543 const gchar *first_path,
544 ...) G_GNUC_NULL_TERMINATED;
545 GLIB_AVAILABLE_IN_2_38
546 const gchar *g_test_get_dir (GTestFileType file_type);
547 GLIB_AVAILABLE_IN_2_38
548 const gchar *g_test_get_filename (GTestFileType file_type,
549 const gchar *first_path,
550 ...) G_GNUC_NULL_TERMINATED;
552 #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
556 #endif /* __G_TEST_UTILS_H__ */