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.
20 #ifndef __G_TEST_UTILS_H__
21 #define __G_TEST_UTILS_H__
27 typedef struct GTestCase GTestCase;
28 typedef struct GTestSuite GTestSuite;
31 #define g_assert_cmpstr(s1, cmp, s2) do { const char *__s1 = (s1), *__s2 = (s2); \
32 if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
33 g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
34 #s1 " " #cmp " " #s2, __s1, #cmp, __s2); } while (0)
35 #define g_assert_cmpint(n1, cmp, n2) do { gint64 __n1 = (n1), __n2 = (n2); \
36 if (__n1 cmp __n2) ; else \
37 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
38 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
39 #define g_assert_cmpuint(n1, cmp, n2) do { guint64 __n1 = (n1), __n2 = (n2); \
40 if (__n1 cmp __n2) ; else \
41 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
42 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
43 #define g_assert_cmphex(n1, cmp, n2) do { guint64 __n1 = (n1), __n2 = (n2); \
44 if (__n1 cmp __n2) ; else \
45 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
46 #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'x'); } while (0)
47 #define g_assert_cmpfloat(n1,cmp,n2) do { long double __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, 'f'); } while (0)
51 int g_strcmp0 (const char *str1,
53 // g_assert(condition) /*...*/
54 // g_assert_not_reached() /*...*/
56 /* report performance results */
57 void g_test_minimized_result (double minimized_quantity,
59 ...) G_GNUC_PRINTF (2, 3);
60 void g_test_maximized_result (double maximized_quantity,
62 ...) G_GNUC_PRINTF (2, 3);
64 /* initialize testing framework */
65 void g_test_init (int *argc,
68 /* query testing framework config */
69 #define g_test_quick() (g_test_config_vars->test_quick)
70 #define g_test_slow() (!g_test_config_vars->test_quick)
71 #define g_test_thorough() (!g_test_config_vars->test_quick)
72 #define g_test_perf() (g_test_config_vars->test_perf)
73 #define g_test_verbose() (g_test_config_vars->test_verbose)
74 #define g_test_quiet() (g_test_config_vars->test_quiet)
75 /* run all tests under toplevel suite (path: /) */
76 int g_test_run (void);
77 /* hook up a test functions under test path */
78 void g_test_add_func (const char *testpath,
79 void (*test_func) (void));
80 void g_test_add_data_func (const char *testpath,
81 gconstpointer test_data,
82 void (*test_func) (gconstpointer));
83 /* hook up a test with fixture under test path */
84 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
85 ((void (*) (const char*, \
88 void (*) (Fixture*, gconstpointer), \
89 void (*) (Fixture*, gconstpointer), \
90 void (*) (Fixture*, gconstpointer))) \
91 (void*) g_test_add_vtable) \
92 (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown)
93 /* add test messages to the test report */
94 void g_test_message (const char *format,
95 ...) G_GNUC_PRINTF (1, 2);
96 void g_test_bug_base (const char *uri_pattern);
97 void g_test_bug (const char *bug_uri_snippet);
98 /* measure test timings */
99 void g_test_timer_start (void);
100 double g_test_timer_elapsed (void); // elapsed seconds
101 double g_test_timer_last (void); // repeat last elapsed() result
103 /* automatically g_free or g_object_unref upon teardown */
104 void g_test_queue_free (gpointer gfree_pointer);
105 void g_test_queue_destroy (GDestroyNotify destroy_func,
106 gpointer destroy_data);
107 #define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject)
109 /* test traps are guards used around forked tests */
111 G_TEST_TRAP_SILENCE_STDOUT = 1 << 7,
112 G_TEST_TRAP_SILENCE_STDERR = 1 << 8,
113 G_TEST_TRAP_INHERIT_STDIN = 1 << 9,
115 gboolean g_test_trap_fork (guint64 usec_timeout,
116 GTestTrapFlags test_trap_flags);
117 gboolean g_test_trap_has_passed (void);
118 gboolean g_test_trap_reached_timeout (void);
119 #define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0, 0, 0)
120 #define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 1, 0, 0)
121 #define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0, soutpattern, 0)
122 #define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0, 0, serrpattern)
124 /* provide seed-able random numbers for tests */
125 #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
126 gint32 g_test_rand_int (void);
127 gint32 g_test_rand_int_range (gint32 begin,
129 double g_test_rand_double (void);
130 double g_test_rand_double_range (double range_start,
133 /* semi-internal API */
134 GTestCase* g_test_create_case (const char *test_name,
136 gconstpointer test_data,
137 void (*data_setup) (void),
138 void (*data_test) (void),
139 void (*data_teardown) (void));
140 GTestSuite* g_test_create_suite (const char *suite_name);
141 GTestSuite* g_test_get_root (void);
142 void g_test_suite_add (GTestSuite *suite,
143 GTestCase *test_case);
144 void g_test_suite_add_suite (GTestSuite *suite,
145 GTestSuite *nestedsuite);
146 int g_test_run_suite (GTestSuite *suite);
149 void g_test_trap_assertions (const char *domain,
155 const char *stdout_pattern,
156 const char *stderr_pattern);
157 void g_assertion_message (const char *domain,
161 const char *message);
162 void g_assertion_message_expr (const char *domain,
167 void g_assertion_message_cmpstr (const char *domain,
175 void g_assertion_message_cmpnum (const char *domain,
184 void g_test_add_vtable (const char *testpath,
186 gconstpointer test_data,
187 void (*data_setup) (void),
188 void (*data_test) (void),
189 void (*data_teardown) (void));
191 gboolean test_initialized;
192 gboolean test_quick; /* disable thorough tests */
193 gboolean test_perf; /* run performance tests */
194 gboolean test_verbose; /* extra info */
195 gboolean test_quiet; /* reduce output */
197 GLIB_VAR const GTestConfig * const g_test_config_vars;
199 /* internal logging API */
202 G_TEST_LOG_ERROR, // s:msg
203 G_TEST_LOG_START_BINARY, // s:binaryname s:seed
204 G_TEST_LOG_LIST_CASE, // s:testpath
205 G_TEST_LOG_SKIP_CASE, // s:testpath
206 G_TEST_LOG_START_CASE, // s:testpath
207 G_TEST_LOG_STOP_CASE, // d:status d:nforks d:elapsed
208 G_TEST_LOG_MIN_RESULT, // s:blurb d:result
209 G_TEST_LOG_MAX_RESULT, // s:blurb d:result
210 G_TEST_LOG_MESSAGE, // s:blurb
214 GTestLogType log_type;
216 gchar **strings; // NULL terminated
226 const char* g_test_log_type_name (GTestLogType log_type);
227 GTestLogBuffer* g_test_log_buffer_new (void);
228 void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
229 void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
231 const guint8 *bytes);
232 GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
233 void g_test_log_msg_free (GTestLogMsg *tmsg);
237 #endif /* __G_TEST_UTILS_H__ */