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