gtestutils.h 30 KB

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