Partly revert "gio: Add filename type annotations"
[glib.git] / glib / gtestutils.h
blobd788eb94b1fc4723f838f502d690651538c43701
1 /* GLib testing utilities
2 * Copyright (C) 2007 Imendio AB
3 * Authors: Tim Janik
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, 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."
24 #endif
26 #include <glib/gmessages.h>
27 #include <glib/gstring.h>
28 #include <glib/gerror.h>
29 #include <glib/gslist.h>
31 G_BEGIN_DECLS
33 typedef struct GTestCase GTestCase;
34 typedef struct GTestSuite GTestSuite;
35 typedef void (*GTestFunc) (void);
36 typedef void (*GTestDataFunc) (gconstpointer user_data);
37 typedef void (*GTestFixtureFunc) (gpointer fixture,
38 gconstpointer user_data);
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 */
122 GLIB_AVAILABLE_IN_ALL
123 int g_strcmp0 (const char *str1,
124 const char *str2);
126 /* report performance results */
127 GLIB_AVAILABLE_IN_ALL
128 void g_test_minimized_result (double minimized_quantity,
129 const char *format,
130 ...) G_GNUC_PRINTF (2, 3);
131 GLIB_AVAILABLE_IN_ALL
132 void g_test_maximized_result (double maximized_quantity,
133 const char *format,
134 ...) G_GNUC_PRINTF (2, 3);
136 /* initialize testing framework */
137 GLIB_AVAILABLE_IN_ALL
138 void 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)
150 GLIB_AVAILABLE_IN_2_38
151 gboolean g_test_subprocess (void);
153 /* run all tests under toplevel suite (path: /) */
154 GLIB_AVAILABLE_IN_ALL
155 int g_test_run (void);
156 /* hook up a test functions under test path */
157 GLIB_AVAILABLE_IN_ALL
158 void g_test_add_func (const char *testpath,
159 GTestFunc test_func);
161 GLIB_AVAILABLE_IN_ALL
162 void g_test_add_data_func (const char *testpath,
163 gconstpointer test_data,
164 GTestDataFunc test_func);
166 GLIB_AVAILABLE_IN_2_34
167 void g_test_add_data_func_full (const char *testpath,
168 gpointer test_data,
169 GTestDataFunc test_func,
170 GDestroyNotify data_free_func);
172 /* tell about failure */
173 GLIB_AVAILABLE_IN_2_30
174 void g_test_fail (void);
175 GLIB_AVAILABLE_IN_2_38
176 void g_test_incomplete (const gchar *msg);
177 GLIB_AVAILABLE_IN_2_38
178 void g_test_skip (const gchar *msg);
179 GLIB_AVAILABLE_IN_2_38
180 gboolean g_test_failed (void);
181 GLIB_AVAILABLE_IN_2_38
182 void g_test_set_nonfatal_assertions (void);
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
197 /* add test messages to the test report */
198 GLIB_AVAILABLE_IN_ALL
199 void g_test_message (const char *format,
200 ...) G_GNUC_PRINTF (1, 2);
201 GLIB_AVAILABLE_IN_ALL
202 void g_test_bug_base (const char *uri_pattern);
203 GLIB_AVAILABLE_IN_ALL
204 void g_test_bug (const char *bug_uri_snippet);
205 /* measure test timings */
206 GLIB_AVAILABLE_IN_ALL
207 void g_test_timer_start (void);
208 GLIB_AVAILABLE_IN_ALL
209 double g_test_timer_elapsed (void); /* elapsed seconds */
210 GLIB_AVAILABLE_IN_ALL
211 double g_test_timer_last (void); /* repeat last elapsed() result */
213 /* automatically g_free or g_object_unref upon teardown */
214 GLIB_AVAILABLE_IN_ALL
215 void g_test_queue_free (gpointer gfree_pointer);
216 GLIB_AVAILABLE_IN_ALL
217 void 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)
221 typedef 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;
227 GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess)
228 gboolean g_test_trap_fork (guint64 usec_timeout,
229 GTestTrapFlags test_trap_flags);
231 typedef 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;
237 GLIB_AVAILABLE_IN_2_38
238 void g_test_trap_subprocess (const char *test_path,
239 guint64 usec_timeout,
240 GTestSubprocessFlags test_flags);
242 GLIB_AVAILABLE_IN_ALL
243 gboolean g_test_trap_has_passed (void);
244 GLIB_AVAILABLE_IN_ALL
245 gboolean 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)
253 /* provide seed-able random numbers for tests */
254 #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
255 GLIB_AVAILABLE_IN_ALL
256 gint32 g_test_rand_int (void);
257 GLIB_AVAILABLE_IN_ALL
258 gint32 g_test_rand_int_range (gint32 begin,
259 gint32 end);
260 GLIB_AVAILABLE_IN_ALL
261 double g_test_rand_double (void);
262 GLIB_AVAILABLE_IN_ALL
263 double g_test_rand_double_range (double range_start,
264 double range_end);
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.
271 GLIB_AVAILABLE_IN_ALL
272 GTestCase* 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);
278 GLIB_AVAILABLE_IN_ALL
279 GTestSuite* g_test_create_suite (const char *suite_name);
280 GLIB_AVAILABLE_IN_ALL
281 GTestSuite* g_test_get_root (void);
282 GLIB_AVAILABLE_IN_ALL
283 void g_test_suite_add (GTestSuite *suite,
284 GTestCase *test_case);
285 GLIB_AVAILABLE_IN_ALL
286 void g_test_suite_add_suite (GTestSuite *suite,
287 GTestSuite *nestedsuite);
288 GLIB_AVAILABLE_IN_ALL
289 int g_test_run_suite (GTestSuite *suite);
291 GLIB_AVAILABLE_IN_ALL
292 void 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);
298 GLIB_AVAILABLE_IN_ALL
299 void g_assertion_message (const char *domain,
300 const char *file,
301 int line,
302 const char *func,
303 const char *message);
304 GLIB_AVAILABLE_IN_ALL
305 void 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;
310 GLIB_AVAILABLE_IN_ALL
311 void 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);
319 GLIB_AVAILABLE_IN_ALL
320 void 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);
329 GLIB_AVAILABLE_IN_ALL
330 void 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);
338 GLIB_AVAILABLE_IN_ALL
339 void 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);
345 typedef 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;
353 GLIB_VAR const GTestConfig * const g_test_config_vars;
355 /* internal logging API */
356 typedef 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;
371 typedef struct {
372 GTestLogType log_type;
373 guint n_strings;
374 gchar **strings; /* NULL terminated */
375 guint n_nums;
376 long double *nums;
377 } GTestLogMsg;
378 typedef struct {
379 /*< private >*/
380 GString *data;
381 GSList *msgs;
382 } GTestLogBuffer;
384 GLIB_AVAILABLE_IN_ALL
385 const char* g_test_log_type_name (GTestLogType log_type);
386 GLIB_AVAILABLE_IN_ALL
387 GTestLogBuffer* g_test_log_buffer_new (void);
388 GLIB_AVAILABLE_IN_ALL
389 void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
390 GLIB_AVAILABLE_IN_ALL
391 void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
392 guint n_bytes,
393 const guint8 *bytes);
394 GLIB_AVAILABLE_IN_ALL
395 GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
396 GLIB_AVAILABLE_IN_ALL
397 void g_test_log_msg_free (GTestLogMsg *tmsg);
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()
406 * Specifies the prototype of fatal log handler functions.
408 * Returns: %TRUE if the program should abort, %FALSE otherwise
410 * Since: 2.22
412 typedef gboolean (*GTestLogFatalFunc) (const gchar *log_domain,
413 GLogLevelFlags log_level,
414 const gchar *message,
415 gpointer user_data);
416 GLIB_AVAILABLE_IN_ALL
417 void
418 g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
419 gpointer user_data);
421 GLIB_AVAILABLE_IN_2_34
422 void g_test_expect_message (const gchar *log_domain,
423 GLogLevelFlags log_level,
424 const gchar *pattern);
425 GLIB_AVAILABLE_IN_2_34
426 void g_test_assert_expected_messages_internal (const char *domain,
427 const char *file,
428 int line,
429 const char *func);
431 typedef enum
433 G_TEST_DIST,
434 G_TEST_BUILT
435 } GTestFileType;
437 GLIB_AVAILABLE_IN_2_38
438 gchar * g_test_build_filename (GTestFileType file_type,
439 const gchar *first_path,
440 ...) G_GNUC_NULL_TERMINATED;
441 GLIB_AVAILABLE_IN_2_38
442 const gchar *g_test_get_dir (GTestFileType file_type);
443 GLIB_AVAILABLE_IN_2_38
444 const gchar *g_test_get_filename (GTestFileType file_type,
445 const gchar *first_path,
446 ...) G_GNUC_NULL_TERMINATED;
448 #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
450 G_END_DECLS
452 #endif /* __G_TEST_UTILS_H__ */