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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22 #error "Only <glib.h> can be included directly."
25 #ifndef __G_TEST_UTILS_H__
26 #define __G_TEST_UTILS_H__
32 typedef struct GTestCase GTestCase;
33 typedef struct GTestSuite GTestSuite;
36 #define g_assert_cmpstr(s1, cmp, s2) do { const char *__s1 = (s1), *__s2 = (s2); \
37 if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
38 g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
39 #s1 " " #cmp " " #s2, __s1, #cmp, __s2); } while (0)
40 #define g_assert_cmpint(n1, cmp, n2) do { gint64 __n1 = (n1), __n2 = (n2); \
41 if (__n1 cmp __n2) ; else \
42 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
43 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
44 #define g_assert_cmpuint(n1, cmp, n2) do { guint64 __n1 = (n1), __n2 = (n2); \
45 if (__n1 cmp __n2) ; else \
46 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
47 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
48 #define g_assert_cmphex(n1, cmp, n2) do { guint64 __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, 'x'); } while (0)
52 #define g_assert_cmpfloat(n1,cmp,n2) do { long double __n1 = (n1), __n2 = (n2); \
53 if (__n1 cmp __n2) ; else \
54 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
55 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); } while (0)
56 #ifdef G_DISABLE_ASSERT
57 #define g_assert_not_reached() do { (void) 0; } while (0)
58 #define g_assert(expr) do { (void) 0; } while (0)
59 #else /* !G_DISABLE_ASSERT */
60 #define g_assert_not_reached() do { g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
61 #define g_assert(expr) do { if G_LIKELY (expr) ; else \
62 g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
64 #endif /* !G_DISABLE_ASSERT */
66 int g_strcmp0 (const char *str1,
69 /* report performance results */
70 void g_test_minimized_result (double minimized_quantity,
72 ...) G_GNUC_PRINTF (2, 3);
73 void g_test_maximized_result (double maximized_quantity,
75 ...) G_GNUC_PRINTF (2, 3);
77 /* initialize testing framework */
78 void g_test_init (int *argc,
81 /* query testing framework config */
82 #define g_test_quick() (g_test_config_vars->test_quick)
83 #define g_test_slow() (!g_test_config_vars->test_quick)
84 #define g_test_thorough() (!g_test_config_vars->test_quick)
85 #define g_test_perf() (g_test_config_vars->test_perf)
86 #define g_test_verbose() (g_test_config_vars->test_verbose)
87 #define g_test_quiet() (g_test_config_vars->test_quiet)
88 /* run all tests under toplevel suite (path: /) */
89 int g_test_run (void);
90 /* hook up a test functions under test path */
91 void g_test_add_func (const char *testpath,
92 void (*test_func) (void));
93 void g_test_add_data_func (const char *testpath,
94 gconstpointer test_data,
95 void (*test_func) (gconstpointer));
96 /* hook up a test with fixture under test path */
97 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
99 void (*add_vtable) (const char*, \
102 void (*) (Fixture*, gconstpointer), \
103 void (*) (Fixture*, gconstpointer), \
104 void (*) (Fixture*, gconstpointer)) = (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
106 (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
109 /* add test messages to the test report */
110 void g_test_message (const char *format,
111 ...) G_GNUC_PRINTF (1, 2);
112 void g_test_bug_base (const char *uri_pattern);
113 void g_test_bug (const char *bug_uri_snippet);
114 /* measure test timings */
115 void g_test_timer_start (void);
116 double g_test_timer_elapsed (void); /* elapsed seconds */
117 double g_test_timer_last (void); /* repeat last elapsed() result */
119 /* automatically g_free or g_object_unref upon teardown */
120 void g_test_queue_free (gpointer gfree_pointer);
121 void g_test_queue_destroy (GDestroyNotify destroy_func,
122 gpointer destroy_data);
123 #define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject)
125 /* test traps are guards used around forked tests */
127 G_TEST_TRAP_SILENCE_STDOUT = 1 << 7,
128 G_TEST_TRAP_SILENCE_STDERR = 1 << 8,
129 G_TEST_TRAP_INHERIT_STDIN = 1 << 9
131 gboolean g_test_trap_fork (guint64 usec_timeout,
132 GTestTrapFlags test_trap_flags);
133 gboolean g_test_trap_has_passed (void);
134 gboolean g_test_trap_reached_timeout (void);
135 #define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
136 #define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
137 #define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
138 #define g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
139 #define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
140 #define g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
142 /* provide seed-able random numbers for tests */
143 #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
144 gint32 g_test_rand_int (void);
145 gint32 g_test_rand_int_range (gint32 begin,
147 double g_test_rand_double (void);
148 double g_test_rand_double_range (double range_start,
151 /* semi-internal API */
152 GTestCase* g_test_create_case (const char *test_name,
154 gconstpointer test_data,
155 void (*data_setup) (void),
156 void (*data_test) (void),
157 void (*data_teardown) (void));
158 GTestSuite* g_test_create_suite (const char *suite_name);
159 GTestSuite* g_test_get_root (void);
160 void g_test_suite_add (GTestSuite *suite,
161 GTestCase *test_case);
162 void g_test_suite_add_suite (GTestSuite *suite,
163 GTestSuite *nestedsuite);
164 int g_test_run_suite (GTestSuite *suite);
167 void g_test_trap_assertions (const char *domain,
171 guint64 assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
172 const char *pattern);
173 void g_assertion_message (const char *domain,
177 const char *message) G_GNUC_NORETURN;
178 void g_assertion_message_expr (const char *domain,
182 const char *expr) G_GNUC_NORETURN;
183 void g_assertion_message_cmpstr (const char *domain,
190 const char *arg2) G_GNUC_NORETURN;
191 void g_assertion_message_cmpnum (const char *domain,
199 char numtype) G_GNUC_NORETURN;
200 void g_test_add_vtable (const char *testpath,
202 gconstpointer test_data,
203 void (*data_setup) (void),
204 void (*data_test) (void),
205 void (*data_teardown) (void));
207 gboolean test_initialized;
208 gboolean test_quick; /* disable thorough tests */
209 gboolean test_perf; /* run performance tests */
210 gboolean test_verbose; /* extra info */
211 gboolean test_quiet; /* reduce output */
213 GLIB_VAR const GTestConfig * const g_test_config_vars;
215 /* internal logging API */
218 G_TEST_LOG_ERROR, /* s:msg */
219 G_TEST_LOG_START_BINARY, /* s:binaryname s:seed */
220 G_TEST_LOG_LIST_CASE, /* s:testpath */
221 G_TEST_LOG_SKIP_CASE, /* s:testpath */
222 G_TEST_LOG_START_CASE, /* s:testpath */
223 G_TEST_LOG_STOP_CASE, /* d:status d:nforks d:elapsed */
224 G_TEST_LOG_MIN_RESULT, /* s:blurb d:result */
225 G_TEST_LOG_MAX_RESULT, /* s:blurb d:result */
226 G_TEST_LOG_MESSAGE /* s:blurb */
230 GTestLogType log_type;
232 gchar **strings; /* NULL terminated */
242 const char* g_test_log_type_name (GTestLogType log_type);
243 GTestLogBuffer* g_test_log_buffer_new (void);
244 void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
245 void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
247 const guint8 *bytes);
248 GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
249 void g_test_log_msg_free (GTestLogMsg *tmsg);
253 #endif /* __G_TEST_UTILS_H__ */