gtestutils.h 23 KB

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