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