gtestutils.h 40 KB

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