| 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_state_dir() |
| 279 | * - g_get_user_runtime_dir() |
| 280 | * |
| 281 | * The subdirectories may not be created by the test harness; as with normal |
| 282 | * calls to functions like g_get_user_cache_dir(), the caller must be prepared |
| 283 | * to create the directory if it doesn’t exist. |
| 284 | * |
| 285 | * Since: 2.60 |
| 286 | */ |
| 287 | #define G_TEST_OPTION_ISOLATE_DIRS "isolate_dirs" |
| 288 | |
| 289 | /* While we discourage its use, g_assert() is often used in unit tests |
| 290 | * (especially in legacy code). g_assert_*() should really be used instead. |
| 291 | * g_assert() can be disabled at client program compile time, which can render |
| 292 | * tests useless. Highlight that to the user. */ |
| 293 | #ifdef G_DISABLE_ASSERT |
| 294 | #if defined(G_HAVE_ISO_VARARGS) |
| 295 | #define g_test_init(argc, argv, ...) \ |
| 296 | G_STMT_START { \ |
| 297 | g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \ |
| 298 | exit (1); \ |
| 299 | } G_STMT_END |
| 300 | #elif defined(G_HAVE_GNUC_VARARGS) |
| 301 | #define g_test_init(argc, argv...) \ |
| 302 | G_STMT_START { \ |
| 303 | g_printerr ("Tests were compiled with G_DISABLE_ASSERT and are likely no-ops. Aborting.\n"); \ |
| 304 | exit (1); \ |
| 305 | } G_STMT_END |
| 306 | #else /* no varargs */ |
| 307 | /* do nothing */ |
| 308 | #endif /* varargs support */ |
| 309 | #endif /* G_DISABLE_ASSERT */ |
| 310 | |
| 311 | /* query testing framework config */ |
| 312 | #define g_test_initialized() (g_test_config_vars->test_initialized) |
| 313 | #define g_test_quick() (g_test_config_vars->test_quick) |
| 314 | #define g_test_slow() (!g_test_config_vars->test_quick) |
| 315 | #define g_test_thorough() (!g_test_config_vars->test_quick) |
| 316 | #define g_test_perf() (g_test_config_vars->test_perf) |
| 317 | #define g_test_verbose() (g_test_config_vars->test_verbose) |
| 318 | #define g_test_quiet() (g_test_config_vars->test_quiet) |
| 319 | #define g_test_undefined() (g_test_config_vars->test_undefined) |
| 320 | GLIB_AVAILABLE_IN_2_38 |
| 321 | gboolean g_test_subprocess (void); |
| 322 | |
| 323 | /* run all tests under toplevel suite (path: /) */ |
| 324 | GLIB_AVAILABLE_IN_ALL |
| 325 | int g_test_run (void); |
| 326 | /* hook up a test functions under test path */ |
| 327 | GLIB_AVAILABLE_IN_ALL |
| 328 | void g_test_add_func (const char *testpath, |
| 329 | GTestFunc test_func); |
| 330 | |
| 331 | GLIB_AVAILABLE_IN_ALL |
| 332 | void g_test_add_data_func (const char *testpath, |
| 333 | gconstpointer test_data, |
| 334 | GTestDataFunc test_func); |
| 335 | |
| 336 | GLIB_AVAILABLE_IN_2_34 |
| 337 | void g_test_add_data_func_full (const char *testpath, |
| 338 | gpointer test_data, |
| 339 | GTestDataFunc test_func, |
| 340 | GDestroyNotify data_free_func); |
| 341 | |
| 342 | /* tell about currently run test */ |
| 343 | GLIB_AVAILABLE_IN_2_68 |
| 344 | const char * g_test_get_path (void); |
| 345 | |
| 346 | /* tell about failure */ |
| 347 | GLIB_AVAILABLE_IN_2_30 |
| 348 | void g_test_fail (void); |
| 349 | GLIB_AVAILABLE_IN_2_70 |
| 350 | void g_test_fail_printf (const char *format, |
| 351 | ...) G_GNUC_PRINTF (1, 2); |
| 352 | GLIB_AVAILABLE_IN_2_38 |
| 353 | void g_test_incomplete (const gchar *msg); |
| 354 | GLIB_AVAILABLE_IN_2_70 |
| 355 | void g_test_incomplete_printf (const char *format, |
| 356 | ...) G_GNUC_PRINTF (1, 2); |
| 357 | GLIB_AVAILABLE_IN_2_38 |
| 358 | void g_test_skip (const gchar *msg); |
| 359 | GLIB_AVAILABLE_IN_2_70 |
| 360 | void g_test_skip_printf (const char *format, |
| 361 | ...) G_GNUC_PRINTF (1, 2); |
| 362 | GLIB_AVAILABLE_IN_2_38 |
| 363 | gboolean g_test_failed (void); |
| 364 | GLIB_AVAILABLE_IN_2_38 |
| 365 | void g_test_set_nonfatal_assertions (void); |
| 366 | |
| 367 | /** |
| 368 | * g_test_add: |
| 369 | * @testpath: The test path for a new test case. |
| 370 | * @Fixture: The type of a fixture data structure. |
| 371 | * @tdata: Data argument for the test functions. |
| 372 | * @fsetup: The function to set up the fixture data. |
| 373 | * @ftest: The actual test function. |
| 374 | * @fteardown: The function to tear down the fixture data. |
| 375 | * |
| 376 | * Hook up a new test case at @testpath, similar to g_test_add_func(). |
| 377 | * A fixture data structure with setup and teardown functions may be provided, |
| 378 | * similar to g_test_create_case(). |
| 379 | * |
| 380 | * g_test_add() is implemented as a macro, so that the fsetup(), ftest() and |
| 381 | * fteardown() callbacks can expect a @Fixture pointer as their first argument |
| 382 | * in a type safe manner. They otherwise have type #GTestFixtureFunc. |
| 383 | * |
| 384 | * Since: 2.16 |
| 385 | */ |
| 386 | #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \ |
| 387 | G_STMT_START { \ |
| 388 | void (*add_vtable) (const char*, \ |
| 389 | gsize, \ |
| 390 | gconstpointer, \ |
| 391 | void (*) (Fixture*, gconstpointer), \ |
| 392 | void (*) (Fixture*, gconstpointer), \ |
| 393 | void (*) (Fixture*, gconstpointer)) = (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \ |
| 394 | add_vtable \ |
| 395 | (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \ |
| 396 | } G_STMT_END |
| 397 | |
| 398 | /* add test messages to the test report */ |
| 399 | GLIB_AVAILABLE_IN_ALL |
| 400 | void g_test_message (const char *format, |
| 401 | ...) G_GNUC_PRINTF (1, 2); |
| 402 | GLIB_AVAILABLE_IN_ALL |
| 403 | void g_test_bug_base (const char *uri_pattern); |
| 404 | GLIB_AVAILABLE_IN_ALL |
| 405 | void g_test_bug (const char *bug_uri_snippet); |
| 406 | GLIB_AVAILABLE_IN_2_62 |
| 407 | void g_test_summary (const char *summary); |
| 408 | /* measure test timings */ |
| 409 | GLIB_AVAILABLE_IN_ALL |
| 410 | void g_test_timer_start (void); |
| 411 | GLIB_AVAILABLE_IN_ALL |
| 412 | double g_test_timer_elapsed (void); /* elapsed seconds */ |
| 413 | GLIB_AVAILABLE_IN_ALL |
| 414 | double g_test_timer_last (void); /* repeat last elapsed() result */ |
| 415 | |
| 416 | /* automatically g_free or g_object_unref upon teardown */ |
| 417 | GLIB_AVAILABLE_IN_ALL |
| 418 | void g_test_queue_free (gpointer gfree_pointer); |
| 419 | GLIB_AVAILABLE_IN_ALL |
| 420 | void g_test_queue_destroy (GDestroyNotify destroy_func, |
| 421 | gpointer destroy_data); |
| 422 | #define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject) |
| 423 | |
| 424 | /** |
| 425 | * GTestTrapFlags: |
| 426 | * @G_TEST_TRAP_SILENCE_STDOUT: Redirect stdout of the test child to |
| 427 | * `/dev/null` so it cannot be observed on the console during test |
| 428 | * runs. The actual output is still captured though to allow later |
| 429 | * tests with g_test_trap_assert_stdout(). |
| 430 | * @G_TEST_TRAP_SILENCE_STDERR: Redirect stderr of the test child to |
| 431 | * `/dev/null` so it cannot be observed on the console during test |
| 432 | * runs. The actual output is still captured though to allow later |
| 433 | * tests with g_test_trap_assert_stderr(). |
| 434 | * @G_TEST_TRAP_INHERIT_STDIN: If this flag is given, stdin of the |
| 435 | * child process is shared with stdin of its parent process. |
| 436 | * It is redirected to `/dev/null` otherwise. |
| 437 | * |
| 438 | * Test traps are guards around forked tests. |
| 439 | * These flags determine what traps to set. |
| 440 | * |
| 441 | * Deprecated: 2.38: #GTestTrapFlags is used only with g_test_trap_fork(), |
| 442 | * which is deprecated. g_test_trap_subprocess() uses |
| 443 | * #GTestSubprocessFlags. |
| 444 | */ |
| 445 | typedef enum { |
| 446 | G_TEST_TRAP_SILENCE_STDOUT = 1 << 7, |
| 447 | G_TEST_TRAP_SILENCE_STDERR = 1 << 8, |
| 448 | G_TEST_TRAP_INHERIT_STDIN = 1 << 9 |
| 449 | } GTestTrapFlags GLIB_DEPRECATED_TYPE_IN_2_38_FOR(GTestSubprocessFlags); |
| 450 | |
| 451 | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 452 | |
| 453 | GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess) |
| 454 | gboolean g_test_trap_fork (guint64 usec_timeout, |
| 455 | GTestTrapFlags test_trap_flags); |
| 456 | |
| 457 | G_GNUC_END_IGNORE_DEPRECATIONS |
| 458 | |
| 459 | typedef enum { |
| 460 | G_TEST_SUBPROCESS_INHERIT_STDIN = 1 << 0, |
| 461 | G_TEST_SUBPROCESS_INHERIT_STDOUT = 1 << 1, |
| 462 | G_TEST_SUBPROCESS_INHERIT_STDERR = 1 << 2 |
| 463 | } GTestSubprocessFlags; |
| 464 | |
| 465 | GLIB_AVAILABLE_IN_2_38 |
| 466 | void g_test_trap_subprocess (const char *test_path, |
| 467 | guint64 usec_timeout, |
| 468 | GTestSubprocessFlags test_flags); |
| 469 | |
| 470 | GLIB_AVAILABLE_IN_ALL |
| 471 | gboolean g_test_trap_has_passed (void); |
| 472 | GLIB_AVAILABLE_IN_ALL |
| 473 | gboolean g_test_trap_reached_timeout (void); |
| 474 | #define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0) |
| 475 | #define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0) |
| 476 | #define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern) |
| 477 | #define g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern) |
| 478 | #define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern) |
| 479 | #define g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern) |
| 480 | |
| 481 | /* provide seed-able random numbers for tests */ |
| 482 | #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15))) |
| 483 | GLIB_AVAILABLE_IN_ALL |
| 484 | gint32 g_test_rand_int (void); |
| 485 | GLIB_AVAILABLE_IN_ALL |
| 486 | gint32 g_test_rand_int_range (gint32 begin, |
| 487 | gint32 end); |
| 488 | GLIB_AVAILABLE_IN_ALL |
| 489 | double g_test_rand_double (void); |
| 490 | GLIB_AVAILABLE_IN_ALL |
| 491 | double g_test_rand_double_range (double range_start, |
| 492 | double range_end); |
| 493 | |
| 494 | /* |
| 495 | * semi-internal API: non-documented symbols with stable ABI. You |
| 496 | * should use the non-internal helper macros instead. However, for |
| 497 | * compatibility reason, you may use this semi-internal API. |
| 498 | */ |
| 499 | GLIB_AVAILABLE_IN_ALL |
| 500 | GTestCase* g_test_create_case (const char *test_name, |
| 501 | gsize data_size, |
| 502 | gconstpointer test_data, |
| 503 | GTestFixtureFunc data_setup, |
| 504 | GTestFixtureFunc data_test, |
| 505 | GTestFixtureFunc data_teardown); |
| 506 | GLIB_AVAILABLE_IN_ALL |
| 507 | GTestSuite* g_test_create_suite (const char *suite_name); |
| 508 | GLIB_AVAILABLE_IN_ALL |
| 509 | GTestSuite* g_test_get_root (void); |
| 510 | GLIB_AVAILABLE_IN_ALL |
| 511 | void g_test_suite_add (GTestSuite *suite, |
| 512 | GTestCase *test_case); |
| 513 | GLIB_AVAILABLE_IN_ALL |
| 514 | void g_test_suite_add_suite (GTestSuite *suite, |
| 515 | GTestSuite *nestedsuite); |
| 516 | GLIB_AVAILABLE_IN_ALL |
| 517 | int g_test_run_suite (GTestSuite *suite); |
| 518 | |
| 519 | GLIB_AVAILABLE_IN_2_70 |
| 520 | void g_test_case_free (GTestCase *test_case); |
| 521 | |
| 522 | GLIB_AVAILABLE_IN_2_70 |
| 523 | void g_test_suite_free (GTestSuite *suite); |
| 524 | |
| 525 | GLIB_AVAILABLE_IN_ALL |
| 526 | void g_test_trap_assertions (const char *domain, |
| 527 | const char *file, |
| 528 | int line, |
| 529 | const char *func, |
| 530 | guint64 assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */ |
| 531 | const char *pattern); |
| 532 | GLIB_AVAILABLE_IN_ALL |
| 533 | void g_assertion_message (const char *domain, |
| 534 | const char *file, |
| 535 | int line, |
| 536 | const char *func, |
| 537 | const char *message) G_ANALYZER_NORETURN; |
| 538 | GLIB_AVAILABLE_IN_ALL |
| 539 | G_NORETURN |
| 540 | void g_assertion_message_expr (const char *domain, |
| 541 | const char *file, |
| 542 | int line, |
| 543 | const char *func, |
| 544 | const char *expr); |
| 545 | GLIB_AVAILABLE_IN_ALL |
| 546 | void g_assertion_message_cmpstr (const char *domain, |
| 547 | const char *file, |
| 548 | int line, |
| 549 | const char *func, |
| 550 | const char *expr, |
| 551 | const char *arg1, |
| 552 | const char *cmp, |
| 553 | const char *arg2) G_ANALYZER_NORETURN; |
| 554 | |
| 555 | GLIB_AVAILABLE_IN_2_68 |
| 556 | void g_assertion_message_cmpstrv (const char *domain, |
| 557 | const char *file, |
| 558 | int line, |
| 559 | const char *func, |
| 560 | const char *expr, |
| 561 | const char * const *arg1, |
| 562 | const char * const *arg2, |
| 563 | gsize first_wrong_idx) G_ANALYZER_NORETURN; |
| 564 | GLIB_AVAILABLE_IN_ALL |
| 565 | void g_assertion_message_cmpnum (const char *domain, |
| 566 | const char *file, |
| 567 | int line, |
| 568 | const char *func, |
| 569 | const char *expr, |
| 570 | long double arg1, |
| 571 | const char *cmp, |
| 572 | long double arg2, |
| 573 | char numtype) G_ANALYZER_NORETURN; |
| 574 | GLIB_AVAILABLE_IN_ALL |
| 575 | void g_assertion_message_error (const char *domain, |
| 576 | const char *file, |
| 577 | int line, |
| 578 | const char *func, |
| 579 | const char *expr, |
| 580 | const GError *error, |
| 581 | GQuark error_domain, |
| 582 | int error_code) G_ANALYZER_NORETURN; |
| 583 | GLIB_AVAILABLE_IN_ALL |
| 584 | void g_test_add_vtable (const char *testpath, |
| 585 | gsize data_size, |
| 586 | gconstpointer test_data, |
| 587 | GTestFixtureFunc data_setup, |
| 588 | GTestFixtureFunc data_test, |
| 589 | GTestFixtureFunc data_teardown); |
| 590 | typedef struct { |
| 591 | gboolean test_initialized; |
| 592 | gboolean test_quick; /* disable thorough tests */ |
| 593 | gboolean test_perf; /* run performance tests */ |
| 594 | gboolean test_verbose; /* extra info */ |
| 595 | gboolean test_quiet; /* reduce output */ |
| 596 | gboolean test_undefined; /* run tests that are meant to assert */ |
| 597 | } GTestConfig; |
| 598 | GLIB_VAR const GTestConfig * const g_test_config_vars; |
| 599 | |
| 600 | /* internal logging API */ |
| 601 | typedef enum { |
| 602 | G_TEST_RUN_SUCCESS, |
| 603 | G_TEST_RUN_SKIPPED, |
| 604 | G_TEST_RUN_FAILURE, |
| 605 | G_TEST_RUN_INCOMPLETE |
| 606 | } GTestResult; |
| 607 | |
| 608 | typedef enum { |
| 609 | G_TEST_LOG_NONE, |
| 610 | G_TEST_LOG_ERROR, /* s:msg */ |
| 611 | G_TEST_LOG_START_BINARY, /* s:binaryname s:seed */ |
| 612 | G_TEST_LOG_LIST_CASE, /* s:testpath */ |
| 613 | G_TEST_LOG_SKIP_CASE, /* s:testpath */ |
| 614 | G_TEST_LOG_START_CASE, /* s:testpath */ |
| 615 | G_TEST_LOG_STOP_CASE, /* d:status d:nforks d:elapsed */ |
| 616 | G_TEST_LOG_MIN_RESULT, /* s:blurb d:result */ |
| 617 | G_TEST_LOG_MAX_RESULT, /* s:blurb d:result */ |
| 618 | G_TEST_LOG_MESSAGE, /* s:blurb */ |
| 619 | G_TEST_LOG_START_SUITE, |
| 620 | G_TEST_LOG_STOP_SUITE |
| 621 | } GTestLogType; |
| 622 | |
| 623 | typedef struct { |
| 624 | GTestLogType log_type; |
| 625 | guint n_strings; |
| 626 | gchar **strings; /* NULL terminated */ |
| 627 | guint n_nums; |
| 628 | long double *nums; |
| 629 | } GTestLogMsg; |
| 630 | typedef struct { |
| 631 | /*< private >*/ |
| 632 | GString *data; |
| 633 | GSList *msgs; |
| 634 | } GTestLogBuffer; |
| 635 | |
| 636 | GLIB_AVAILABLE_IN_ALL |
| 637 | const char* g_test_log_type_name (GTestLogType log_type); |
| 638 | GLIB_AVAILABLE_IN_ALL |
| 639 | GTestLogBuffer* g_test_log_buffer_new (void); |
| 640 | GLIB_AVAILABLE_IN_ALL |
| 641 | void g_test_log_buffer_free (GTestLogBuffer *tbuffer); |
| 642 | GLIB_AVAILABLE_IN_ALL |
| 643 | void g_test_log_buffer_push (GTestLogBuffer *tbuffer, |
| 644 | guint n_bytes, |
| 645 | const guint8 *bytes); |
| 646 | GLIB_AVAILABLE_IN_ALL |
| 647 | GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer); |
| 648 | GLIB_AVAILABLE_IN_ALL |
| 649 | void g_test_log_msg_free (GTestLogMsg *tmsg); |
| 650 | |
| 651 | /** |
| 652 | * GTestLogFatalFunc: |
| 653 | * @log_domain: the log domain of the message |
| 654 | * @log_level: the log level of the message (including the fatal and recursion flags) |
| 655 | * @message: the message to process |
| 656 | * @user_data: user data, set in g_test_log_set_fatal_handler() |
| 657 | * |
| 658 | * Specifies the prototype of fatal log handler functions. |
| 659 | * |
| 660 | * Returns: %TRUE if the program should abort, %FALSE otherwise |
| 661 | * |
| 662 | * Since: 2.22 |
| 663 | */ |
| 664 | typedef gboolean (*GTestLogFatalFunc) (const gchar *log_domain, |
| 665 | GLogLevelFlags log_level, |
| 666 | const gchar *message, |
| 667 | gpointer user_data); |
| 668 | GLIB_AVAILABLE_IN_ALL |
| 669 | void |
| 670 | g_test_log_set_fatal_handler (GTestLogFatalFunc log_func, |
| 671 | gpointer user_data); |
| 672 | |
| 673 | GLIB_AVAILABLE_IN_2_34 |
| 674 | void g_test_expect_message (const gchar *log_domain, |
| 675 | GLogLevelFlags log_level, |
| 676 | const gchar *pattern); |
| 677 | GLIB_AVAILABLE_IN_2_34 |
| 678 | void g_test_assert_expected_messages_internal (const char *domain, |
| 679 | const char *file, |
| 680 | int line, |
| 681 | const char *func); |
| 682 | |
| 683 | typedef enum |
| 684 | { |
| 685 | G_TEST_DIST, |
| 686 | G_TEST_BUILT |
| 687 | } GTestFileType; |
| 688 | |
| 689 | GLIB_AVAILABLE_IN_2_38 |
| 690 | gchar * g_test_build_filename (GTestFileType file_type, |
| 691 | const gchar *first_path, |
| 692 | ...) G_GNUC_NULL_TERMINATED; |
| 693 | GLIB_AVAILABLE_IN_2_38 |
| 694 | const gchar *g_test_get_dir (GTestFileType file_type); |
| 695 | GLIB_AVAILABLE_IN_2_38 |
| 696 | const gchar *g_test_get_filename (GTestFileType file_type, |
| 697 | const gchar *first_path, |
| 698 | ...) G_GNUC_NULL_TERMINATED; |
| 699 | |
| 700 | #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC) |
| 701 | |
| 702 | G_END_DECLS |
| 703 | |
| 704 | #endif /* __G_TEST_UTILS_H__ */ |
| 705 | |