• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <errno.h>
31 #include <string.h>
32 
33 G_BEGIN_DECLS
34 
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);
41 
42 /* assertion API */
43 #define g_assert_cmpstr(s1, cmp, s2)    G_STMT_START { \
44                                              const char *__s1 = (s1), *__s2 = (s2); \
45                                              if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
46                                                g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
47                                                  #s1 " " #cmp " " #s2, __s1, #cmp, __s2); \
48                                         } G_STMT_END
49 #define g_assert_cmpint(n1, cmp, n2)    G_STMT_START { \
50                                              gint64 __n1 = (n1), __n2 = (n2); \
51                                              if (__n1 cmp __n2) ; else \
52                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
53                                                  #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'i'); \
54                                         } G_STMT_END
55 #define g_assert_cmpuint(n1, cmp, n2)   G_STMT_START { \
56                                              guint64 __n1 = (n1), __n2 = (n2); \
57                                              if (__n1 cmp __n2) ; else \
58                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
59                                                  #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'i'); \
60                                         } G_STMT_END
61 #define g_assert_cmphex(n1, cmp, n2)    G_STMT_START {\
62                                              guint64 __n1 = (n1), __n2 = (n2); \
63                                              if (__n1 cmp __n2) ; else \
64                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
65                                                  #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'x'); \
66                                         } G_STMT_END
67 #define g_assert_cmpfloat(n1,cmp,n2)    G_STMT_START { \
68                                              long double __n1 = (long double) (n1), __n2 = (long double) (n2); \
69                                              if (__n1 cmp __n2) ; else \
70                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
71                                                  #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'f'); \
72                                         } G_STMT_END
73 #define g_assert_cmpfloat_with_epsilon(n1,n2,epsilon) \
74                                         G_STMT_START { \
75                                              double __n1 = (n1), __n2 = (n2), __epsilon = (epsilon); \
76                                              if (G_APPROX_VALUE (__n1,  __n2, __epsilon)) ; else \
77                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
78                                                  #n1 " == " #n2 " (+/- " #epsilon ")", __n1, "==", __n2, 'f'); \
79                                         } G_STMT_END
80 #define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
81                                              gconstpointer __m1 = m1, __m2 = m2; \
82                                              int __l1 = l1, __l2 = l2; \
83                                              if (__l1 != 0 && __m1 == NULL) \
84                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
85                                                                     "assertion failed (" #l1 " == 0 || " #m1 " != NULL)"); \
86                                              else if (__l2 != 0 && __m2 == NULL) \
87                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
88                                                                     "assertion failed (" #l2 " == 0 || " #m2 " != NULL)"); \
89                                              else if (__l1 != __l2) \
90                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
91                                                                            #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", \
92                                                                            (long double) __l1, "==", (long double) __l2, 'i'); \
93                                              else if (__l1 != 0 && __m2 != NULL && memcmp (__m1, __m2, __l1) != 0) \
94                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
95                                                                     "assertion failed (" #m1 " == " #m2 ")"); \
96                                         } G_STMT_END
97 #define g_assert_cmpvariant(v1, v2) \
98   G_STMT_START \
99   { \
100     GVariant *__v1 = (v1), *__v2 = (v2); \
101     if (!g_variant_equal (__v1, __v2)) \
102       { \
103         gchar *__s1, *__s2, *__msg; \
104         __s1 = g_variant_print (__v1, TRUE); \
105         __s2 = g_variant_print (__v2, TRUE); \
106         __msg = g_strdup_printf ("assertion failed (" #v1 " == " #v2 "): %s does not equal %s", __s1, __s2); \
107         g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
108         g_free (__s1); \
109         g_free (__s2); \
110         g_free (__msg); \
111       } \
112   } \
113   G_STMT_END
114 #define g_assert_cmpstrv(strv1, strv2) \
115   G_STMT_START \
116   { \
117     const char * const *__strv1 = (const char * const *) (strv1); \
118     const char * const *__strv2 = (const char * const *) (strv2); \
119     if (!__strv1 || !__strv2) \
120       { \
121         if (__strv1) \
122           { \
123             g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
124                                  "assertion failed (" #strv1 " == " #strv2 "): " #strv2 " is NULL, but " #strv1 " is not"); \
125           } \
126         else if (__strv2) \
127           { \
128             g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
129                                  "assertion failed (" #strv1 " == " #strv2 "): " #strv1 " is NULL, but " #strv2 " is not"); \
130           } \
131       } \
132     else \
133       { \
134         guint __l1 = g_strv_length ((char **) __strv1); \
135         guint __l2 = g_strv_length ((char **) __strv2); \
136         if (__l1 != __l2) \
137           { \
138             char *__msg; \
139             __msg = g_strdup_printf ("assertion failed (" #strv1 " == " #strv2 "): length %u does not equal length %u", __l1, __l2); \
140             g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
141             g_free (__msg); \
142           } \
143         else \
144           { \
145             guint __i; \
146             for (__i = 0; __i < __l1; __i++) \
147               { \
148                 if (g_strcmp0 (__strv1[__i], __strv2[__i]) != 0) \
149                   { \
150                     g_assertion_message_cmpstrv (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
151                                                  #strv1 " == " #strv2, \
152                                                  __strv1, __strv2, __i); \
153                   } \
154               } \
155           } \
156       } \
157   } \
158   G_STMT_END
159 #define g_assert_no_errno(expr)         G_STMT_START { \
160                                              int __ret, __errsv; \
161                                              errno = 0; \
162                                              __ret = expr; \
163                                              __errsv = errno; \
164                                              if (__ret < 0) \
165                                                { \
166                                                  gchar *__msg; \
167                                                  __msg = g_strdup_printf ("assertion failed (" #expr " >= 0): errno %i: %s", __errsv, g_strerror (__errsv)); \
168                                                  g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
169                                                  g_free (__msg); \
170                                                } \
171                                         } G_STMT_END \
172                                         GLIB_AVAILABLE_MACRO_IN_2_66
173 #define g_assert_no_error(err)          G_STMT_START { \
174                                              if (err) \
175                                                g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
176                                                  #err, err, 0, 0); \
177                                         } G_STMT_END
178 #define g_assert_error(err, dom, c)     G_STMT_START { \
179                                                if (!err || (err)->domain != dom || (err)->code != c) \
180                                                g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
181                                                  #err, err, dom, c); \
182                                         } G_STMT_END
183 #define g_assert_true(expr)             G_STMT_START { \
184                                              if G_LIKELY (expr) ; else \
185                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
186                                                                     "'" #expr "' should be TRUE"); \
187                                         } G_STMT_END
188 #define g_assert_false(expr)            G_STMT_START { \
189                                              if G_LIKELY (!(expr)) ; else \
190                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
191                                                                     "'" #expr "' should be FALSE"); \
192                                         } G_STMT_END
193 
194 /* Use nullptr in C++ to catch misuse of these macros. */
195 #if defined(__cplusplus) && __cplusplus >= 201100L
196 #define g_assert_null(expr)             G_STMT_START { if G_LIKELY ((expr) == nullptr) ; else \
197                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
198                                                                     "'" #expr "' should be nullptr"); \
199                                         } G_STMT_END
200 #define g_assert_nonnull(expr)          G_STMT_START { \
201                                              if G_LIKELY ((expr) != nullptr) ; else \
202                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
203                                                                     "'" #expr "' should not be nullptr"); \
204                                         } G_STMT_END
205 #else /* not C++ */
206 #define g_assert_null(expr)             G_STMT_START { if G_LIKELY ((expr) == NULL) ; else \
207                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
208                                                                     "'" #expr "' should be NULL"); \
209                                         } G_STMT_END
210 #define g_assert_nonnull(expr)          G_STMT_START { \
211                                              if G_LIKELY ((expr) != NULL) ; else \
212                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
213                                                                     "'" #expr "' should not be NULL"); \
214                                         } G_STMT_END
215 #endif
216 
217 #ifdef G_DISABLE_ASSERT
218 /* https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable
219  * GCC 5 is not a strict lower bound for versions of GCC which provide __builtin_unreachable(). */
220 #if __GNUC__ >= 5 || g_macro__has_builtin(__builtin_unreachable)
221 #define g_assert_not_reached()          G_STMT_START { (void) 0; __builtin_unreachable (); } G_STMT_END
222 #elif defined (_MSC_VER)
223 #define g_assert_not_reached()          G_STMT_START { (void) 0; __assume (0); } G_STMT_END
224 #else  /* if __builtin_unreachable() is not supported: */
225 #define g_assert_not_reached()          G_STMT_START { (void) 0; } G_STMT_END
226 #endif
227 
228 #define g_assert(expr)                  G_STMT_START { (void) 0; } G_STMT_END
229 #else /* !G_DISABLE_ASSERT */
230 #define g_assert_not_reached()          G_STMT_START { g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } G_STMT_END
231 #define g_assert(expr)                  G_STMT_START { \
232                                              if G_LIKELY (expr) ; else \
233                                                g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
234                                                                          #expr); \
235                                         } G_STMT_END
236 #endif /* !G_DISABLE_ASSERT */
237 
238 GLIB_AVAILABLE_IN_ALL
239 int     g_strcmp0                       (const char     *str1,
240                                          const char     *str2);
241 
242 /* report performance results */
243 GLIB_AVAILABLE_IN_ALL
244 void    g_test_minimized_result         (double          minimized_quantity,
245                                          const char     *format,
246                                          ...) G_GNUC_PRINTF (2, 3);
247 GLIB_AVAILABLE_IN_ALL
248 void    g_test_maximized_result         (double          maximized_quantity,
249                                          const char     *format,
250                                          ...) G_GNUC_PRINTF (2, 3);
251 
252 /* initialize testing framework */
253 GLIB_AVAILABLE_IN_ALL
254 void    g_test_init                     (int            *argc,
255                                          char         ***argv,
256                                          ...) G_GNUC_NULL_TERMINATED;
257 
258 /**
259  * G_TEST_OPTION_ISOLATE_DIRS:
260  *
261  * Creates a unique temporary directory for each unit test and uses
262  * g_set_user_dirs() to set XDG directories to point into subdirectories of it
263  * for the duration of the unit test. The directory tree is cleaned up after the
264  * test finishes successfully. Note that this doesn’t take effect until
265  * g_test_run() is called, so calls to (for example) g_get_user_home_dir() will
266  * return the system-wide value when made in a test program’s main() function.
267  *
268  * The following functions will return subdirectories of the temporary directory
269  * when this option is used. The specific subdirectory paths in use are not
270  * guaranteed to be stable API — always use a getter function to retrieve them.
271  *
272  *  - g_get_home_dir()
273  *  - g_get_user_cache_dir()
274  *  - g_get_system_config_dirs()
275  *  - g_get_user_config_dir()
276  *  - g_get_system_data_dirs()
277  *  - g_get_user_data_dir()
278  *  - g_get_user_runtime_dir()
279  *
280  * The subdirectories may not be created by the test harness; as with normal
281  * calls to functions like g_get_user_cache_dir(), the caller must be prepared
282  * to create the directory if it doesn’t exist.
283  *
284  * Since: 2.60
285  */
286 #define G_TEST_OPTION_ISOLATE_DIRS "isolate_dirs"
287 
288 /* While we discourage its use, g_assert() is often used in unit tests
289  * (especially in legacy code). g_assert_*() should really be used instead.
290  * g_assert() can be disabled at client program compile time, which can render
291  * tests useless. Highlight that to the user. */
292 #ifdef G_DISABLE_ASSERT
293 #if defined(G_HAVE_ISO_VARARGS)
294 #define g_test_init(argc, argv, ...) \
295   G_STMT_START { \
296     g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \
297     exit (1); \
298   } G_STMT_END
299 #elif defined(G_HAVE_GNUC_VARARGS)
300 #define g_test_init(argc, argv...) \
301   G_STMT_START { \
302     g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \
303     exit (1); \
304   } G_STMT_END
305 #else  /* no varargs */
306   /* do nothing */
307 #endif  /* varargs support */
308 #endif  /* G_DISABLE_ASSERT */
309 
310 /* query testing framework config */
311 #define g_test_initialized()            (g_test_config_vars->test_initialized)
312 #define g_test_quick()                  (g_test_config_vars->test_quick)
313 #define g_test_slow()                   (!g_test_config_vars->test_quick)
314 #define g_test_thorough()               (!g_test_config_vars->test_quick)
315 #define g_test_perf()                   (g_test_config_vars->test_perf)
316 #define g_test_verbose()                (g_test_config_vars->test_verbose)
317 #define g_test_quiet()                  (g_test_config_vars->test_quiet)
318 #define g_test_undefined()              (g_test_config_vars->test_undefined)
319 GLIB_AVAILABLE_IN_2_38
320 gboolean g_test_subprocess (void);
321 
322 /* run all tests under toplevel suite (path: /) */
323 GLIB_AVAILABLE_IN_ALL
324 int     g_test_run                      (void);
325 /* hook up a test functions under test path */
326 GLIB_AVAILABLE_IN_ALL
327 void    g_test_add_func                 (const char     *testpath,
328                                          GTestFunc       test_func);
329 
330 GLIB_AVAILABLE_IN_ALL
331 void    g_test_add_data_func            (const char     *testpath,
332                                          gconstpointer   test_data,
333                                          GTestDataFunc   test_func);
334 
335 GLIB_AVAILABLE_IN_2_34
336 void    g_test_add_data_func_full       (const char     *testpath,
337                                          gpointer        test_data,
338                                          GTestDataFunc   test_func,
339                                          GDestroyNotify  data_free_func);
340 
341 /* tell about currently run test */
342 GLIB_AVAILABLE_IN_2_68
343 const char * g_test_get_path            (void);
344 
345 /* tell about failure */
346 GLIB_AVAILABLE_IN_2_30
347 void    g_test_fail                     (void);
348 GLIB_AVAILABLE_IN_2_38
349 void    g_test_incomplete               (const gchar *msg);
350 GLIB_AVAILABLE_IN_2_38
351 void    g_test_skip                     (const gchar *msg);
352 GLIB_AVAILABLE_IN_2_38
353 gboolean g_test_failed                  (void);
354 GLIB_AVAILABLE_IN_2_38
355 void    g_test_set_nonfatal_assertions  (void);
356 
357 /**
358  * g_test_add:
359  * @testpath:  The test path for a new test case.
360  * @Fixture:   The type of a fixture data structure.
361  * @tdata:     Data argument for the test functions.
362  * @fsetup:    The function to set up the fixture data.
363  * @ftest:     The actual test function.
364  * @fteardown: The function to tear down the fixture data.
365  *
366  * Hook up a new test case at @testpath, similar to g_test_add_func().
367  * A fixture data structure with setup and teardown functions may be provided,
368  * similar to g_test_create_case().
369  *
370  * g_test_add() is implemented as a macro, so that the fsetup(), ftest() and
371  * fteardown() callbacks can expect a @Fixture pointer as their first argument
372  * in a type safe manner. They otherwise have type #GTestFixtureFunc.
373  *
374  * Since: 2.16
375  */
376 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
377 					G_STMT_START {			\
378                                          void (*add_vtable) (const char*,       \
379                                                     gsize,             \
380                                                     gconstpointer,     \
381                                                     void (*) (Fixture*, gconstpointer),   \
382                                                     void (*) (Fixture*, gconstpointer),   \
383                                                     void (*) (Fixture*, gconstpointer)) =  (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
384                                          add_vtable \
385                                           (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
386 					} G_STMT_END
387 
388 /* add test messages to the test report */
389 GLIB_AVAILABLE_IN_ALL
390 void    g_test_message                  (const char *format,
391                                          ...) G_GNUC_PRINTF (1, 2);
392 GLIB_AVAILABLE_IN_ALL
393 void    g_test_bug_base                 (const char *uri_pattern);
394 GLIB_AVAILABLE_IN_ALL
395 void    g_test_bug                      (const char *bug_uri_snippet);
396 GLIB_AVAILABLE_IN_2_62
397 void    g_test_summary                  (const char *summary);
398 /* measure test timings */
399 GLIB_AVAILABLE_IN_ALL
400 void    g_test_timer_start              (void);
401 GLIB_AVAILABLE_IN_ALL
402 double  g_test_timer_elapsed            (void); /* elapsed seconds */
403 GLIB_AVAILABLE_IN_ALL
404 double  g_test_timer_last               (void); /* repeat last elapsed() result */
405 
406 /* automatically g_free or g_object_unref upon teardown */
407 GLIB_AVAILABLE_IN_ALL
408 void    g_test_queue_free               (gpointer gfree_pointer);
409 GLIB_AVAILABLE_IN_ALL
410 void    g_test_queue_destroy            (GDestroyNotify destroy_func,
411                                          gpointer       destroy_data);
412 #define g_test_queue_unref(gobject)     g_test_queue_destroy (g_object_unref, gobject)
413 
414 /**
415  * GTestTrapFlags:
416  * @G_TEST_TRAP_SILENCE_STDOUT: Redirect stdout of the test child to
417  *     `/dev/null` so it cannot be observed on the console during test
418  *     runs. The actual output is still captured though to allow later
419  *     tests with g_test_trap_assert_stdout().
420  * @G_TEST_TRAP_SILENCE_STDERR: Redirect stderr of the test child to
421  *     `/dev/null` so it cannot be observed on the console during test
422  *     runs. The actual output is still captured though to allow later
423  *     tests with g_test_trap_assert_stderr().
424  * @G_TEST_TRAP_INHERIT_STDIN: If this flag is given, stdin of the
425  *     child process is shared with stdin of its parent process.
426  *     It is redirected to `/dev/null` otherwise.
427  *
428  * Test traps are guards around forked tests.
429  * These flags determine what traps to set.
430  *
431  * Deprecated: 2.38: #GTestTrapFlags is used only with g_test_trap_fork(),
432  * which is deprecated. g_test_trap_subprocess() uses
433  * #GTestSubprocessFlags.
434  */
435 typedef enum {
436   G_TEST_TRAP_SILENCE_STDOUT    = 1 << 7,
437   G_TEST_TRAP_SILENCE_STDERR    = 1 << 8,
438   G_TEST_TRAP_INHERIT_STDIN     = 1 << 9
439 } GTestTrapFlags GLIB_DEPRECATED_TYPE_IN_2_38_FOR(GTestSubprocessFlags);
440 
441 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
442 
443 GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess)
444 gboolean g_test_trap_fork               (guint64              usec_timeout,
445                                          GTestTrapFlags       test_trap_flags);
446 
447 G_GNUC_END_IGNORE_DEPRECATIONS
448 
449 typedef enum {
450   G_TEST_SUBPROCESS_INHERIT_STDIN  = 1 << 0,
451   G_TEST_SUBPROCESS_INHERIT_STDOUT = 1 << 1,
452   G_TEST_SUBPROCESS_INHERIT_STDERR = 1 << 2
453 } GTestSubprocessFlags;
454 
455 GLIB_AVAILABLE_IN_2_38
456 void     g_test_trap_subprocess         (const char           *test_path,
457                                          guint64               usec_timeout,
458                                          GTestSubprocessFlags  test_flags);
459 
460 GLIB_AVAILABLE_IN_ALL
461 gboolean g_test_trap_has_passed         (void);
462 GLIB_AVAILABLE_IN_ALL
463 gboolean g_test_trap_reached_timeout    (void);
464 #define  g_test_trap_assert_passed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
465 #define  g_test_trap_assert_failed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
466 #define  g_test_trap_assert_stdout(soutpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
467 #define  g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
468 #define  g_test_trap_assert_stderr(serrpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
469 #define  g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
470 
471 /* provide seed-able random numbers for tests */
472 #define  g_test_rand_bit()              (0 != (g_test_rand_int() & (1 << 15)))
473 GLIB_AVAILABLE_IN_ALL
474 gint32   g_test_rand_int                (void);
475 GLIB_AVAILABLE_IN_ALL
476 gint32   g_test_rand_int_range          (gint32          begin,
477                                          gint32          end);
478 GLIB_AVAILABLE_IN_ALL
479 double   g_test_rand_double             (void);
480 GLIB_AVAILABLE_IN_ALL
481 double   g_test_rand_double_range       (double          range_start,
482                                          double          range_end);
483 
484 /*
485  * semi-internal API: non-documented symbols with stable ABI. You
486  * should use the non-internal helper macros instead. However, for
487  * compatibility reason, you may use this semi-internal API.
488  */
489 GLIB_AVAILABLE_IN_ALL
490 GTestCase*    g_test_create_case        (const char       *test_name,
491                                          gsize             data_size,
492                                          gconstpointer     test_data,
493                                          GTestFixtureFunc  data_setup,
494                                          GTestFixtureFunc  data_test,
495                                          GTestFixtureFunc  data_teardown);
496 GLIB_AVAILABLE_IN_ALL
497 GTestSuite*   g_test_create_suite       (const char       *suite_name);
498 GLIB_AVAILABLE_IN_ALL
499 GTestSuite*   g_test_get_root           (void);
500 GLIB_AVAILABLE_IN_ALL
501 void          g_test_suite_add          (GTestSuite     *suite,
502                                          GTestCase      *test_case);
503 GLIB_AVAILABLE_IN_ALL
504 void          g_test_suite_add_suite    (GTestSuite     *suite,
505                                          GTestSuite     *nestedsuite);
506 GLIB_AVAILABLE_IN_ALL
507 int           g_test_run_suite          (GTestSuite     *suite);
508 
509 GLIB_AVAILABLE_IN_ALL
510 void    g_test_trap_assertions          (const char     *domain,
511                                          const char     *file,
512                                          int             line,
513                                          const char     *func,
514                                          guint64         assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
515                                          const char     *pattern);
516 GLIB_AVAILABLE_IN_ALL
517 void    g_assertion_message             (const char     *domain,
518                                          const char     *file,
519                                          int             line,
520                                          const char     *func,
521                                          const char     *message) G_ANALYZER_NORETURN;
522 GLIB_AVAILABLE_IN_ALL
523 G_NORETURN
524 void    g_assertion_message_expr        (const char     *domain,
525                                          const char     *file,
526                                          int             line,
527                                          const char     *func,
528                                          const char     *expr);
529 GLIB_AVAILABLE_IN_ALL
530 void    g_assertion_message_cmpstr      (const char     *domain,
531                                          const char     *file,
532                                          int             line,
533                                          const char     *func,
534                                          const char     *expr,
535                                          const char     *arg1,
536                                          const char     *cmp,
537                                          const char     *arg2) G_ANALYZER_NORETURN;
538 
539 GLIB_AVAILABLE_IN_2_68
540 void    g_assertion_message_cmpstrv     (const char         *domain,
541                                          const char         *file,
542                                          int                 line,
543                                          const char         *func,
544                                          const char         *expr,
545                                          const char * const *arg1,
546                                          const char * const *arg2,
547                                          gsize               first_wrong_idx) G_ANALYZER_NORETURN;
548 GLIB_AVAILABLE_IN_ALL
549 void    g_assertion_message_cmpnum      (const char     *domain,
550                                          const char     *file,
551                                          int             line,
552                                          const char     *func,
553                                          const char     *expr,
554                                          long double     arg1,
555                                          const char     *cmp,
556                                          long double     arg2,
557                                          char            numtype) G_ANALYZER_NORETURN;
558 GLIB_AVAILABLE_IN_ALL
559 void    g_assertion_message_error       (const char     *domain,
560                                          const char     *file,
561                                          int             line,
562                                          const char     *func,
563                                          const char     *expr,
564                                          const GError   *error,
565                                          GQuark          error_domain,
566                                          int             error_code) G_ANALYZER_NORETURN;
567 GLIB_AVAILABLE_IN_ALL
568 void    g_test_add_vtable               (const char     *testpath,
569                                          gsize           data_size,
570                                          gconstpointer   test_data,
571                                          GTestFixtureFunc  data_setup,
572                                          GTestFixtureFunc  data_test,
573                                          GTestFixtureFunc  data_teardown);
574 typedef struct {
575   gboolean      test_initialized;
576   gboolean      test_quick;     /* disable thorough tests */
577   gboolean      test_perf;      /* run performance tests */
578   gboolean      test_verbose;   /* extra info */
579   gboolean      test_quiet;     /* reduce output */
580   gboolean      test_undefined; /* run tests that are meant to assert */
581 } GTestConfig;
582 GLIB_VAR const GTestConfig * const g_test_config_vars;
583 
584 /* internal logging API */
585 typedef enum {
586   G_TEST_RUN_SUCCESS,
587   G_TEST_RUN_SKIPPED,
588   G_TEST_RUN_FAILURE,
589   G_TEST_RUN_INCOMPLETE
590 } GTestResult;
591 
592 typedef enum {
593   G_TEST_LOG_NONE,
594   G_TEST_LOG_ERROR,             /* s:msg */
595   G_TEST_LOG_START_BINARY,      /* s:binaryname s:seed */
596   G_TEST_LOG_LIST_CASE,         /* s:testpath */
597   G_TEST_LOG_SKIP_CASE,         /* s:testpath */
598   G_TEST_LOG_START_CASE,        /* s:testpath */
599   G_TEST_LOG_STOP_CASE,         /* d:status d:nforks d:elapsed */
600   G_TEST_LOG_MIN_RESULT,        /* s:blurb d:result */
601   G_TEST_LOG_MAX_RESULT,        /* s:blurb d:result */
602   G_TEST_LOG_MESSAGE,           /* s:blurb */
603   G_TEST_LOG_START_SUITE,
604   G_TEST_LOG_STOP_SUITE
605 } GTestLogType;
606 
607 typedef struct {
608   GTestLogType  log_type;
609   guint         n_strings;
610   gchar       **strings; /* NULL terminated */
611   guint         n_nums;
612   long double  *nums;
613 } GTestLogMsg;
614 typedef struct {
615   /*< private >*/
616   GString     *data;
617   GSList      *msgs;
618 } GTestLogBuffer;
619 
620 GLIB_AVAILABLE_IN_ALL
621 const char*     g_test_log_type_name    (GTestLogType    log_type);
622 GLIB_AVAILABLE_IN_ALL
623 GTestLogBuffer* g_test_log_buffer_new   (void);
624 GLIB_AVAILABLE_IN_ALL
625 void            g_test_log_buffer_free  (GTestLogBuffer *tbuffer);
626 GLIB_AVAILABLE_IN_ALL
627 void            g_test_log_buffer_push  (GTestLogBuffer *tbuffer,
628                                          guint           n_bytes,
629                                          const guint8   *bytes);
630 GLIB_AVAILABLE_IN_ALL
631 GTestLogMsg*    g_test_log_buffer_pop   (GTestLogBuffer *tbuffer);
632 GLIB_AVAILABLE_IN_ALL
633 void            g_test_log_msg_free     (GTestLogMsg    *tmsg);
634 
635 /**
636  * GTestLogFatalFunc:
637  * @log_domain: the log domain of the message
638  * @log_level: the log level of the message (including the fatal and recursion flags)
639  * @message: the message to process
640  * @user_data: user data, set in g_test_log_set_fatal_handler()
641  *
642  * Specifies the prototype of fatal log handler functions.
643  *
644  * Returns: %TRUE if the program should abort, %FALSE otherwise
645  *
646  * Since: 2.22
647  */
648 typedef gboolean        (*GTestLogFatalFunc)    (const gchar    *log_domain,
649                                                  GLogLevelFlags  log_level,
650                                                  const gchar    *message,
651                                                  gpointer        user_data);
652 GLIB_AVAILABLE_IN_ALL
653 void
654 g_test_log_set_fatal_handler            (GTestLogFatalFunc log_func,
655                                          gpointer          user_data);
656 
657 GLIB_AVAILABLE_IN_2_34
658 void    g_test_expect_message                    (const gchar    *log_domain,
659                                                   GLogLevelFlags  log_level,
660                                                   const gchar    *pattern);
661 GLIB_AVAILABLE_IN_2_34
662 void    g_test_assert_expected_messages_internal (const char     *domain,
663                                                   const char     *file,
664                                                   int             line,
665                                                   const char     *func);
666 
667 typedef enum
668 {
669   G_TEST_DIST,
670   G_TEST_BUILT
671 } GTestFileType;
672 
673 GLIB_AVAILABLE_IN_2_38
674 gchar * g_test_build_filename                    (GTestFileType   file_type,
675                                                   const gchar    *first_path,
676                                                   ...) G_GNUC_NULL_TERMINATED;
677 GLIB_AVAILABLE_IN_2_38
678 const gchar *g_test_get_dir                      (GTestFileType   file_type);
679 GLIB_AVAILABLE_IN_2_38
680 const gchar *g_test_get_filename                 (GTestFileType   file_type,
681                                                   const gchar    *first_path,
682                                                   ...) G_GNUC_NULL_TERMINATED;
683 
684 #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
685 
686 G_END_DECLS
687 
688 #endif /* __G_TEST_UTILS_H__ */
689