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