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 = (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 */
123
124GLIB_AVAILABLE_IN_ALL
125int g_strcmp0 (const char *str1,
126 const char *str2);
127
128/* report performance results */
129GLIB_AVAILABLE_IN_ALL
130void g_test_minimized_result (double minimized_quantity,
131 const char *format,
132 ...) G_GNUC_PRINTF (2, 3);
133GLIB_AVAILABLE_IN_ALL
134void g_test_maximized_result (double maximized_quantity,
135 const char *format,
136 ...) G_GNUC_PRINTF (2, 3);
137
138/* initialize testing framework */
139GLIB_AVAILABLE_IN_ALL
140void 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)
152GLIB_AVAILABLE_IN_2_38
153gboolean g_test_subprocess (void);
154
155/* run all tests under toplevel suite (path: /) */
156GLIB_AVAILABLE_IN_ALL
157int g_test_run (void);
158/* hook up a test functions under test path */
159GLIB_AVAILABLE_IN_ALL
160void g_test_add_func (const char *testpath,
161 GTestFunc test_func);
162
163GLIB_AVAILABLE_IN_ALL
164void g_test_add_data_func (const char *testpath,
165 gconstpointer test_data,
166 GTestDataFunc test_func);
167
168GLIB_AVAILABLE_IN_2_34
169void g_test_add_data_func_full (const char *testpath,
170 gpointer test_data,
171 GTestDataFunc test_func,
172 GDestroyNotify data_free_func);
173
174/* tell about failure */
175GLIB_AVAILABLE_IN_2_30
176void g_test_fail (void);
177GLIB_AVAILABLE_IN_2_38
178void g_test_incomplete (const gchar *msg);
179GLIB_AVAILABLE_IN_2_38
180void g_test_skip (const gchar *msg);
181GLIB_AVAILABLE_IN_2_38
182gboolean g_test_failed (void);
183GLIB_AVAILABLE_IN_2_38
184void g_test_set_nonfatal_assertions (void);
185
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
198
199/* add test messages to the test report */
200GLIB_AVAILABLE_IN_ALL
201void g_test_message (const char *format,
202 ...) G_GNUC_PRINTF (1, 2);
203GLIB_AVAILABLE_IN_ALL
204void g_test_bug_base (const char *uri_pattern);
205GLIB_AVAILABLE_IN_ALL
206void g_test_bug (const char *bug_uri_snippet);
207/* measure test timings */
208GLIB_AVAILABLE_IN_ALL
209void g_test_timer_start (void);
210GLIB_AVAILABLE_IN_ALL
211double g_test_timer_elapsed (void); /* elapsed seconds */
212GLIB_AVAILABLE_IN_ALL
213double g_test_timer_last (void); /* repeat last elapsed() result */
214
215/* automatically g_free or g_object_unref upon teardown */
216GLIB_AVAILABLE_IN_ALL
217void g_test_queue_free (gpointer gfree_pointer);
218GLIB_AVAILABLE_IN_ALL
219void 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)
222
223typedef 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;
228
229GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess)
230gboolean g_test_trap_fork (guint64 usec_timeout,
231 GTestTrapFlags test_trap_flags);
232
233typedef 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;
238
239GLIB_AVAILABLE_IN_2_38
240void g_test_trap_subprocess (const char *test_path,
241 guint64 usec_timeout,
242 GTestSubprocessFlags test_flags);
243
244GLIB_AVAILABLE_IN_ALL
245gboolean g_test_trap_has_passed (void);
246GLIB_AVAILABLE_IN_ALL
247gboolean 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)
254
255/* provide seed-able random numbers for tests */
256#define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
257GLIB_AVAILABLE_IN_ALL
258gint32 g_test_rand_int (void);
259GLIB_AVAILABLE_IN_ALL
260gint32 g_test_rand_int_range (gint32 begin,
261 gint32 end);
262GLIB_AVAILABLE_IN_ALL
263double g_test_rand_double (void);
264GLIB_AVAILABLE_IN_ALL
265double g_test_rand_double_range (double range_start,
266 double range_end);
267
268/*
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.
272 */
273GLIB_AVAILABLE_IN_ALL
274GTestCase* 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);
280GLIB_AVAILABLE_IN_ALL
281GTestSuite* g_test_create_suite (const char *suite_name);
282GLIB_AVAILABLE_IN_ALL
283GTestSuite* g_test_get_root (void);
284GLIB_AVAILABLE_IN_ALL
285void g_test_suite_add (GTestSuite *suite,
286 GTestCase *test_case);
287GLIB_AVAILABLE_IN_ALL
288void g_test_suite_add_suite (GTestSuite *suite,
289 GTestSuite *nestedsuite);
290GLIB_AVAILABLE_IN_ALL
291int g_test_run_suite (GTestSuite *suite);
292
293GLIB_AVAILABLE_IN_ALL
294void 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);
300GLIB_AVAILABLE_IN_ALL
301void g_assertion_message (const char *domain,
302 const char *file,
303 int line,
304 const char *func,
305 const char *message);
306GLIB_AVAILABLE_IN_ALL
307void 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;
312GLIB_AVAILABLE_IN_ALL
313void 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);
321GLIB_AVAILABLE_IN_ALL
322void 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);
331GLIB_AVAILABLE_IN_ALL
332void 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);
340GLIB_AVAILABLE_IN_ALL
341void 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);
347typedef 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;
355GLIB_VAR const GTestConfig * const g_test_config_vars;
356
357/* internal logging API */
358typedef enum {
359 G_TEST_RUN_SUCCESS,
360 G_TEST_RUN_SKIPPED,
361 G_TEST_RUN_FAILURE,
362 G_TEST_RUN_INCOMPLETE
363} GTestResult;
364
365typedef 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;
379
380typedef struct {
381 GTestLogType log_type;
382 guint n_strings;
383 gchar **strings; /* NULL terminated */
384 guint n_nums;
385 long double *nums;
386} GTestLogMsg;
387typedef struct {
388 /*< private >*/
389 GString *data;
390 GSList *msgs;
391} GTestLogBuffer;
392
393GLIB_AVAILABLE_IN_ALL
394const char* g_test_log_type_name (GTestLogType log_type);
395GLIB_AVAILABLE_IN_ALL
396GTestLogBuffer* g_test_log_buffer_new (void);
397GLIB_AVAILABLE_IN_ALL
398void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
399GLIB_AVAILABLE_IN_ALL
400void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
401 guint n_bytes,
402 const guint8 *bytes);
403GLIB_AVAILABLE_IN_ALL
404GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
405GLIB_AVAILABLE_IN_ALL
406void g_test_log_msg_free (GTestLogMsg *tmsg);
407
408/**
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()
414 *
415 * Specifies the prototype of fatal log handler functions.
416 *
417 * Returns: %TRUE if the program should abort, %FALSE otherwise
418 *
419 * Since: 2.22
420 */
421typedef gboolean (*GTestLogFatalFunc) (const gchar *log_domain,
422 GLogLevelFlags log_level,
423 const gchar *message,
424 gpointer user_data);
425GLIB_AVAILABLE_IN_ALL
426void
427g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
428 gpointer user_data);
429
430GLIB_AVAILABLE_IN_2_34
431void g_test_expect_message (const gchar *log_domain,
432 GLogLevelFlags log_level,
433 const gchar *pattern);
434GLIB_AVAILABLE_IN_2_34
435void g_test_assert_expected_messages_internal (const char *domain,
436 const char *file,
437 int line,
438 const char *func);
439
440typedef enum
441{
442 G_TEST_DIST,
443 G_TEST_BUILT
444} GTestFileType;
445
446GLIB_AVAILABLE_IN_2_38
447gchar * g_test_build_filename (GTestFileType file_type,
448 const gchar *first_path,
449 ...) G_GNUC_NULL_TERMINATED;
450GLIB_AVAILABLE_IN_2_38
451const gchar *g_test_get_dir (GTestFileType file_type);
452GLIB_AVAILABLE_IN_2_38
453const gchar *g_test_get_filename (GTestFileType file_type,
454 const gchar *first_path,
455 ...) G_GNUC_NULL_TERMINATED;
456
457#define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
458
459G_END_DECLS
460
461#endif /* __G_TEST_UTILS_H__ */
462