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