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