Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-18 09:15:35

0001 /* GLib testing utilities
0002  * Copyright (C) 2007 Imendio AB
0003  * Authors: Tim Janik
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-or-later
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Lesser General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2.1 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #ifndef __G_TEST_UTILS_H__
0022 #define __G_TEST_UTILS_H__
0023 
0024 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
0025 #error "Only <glib.h> can be included directly."
0026 #endif
0027 
0028 #include <glib/gmessages.h>
0029 #include <glib/gstring.h>
0030 #include <glib/gerror.h>
0031 #include <glib/gslist.h>
0032 #include <errno.h>
0033 #include <stdlib.h>
0034 #include <string.h>
0035 
0036 G_BEGIN_DECLS
0037 
0038 typedef struct GTestCase  GTestCase;
0039 typedef struct GTestSuite GTestSuite;
0040 typedef void (*GTestFunc)        (void);
0041 typedef void (*GTestDataFunc)    (gconstpointer user_data);
0042 typedef void (*GTestFixtureFunc) (gpointer      fixture,
0043                                   gconstpointer user_data);
0044 
0045 /* assertion API */
0046 #define g_assert_cmpstr(s1, cmp, s2)    G_STMT_START { \
0047                                              const char *__s1 = (s1), *__s2 = (s2); \
0048                                              if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
0049                                                g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0050                                                  #s1 " " #cmp " " #s2, __s1, #cmp, __s2); \
0051                                         } G_STMT_END
0052 #if GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_78
0053 #define g_assert_cmpint(n1, cmp, n2)    G_STMT_START { \
0054                                              gint64 __n1 = (n1), __n2 = (n2); \
0055                                              if (__n1 cmp __n2) ; else \
0056                                                g_assertion_message_cmpint (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0057                                                  #n1 " " #cmp " " #n2, (guint64)__n1, #cmp, (guint64)__n2, 'i'); \
0058                                         } G_STMT_END
0059 #define g_assert_cmpuint(n1, cmp, n2)   G_STMT_START { \
0060                                              guint64 __n1 = (n1), __n2 = (n2); \
0061                                              if (__n1 cmp __n2) ; else \
0062                                                g_assertion_message_cmpint (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0063                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'u'); \
0064                                         } G_STMT_END
0065 #define g_assert_cmphex(n1, cmp, n2)    G_STMT_START { \
0066                                              guint64 __n1 = (n1), __n2 = (n2); \
0067                                              if (__n1 cmp __n2) ; else \
0068                                                g_assertion_message_cmpint (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0069                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'x'); \
0070                                         } G_STMT_END
0071 #else /* GLIB_VERSION_MIN_REQUIRED < GLIB_VERSION_2_78 */
0072 #define g_assert_cmpint(n1, cmp, n2)    G_STMT_START { \
0073                                              gint64 __n1 = (n1), __n2 = (n2); \
0074                                              if (__n1 cmp __n2) ; else \
0075                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0076                                                  #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'i'); \
0077                                         } G_STMT_END
0078 #define g_assert_cmpuint(n1, cmp, n2)   G_STMT_START { \
0079                                              guint64 __n1 = (n1), __n2 = (n2); \
0080                                              if (__n1 cmp __n2) ; else \
0081                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0082                                                  #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'i'); \
0083                                         } G_STMT_END
0084 #define g_assert_cmphex(n1, cmp, n2)    G_STMT_START {\
0085                                              guint64 __n1 = (n1), __n2 = (n2); \
0086                                              if (__n1 cmp __n2) ; else \
0087                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0088                                                  #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'x'); \
0089                                         } G_STMT_END
0090 #endif /* GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_78 */
0091 #define g_assert_cmpfloat(n1,cmp,n2)    G_STMT_START { \
0092                                              long double __n1 = (long double) (n1), __n2 = (long double) (n2); \
0093                                              if (__n1 cmp __n2) ; else \
0094                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0095                                                  #n1 " " #cmp " " #n2, (long double) __n1, #cmp, (long double) __n2, 'f'); \
0096                                         } G_STMT_END
0097 #define g_assert_cmpfloat_with_epsilon(n1,n2,epsilon) \
0098                                         G_STMT_START { \
0099                                              double __n1 = (n1), __n2 = (n2), __epsilon = (epsilon); \
0100                                              if (G_APPROX_VALUE (__n1,  __n2, __epsilon)) ; else \
0101                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0102                                                  #n1 " == " #n2 " (+/- " #epsilon ")", __n1, "==", __n2, 'f'); \
0103                                         } G_STMT_END
0104 #if GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_78
0105 #define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
0106                                              gconstpointer __m1 = m1, __m2 = m2; \
0107                                              size_t __l1 = (size_t) l1, __l2 = (size_t) l2; \
0108                                              if (__l1 != 0 && __m1 == NULL) \
0109                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0110                                                                     "assertion failed (" #l1 " == 0 || " #m1 " != NULL)"); \
0111                                              else if (__l2 != 0 && __m2 == NULL) \
0112                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0113                                                                     "assertion failed (" #l2 " == 0 || " #m2 " != NULL)"); \
0114                                              else if (__l1 != __l2) \
0115                                                g_assertion_message_cmpint (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0116                                                                            #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", \
0117                                                                            __l1, "==", __l2, 'u'); \
0118                                              else if (__l1 != 0 && __m2 != NULL && memcmp (__m1, __m2, __l1) != 0) \
0119                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0120                                                                     "assertion failed (" #m1 " == " #m2 ")"); \
0121                                         } G_STMT_END
0122 #else /* GLIB_VERSION_MIN_REQUIRED < GLIB_VERSION_2_78 */
0123 #define g_assert_cmpmem(m1, l1, m2, l2) G_STMT_START {\
0124                                              gconstpointer __m1 = m1, __m2 = m2; \
0125                                              size_t __l1 = (size_t) l1, __l2 = (size_t) l2; \
0126                                              if (__l1 != 0 && __m1 == NULL) \
0127                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0128                                                                     "assertion failed (" #l1 " == 0 || " #m1 " != NULL)"); \
0129                                              else if (__l2 != 0 && __m2 == NULL) \
0130                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0131                                                                     "assertion failed (" #l2 " == 0 || " #m2 " != NULL)"); \
0132                                              else if (__l1 != __l2) \
0133                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0134                                                                            #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", \
0135                                                                            (long double) __l1, "==", (long double) __l2, 'i'); \
0136                                              else if (__l1 != 0 && __m2 != NULL && memcmp (__m1, __m2, __l1) != 0) \
0137                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0138                                                                     "assertion failed (" #m1 " == " #m2 ")"); \
0139                                         } G_STMT_END
0140 #endif /* GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_78 */
0141 #define g_assert_cmpvariant(v1, v2) \
0142   G_STMT_START \
0143   { \
0144     GVariant *__v1 = (v1), *__v2 = (v2); \
0145     if (!g_variant_equal (__v1, __v2)) \
0146       { \
0147         gchar *__s1, *__s2, *__msg; \
0148         __s1 = g_variant_print (__v1, TRUE); \
0149         __s2 = g_variant_print (__v2, TRUE); \
0150         __msg = g_strdup_printf ("assertion failed (" #v1 " == " #v2 "): %s does not equal %s", __s1, __s2); \
0151         g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
0152         g_free (__s1); \
0153         g_free (__s2); \
0154         g_free (__msg); \
0155       } \
0156   } \
0157   G_STMT_END
0158 #define g_assert_cmpstrv(strv1, strv2) \
0159   G_STMT_START \
0160   { \
0161     const char * const *__strv1 = (const char * const *) (strv1); \
0162     const char * const *__strv2 = (const char * const *) (strv2); \
0163     if (!__strv1 || !__strv2) \
0164       { \
0165         if (__strv1) \
0166           { \
0167             g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0168                                  "assertion failed (" #strv1 " == " #strv2 "): " #strv2 " is NULL, but " #strv1 " is not"); \
0169           } \
0170         else if (__strv2) \
0171           { \
0172             g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0173                                  "assertion failed (" #strv1 " == " #strv2 "): " #strv1 " is NULL, but " #strv2 " is not"); \
0174           } \
0175       } \
0176     else \
0177       { \
0178         guint __l1 = g_strv_length ((char **) __strv1); \
0179         guint __l2 = g_strv_length ((char **) __strv2); \
0180         if (__l1 != __l2) \
0181           { \
0182             char *__msg; \
0183             __msg = g_strdup_printf ("assertion failed (" #strv1 " == " #strv2 "): length %u does not equal length %u", __l1, __l2); \
0184             g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
0185             g_free (__msg); \
0186           } \
0187         else \
0188           { \
0189             guint __i; \
0190             for (__i = 0; __i < __l1; __i++) \
0191               { \
0192                 if (g_strcmp0 (__strv1[__i], __strv2[__i]) != 0) \
0193                   { \
0194                     g_assertion_message_cmpstrv (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0195                                                  #strv1 " == " #strv2, \
0196                                                  __strv1, __strv2, __i); \
0197                   } \
0198               } \
0199           } \
0200       } \
0201   } \
0202   G_STMT_END
0203 #define g_assert_no_errno(expr)         G_STMT_START { \
0204                                              int __ret, __errsv; \
0205                                              errno = 0; \
0206                                              __ret = expr; \
0207                                              __errsv = errno; \
0208                                              if (__ret < 0) \
0209                                                { \
0210                                                  gchar *__msg; \
0211                                                  __msg = g_strdup_printf ("assertion failed (" #expr " >= 0): errno %i: %s", __errsv, g_strerror (__errsv)); \
0212                                                  g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, __msg); \
0213                                                  g_free (__msg); \
0214                                                } \
0215                                         } G_STMT_END \
0216                                         GLIB_AVAILABLE_MACRO_IN_2_66
0217 #define g_assert_no_error(err)          G_STMT_START { \
0218                                              if (err) \
0219                                                g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0220                                                  #err, err, 0, 0); \
0221                                         } G_STMT_END
0222 #define g_assert_error(err, dom, c)     G_STMT_START { \
0223                                                if (!err || (err)->domain != dom || (err)->code != c) \
0224                                                g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0225                                                  #err, err, dom, c); \
0226                                         } G_STMT_END
0227 #define g_assert_true(expr)             G_STMT_START { \
0228                                              if G_LIKELY (expr) ; else \
0229                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0230                                                                     "'" #expr "' should be TRUE"); \
0231                                         } G_STMT_END
0232 #define g_assert_false(expr)            G_STMT_START { \
0233                                              if G_LIKELY (!(expr)) ; else \
0234                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0235                                                                     "'" #expr "' should be FALSE"); \
0236                                         } G_STMT_END
0237 
0238 /* Use nullptr in C++ to catch misuse of these macros. */
0239 #if G_CXX_STD_CHECK_VERSION (11)
0240 #define g_assert_null(expr)             G_STMT_START { if G_LIKELY ((expr) == nullptr) ; else \
0241                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0242                                                                     "'" #expr "' should be nullptr"); \
0243                                         } G_STMT_END
0244 #define g_assert_nonnull(expr)          G_STMT_START { \
0245                                              if G_LIKELY ((expr) != nullptr) ; else \
0246                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0247                                                                     "'" #expr "' should not be nullptr"); \
0248                                         } G_STMT_END
0249 #else /* not C++ */
0250 #define g_assert_null(expr)             G_STMT_START { if G_LIKELY ((expr) == NULL) ; else \
0251                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0252                                                                     "'" #expr "' should be NULL"); \
0253                                         } G_STMT_END
0254 #define g_assert_nonnull(expr)          G_STMT_START { \
0255                                              if G_LIKELY ((expr) != NULL) ; else \
0256                                                g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0257                                                                     "'" #expr "' should not be NULL"); \
0258                                         } G_STMT_END
0259 #endif
0260 
0261 #ifdef G_DISABLE_ASSERT
0262 /* https://gcc.gnu.org/onlinedocs/gcc-8.3.0/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable
0263  * GCC 5 is not a strict lower bound for versions of GCC which provide __builtin_unreachable(). */
0264 #if __GNUC__ >= 5 || g_macro__has_builtin(__builtin_unreachable)
0265 #define g_assert_not_reached()          G_STMT_START { (void) 0; __builtin_unreachable (); } G_STMT_END
0266 #elif defined (_MSC_VER)
0267 #define g_assert_not_reached()          G_STMT_START { (void) 0; __assume (0); } G_STMT_END
0268 #else  /* if __builtin_unreachable() is not supported: */
0269 #define g_assert_not_reached()          G_STMT_START { (void) 0; } G_STMT_END
0270 #endif
0271 
0272 #define g_assert(expr)                  G_STMT_START { (void) 0; } G_STMT_END
0273 #else /* !G_DISABLE_ASSERT */
0274 #define g_assert_not_reached()          G_STMT_START { g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } G_STMT_END
0275 #define g_assert(expr)                  G_STMT_START { \
0276                                              if G_LIKELY (expr) ; else \
0277                                                g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
0278                                                                          #expr); \
0279                                         } G_STMT_END
0280 #endif /* !G_DISABLE_ASSERT */
0281 
0282 GLIB_AVAILABLE_IN_ALL
0283 int     g_strcmp0                       (const char     *str1,
0284                                          const char     *str2);
0285 
0286 /* report performance results */
0287 GLIB_AVAILABLE_IN_ALL
0288 void    g_test_minimized_result         (double          minimized_quantity,
0289                                          const char     *format,
0290                                          ...) G_GNUC_PRINTF (2, 3);
0291 GLIB_AVAILABLE_IN_ALL
0292 void    g_test_maximized_result         (double          maximized_quantity,
0293                                          const char     *format,
0294                                          ...) G_GNUC_PRINTF (2, 3);
0295 
0296 /* initialize testing framework */
0297 GLIB_AVAILABLE_IN_ALL
0298 void    g_test_init                     (int            *argc,
0299                                          char         ***argv,
0300                                          ...) G_GNUC_NULL_TERMINATED;
0301 
0302 /**
0303  * G_TEST_OPTION_ISOLATE_DIRS:
0304  *
0305  * A value that can be passed as an option to [func@GLib.test_init].
0306  *
0307  * Creates a unique temporary directory for each unit test and uses sets
0308  * XDG directories to point into subdirectories of it for the duration of
0309  * the unit test. The directory tree is cleaned up after the test finishes
0310  * successfully.
0311  *
0312  * Note that this doesn’t take effect until [func@GLib.test_run] is called,
0313  * so calls to (for example) [func@GLib.get_home_dir] will return the
0314  * system-wide value when made in a test program’s main() function.
0315  *
0316  * The following functions will return subdirectories of the temporary directory
0317  * when this option is used. The specific subdirectory paths in use are not
0318  * guaranteed to be stable API — always use a getter function to retrieve them.
0319  *
0320  *  - [func@GLib.get_home_dir]
0321  *  - [func@GLib.get_user_cache_dir]
0322  *  - [func@GLib.get_system_config_dirs]
0323  *  - [func@GLib.get_user_config_dir]
0324  *  - [func@GLib.get_system_data_dirs]
0325  *  - [func@GLib.get_user_data_dir]
0326  *  - [func@GLib.get_user_state_dir]
0327  *  - [func@GLib.get_user_runtime_dir]
0328  *
0329  * The subdirectories may not be created by the test harness; as with normal
0330  * calls to functions like [func@GLib.get_user_cache_dir], the caller must
0331  * be prepared to create the directory if it doesn’t exist.
0332  *
0333  * Since: 2.60
0334  */
0335 #define G_TEST_OPTION_ISOLATE_DIRS "isolate_dirs"
0336 
0337 /**
0338  * G_TEST_OPTION_NO_PRGNAME:
0339  *
0340  * A value that can be passed as an option to [func@GLib.test_init].
0341  *
0342  * If this option is given, [func@GLib.test_init] will not call [func@GLib.set_prgname].
0343  *
0344  * Since: 2.84
0345  */
0346 #define G_TEST_OPTION_NO_PRGNAME "no_g_set_prgname"
0347 
0348 /**
0349  * G_TEST_OPTION_NONFATAL_ASSERTIONS:
0350  *
0351  * A value that can be passed as an option to [func@GLib.test_init].
0352  *
0353  * If this option is given, assertions will not abort the process, but
0354  * call [func@GLib.test_fail]. Equivalent to [func@GLib.test_set_nonfatal_assertions].
0355  *
0356  * Since: 2.84
0357  */
0358 #define G_TEST_OPTION_NONFATAL_ASSERTIONS "nonfatal-assertions"
0359 
0360 /* While we discourage its use, g_assert() is often used in unit tests
0361  * (especially in legacy code). g_assert_*() should really be used instead.
0362  * g_assert() can be disabled at client program compile time, which can render
0363  * tests useless. Highlight that to the user. */
0364 #ifdef G_DISABLE_ASSERT
0365 #if defined(G_HAVE_ISO_VARARGS)
0366 #define g_test_init(argc, argv, ...) \
0367   G_STMT_START { \
0368     g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \
0369     exit (1); \
0370   } G_STMT_END
0371 #elif defined(G_HAVE_GNUC_VARARGS)
0372 #define g_test_init(argc, argv...) \
0373   G_STMT_START { \
0374     g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \
0375     exit (1); \
0376   } G_STMT_END
0377 #else  /* no varargs */
0378   /* do nothing */
0379 #endif  /* varargs support */
0380 #endif  /* G_DISABLE_ASSERT */
0381 
0382 /* query testing framework config */
0383 #define g_test_initialized()            (g_test_config_vars->test_initialized)
0384 #define g_test_quick()                  (g_test_config_vars->test_quick)
0385 #define g_test_slow()                   (!g_test_config_vars->test_quick)
0386 #define g_test_thorough()               (!g_test_config_vars->test_quick)
0387 #define g_test_perf()                   (g_test_config_vars->test_perf)
0388 #define g_test_verbose()                (g_test_config_vars->test_verbose)
0389 #define g_test_quiet()                  (g_test_config_vars->test_quiet)
0390 #define g_test_undefined()              (g_test_config_vars->test_undefined)
0391 GLIB_AVAILABLE_IN_2_38
0392 gboolean g_test_subprocess (void);
0393 
0394 /* run all tests under toplevel suite (path: /) */
0395 GLIB_AVAILABLE_IN_ALL
0396 int     g_test_run                      (void);
0397 /* hook up a test functions under test path */
0398 GLIB_AVAILABLE_IN_ALL
0399 void    g_test_add_func                 (const char     *testpath,
0400                                          GTestFunc       test_func);
0401 
0402 GLIB_AVAILABLE_IN_ALL
0403 void    g_test_add_data_func            (const char     *testpath,
0404                                          gconstpointer   test_data,
0405                                          GTestDataFunc   test_func);
0406 
0407 GLIB_AVAILABLE_IN_2_34
0408 void    g_test_add_data_func_full       (const char     *testpath,
0409                                          gpointer        test_data,
0410                                          GTestDataFunc   test_func,
0411                                          GDestroyNotify  data_free_func);
0412 
0413 /* tell about currently run test */
0414 GLIB_AVAILABLE_IN_2_68
0415 const char * g_test_get_path            (void);
0416 
0417 /* tell about failure */
0418 GLIB_AVAILABLE_IN_2_30
0419 void    g_test_fail                     (void);
0420 GLIB_AVAILABLE_IN_2_70
0421 void    g_test_fail_printf              (const char *format,
0422                                          ...) G_GNUC_PRINTF (1, 2);
0423 GLIB_AVAILABLE_IN_2_38
0424 void    g_test_incomplete               (const gchar *msg);
0425 GLIB_AVAILABLE_IN_2_70
0426 void    g_test_incomplete_printf        (const char *format,
0427                                          ...) G_GNUC_PRINTF (1, 2);
0428 GLIB_AVAILABLE_IN_2_38
0429 void    g_test_skip                     (const gchar *msg);
0430 GLIB_AVAILABLE_IN_2_70
0431 void    g_test_skip_printf              (const char *format,
0432                                          ...) G_GNUC_PRINTF (1, 2);
0433 GLIB_AVAILABLE_IN_2_38
0434 gboolean g_test_failed                  (void);
0435 GLIB_AVAILABLE_IN_2_38
0436 void    g_test_set_nonfatal_assertions  (void);
0437 GLIB_AVAILABLE_IN_2_78
0438 void    g_test_disable_crash_reporting  (void);
0439 
0440 /**
0441  * g_test_add:
0442  * @testpath: the test path for a new test case
0443  * @Fixture: the type of a fixture data structure
0444  * @tdata: data argument for the test functions
0445  * @fsetup: the function to set up the fixture data
0446  * @ftest: the actual test function
0447  * @fteardown: the function to tear down the fixture data
0448  *
0449  * Hooks up a new test case at @testpath.
0450  *
0451  * This function is similar to [func@GLib.test_add_func].
0452  *
0453  * A fixture data structure with setup and teardown functions
0454  * may be provided, similar to [func@GLib.test_create_case].
0455  *
0456  * `g_test_add()` is implemented as a macro, so that the @fsetup,
0457  * @ftest and @fteardown callbacks can expect a @Fixture pointer
0458  * as their first argument in a type safe manner. They otherwise
0459  * have type `GTestFixtureFunc`.
0460  *
0461  * Since: 2.16
0462  */
0463 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
0464                     G_STMT_START {          \
0465                                          void (*add_vtable) (const char*,       \
0466                                                     gsize,             \
0467                                                     gconstpointer,     \
0468                                                     void (*) (Fixture*, gconstpointer),   \
0469                                                     void (*) (Fixture*, gconstpointer),   \
0470                                                     void (*) (Fixture*, gconstpointer)) =  (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
0471                                          add_vtable \
0472                                           (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
0473                     } G_STMT_END
0474 
0475 /* add test messages to the test report */
0476 GLIB_AVAILABLE_IN_ALL
0477 void    g_test_message                  (const char *format,
0478                                          ...) G_GNUC_PRINTF (1, 2);
0479 GLIB_AVAILABLE_IN_ALL
0480 void    g_test_bug_base                 (const char *uri_pattern);
0481 GLIB_AVAILABLE_IN_ALL
0482 void    g_test_bug                      (const char *bug_uri_snippet);
0483 GLIB_AVAILABLE_IN_2_62
0484 void    g_test_summary                  (const char *summary);
0485 /* measure test timings */
0486 GLIB_AVAILABLE_IN_ALL
0487 void    g_test_timer_start              (void);
0488 GLIB_AVAILABLE_IN_ALL
0489 double  g_test_timer_elapsed            (void); /* elapsed seconds */
0490 GLIB_AVAILABLE_IN_ALL
0491 double  g_test_timer_last               (void); /* repeat last elapsed() result */
0492 
0493 /* automatically g_free or g_object_unref upon teardown */
0494 GLIB_AVAILABLE_IN_ALL
0495 void    g_test_queue_free               (gpointer gfree_pointer);
0496 GLIB_AVAILABLE_IN_ALL
0497 void    g_test_queue_destroy            (GDestroyNotify destroy_func,
0498                                          gpointer       destroy_data);
0499 #define g_test_queue_unref(gobject)     g_test_queue_destroy (g_object_unref, gobject)
0500 
0501 /**
0502  * GTestTrapFlags:
0503  * @G_TEST_TRAP_DEFAULT: Default behaviour. Since: 2.74
0504  * @G_TEST_TRAP_SILENCE_STDOUT: Redirect stdout of the test child to
0505  *     `/dev/null` so it cannot be observed on the console during test
0506  *     runs. The actual output is still captured though to allow later
0507  *     tests with g_test_trap_assert_stdout().
0508  * @G_TEST_TRAP_SILENCE_STDERR: Redirect stderr of the test child to
0509  *     `/dev/null` so it cannot be observed on the console during test
0510  *     runs. The actual output is still captured though to allow later
0511  *     tests with g_test_trap_assert_stderr().
0512  * @G_TEST_TRAP_INHERIT_STDIN: If this flag is given, stdin of the
0513  *     child process is shared with stdin of its parent process.
0514  *     It is redirected to `/dev/null` otherwise.
0515  *
0516  * Flags to pass to [func@GLib.test_trap_fork] to control input and output.
0517  *
0518  * Test traps are guards around forked tests. These flags determine what traps to set.
0519  *
0520  * Deprecated: 2.38: `GTestTrapFlags` is used only with [func@GLib.test_trap_fork],
0521  *   which is deprecated. Its replacement, [func@GLib.test_trap_subprocess] uses
0522  *   [flags@GLib.TestSubprocessFlags].
0523  */
0524 typedef enum {
0525   G_TEST_TRAP_DEFAULT GLIB_AVAILABLE_ENUMERATOR_IN_2_74 = 0,
0526   G_TEST_TRAP_SILENCE_STDOUT    = 1 << 7,
0527   G_TEST_TRAP_SILENCE_STDERR    = 1 << 8,
0528   G_TEST_TRAP_INHERIT_STDIN     = 1 << 9
0529 } G_GNUC_FLAG_ENUM GTestTrapFlags GLIB_DEPRECATED_TYPE_IN_2_38_FOR(GTestSubprocessFlags);
0530 
0531 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
0532 
0533 GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess)
0534 gboolean g_test_trap_fork               (guint64              usec_timeout,
0535                                          GTestTrapFlags       test_trap_flags);
0536 
0537 G_GNUC_END_IGNORE_DEPRECATIONS
0538 
0539 typedef enum {
0540   G_TEST_SUBPROCESS_DEFAULT GLIB_AVAILABLE_ENUMERATOR_IN_2_74 = 0,
0541   G_TEST_SUBPROCESS_INHERIT_STDIN  = 1 << 0,
0542   G_TEST_SUBPROCESS_INHERIT_STDOUT = 1 << 1,
0543   G_TEST_SUBPROCESS_INHERIT_STDERR = 1 << 2,
0544   G_TEST_SUBPROCESS_INHERIT_DESCRIPTORS GLIB_AVAILABLE_ENUMERATOR_IN_2_88 = 1 << 3,
0545 } G_GNUC_FLAG_ENUM GTestSubprocessFlags;
0546 
0547 GLIB_AVAILABLE_IN_2_38
0548 void     g_test_trap_subprocess         (const char           *test_path,
0549                                          guint64               usec_timeout,
0550                                          GTestSubprocessFlags  test_flags);
0551 GLIB_AVAILABLE_IN_2_80
0552 void     g_test_trap_subprocess_with_envp (const char           *test_path,
0553                                            const char * const   *envp,
0554                                            guint64               usec_timeout,
0555                                            GTestSubprocessFlags  test_flags);
0556 
0557 GLIB_AVAILABLE_IN_ALL
0558 gboolean g_test_trap_has_passed         (void);
0559 GLIB_AVAILABLE_IN_2_88
0560 gboolean g_test_trap_has_skipped        (void);
0561 GLIB_AVAILABLE_IN_ALL
0562 gboolean g_test_trap_reached_timeout    (void);
0563 #define  g_test_trap_assert_passed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
0564 #define  g_test_trap_assert_failed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
0565 #define  g_test_trap_assert_stdout(soutpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
0566 #define  g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
0567 #define  g_test_trap_assert_stderr(serrpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
0568 #define  g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
0569 
0570 /* provide seed-able random numbers for tests */
0571 #define  g_test_rand_bit()              (0 != (g_test_rand_int() & (1 << 15)))
0572 GLIB_AVAILABLE_IN_ALL
0573 gint32   g_test_rand_int                (void);
0574 GLIB_AVAILABLE_IN_ALL
0575 gint32   g_test_rand_int_range          (gint32          begin,
0576                                          gint32          end);
0577 GLIB_AVAILABLE_IN_ALL
0578 double   g_test_rand_double             (void);
0579 GLIB_AVAILABLE_IN_ALL
0580 double   g_test_rand_double_range       (double          range_start,
0581                                          double          range_end);
0582 
0583 /*
0584  * semi-internal API: non-documented symbols with stable ABI. You
0585  * should use the non-internal helper macros instead. However, for
0586  * compatibility reason, you may use this semi-internal API.
0587  */
0588 GLIB_AVAILABLE_IN_ALL
0589 GTestCase*    g_test_create_case        (const char       *test_name,
0590                                          gsize             data_size,
0591                                          gconstpointer     test_data,
0592                                          GTestFixtureFunc  data_setup,
0593                                          GTestFixtureFunc  data_test,
0594                                          GTestFixtureFunc  data_teardown);
0595 GLIB_AVAILABLE_IN_ALL
0596 GTestSuite*   g_test_create_suite       (const char       *suite_name);
0597 GLIB_AVAILABLE_IN_ALL
0598 GTestSuite*   g_test_get_root           (void);
0599 GLIB_AVAILABLE_IN_ALL
0600 void          g_test_suite_add          (GTestSuite     *suite,
0601                                          GTestCase      *test_case);
0602 GLIB_AVAILABLE_IN_ALL
0603 void          g_test_suite_add_suite    (GTestSuite     *suite,
0604                                          GTestSuite     *nestedsuite);
0605 GLIB_AVAILABLE_IN_ALL
0606 int           g_test_run_suite          (GTestSuite     *suite);
0607 
0608 GLIB_AVAILABLE_IN_2_70
0609 void          g_test_case_free          (GTestCase *test_case);
0610 
0611 GLIB_AVAILABLE_IN_2_70
0612 void          g_test_suite_free         (GTestSuite     *suite);
0613 
0614 GLIB_AVAILABLE_IN_ALL
0615 void    g_test_trap_assertions          (const char     *domain,
0616                                          const char     *file,
0617                                          int             line,
0618                                          const char     *func,
0619                                          guint64         assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
0620                                          const char     *pattern);
0621 GLIB_AVAILABLE_IN_ALL
0622 void    g_assertion_message             (const char     *domain,
0623                                          const char     *file,
0624                                          int             line,
0625                                          const char     *func,
0626                                          const char     *message) G_ANALYZER_NORETURN;
0627 G_NORETURN
0628 GLIB_AVAILABLE_IN_ALL
0629 void    g_assertion_message_expr        (const char     *domain,
0630                                          const char     *file,
0631                                          int             line,
0632                                          const char     *func,
0633                                          const char     *expr);
0634 GLIB_AVAILABLE_IN_ALL
0635 void    g_assertion_message_cmpstr      (const char     *domain,
0636                                          const char     *file,
0637                                          int             line,
0638                                          const char     *func,
0639                                          const char     *expr,
0640                                          const char     *arg1,
0641                                          const char     *cmp,
0642                                          const char     *arg2) G_ANALYZER_NORETURN;
0643 
0644 GLIB_AVAILABLE_IN_2_68
0645 void    g_assertion_message_cmpstrv     (const char         *domain,
0646                                          const char         *file,
0647                                          int                 line,
0648                                          const char         *func,
0649                                          const char         *expr,
0650                                          const char * const *arg1,
0651                                          const char * const *arg2,
0652                                          gsize               first_wrong_idx) G_ANALYZER_NORETURN;
0653 GLIB_AVAILABLE_IN_2_78
0654 void    g_assertion_message_cmpint      (const char     *domain,
0655                                          const char     *file,
0656                                          int             line,
0657                                          const char     *func,
0658                                          const char     *expr,
0659                                          guint64         arg1,
0660                                          const char     *cmp,
0661                                          guint64         arg2,
0662                                          char            numtype) G_ANALYZER_NORETURN;
0663 GLIB_AVAILABLE_IN_ALL
0664 void    g_assertion_message_cmpnum      (const char     *domain,
0665                                          const char     *file,
0666                                          int             line,
0667                                          const char     *func,
0668                                          const char     *expr,
0669                                          long double     arg1,
0670                                          const char     *cmp,
0671                                          long double     arg2,
0672                                          char            numtype) G_ANALYZER_NORETURN;
0673 GLIB_AVAILABLE_IN_ALL
0674 void    g_assertion_message_error       (const char     *domain,
0675                                          const char     *file,
0676                                          int             line,
0677                                          const char     *func,
0678                                          const char     *expr,
0679                                          const GError   *error,
0680                                          GQuark          error_domain,
0681                                          int             error_code) G_ANALYZER_NORETURN;
0682 GLIB_AVAILABLE_IN_ALL
0683 void    g_test_add_vtable               (const char     *testpath,
0684                                          gsize           data_size,
0685                                          gconstpointer   test_data,
0686                                          GTestFixtureFunc  data_setup,
0687                                          GTestFixtureFunc  data_test,
0688                                          GTestFixtureFunc  data_teardown);
0689 typedef struct {
0690   gboolean      test_initialized;
0691   gboolean      test_quick;     /* disable thorough tests */
0692   gboolean      test_perf;      /* run performance tests */
0693   gboolean      test_verbose;   /* extra info */
0694   gboolean      test_quiet;     /* reduce output */
0695   gboolean      test_undefined; /* run tests that are meant to assert */
0696 } GTestConfig;
0697 GLIB_VAR const GTestConfig * const g_test_config_vars;
0698 
0699 /* internal logging API */
0700 typedef enum {
0701   G_TEST_RUN_SUCCESS,
0702   G_TEST_RUN_SKIPPED,
0703   G_TEST_RUN_FAILURE,
0704   G_TEST_RUN_INCOMPLETE
0705 } GTestResult;
0706 
0707 typedef enum {
0708   G_TEST_LOG_NONE,
0709   G_TEST_LOG_ERROR,             /* s:msg */
0710   G_TEST_LOG_START_BINARY,      /* s:binaryname s:seed */
0711   G_TEST_LOG_LIST_CASE,         /* s:testpath */
0712   G_TEST_LOG_SKIP_CASE,         /* s:testpath */
0713   G_TEST_LOG_START_CASE,        /* s:testpath */
0714   G_TEST_LOG_STOP_CASE,         /* d:status d:nforks d:elapsed */
0715   G_TEST_LOG_MIN_RESULT,        /* s:blurb d:result */
0716   G_TEST_LOG_MAX_RESULT,        /* s:blurb d:result */
0717   G_TEST_LOG_MESSAGE,           /* s:blurb */
0718   G_TEST_LOG_START_SUITE,
0719   G_TEST_LOG_STOP_SUITE
0720 } GTestLogType;
0721 
0722 typedef struct {
0723   GTestLogType  log_type;
0724   guint         n_strings;
0725   gchar       **strings; /* NULL terminated */
0726   guint         n_nums;
0727   long double  *nums;
0728 } GTestLogMsg;
0729 typedef struct {
0730   /*< private >*/
0731   GString     *data;
0732   GSList      *msgs;
0733 } GTestLogBuffer;
0734 
0735 GLIB_AVAILABLE_IN_ALL
0736 const char*     g_test_log_type_name    (GTestLogType    log_type);
0737 GLIB_AVAILABLE_IN_ALL
0738 GTestLogBuffer* g_test_log_buffer_new   (void);
0739 GLIB_AVAILABLE_IN_ALL
0740 void            g_test_log_buffer_free  (GTestLogBuffer *tbuffer);
0741 GLIB_AVAILABLE_IN_ALL
0742 void            g_test_log_buffer_push  (GTestLogBuffer *tbuffer,
0743                                          guint           n_bytes,
0744                                          const guint8   *bytes);
0745 GLIB_AVAILABLE_IN_ALL
0746 GTestLogMsg*    g_test_log_buffer_pop   (GTestLogBuffer *tbuffer);
0747 GLIB_AVAILABLE_IN_ALL
0748 void            g_test_log_msg_free     (GTestLogMsg    *tmsg);
0749 
0750 /**
0751  * GTestLogFatalFunc:
0752  * @log_domain: the log domain of the message
0753  * @log_level: the log level of the message (including the fatal and recursion flags)
0754  * @message: the message to process
0755  * @user_data: user data, set in g_test_log_set_fatal_handler()
0756  *
0757  * Specifies the prototype of fatal log handler functions.
0758  *
0759  * Returns: %TRUE if the program should abort, %FALSE otherwise
0760  *
0761  * Since: 2.22
0762  */
0763 typedef gboolean        (*GTestLogFatalFunc)    (const gchar    *log_domain,
0764                                                  GLogLevelFlags  log_level,
0765                                                  const gchar    *message,
0766                                                  gpointer        user_data);
0767 GLIB_AVAILABLE_IN_ALL
0768 void
0769 g_test_log_set_fatal_handler            (GTestLogFatalFunc log_func,
0770                                          gpointer          user_data);
0771 
0772 GLIB_AVAILABLE_IN_2_34
0773 void    g_test_expect_message                    (const gchar    *log_domain,
0774                                                   GLogLevelFlags  log_level,
0775                                                   const gchar    *pattern);
0776 GLIB_AVAILABLE_IN_2_34
0777 void    g_test_assert_expected_messages_internal (const char     *domain,
0778                                                   const char     *file,
0779                                                   int             line,
0780                                                   const char     *func);
0781 
0782 typedef enum
0783 {
0784   G_TEST_DIST,
0785   G_TEST_BUILT
0786 } GTestFileType;
0787 
0788 GLIB_AVAILABLE_IN_2_38
0789 gchar * g_test_build_filename                    (GTestFileType   file_type,
0790                                                   const gchar    *first_path,
0791                                                   ...) G_GNUC_NULL_TERMINATED;
0792 GLIB_AVAILABLE_IN_2_38
0793 const gchar *g_test_get_dir                      (GTestFileType   file_type);
0794 GLIB_AVAILABLE_IN_2_38
0795 const gchar *g_test_get_filename                 (GTestFileType   file_type,
0796                                                   const gchar    *first_path,
0797                                                   ...) G_GNUC_NULL_TERMINATED;
0798 
0799 #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
0800 
0801 G_END_DECLS
0802 
0803 #endif /* __G_TEST_UTILS_H__ */