1/* GLib testing utilities
2 * Copyright (C) 2007 Imendio AB
3 * Authors: Tim Janik
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
19#ifndef __G_TEST_UTILS_H__
20#define __G_TEST_UTILS_H__
21
22#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23#error "Only <glib.h> can be included directly."
24#endif
25
26#include <glib/gmessages.h>
27#include <glib/gstring.h>
28#include <glib/gerror.h>
29#include <glib/gslist.h>
30#include <string.h>
31
32G_BEGIN_DECLS
33
34typedef struct GTestCase GTestCase;
35typedef struct GTestSuite GTestSuite;
36typedef void (*GTestFunc) (void);
37typedef void (*GTestDataFunc) (gconstpointer user_data);
38typedef void (*GTestFixtureFunc) (gpointer fixture,
39 gconstpointer user_data);
40
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 = (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'); \
71 } G_STMT_END
72#define g_assert_cmpfloat_with_epsilon(n1,n2,epsilon) \
73 G_STMT_START { \
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'); \
78 } G_STMT_END
79#define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
80 gconstpointer __m1 = m1, __m2 = m2; \
81 int __l1 = l1, __l2 = l2; \
82 if (__l1 != __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 ")"); \
89 } G_STMT_END
90#define g_assert_no_error(err) G_STMT_START { \
91 if (err) \
92 g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
93 #err, err, 0, 0); \
94 } G_STMT_END
95#define g_assert_error(err, dom, c) G_STMT_START { \
96 if (!err || (err)->domain != dom || (err)->code != c) \
97 g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
98 #err, err, dom, c); \
99 } G_STMT_END
100#define g_assert_true(expr) G_STMT_START { \
101 if G_LIKELY (expr) ; else \
102 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
103 "'" #expr "' should be TRUE"); \
104 } G_STMT_END
105#define g_assert_false(expr) G_STMT_START { \
106 if G_LIKELY (!(expr)) ; else \
107 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
108 "'" #expr "' should be FALSE"); \
109 } G_STMT_END
110#define g_assert_null(expr) G_STMT_START { if G_LIKELY ((expr) == NULL) ; else \
111 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
112 "'" #expr "' should be NULL"); \
113 } G_STMT_END
114#define g_assert_nonnull(expr) G_STMT_START { \
115 if G_LIKELY ((expr) != NULL) ; else \
116 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
117 "'" #expr "' should not be NULL"); \
118 } G_STMT_END
119#ifdef G_DISABLE_ASSERT
120#define g_assert_not_reached() G_STMT_START { (void) 0; } G_STMT_END
121#define g_assert(expr) G_STMT_START { (void) 0; } G_STMT_END
122#else /* !G_DISABLE_ASSERT */
123#define g_assert_not_reached() G_STMT_START { g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } G_STMT_END
124#define g_assert(expr) G_STMT_START { \
125 if G_LIKELY (expr) ; else \
126 g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
127 #expr); \
128 } G_STMT_END
129#endif /* !G_DISABLE_ASSERT */
130
131GLIB_AVAILABLE_IN_ALL
132int g_strcmp0 (const char *str1,
133 const char *str2);
134
135/* report performance results */
136GLIB_AVAILABLE_IN_ALL
137void g_test_minimized_result (double minimized_quantity,
138 const char *format,
139 ...) G_GNUC_PRINTF (2, 3);
140GLIB_AVAILABLE_IN_ALL
141void g_test_maximized_result (double maximized_quantity,
142 const char *format,
143 ...) G_GNUC_PRINTF (2, 3);
144
145/* initialize testing framework */
146GLIB_AVAILABLE_IN_ALL
147void g_test_init (int *argc,
148 char ***argv,
149 ...) G_GNUC_NULL_TERMINATED;
150
151/* While we discourage its use, g_assert() is often used in unit tests
152 * (especially in legacy code). g_assert_*() should really be used instead.
153 * g_assert() can be disabled at client program compile time, which can render
154 * tests useless. Highlight that to the user. */
155#ifdef G_DISABLE_ASSERT
156#if defined(G_HAVE_ISO_VARARGS)
157#define g_test_init(argc, argv, ...) \
158 G_STMT_START { \
159 g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \
160 exit (1); \
161 } G_STMT_END
162#elif defined(G_HAVE_GNUC_VARARGS)
163#define g_test_init(argc, argv...) \
164 G_STMT_START { \
165 g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \
166 exit (1); \
167 } G_STMT_END
168#else /* no varargs */
169 /* do nothing */
170#endif /* varargs support */
171#endif /* G_DISABLE_ASSERT */
172
173/* query testing framework config */
174#define g_test_initialized() (g_test_config_vars->test_initialized)
175#define g_test_quick() (g_test_config_vars->test_quick)
176#define g_test_slow() (!g_test_config_vars->test_quick)
177#define g_test_thorough() (!g_test_config_vars->test_quick)
178#define g_test_perf() (g_test_config_vars->test_perf)
179#define g_test_verbose() (g_test_config_vars->test_verbose)
180#define g_test_quiet() (g_test_config_vars->test_quiet)
181#define g_test_undefined() (g_test_config_vars->test_undefined)
182GLIB_AVAILABLE_IN_2_38
183gboolean g_test_subprocess (void);
184
185/* run all tests under toplevel suite (path: /) */
186GLIB_AVAILABLE_IN_ALL
187int g_test_run (void);
188/* hook up a test functions under test path */
189GLIB_AVAILABLE_IN_ALL
190void g_test_add_func (const char *testpath,
191 GTestFunc test_func);
192
193GLIB_AVAILABLE_IN_ALL
194void g_test_add_data_func (const char *testpath,
195 gconstpointer test_data,
196 GTestDataFunc test_func);
197
198GLIB_AVAILABLE_IN_2_34
199void g_test_add_data_func_full (const char *testpath,
200 gpointer test_data,
201 GTestDataFunc test_func,
202 GDestroyNotify data_free_func);
203
204/* tell about failure */
205GLIB_AVAILABLE_IN_2_30
206void g_test_fail (void);
207GLIB_AVAILABLE_IN_2_38
208void g_test_incomplete (const gchar *msg);
209GLIB_AVAILABLE_IN_2_38
210void g_test_skip (const gchar *msg);
211GLIB_AVAILABLE_IN_2_38
212gboolean g_test_failed (void);
213GLIB_AVAILABLE_IN_2_38
214void g_test_set_nonfatal_assertions (void);
215
216/* hook up a test with fixture under test path */
217#define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
218 G_STMT_START { \
219 void (*add_vtable) (const char*, \
220 gsize, \
221 gconstpointer, \
222 void (*) (Fixture*, gconstpointer), \
223 void (*) (Fixture*, gconstpointer), \
224 void (*) (Fixture*, gconstpointer)) = (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
225 add_vtable \
226 (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
227 } G_STMT_END
228
229/* add test messages to the test report */
230GLIB_AVAILABLE_IN_ALL
231void g_test_message (const char *format,
232 ...) G_GNUC_PRINTF (1, 2);
233GLIB_AVAILABLE_IN_ALL
234void g_test_bug_base (const char *uri_pattern);
235GLIB_AVAILABLE_IN_ALL
236void g_test_bug (const char *bug_uri_snippet);
237/* measure test timings */
238GLIB_AVAILABLE_IN_ALL
239void g_test_timer_start (void);
240GLIB_AVAILABLE_IN_ALL
241double g_test_timer_elapsed (void); /* elapsed seconds */
242GLIB_AVAILABLE_IN_ALL
243double g_test_timer_last (void); /* repeat last elapsed() result */
244
245/* automatically g_free or g_object_unref upon teardown */
246GLIB_AVAILABLE_IN_ALL
247void g_test_queue_free (gpointer gfree_pointer);
248GLIB_AVAILABLE_IN_ALL
249void g_test_queue_destroy (GDestroyNotify destroy_func,
250 gpointer destroy_data);
251#define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject)
252
253typedef enum {
254 G_TEST_TRAP_SILENCE_STDOUT = 1 << 7,
255 G_TEST_TRAP_SILENCE_STDERR = 1 << 8,
256 G_TEST_TRAP_INHERIT_STDIN = 1 << 9
257} GTestTrapFlags;
258
259GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess)
260gboolean g_test_trap_fork (guint64 usec_timeout,
261 GTestTrapFlags test_trap_flags);
262
263typedef enum {
264 G_TEST_SUBPROCESS_INHERIT_STDIN = 1 << 0,
265 G_TEST_SUBPROCESS_INHERIT_STDOUT = 1 << 1,
266 G_TEST_SUBPROCESS_INHERIT_STDERR = 1 << 2
267} GTestSubprocessFlags;
268
269GLIB_AVAILABLE_IN_2_38
270void g_test_trap_subprocess (const char *test_path,
271 guint64 usec_timeout,
272 GTestSubprocessFlags test_flags);
273
274GLIB_AVAILABLE_IN_ALL
275gboolean g_test_trap_has_passed (void);
276GLIB_AVAILABLE_IN_ALL
277gboolean g_test_trap_reached_timeout (void);
278#define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
279#define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
280#define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
281#define g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
282#define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
283#define g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
284
285/* provide seed-able random numbers for tests */
286#define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
287GLIB_AVAILABLE_IN_ALL
288gint32 g_test_rand_int (void);
289GLIB_AVAILABLE_IN_ALL
290gint32 g_test_rand_int_range (gint32 begin,
291 gint32 end);
292GLIB_AVAILABLE_IN_ALL
293double g_test_rand_double (void);
294GLIB_AVAILABLE_IN_ALL
295double g_test_rand_double_range (double range_start,
296 double range_end);
297
298/*
299 * semi-internal API: non-documented symbols with stable ABI. You
300 * should use the non-internal helper macros instead. However, for
301 * compatibility reason, you may use this semi-internal API.
302 */
303GLIB_AVAILABLE_IN_ALL
304GTestCase* g_test_create_case (const char *test_name,
305 gsize data_size,
306 gconstpointer test_data,
307 GTestFixtureFunc data_setup,
308 GTestFixtureFunc data_test,
309 GTestFixtureFunc data_teardown);
310GLIB_AVAILABLE_IN_ALL
311GTestSuite* g_test_create_suite (const char *suite_name);
312GLIB_AVAILABLE_IN_ALL
313GTestSuite* g_test_get_root (void);
314GLIB_AVAILABLE_IN_ALL
315void g_test_suite_add (GTestSuite *suite,
316 GTestCase *test_case);
317GLIB_AVAILABLE_IN_ALL
318void g_test_suite_add_suite (GTestSuite *suite,
319 GTestSuite *nestedsuite);
320GLIB_AVAILABLE_IN_ALL
321int g_test_run_suite (GTestSuite *suite);
322
323GLIB_AVAILABLE_IN_ALL
324void g_test_trap_assertions (const char *domain,
325 const char *file,
326 int line,
327 const char *func,
328 guint64 assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
329 const char *pattern);
330GLIB_AVAILABLE_IN_ALL
331void g_assertion_message (const char *domain,
332 const char *file,
333 int line,
334 const char *func,
335 const char *message);
336GLIB_AVAILABLE_IN_ALL
337void g_assertion_message_expr (const char *domain,
338 const char *file,
339 int line,
340 const char *func,
341 const char *expr) G_GNUC_NORETURN;
342GLIB_AVAILABLE_IN_ALL
343void g_assertion_message_cmpstr (const char *domain,
344 const char *file,
345 int line,
346 const char *func,
347 const char *expr,
348 const char *arg1,
349 const char *cmp,
350 const char *arg2);
351GLIB_AVAILABLE_IN_ALL
352void g_assertion_message_cmpnum (const char *domain,
353 const char *file,
354 int line,
355 const char *func,
356 const char *expr,
357 long double arg1,
358 const char *cmp,
359 long double arg2,
360 char numtype);
361GLIB_AVAILABLE_IN_ALL
362void g_assertion_message_error (const char *domain,
363 const char *file,
364 int line,
365 const char *func,
366 const char *expr,
367 const GError *error,
368 GQuark error_domain,
369 int error_code);
370GLIB_AVAILABLE_IN_ALL
371void g_test_add_vtable (const char *testpath,
372 gsize data_size,
373 gconstpointer test_data,
374 GTestFixtureFunc data_setup,
375 GTestFixtureFunc data_test,
376 GTestFixtureFunc data_teardown);
377typedef struct {
378 gboolean test_initialized;
379 gboolean test_quick; /* disable thorough tests */
380 gboolean test_perf; /* run performance tests */
381 gboolean test_verbose; /* extra info */
382 gboolean test_quiet; /* reduce output */
383 gboolean test_undefined; /* run tests that are meant to assert */
384} GTestConfig;
385GLIB_VAR const GTestConfig * const g_test_config_vars;
386
387/* internal logging API */
388typedef enum {
389 G_TEST_RUN_SUCCESS,
390 G_TEST_RUN_SKIPPED,
391 G_TEST_RUN_FAILURE,
392 G_TEST_RUN_INCOMPLETE
393} GTestResult;
394
395typedef enum {
396 G_TEST_LOG_NONE,
397 G_TEST_LOG_ERROR, /* s:msg */
398 G_TEST_LOG_START_BINARY, /* s:binaryname s:seed */
399 G_TEST_LOG_LIST_CASE, /* s:testpath */
400 G_TEST_LOG_SKIP_CASE, /* s:testpath */
401 G_TEST_LOG_START_CASE, /* s:testpath */
402 G_TEST_LOG_STOP_CASE, /* d:status d:nforks d:elapsed */
403 G_TEST_LOG_MIN_RESULT, /* s:blurb d:result */
404 G_TEST_LOG_MAX_RESULT, /* s:blurb d:result */
405 G_TEST_LOG_MESSAGE, /* s:blurb */
406 G_TEST_LOG_START_SUITE,
407 G_TEST_LOG_STOP_SUITE
408} GTestLogType;
409
410typedef struct {
411 GTestLogType log_type;
412 guint n_strings;
413 gchar **strings; /* NULL terminated */
414 guint n_nums;
415 long double *nums;
416} GTestLogMsg;
417typedef struct {
418 /*< private >*/
419 GString *data;
420 GSList *msgs;
421} GTestLogBuffer;
422
423GLIB_AVAILABLE_IN_ALL
424const char* g_test_log_type_name (GTestLogType log_type);
425GLIB_AVAILABLE_IN_ALL
426GTestLogBuffer* g_test_log_buffer_new (void);
427GLIB_AVAILABLE_IN_ALL
428void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
429GLIB_AVAILABLE_IN_ALL
430void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
431 guint n_bytes,
432 const guint8 *bytes);
433GLIB_AVAILABLE_IN_ALL
434GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
435GLIB_AVAILABLE_IN_ALL
436void g_test_log_msg_free (GTestLogMsg *tmsg);
437
438/**
439 * GTestLogFatalFunc:
440 * @log_domain: the log domain of the message
441 * @log_level: the log level of the message (including the fatal and recursion flags)
442 * @message: the message to process
443 * @user_data: user data, set in g_test_log_set_fatal_handler()
444 *
445 * Specifies the prototype of fatal log handler functions.
446 *
447 * Returns: %TRUE if the program should abort, %FALSE otherwise
448 *
449 * Since: 2.22
450 */
451typedef gboolean (*GTestLogFatalFunc) (const gchar *log_domain,
452 GLogLevelFlags log_level,
453 const gchar *message,
454 gpointer user_data);
455GLIB_AVAILABLE_IN_ALL
456void
457g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
458 gpointer user_data);
459
460GLIB_AVAILABLE_IN_2_34
461void g_test_expect_message (const gchar *log_domain,
462 GLogLevelFlags log_level,
463 const gchar *pattern);
464GLIB_AVAILABLE_IN_2_34
465void g_test_assert_expected_messages_internal (const char *domain,
466 const char *file,
467 int line,
468 const char *func);
469
470typedef enum
471{
472 G_TEST_DIST,
473 G_TEST_BUILT
474} GTestFileType;
475
476GLIB_AVAILABLE_IN_2_38
477gchar * g_test_build_filename (GTestFileType file_type,
478 const gchar *first_path,
479 ...) G_GNUC_NULL_TERMINATED;
480GLIB_AVAILABLE_IN_2_38
481const gchar *g_test_get_dir (GTestFileType file_type);
482GLIB_AVAILABLE_IN_2_38
483const gchar *g_test_get_filename (GTestFileType file_type,
484 const gchar *first_path,
485 ...) G_GNUC_NULL_TERMINATED;
486
487#define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
488
489G_END_DECLS
490
491#endif /* __G_TEST_UTILS_H__ */
492