You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

298 lines
16 KiB

  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, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. * Boston, MA 02111-1307, USA.
  19. */
  20. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  21. #error "Only <glib.h> can be included directly."
  22. #endif
  23. #ifndef __G_TEST_UTILS_H__
  24. #define __G_TEST_UTILS_H__
  25. #include <glib/gmessages.h>
  26. #include <glib/gstring.h>
  27. #include <glib/gerror.h>
  28. #include <glib/gslist.h>
  29. G_BEGIN_DECLS
  30. typedef struct GTestCase GTestCase;
  31. typedef struct GTestSuite GTestSuite;
  32. typedef void (*GTestFunc) (void);
  33. typedef void (*GTestDataFunc) (gconstpointer user_data);
  34. typedef void (*GTestFixtureFunc) (gpointer fixture,
  35. gconstpointer user_data);
  36. /* assertion API */
  37. #define g_assert_cmpstr(s1, cmp, s2) do { 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); } while (0)
  41. #define g_assert_cmpint(n1, cmp, n2) do { gint64 __n1 = (n1), __n2 = (n2); \
  42. if (__n1 cmp __n2) ; else \
  43. g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  44. #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
  45. #define g_assert_cmpuint(n1, cmp, n2) do { guint64 __n1 = (n1), __n2 = (n2); \
  46. if (__n1 cmp __n2) ; else \
  47. g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  48. #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
  49. #define g_assert_cmphex(n1, cmp, n2) do { 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, __n1, #cmp, __n2, 'x'); } while (0)
  53. #define g_assert_cmpfloat(n1,cmp,n2) do { long double __n1 = (n1), __n2 = (n2); \
  54. if (__n1 cmp __n2) ; else \
  55. g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  56. #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); } while (0)
  57. #define g_assert_no_error(err) do { if (err) \
  58. g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  59. #err, err, 0, 0); } while (0)
  60. #define g_assert_error(err, dom, c) do { if (!err || (err)->domain != dom || (err)->code != c) \
  61. g_assertion_message_error (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  62. #err, err, dom, c); } while (0)
  63. #ifdef G_DISABLE_ASSERT
  64. #define g_assert_not_reached() do { (void) 0; } while (0)
  65. #define g_assert(expr) do { (void) 0; } while (0)
  66. #else /* !G_DISABLE_ASSERT */
  67. #define g_assert_not_reached() do { g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
  68. #define g_assert(expr) do { if G_LIKELY (expr) ; else \
  69. g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
  70. #expr); } while (0)
  71. #endif /* !G_DISABLE_ASSERT */
  72. int g_strcmp0 (const char *str1,
  73. const char *str2);
  74. /* report performance results */
  75. void g_test_minimized_result (double minimized_quantity,
  76. const char *format,
  77. ...) G_GNUC_PRINTF (2, 3);
  78. void g_test_maximized_result (double maximized_quantity,
  79. const char *format,
  80. ...) G_GNUC_PRINTF (2, 3);
  81. /* initialize testing framework */
  82. void g_test_init (int *argc,
  83. char ***argv,
  84. ...);
  85. /* query testing framework config */
  86. #define g_test_quick() (g_test_config_vars->test_quick)
  87. #define g_test_slow() (!g_test_config_vars->test_quick)
  88. #define g_test_thorough() (!g_test_config_vars->test_quick)
  89. #define g_test_perf() (g_test_config_vars->test_perf)
  90. #define g_test_verbose() (g_test_config_vars->test_verbose)
  91. #define g_test_quiet() (g_test_config_vars->test_quiet)
  92. /* run all tests under toplevel suite (path: /) */
  93. int g_test_run (void);
  94. /* hook up a test functions under test path */
  95. void g_test_add_func (const char *testpath,
  96. GTestFunc test_func);
  97. void g_test_add_data_func (const char *testpath,
  98. gconstpointer test_data,
  99. GTestDataFunc test_func);
  100. /* hook up a test with fixture under test path */
  101. #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
  102. G_STMT_START { \
  103. void (*add_vtable) (const char*, \
  104. gsize, \
  105. gconstpointer, \
  106. void (*) (Fixture*, gconstpointer), \
  107. void (*) (Fixture*, gconstpointer), \
  108. void (*) (Fixture*, gconstpointer)) = (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
  109. add_vtable \
  110. (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
  111. } G_STMT_END
  112. /* add test messages to the test report */
  113. void g_test_message (const char *format,
  114. ...) G_GNUC_PRINTF (1, 2);
  115. void g_test_bug_base (const char *uri_pattern);
  116. void g_test_bug (const char *bug_uri_snippet);
  117. /* measure test timings */
  118. void g_test_timer_start (void);
  119. double g_test_timer_elapsed (void); /* elapsed seconds */
  120. double g_test_timer_last (void); /* repeat last elapsed() result */
  121. /* automatically g_free or g_object_unref upon teardown */
  122. void g_test_queue_free (gpointer gfree_pointer);
  123. void g_test_queue_destroy (GDestroyNotify destroy_func,
  124. gpointer destroy_data);
  125. #define g_test_queue_unref(gobject) g_test_queue_destroy (g_object_unref, gobject)
  126. /* test traps are guards used around forked tests */
  127. typedef enum {
  128. G_TEST_TRAP_SILENCE_STDOUT = 1 << 7,
  129. G_TEST_TRAP_SILENCE_STDERR = 1 << 8,
  130. G_TEST_TRAP_INHERIT_STDIN = 1 << 9
  131. } GTestTrapFlags;
  132. gboolean g_test_trap_fork (guint64 usec_timeout,
  133. GTestTrapFlags test_trap_flags);
  134. gboolean g_test_trap_has_passed (void);
  135. gboolean g_test_trap_reached_timeout (void);
  136. #define g_test_trap_assert_passed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
  137. #define g_test_trap_assert_failed() g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
  138. #define g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
  139. #define g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
  140. #define g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
  141. #define g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
  142. /* provide seed-able random numbers for tests */
  143. #define g_test_rand_bit() (0 != (g_test_rand_int() & (1 << 15)))
  144. gint32 g_test_rand_int (void);
  145. gint32 g_test_rand_int_range (gint32 begin,
  146. gint32 end);
  147. double g_test_rand_double (void);
  148. double g_test_rand_double_range (double range_start,
  149. double range_end);
  150. /* semi-internal API */
  151. GTestCase* g_test_create_case (const char *test_name,
  152. gsize data_size,
  153. gconstpointer test_data,
  154. GTestFixtureFunc data_setup,
  155. GTestFixtureFunc data_test,
  156. GTestFixtureFunc data_teardown);
  157. GTestSuite* g_test_create_suite (const char *suite_name);
  158. GTestSuite* g_test_get_root (void);
  159. void g_test_suite_add (GTestSuite *suite,
  160. GTestCase *test_case);
  161. void g_test_suite_add_suite (GTestSuite *suite,
  162. GTestSuite *nestedsuite);
  163. int g_test_run_suite (GTestSuite *suite);
  164. /* internal ABI */
  165. void g_test_trap_assertions (const char *domain,
  166. const char *file,
  167. int line,
  168. const char *func,
  169. guint64 assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
  170. const char *pattern);
  171. void g_assertion_message (const char *domain,
  172. const char *file,
  173. int line,
  174. const char *func,
  175. const char *message) G_GNUC_NORETURN;
  176. void g_assertion_message_expr (const char *domain,
  177. const char *file,
  178. int line,
  179. const char *func,
  180. const char *expr) G_GNUC_NORETURN;
  181. void g_assertion_message_cmpstr (const char *domain,
  182. const char *file,
  183. int line,
  184. const char *func,
  185. const char *expr,
  186. const char *arg1,
  187. const char *cmp,
  188. const char *arg2) G_GNUC_NORETURN;
  189. void g_assertion_message_cmpnum (const char *domain,
  190. const char *file,
  191. int line,
  192. const char *func,
  193. const char *expr,
  194. long double arg1,
  195. const char *cmp,
  196. long double arg2,
  197. char numtype) G_GNUC_NORETURN;
  198. void g_assertion_message_error (const char *domain,
  199. const char *file,
  200. int line,
  201. const char *func,
  202. const char *expr,
  203. const GError *error,
  204. GQuark error_domain,
  205. int error_code) G_GNUC_NORETURN;
  206. void g_test_add_vtable (const char *testpath,
  207. gsize data_size,
  208. gconstpointer test_data,
  209. GTestFixtureFunc data_setup,
  210. GTestFixtureFunc data_test,
  211. GTestFixtureFunc data_teardown);
  212. typedef struct {
  213. gboolean test_initialized;
  214. gboolean test_quick; /* disable thorough tests */
  215. gboolean test_perf; /* run performance tests */
  216. gboolean test_verbose; /* extra info */
  217. gboolean test_quiet; /* reduce output */
  218. } GTestConfig;
  219. GLIB_VAR const GTestConfig * const g_test_config_vars;
  220. /* internal logging API */
  221. typedef enum {
  222. G_TEST_LOG_NONE,
  223. G_TEST_LOG_ERROR, /* s:msg */
  224. G_TEST_LOG_START_BINARY, /* s:binaryname s:seed */
  225. G_TEST_LOG_LIST_CASE, /* s:testpath */
  226. G_TEST_LOG_SKIP_CASE, /* s:testpath */
  227. G_TEST_LOG_START_CASE, /* s:testpath */
  228. G_TEST_LOG_STOP_CASE, /* d:status d:nforks d:elapsed */
  229. G_TEST_LOG_MIN_RESULT, /* s:blurb d:result */
  230. G_TEST_LOG_MAX_RESULT, /* s:blurb d:result */
  231. G_TEST_LOG_MESSAGE /* s:blurb */
  232. } GTestLogType;
  233. typedef struct {
  234. GTestLogType log_type;
  235. guint n_strings;
  236. gchar **strings; /* NULL terminated */
  237. guint n_nums;
  238. long double *nums;
  239. } GTestLogMsg;
  240. typedef struct {
  241. /*< private >*/
  242. GString *data;
  243. GSList *msgs;
  244. } GTestLogBuffer;
  245. const char* g_test_log_type_name (GTestLogType log_type);
  246. GTestLogBuffer* g_test_log_buffer_new (void);
  247. void g_test_log_buffer_free (GTestLogBuffer *tbuffer);
  248. void g_test_log_buffer_push (GTestLogBuffer *tbuffer,
  249. guint n_bytes,
  250. const guint8 *bytes);
  251. GTestLogMsg* g_test_log_buffer_pop (GTestLogBuffer *tbuffer);
  252. void g_test_log_msg_free (GTestLogMsg *tmsg);
  253. /**
  254. * GTestLogFatalFunc:
  255. * @log_domain: the log domain of the message
  256. * @log_level: the log level of the message (including the fatal and recursion flags)
  257. * @message: the message to process
  258. * @user_data: user data, set in g_test_log_set_fatal_handler()
  259. *
  260. * Specifies the prototype of fatal log handler functions.
  261. *
  262. * Return value: %TRUE if the program should abort, %FALSE otherwise
  263. *
  264. * Since: 2.22
  265. */
  266. typedef gboolean (*GTestLogFatalFunc) (const gchar *log_domain,
  267. GLogLevelFlags log_level,
  268. const gchar *message,
  269. gpointer user_data);
  270. void
  271. g_test_log_set_fatal_handler (GTestLogFatalFunc log_func,
  272. gpointer user_data);
  273. G_END_DECLS
  274. #endif /* __G_TEST_UTILS_H__ */