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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #ifndef __G_TEST_UTILS_H__
22 #define __G_TEST_UTILS_H__
24 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
25 #error "Only <glib.h> can be included directly."
28 #include <glib/gmessages.h>
29 #include <glib/gstring.h>
30 #include <glib/gerror.h>
31 #include <glib/gslist.h>
35 typedef struct GTestCase GTestCase
;
36 typedef struct GTestSuite GTestSuite
;
37 typedef void (*GTestFunc
) (void);
38 typedef void (*GTestDataFunc
) (gconstpointer user_data
);
39 typedef void (*GTestFixtureFunc
) (gpointer fixture
,
40 gconstpointer user_data
);
43 #define g_assert_cmpstr(s1, cmp, s2) do { 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); } while (0)
47 #define g_assert_cmpint(n1, cmp, n2) do { gint64 __n1 = (n1), __n2 = (n2); \
48 if (__n1 cmp __n2) ; else \
49 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
50 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
51 #define g_assert_cmpuint(n1, cmp, n2) do { guint64 __n1 = (n1), __n2 = (n2); \
52 if (__n1 cmp __n2) ; else \
53 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
54 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
55 #define g_assert_cmphex(n1, cmp, n2) do { 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, __n1, #cmp, __n2, 'x'); } while (0)
59 #define g_assert_cmpfloat(n1,cmp,n2) do { long double __n1 = (n1), __n2 = (n2); \
60 if (__n1 cmp __n2) ; else \
61 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
62 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); } while (0)
63 #define g_assert_no_error(err) do { if (err) \
64 g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
65 #err, err, 0, 0); } while (0)
66 #define g_assert_error(err, dom, c) do { if (!err || (err)->domain != dom || (err)->code != c) \
67 g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
68 #err, err, dom, c); } while (0)
69 #ifdef G_DISABLE_ASSERT
70 #define g_assert_not_reached() do { (void) 0; } while (0)
71 #define g_assert(expr) do { (void) 0; } while (0)
72 #else /* !G_DISABLE_ASSERT */
73 #define g_assert_not_reached() do { g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
74 #define g_assert(expr) do { if G_LIKELY (expr) ; else \
75 g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
77 #endif /* !G_DISABLE_ASSERT */
80 int g_strcmp0 (const char *str1
,
83 /* report performance results */
85 void g_test_minimized_result (double minimized_quantity
,
87 ...) G_GNUC_PRINTF (2, 3);
89 void g_test_maximized_result (double maximized_quantity
,
91 ...) G_GNUC_PRINTF (2, 3);
93 /* initialize testing framework */
95 void g_test_init (int *argc
,
98 /* query testing framework config */
99 #define g_test_initialized() (g_test_config_vars->test_initialized)
100 #define g_test_quick() (g_test_config_vars->test_quick)
101 #define g_test_slow() (!g_test_config_vars->test_quick)
102 #define g_test_thorough() (!g_test_config_vars->test_quick)
103 #define g_test_perf() (g_test_config_vars->test_perf)
104 #define g_test_verbose() (g_test_config_vars->test_verbose)
105 #define g_test_quiet() (g_test_config_vars->test_quiet)
106 #define g_test_undefined() (g_test_config_vars->test_undefined)
107 /* run all tests under toplevel suite (path: /) */
108 GLIB_AVAILABLE_IN_ALL
109 int g_test_run (void);
110 /* hook up a test functions under test path */
111 GLIB_AVAILABLE_IN_ALL
112 void g_test_add_func (const char *testpath
,
113 GTestFunc test_func
);
115 GLIB_AVAILABLE_IN_ALL
116 void g_test_add_data_func (const char *testpath
,
117 gconstpointer test_data
,
118 GTestDataFunc test_func
);
120 GLIB_AVAILABLE_IN_2_34
121 void g_test_add_data_func_full (const char *testpath
,
123 GTestDataFunc test_func
,
124 GDestroyNotify data_free_func
);
126 /* tell about failure */
127 GLIB_AVAILABLE_IN_2_30
128 void g_test_fail (void);
130 /* hook up a test with fixture under test path */
131 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
133 void (*add_vtable) (const char*, \
136 void (*) (Fixture*, gconstpointer), \
137 void (*) (Fixture*, gconstpointer), \
138 void (*) (Fixture*, gconstpointer)) = (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
140 (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
143 /* add test messages to the test report */
144 GLIB_AVAILABLE_IN_ALL
145 void g_test_message (const char *format
,
146 ...) G_GNUC_PRINTF (1, 2);
147 GLIB_AVAILABLE_IN_ALL
148 void g_test_bug_base (const char *uri_pattern
);
149 GLIB_AVAILABLE_IN_ALL
150 void g_test_bug (const char *bug_uri_snippet
);
151 /* measure test timings */
152 GLIB_AVAILABLE_IN_ALL
153 void g_test_timer_start (void);
154 GLIB_AVAILABLE_IN_ALL
155 double g_test_timer_elapsed (void); /* elapsed seconds */
156 GLIB_AVAILABLE_IN_ALL
157 double g_test_timer_last (void); /* repeat last elapsed() result */
159 /* automatically g_free or g_object_unref upon teardown */
160 GLIB_AVAILABLE_IN_ALL
161 void g_test_queue_free (gpointer gfree_pointer
);
162 GLIB_AVAILABLE_IN_ALL
163 void g_test_queue_destroy (GDestroyNotify destroy_func
,
164 gpointer destroy_data
);
165 #define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject)
167 /* test traps are guards used around forked tests */
169 G_TEST_TRAP_SILENCE_STDOUT
= 1 << 7,
170 G_TEST_TRAP_SILENCE_STDERR
= 1 << 8,
171 G_TEST_TRAP_INHERIT_STDIN
= 1 << 9
173 GLIB_AVAILABLE_IN_ALL
174 gboolean
g_test_trap_fork (guint64 usec_timeout
,
175 GTestTrapFlags test_trap_flags
);
176 GLIB_AVAILABLE_IN_ALL
177 gboolean
g_test_trap_has_passed (void);
178 GLIB_AVAILABLE_IN_ALL
179 gboolean
g_test_trap_reached_timeout (void);
180 #define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
181 #define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
182 #define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
183 #define g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
184 #define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
185 #define g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
187 /* provide seed-able random numbers for tests */
188 #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
189 GLIB_AVAILABLE_IN_ALL
190 gint32
g_test_rand_int (void);
191 GLIB_AVAILABLE_IN_ALL
192 gint32
g_test_rand_int_range (gint32 begin
,
194 GLIB_AVAILABLE_IN_ALL
195 double g_test_rand_double (void);
196 GLIB_AVAILABLE_IN_ALL
197 double g_test_rand_double_range (double range_start
,
200 /* semi-internal API */
201 GLIB_AVAILABLE_IN_ALL
202 GTestCase
* g_test_create_case (const char *test_name
,
204 gconstpointer test_data
,
205 GTestFixtureFunc data_setup
,
206 GTestFixtureFunc data_test
,
207 GTestFixtureFunc data_teardown
);
208 GLIB_AVAILABLE_IN_ALL
209 GTestSuite
* g_test_create_suite (const char *suite_name
);
210 GLIB_AVAILABLE_IN_ALL
211 GTestSuite
* g_test_get_root (void);
212 GLIB_AVAILABLE_IN_ALL
213 void g_test_suite_add (GTestSuite
*suite
,
214 GTestCase
*test_case
);
215 GLIB_AVAILABLE_IN_ALL
216 void g_test_suite_add_suite (GTestSuite
*suite
,
217 GTestSuite
*nestedsuite
);
218 GLIB_AVAILABLE_IN_ALL
219 int g_test_run_suite (GTestSuite
*suite
);
222 GLIB_AVAILABLE_IN_ALL
223 void g_test_trap_assertions (const char *domain
,
227 guint64 assertion_flags
, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
228 const char *pattern
);
229 GLIB_AVAILABLE_IN_ALL
230 void g_assertion_message (const char *domain
,
234 const char *message
) G_GNUC_NORETURN
;
235 GLIB_AVAILABLE_IN_ALL
236 void g_assertion_message_expr (const char *domain
,
240 const char *expr
) G_GNUC_NORETURN
;
241 GLIB_AVAILABLE_IN_ALL
242 void g_assertion_message_cmpstr (const char *domain
,
249 const char *arg2
) G_GNUC_NORETURN
;
250 GLIB_AVAILABLE_IN_ALL
251 void g_assertion_message_cmpnum (const char *domain
,
259 char numtype
) G_GNUC_NORETURN
;
260 GLIB_AVAILABLE_IN_ALL
261 void g_assertion_message_error (const char *domain
,
268 int error_code
) G_GNUC_NORETURN
;
269 GLIB_AVAILABLE_IN_ALL
270 void g_test_add_vtable (const char *testpath
,
272 gconstpointer test_data
,
273 GTestFixtureFunc data_setup
,
274 GTestFixtureFunc data_test
,
275 GTestFixtureFunc data_teardown
);
277 gboolean test_initialized
;
278 gboolean test_quick
; /* disable thorough tests */
279 gboolean test_perf
; /* run performance tests */
280 gboolean test_verbose
; /* extra info */
281 gboolean test_quiet
; /* reduce output */
282 gboolean test_undefined
; /* run tests that are meant to assert */
284 GLIB_VAR
const GTestConfig
* const g_test_config_vars
;
286 /* internal logging API */
289 G_TEST_LOG_ERROR
, /* s:msg */
290 G_TEST_LOG_START_BINARY
, /* s:binaryname s:seed */
291 G_TEST_LOG_LIST_CASE
, /* s:testpath */
292 G_TEST_LOG_SKIP_CASE
, /* s:testpath */
293 G_TEST_LOG_START_CASE
, /* s:testpath */
294 G_TEST_LOG_STOP_CASE
, /* d:status d:nforks d:elapsed */
295 G_TEST_LOG_MIN_RESULT
, /* s:blurb d:result */
296 G_TEST_LOG_MAX_RESULT
, /* s:blurb d:result */
297 G_TEST_LOG_MESSAGE
/* s:blurb */
301 GTestLogType log_type
;
303 gchar
**strings
; /* NULL terminated */
313 GLIB_AVAILABLE_IN_ALL
314 const char* g_test_log_type_name (GTestLogType log_type
);
315 GLIB_AVAILABLE_IN_ALL
316 GTestLogBuffer
* g_test_log_buffer_new (void);
317 GLIB_AVAILABLE_IN_ALL
318 void g_test_log_buffer_free (GTestLogBuffer
*tbuffer
);
319 GLIB_AVAILABLE_IN_ALL
320 void g_test_log_buffer_push (GTestLogBuffer
*tbuffer
,
322 const guint8
*bytes
);
323 GLIB_AVAILABLE_IN_ALL
324 GTestLogMsg
* g_test_log_buffer_pop (GTestLogBuffer
*tbuffer
);
325 GLIB_AVAILABLE_IN_ALL
326 void g_test_log_msg_free (GTestLogMsg
*tmsg
);
330 * @log_domain: the log domain of the message
331 * @log_level: the log level of the message (including the fatal and recursion flags)
332 * @message: the message to process
333 * @user_data: user data, set in g_test_log_set_fatal_handler()
335 * Specifies the prototype of fatal log handler functions.
337 * Return value: %TRUE if the program should abort, %FALSE otherwise
341 typedef gboolean (*GTestLogFatalFunc
) (const gchar
*log_domain
,
342 GLogLevelFlags log_level
,
343 const gchar
*message
,
345 GLIB_AVAILABLE_IN_ALL
347 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func
,
350 GLIB_AVAILABLE_IN_ALL
351 void g_test_expect_message (const gchar
*log_domain
,
352 GLogLevelFlags log_level
,
353 const gchar
*pattern
);
354 GLIB_AVAILABLE_IN_ALL
355 void g_test_assert_expected_messages_internal (const char *domain
,
360 #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
364 #endif /* __G_TEST_UTILS_H__ */