gtestutils.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 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. G_BEGIN_DECLS
  28. typedef struct GTestCase GTestCase;
  29. typedef struct GTestSuite GTestSuite;
  30. typedef void (*GTestFunc) (void);
  31. typedef void (*GTestDataFunc) (gconstpointer user_data);
  32. typedef void (*GTestFixtureFunc) (gpointer fixture,
  33. gconstpointer user_data);
  34. /* assertion API */
  35. #define g_assert_cmpstr(s1, cmp, s2) do { const char *__s1 = (s1), *__s2 = (s2); \
  36. if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
  37. g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  38. #s1 " " #cmp " " #s2, __s1, #cmp, __s2); } while (0)
  39. #define g_assert_cmpint(n1, cmp, n2) do { gint64 __n1 = (n1), __n2 = (n2); \
  40. if (__n1 cmp __n2) ; else \
  41. g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  42. #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
  43. #define g_assert_cmpuint(n1, cmp, n2) do { guint64 __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, __n1, #cmp, __n2, 'i'); } while (0)
  47. #define g_assert_cmphex(n1, cmp, n2) do { guint64 __n1 = (n1), __n2 = (n2); \
  48. if (__n1 cmp __n2) ; else \
  49. g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  50. #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'x'); } while (0)
  51. #define g_assert_cmpfloat(n1,cmp,n2) do { long double __n1 = (n1), __n2 = (n2); \
  52. if (__n1 cmp __n2) ; else \
  53. g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  54. #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); } while (0)
  55. #define g_assert_no_error(err) do { if (err) \
  56. g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  57. #err, err, 0, 0); } while (0)
  58. #define g_assert_error(err, dom, c) do { if (!err || (err)->domain != dom || (err)->code != c) \
  59. g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  60. #err, err, dom, c); } while (0)
  61. #define g_assert_true(expr) do { if G_LIKELY (expr) ; else \
  62. g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  63. "'" #expr "' should be TRUE"); \
  64. } while (0)
  65. #define g_assert_false(expr) do { if G_LIKELY (!(expr)) ; else \
  66. g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  67. "'" #expr "' should be FALSE"); \
  68. } while (0)
  69. #define g_assert_null(expr) do { if G_LIKELY ((expr) == NULL) ; else \
  70. g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  71. "'" #expr "' should be NULL"); \
  72. } while (0)
  73. #define g_assert_nonnull(expr) do { if G_LIKELY ((expr) != NULL) ; else \
  74. g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  75. "'" #expr "' should not be NULL"); \
  76. } while (0)
  77. #ifdef G_DISABLE_ASSERT
  78. #define g_assert_not_reached() do { (void) 0; } while (0)
  79. #define g_assert(expr) do { (void) 0; } while (0)
  80. #else /* !G_DISABLE_ASSERT */
  81. #define g_assert_not_reached() do { g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
  82. #define g_assert(expr) do { if G_LIKELY (expr) ; else \
  83. g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  84. #expr); \
  85. } while (0)
  86. #endif /* !G_DISABLE_ASSERT */
  87. GLIB_AVAILABLE_IN_ALL
  88. int g_strcmp0 (const char *str1,
  89. const char *str2);
  90. /* report performance results */
  91. GLIB_AVAILABLE_IN_ALL
  92. void g_test_minimized_result (double minimized_quantity,
  93. const char *format,
  94. ...) G_GNUC_PRINTF (2, 3);
  95. GLIB_AVAILABLE_IN_ALL
  96. void g_test_maximized_result (double maximized_quantity,
  97. const char *format,
  98. ...) G_GNUC_PRINTF (2, 3);
  99. /* initialize testing framework */
  100. GLIB_AVAILABLE_IN_ALL
  101. void g_test_init (int *argc,
  102. char ***argv,
  103. ...) G_GNUC_NULL_TERMINATED;
  104. /* query testing framework config */
  105. #define g_test_initialized() (g_test_config_vars->test_initialized)
  106. #define g_test_quick() (g_test_config_vars->test_quick)
  107. #define g_test_slow() (!g_test_config_vars->test_quick)
  108. #define g_test_thorough() (!g_test_config_vars->test_quick)
  109. #define g_test_perf() (g_test_config_vars->test_perf)
  110. #define g_test_verbose() (g_test_config_vars->test_verbose)
  111. #define g_test_quiet() (g_test_config_vars->test_quiet)
  112. #define g_test_undefined() (g_test_config_vars->test_undefined)
  113. GLIB_AVAILABLE_IN_2_38
  114. gboolean g_test_subprocess (void);
  115. /* run all tests under toplevel suite (path: /) */
  116. GLIB_AVAILABLE_IN_ALL
  117. int g_test_run (void);
  118. /* hook up a test functions under test path */
  119. GLIB_AVAILABLE_IN_ALL
  120. void g_test_add_func (const char *testpath,
  121. GTestFunc test_func);
  122. GLIB_AVAILABLE_IN_ALL
  123. void g_test_add_data_func (const char *testpath,
  124. gconstpointer test_data,
  125. GTestDataFunc test_func);
  126. GLIB_AVAILABLE_IN_2_34
  127. void g_test_add_data_func_full (const char *testpath,
  128. gpointer test_data,
  129. GTestDataFunc test_func,
  130. GDestroyNotify data_free_func);
  131. /* tell about failure */
  132. GLIB_AVAILABLE_IN_2_30
  133. void g_test_fail (void);
  134. GLIB_AVAILABLE_IN_2_38
  135. void g_test_incomplete (const gchar *msg);
  136. GLIB_AVAILABLE_IN_2_38
  137. void g_test_skip (const gchar *msg);
  138. GLIB_AVAILABLE_IN_2_38
  139. gboolean g_test_failed (void);
  140. GLIB_AVAILABLE_IN_2_38
  141. void g_test_set_nonfatal_assertions (void);
  142. /* hook up a test with fixture under test path */
  143. #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
  144. G_STMT_START { \
  145. void (*add_vtable) (const char*, \
  146. gsize, \
  147. gconstpointer, \
  148. void (*) (Fixture*, gconstpointer), \
  149. void (*) (Fixture*, gconstpointer), \
  150. void (*) (Fixture*, gconstpointer)) = (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
  151. add_vtable \
  152. (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
  153. } G_STMT_END
  154. /* add test messages to the test report */
  155. GLIB_AVAILABLE_IN_ALL
  156. void g_test_message (const char *format,
  157. ...) G_GNUC_PRINTF (1, 2);
  158. GLIB_AVAILABLE_IN_ALL
  159. void g_test_bug_base (const char *uri_pattern);
  160. GLIB_AVAILABLE_IN_ALL
  161. void g_test_bug (const char *bug_uri_snippet);
  162. /* measure test timings */
  163. GLIB_AVAILABLE_IN_ALL
  164. void g_test_timer_start (void);
  165. GLIB_AVAILABLE_IN_ALL
  166. double g_test_timer_elapsed (void); /* elapsed seconds */
  167. GLIB_AVAILABLE_IN_ALL
  168. double g_test_timer_last (void); /* repeat last elapsed() result */
  169. /* automatically g_free or g_object_unref upon teardown */
  170. GLIB_AVAILABLE_IN_ALL
  171. void g_test_queue_free (gpointer gfree_pointer);
  172. GLIB_AVAILABLE_IN_ALL
  173. void g_test_queue_destroy (GDestroyNotify destroy_func,
  174. gpointer destroy_data);
  175. #define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject)
  176. typedef enum {
  177. G_TEST_TRAP_SILENCE_STDOUT = 1 << 7,
  178. G_TEST_TRAP_SILENCE_STDERR = 1 << 8,
  179. G_TEST_TRAP_INHERIT_STDIN = 1 << 9
  180. } GTestTrapFlags;
  181. GLIB_DEPRECATED_IN_2_38_FOR (g_test_trap_subprocess)
  182. gboolean g_test_trap_fork (guint64 usec_timeout,
  183. GTestTrapFlags test_trap_flags);
  184. typedef enum {
  185. G_TEST_SUBPROCESS_INHERIT_STDIN = 1 << 0,
  186. G_TEST_SUBPROCESS_INHERIT_STDOUT = 1 << 1,
  187. G_TEST_SUBPROCESS_INHERIT_STDERR = 1 << 2
  188. } GTestSubprocessFlags;
  189. GLIB_AVAILABLE_IN_2_38
  190. void g_test_trap_subprocess (const char *test_path,
  191. guint64 usec_timeout,
  192. GTestSubprocessFlags test_flags);
  193. GLIB_AVAILABLE_IN_ALL
  194. gboolean g_test_trap_has_passed (void);
  195. GLIB_AVAILABLE_IN_ALL
  196. gboolean g_test_trap_reached_timeout (void);
  197. #define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
  198. #define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
  199. #define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
  200. #define g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
  201. #define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
  202. #define g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
  203. /* provide seed-able random numbers for tests */
  204. #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
  205. GLIB_AVAILABLE_IN_ALL
  206. gint32 g_test_rand_int (void);
  207. GLIB_AVAILABLE_IN_ALL
  208. gint32 g_test_rand_int_range (gint32 begin,
  209. gint32 end);
  210. GLIB_AVAILABLE_IN_ALL
  211. double g_test_rand_double (void);
  212. GLIB_AVAILABLE_IN_ALL
  213. double g_test_rand_double_range (double range_start,
  214. double range_end);
  215. /* semi-internal API */
  216. GLIB_AVAILABLE_IN_ALL
  217. GTestCase* g_test_create_case (const char *test_name,
  218. gsize data_size,
  219. gconstpointer test_data,
  220. GTestFixtureFunc data_setup,
  221. GTestFixtureFunc data_test,
  222. GTestFixtureFunc data_teardown);
  223. GLIB_AVAILABLE_IN_ALL
  224. GTestSuite* g_test_create_suite (const char *suite_name);
  225. GLIB_AVAILABLE_IN_ALL
  226. GTestSuite* g_test_get_root (void);
  227. GLIB_AVAILABLE_IN_ALL
  228. void g_test_suite_add (GTestSuite *suite,
  229. GTestCase *test_case);
  230. GLIB_AVAILABLE_IN_ALL
  231. void g_test_suite_add_suite (GTestSuite *suite,
  232. GTestSuite *nestedsuite);
  233. GLIB_AVAILABLE_IN_ALL
  234. int g_test_run_suite (GTestSuite *suite);
  235. /* internal ABI */
  236. GLIB_AVAILABLE_IN_ALL
  237. void g_test_trap_assertions (const char *domain,
  238. const char *file,
  239. int line,
  240. const char *func,
  241. guint64 assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
  242. const char *pattern);
  243. GLIB_AVAILABLE_IN_ALL
  244. void g_assertion_message (const char *domain,
  245. const char *file,
  246. int line,
  247. const char *func,
  248. const char *message);
  249. GLIB_AVAILABLE_IN_ALL
  250. void g_assertion_message_expr (const char *domain,
  251. const char *file,
  252. int line,
  253. const char *func,
  254. const char *expr) G_GNUC_NORETURN;
  255. GLIB_AVAILABLE_IN_ALL
  256. void g_assertion_message_cmpstr (const char *domain,
  257. const char *file,
  258. int line,
  259. const char *func,
  260. const char *expr,
  261. const char *arg1,
  262. const char *cmp,
  263. const char *arg2);
  264. GLIB_AVAILABLE_IN_ALL
  265. void g_assertion_message_cmpnum (const char *domain,
  266. const char *file,
  267. int line,
  268. const char *func,
  269. const char *expr,
  270. long double arg1,
  271. const char *cmp,
  272. long double arg2,
  273. char numtype);
  274. GLIB_AVAILABLE_IN_ALL
  275. void g_assertion_message_error (const char *domain,
  276. const char *file,
  277. int line,
  278. const char *func,
  279. const char *expr,
  280. const GError *error,
  281. GQuark error_domain,
  282. int error_code);
  283. GLIB_AVAILABLE_IN_ALL
  284. void g_test_add_vtable (const char *testpath,
  285. gsize data_size,
  286. gconstpointer test_data,
  287. GTestFixtureFunc data_setup,
  288. GTestFixtureFunc data_test,
  289. GTestFixtureFunc data_teardown);
  290. typedef struct {
  291. gboolean test_initialized;
  292. gboolean test_quick; /* disable thorough tests */
  293. gboolean test_perf; /* run performance tests */
  294. gboolean test_verbose; /* extra info */
  295. gboolean test_quiet; /* reduce output */
  296. gboolean test_undefined; /* run tests that are meant to assert */
  297. } GTestConfig;
  298. GLIB_VAR const GTestConfig * const g_test_config_vars;
  299. /* internal logging API */
  300. typedef enum {
  301. G_TEST_LOG_NONE,
  302. G_TEST_LOG_ERROR, /* s:msg */
  303. G_TEST_LOG_START_BINARY, /* s:binaryname s:seed */
  304. G_TEST_LOG_LIST_CASE, /* s:testpath */
  305. G_TEST_LOG_SKIP_CASE, /* s:testpath */
  306. G_TEST_LOG_START_CASE, /* s:testpath */
  307. G_TEST_LOG_STOP_CASE, /* d:status d:nforks d:elapsed */
  308. G_TEST_LOG_MIN_RESULT, /* s:blurb d:result */
  309. G_TEST_LOG_MAX_RESULT, /* s:blurb d:result */
  310. G_TEST_LOG_MESSAGE, /* s:blurb */
  311. G_TEST_LOG_START_SUITE,
  312. G_TEST_LOG_STOP_SUITE
  313. } GTestLogType;
  314. typedef struct {
  315. GTestLogType log_type;
  316. guint n_strings;
  317. gchar **strings; /* NULL terminated */
  318. guint n_nums;
  319. long double *nums;
  320. } GTestLogMsg;
  321. typedef struct {
  322. /*< private >*/
  323. GString *data;
  324. GSList *msgs;
  325. } GTestLogBuffer;
  326. GLIB_AVAILABLE_IN_ALL
  327. const char* g_test_log_type_name (GTestLogType log_type);
  328. GLIB_AVAILABLE_IN_ALL
  329. GTestLogBuffer* g_test_log_buffer_new (void);
  330. GLIB_AVAILABLE_IN_ALL
  331. void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
  332. GLIB_AVAILABLE_IN_ALL
  333. void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
  334. guint n_bytes,
  335. const guint8 *bytes);
  336. GLIB_AVAILABLE_IN_ALL
  337. GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
  338. GLIB_AVAILABLE_IN_ALL
  339. void g_test_log_msg_free (GTestLogMsg *tmsg);
  340. /**
  341. * GTestLogFatalFunc:
  342. * @log_domain: the log domain of the message
  343. * @log_level: the log level of the message (including the fatal and recursion flags)
  344. * @message: the message to process
  345. * @user_data: user data, set in g_test_log_set_fatal_handler()
  346. *
  347. * Specifies the prototype of fatal log handler functions.
  348. *
  349. * Returns: %TRUE if the program should abort, %FALSE otherwise
  350. *
  351. * Since: 2.22
  352. */
  353. typedef gboolean (*GTestLogFatalFunc) (const gchar *log_domain,
  354. GLogLevelFlags log_level,
  355. const gchar *message,
  356. gpointer user_data);
  357. GLIB_AVAILABLE_IN_ALL
  358. void
  359. g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
  360. gpointer user_data);
  361. GLIB_AVAILABLE_IN_2_34
  362. void g_test_expect_message (const gchar *log_domain,
  363. GLogLevelFlags log_level,
  364. const gchar *pattern);
  365. GLIB_AVAILABLE_IN_2_34
  366. void g_test_assert_expected_messages_internal (const char *domain,
  367. const char *file,
  368. int line,
  369. const char *func);
  370. typedef enum
  371. {
  372. G_TEST_DIST,
  373. G_TEST_BUILT
  374. } GTestFileType;
  375. GLIB_AVAILABLE_IN_2_38
  376. gchar * g_test_build_filename (GTestFileType file_type,
  377. const gchar *first_path,
  378. ...) G_GNUC_NULL_TERMINATED;
  379. GLIB_AVAILABLE_IN_2_38
  380. const gchar *g_test_get_dir (GTestFileType file_type);
  381. GLIB_AVAILABLE_IN_2_38
  382. const gchar *g_test_get_filename (GTestFileType file_type,
  383. const gchar *first_path,
  384. ...) G_GNUC_NULL_TERMINATED;
  385. #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
  386. G_END_DECLS
  387. #endif /* __G_TEST_UTILS_H__ */