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