gtestutils: add g_test_add_data_func_full()
[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 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22 #error "Only <glib.h> can be included directly."
23 #endif
24
25 #ifndef __G_TEST_UTILS_H__
26 #define __G_TEST_UTILS_H__
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 int     g_strcmp0                       (const char     *str1,
80                                          const char     *str2);
81
82 /* report performance results */
83 void    g_test_minimized_result         (double          minimized_quantity,
84                                          const char     *format,
85                                          ...) G_GNUC_PRINTF (2, 3);
86 void    g_test_maximized_result         (double          maximized_quantity,
87                                          const char     *format,
88                                          ...) G_GNUC_PRINTF (2, 3);
89
90 /* initialize testing framework */
91 void    g_test_init                     (int            *argc,
92                                          char         ***argv,
93                                          ...);
94 /* query testing framework config */
95 #define g_test_quick()                  (g_test_config_vars->test_quick)
96 #define g_test_slow()                   (!g_test_config_vars->test_quick)
97 #define g_test_thorough()               (!g_test_config_vars->test_quick)
98 #define g_test_perf()                   (g_test_config_vars->test_perf)
99 #define g_test_verbose()                (g_test_config_vars->test_verbose)
100 #define g_test_quiet()                  (g_test_config_vars->test_quiet)
101 #define g_test_undefined()              (g_test_config_vars->test_undefined)
102 /* run all tests under toplevel suite (path: /) */
103 int     g_test_run                      (void);
104 /* hook up a test functions under test path */
105 void    g_test_add_func                 (const char     *testpath,
106                                          GTestFunc       test_func);
107
108 void    g_test_add_data_func            (const char     *testpath,
109                                          gconstpointer   test_data,
110                                          GTestDataFunc   test_func);
111
112 GLIB_AVAILABLE_IN_2_34
113 void    g_test_add_data_func_full       (const char     *testpath,
114                                          gpointer        test_data,
115                                          GTestDataFunc   test_func,
116                                          GDestroyNotify  data_free_func);
117
118 /* tell about failure */
119 void    g_test_fail                     (void);
120
121 /* hook up a test with fixture under test path */
122 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
123                                         G_STMT_START {                  \
124                                          void (*add_vtable) (const char*,       \
125                                                     gsize,             \
126                                                     gconstpointer,     \
127                                                     void (*) (Fixture*, gconstpointer),   \
128                                                     void (*) (Fixture*, gconstpointer),   \
129                                                     void (*) (Fixture*, gconstpointer)) =  (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
130                                          add_vtable \
131                                           (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
132                                         } G_STMT_END
133
134 /* add test messages to the test report */
135 void    g_test_message                  (const char *format,
136                                          ...) G_GNUC_PRINTF (1, 2);
137 void    g_test_bug_base                 (const char *uri_pattern);
138 void    g_test_bug                      (const char *bug_uri_snippet);
139 /* measure test timings */
140 void    g_test_timer_start              (void);
141 double  g_test_timer_elapsed            (void); /* elapsed seconds */
142 double  g_test_timer_last               (void); /* repeat last elapsed() result */
143
144 /* automatically g_free or g_object_unref upon teardown */
145 void    g_test_queue_free               (gpointer gfree_pointer);
146 void    g_test_queue_destroy            (GDestroyNotify destroy_func,
147                                          gpointer       destroy_data);
148 #define g_test_queue_unref(gobject)     g_test_queue_destroy (g_object_unref, gobject)
149
150 /* test traps are guards used around forked tests */
151 typedef enum {
152   G_TEST_TRAP_SILENCE_STDOUT    = 1 << 7,
153   G_TEST_TRAP_SILENCE_STDERR    = 1 << 8,
154   G_TEST_TRAP_INHERIT_STDIN     = 1 << 9
155 } GTestTrapFlags;
156 gboolean g_test_trap_fork               (guint64              usec_timeout,
157                                          GTestTrapFlags       test_trap_flags);
158 gboolean g_test_trap_has_passed         (void);
159 gboolean g_test_trap_reached_timeout    (void);
160 #define  g_test_trap_assert_passed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
161 #define  g_test_trap_assert_failed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
162 #define  g_test_trap_assert_stdout(soutpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
163 #define  g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
164 #define  g_test_trap_assert_stderr(serrpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
165 #define  g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
166
167 /* provide seed-able random numbers for tests */
168 #define  g_test_rand_bit()              (0 != (g_test_rand_int() & (1 << 15)))
169 gint32   g_test_rand_int                (void);
170 gint32   g_test_rand_int_range          (gint32          begin,
171                                          gint32          end);
172 double   g_test_rand_double             (void);
173 double   g_test_rand_double_range       (double          range_start,
174                                          double          range_end);
175
176 /* semi-internal API */
177 GTestCase*    g_test_create_case        (const char       *test_name,
178                                          gsize             data_size,
179                                          gconstpointer     test_data,
180                                          GTestFixtureFunc  data_setup,
181                                          GTestFixtureFunc  data_test,
182                                          GTestFixtureFunc  data_teardown);
183 GTestSuite*   g_test_create_suite       (const char       *suite_name);
184 GTestSuite*   g_test_get_root           (void);
185 void          g_test_suite_add          (GTestSuite     *suite,
186                                          GTestCase      *test_case);
187 void          g_test_suite_add_suite    (GTestSuite     *suite,
188                                          GTestSuite     *nestedsuite);
189 int           g_test_run_suite          (GTestSuite     *suite);
190
191 /* internal ABI */
192 void    g_test_trap_assertions          (const char     *domain,
193                                          const char     *file,
194                                          int             line,
195                                          const char     *func,
196                                          guint64         assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
197                                          const char     *pattern);
198 void    g_assertion_message             (const char     *domain,
199                                          const char     *file,
200                                          int             line,
201                                          const char     *func,
202                                          const char     *message) G_GNUC_NORETURN;
203 void    g_assertion_message_expr        (const char     *domain,
204                                          const char     *file,
205                                          int             line,
206                                          const char     *func,
207                                          const char     *expr) G_GNUC_NORETURN;
208 void    g_assertion_message_cmpstr      (const char     *domain,
209                                          const char     *file,
210                                          int             line,
211                                          const char     *func,
212                                          const char     *expr,
213                                          const char     *arg1,
214                                          const char     *cmp,
215                                          const char     *arg2) G_GNUC_NORETURN;
216 void    g_assertion_message_cmpnum      (const char     *domain,
217                                          const char     *file,
218                                          int             line,
219                                          const char     *func,
220                                          const char     *expr,
221                                          long double     arg1,
222                                          const char     *cmp,
223                                          long double     arg2,
224                                          char            numtype) G_GNUC_NORETURN;
225 void    g_assertion_message_error       (const char     *domain,
226                                          const char     *file,
227                                          int             line,
228                                          const char     *func,
229                                          const char     *expr,
230                                          const GError   *error,
231                                          GQuark          error_domain,
232                                          int             error_code) G_GNUC_NORETURN;
233 void    g_test_add_vtable               (const char     *testpath,
234                                          gsize           data_size,
235                                          gconstpointer   test_data,
236                                          GTestFixtureFunc  data_setup,
237                                          GTestFixtureFunc  data_test,
238                                          GTestFixtureFunc  data_teardown);
239 typedef struct {
240   gboolean      test_initialized;
241   gboolean      test_quick;     /* disable thorough tests */
242   gboolean      test_perf;      /* run performance tests */
243   gboolean      test_verbose;   /* extra info */
244   gboolean      test_quiet;     /* reduce output */
245   gboolean      test_undefined; /* run tests that are meant to assert */
246 } GTestConfig;
247 GLIB_VAR const GTestConfig * const g_test_config_vars;
248
249 /* internal logging API */
250 typedef enum {
251   G_TEST_LOG_NONE,
252   G_TEST_LOG_ERROR,             /* s:msg */
253   G_TEST_LOG_START_BINARY,      /* s:binaryname s:seed */
254   G_TEST_LOG_LIST_CASE,         /* s:testpath */
255   G_TEST_LOG_SKIP_CASE,         /* s:testpath */
256   G_TEST_LOG_START_CASE,        /* s:testpath */
257   G_TEST_LOG_STOP_CASE,         /* d:status d:nforks d:elapsed */
258   G_TEST_LOG_MIN_RESULT,        /* s:blurb d:result */
259   G_TEST_LOG_MAX_RESULT,        /* s:blurb d:result */
260   G_TEST_LOG_MESSAGE            /* s:blurb */
261 } GTestLogType;
262
263 typedef struct {
264   GTestLogType  log_type;
265   guint         n_strings;
266   gchar       **strings; /* NULL terminated */
267   guint         n_nums;
268   long double  *nums;
269 } GTestLogMsg;
270 typedef struct {
271   /*< private >*/
272   GString     *data;
273   GSList      *msgs;
274 } GTestLogBuffer;
275
276 const char*     g_test_log_type_name    (GTestLogType    log_type);
277 GTestLogBuffer* g_test_log_buffer_new   (void);
278 void            g_test_log_buffer_free  (GTestLogBuffer *tbuffer);
279 void            g_test_log_buffer_push  (GTestLogBuffer *tbuffer,
280                                          guint           n_bytes,
281                                          const guint8   *bytes);
282 GTestLogMsg*    g_test_log_buffer_pop   (GTestLogBuffer *tbuffer);
283 void            g_test_log_msg_free     (GTestLogMsg    *tmsg);
284
285 /**
286  * GTestLogFatalFunc:
287  * @log_domain: the log domain of the message
288  * @log_level: the log level of the message (including the fatal and recursion flags)
289  * @message: the message to process
290  * @user_data: user data, set in g_test_log_set_fatal_handler()
291  *
292  * Specifies the prototype of fatal log handler functions.
293  *
294  * Return value: %TRUE if the program should abort, %FALSE otherwise
295  *
296  * Since: 2.22
297  */
298 typedef gboolean        (*GTestLogFatalFunc)    (const gchar    *log_domain,
299                                                  GLogLevelFlags  log_level,
300                                                  const gchar    *message,
301                                                  gpointer        user_data);
302 void
303 g_test_log_set_fatal_handler            (GTestLogFatalFunc log_func,
304                                          gpointer          user_data);
305
306 void    g_test_expect_message                    (const gchar    *log_domain,
307                                                   GLogLevelFlags  log_level,
308                                                   const gchar    *pattern);
309 void    g_test_assert_expected_messages_internal (const char     *domain,
310                                                   const char     *file,
311                                                   int             line,
312                                                   const char     *func);
313
314 #define g_test_assert_expected_messages() g_test_assert_expected_messages_internal (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC)
315
316 G_END_DECLS
317
318 #endif /* __G_TEST_UTILS_H__ */