7deb2a388903dcf89f94542df7b3744c8b55bd1b
[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 #ifndef __G_TEST_UTILS_H__
21 #define __G_TEST_UTILS_H__
22
23 #include <glib.h>
24
25 G_BEGIN_DECLS
26
27 typedef struct GTestCase  GTestCase;
28 typedef struct GTestSuite GTestSuite;
29
30 /* assertion API */
31 #define g_assert_cmpstr(s1, cmp, s2)    do { const char *__s1 = (s1), *__s2 = (s2); \
32                                              if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
33                                                g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
34                                                  #s1 " " #cmp " " #s2, __s1, #cmp, __s2); } while (0)
35 #define g_assert_cmpint(n1, cmp, n2)    do { gint64 __n1 = (n1), __n2 = (n2); \
36                                              if (__n1 cmp __n2) ; else \
37                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
38                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
39 #define g_assert_cmpuint(n1, cmp, n2)   do { guint64 __n1 = (n1), __n2 = (n2); \
40                                              if (__n1 cmp __n2) ; else \
41                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
42                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
43 #define g_assert_cmphex(n1, cmp, n2)    do { guint64 __n1 = (n1), __n2 = (n2); \
44                                              if (__n1 cmp __n2) ; else \
45                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
46                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'x'); } while (0)
47 #define g_assert_cmpfloat(n1,cmp,n2)    do { long double __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, 'f'); } while (0)
51 #ifdef G_DISABLE_ASSERT
52 #define g_assert_not_reached()          do { (void) 0; } while (0)
53 #define g_assert(expr)                  do { (void) 0; } while (0)
54 #else /* !G_DISABLE_ASSERT */
55 #define g_assert_not_reached()          do { g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, NULL); } while (0)
56 #define g_assert(expr)                  do { if G_LIKELY (expr) ; else \
57                                                g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
58                                                  #expr); } while (0)
59 #endif /* !G_DISABLE_ASSERT */
60
61 int     g_strcmp0                       (const char     *str1,
62                                          const char     *str2);
63
64 /* report performance results */
65 void    g_test_minimized_result         (double          minimized_quantity,
66                                          const char     *format,
67                                          ...) G_GNUC_PRINTF (2, 3);
68 void    g_test_maximized_result         (double          maximized_quantity,
69                                          const char     *format,
70                                          ...) G_GNUC_PRINTF (2, 3);
71
72 /* initialize testing framework */
73 void    g_test_init                     (int            *argc,
74                                          char         ***argv,
75                                          ...);
76 /* query testing framework config */
77 #define g_test_quick()                  (g_test_config_vars->test_quick)
78 #define g_test_slow()                   (!g_test_config_vars->test_quick)
79 #define g_test_thorough()               (!g_test_config_vars->test_quick)
80 #define g_test_perf()                   (g_test_config_vars->test_perf)
81 #define g_test_verbose()                (g_test_config_vars->test_verbose)
82 #define g_test_quiet()                  (g_test_config_vars->test_quiet)
83 /* run all tests under toplevel suite (path: /) */
84 int     g_test_run                      (void);
85 /* hook up a test functions under test path */
86 void    g_test_add_func                 (const char     *testpath,
87                                          void          (*test_func) (void));
88 void    g_test_add_data_func            (const char     *testpath,
89                                          gconstpointer   test_data,
90                                          void          (*test_func) (gconstpointer));
91 /* hook up a test with fixture under test path */
92 #define g_test_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
93                                         G_STMT_START {                  \
94                                          void (*add_vtable) (const char*,       \
95                                                     gsize,             \
96                                                     gconstpointer,     \
97                                                     void (*) (Fixture*, gconstpointer),   \
98                                                     void (*) (Fixture*, gconstpointer),   \
99                                                     void (*) (Fixture*, gconstpointer)) =  (void (*) (const gchar *, gsize, gconstpointer, void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer), void (*) (Fixture*, gconstpointer))) g_test_add_vtable; \
100                                          add_vtable \
101                                           (testpath, sizeof (Fixture), tdata, fsetup, ftest, fteardown); \
102                                         } G_STMT_END
103
104 /* add test messages to the test report */
105 void    g_test_message                  (const char *format,
106                                          ...) G_GNUC_PRINTF (1, 2);
107 void    g_test_bug_base                 (const char *uri_pattern);
108 void    g_test_bug                      (const char *bug_uri_snippet);
109 /* measure test timings */
110 void    g_test_timer_start              (void);
111 double  g_test_timer_elapsed            (void); /* elapsed seconds */
112 double  g_test_timer_last               (void); /* repeat last elapsed() result */
113
114 /* automatically g_free or g_object_unref upon teardown */
115 void    g_test_queue_free               (gpointer gfree_pointer);
116 void    g_test_queue_destroy            (GDestroyNotify destroy_func,
117                                          gpointer       destroy_data);
118 #define g_test_queue_unref(gobject)     g_test_queue_destroy (g_object_unref, gobject)
119
120 /* test traps are guards used around forked tests */
121 typedef enum {
122   G_TEST_TRAP_SILENCE_STDOUT    = 1 << 7,
123   G_TEST_TRAP_SILENCE_STDERR    = 1 << 8,
124   G_TEST_TRAP_INHERIT_STDIN     = 1 << 9
125 } GTestTrapFlags;
126 gboolean g_test_trap_fork               (guint64              usec_timeout,
127                                          GTestTrapFlags       test_trap_flags);
128 gboolean g_test_trap_has_passed         (void);
129 gboolean g_test_trap_reached_timeout    (void);
130 #define  g_test_trap_assert_passed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0)
131 #define  g_test_trap_assert_failed()                      g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0)
132 #define  g_test_trap_assert_stdout(soutpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 2, soutpattern)
133 #define  g_test_trap_assert_stdout_unmatched(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 3, soutpattern)
134 #define  g_test_trap_assert_stderr(serrpattern)           g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 4, serrpattern)
135 #define  g_test_trap_assert_stderr_unmatched(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 5, serrpattern)
136
137 /* provide seed-able random numbers for tests */
138 #define  g_test_rand_bit()              (0 != (g_test_rand_int() & (1 << 15)))
139 gint32   g_test_rand_int                (void);
140 gint32   g_test_rand_int_range          (gint32          begin,
141                                          gint32          end);
142 double   g_test_rand_double             (void);
143 double   g_test_rand_double_range       (double          range_start,
144                                          double          range_end);
145
146 /* semi-internal API */
147 GTestCase*    g_test_create_case        (const char     *test_name,
148                                          gsize           data_size,
149                                          gconstpointer   test_data,
150                                          void          (*data_setup) (void),
151                                          void          (*data_test) (void),
152                                          void          (*data_teardown) (void));
153 GTestSuite*   g_test_create_suite       (const char     *suite_name);
154 GTestSuite*   g_test_get_root           (void);
155 void          g_test_suite_add          (GTestSuite     *suite,
156                                          GTestCase      *test_case);
157 void          g_test_suite_add_suite    (GTestSuite     *suite,
158                                          GTestSuite     *nestedsuite);
159 int           g_test_run_suite          (GTestSuite     *suite);
160
161 /* internal ABI */
162 void    g_test_trap_assertions          (const char     *domain,
163                                          const char     *file,
164                                          int             line,
165                                          const char     *func,
166                                          guint64         assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
167                                          const char     *pattern);
168 void    g_assertion_message             (const char     *domain,
169                                          const char     *file,
170                                          int             line,
171                                          const char     *func,
172                                          const char     *message) G_GNUC_NORETURN;
173 void    g_assertion_message_expr        (const char     *domain,
174                                          const char     *file,
175                                          int             line,
176                                          const char     *func,
177                                          const char     *expr) G_GNUC_NORETURN;
178 void    g_assertion_message_cmpstr      (const char     *domain,
179                                          const char     *file,
180                                          int             line,
181                                          const char     *func,
182                                          const char     *expr,
183                                          const char     *arg1,
184                                          const char     *cmp,
185                                          const char     *arg2) G_GNUC_NORETURN;
186 void    g_assertion_message_cmpnum      (const char     *domain,
187                                          const char     *file,
188                                          int             line,
189                                          const char     *func,
190                                          const char     *expr,
191                                          long double     arg1,
192                                          const char     *cmp,
193                                          long double     arg2,
194                                          char            numtype) G_GNUC_NORETURN;
195 void    g_test_add_vtable               (const char     *testpath,
196                                          gsize           data_size,
197                                          gconstpointer   test_data,
198                                          void          (*data_setup)    (void),
199                                          void          (*data_test)     (void),
200                                          void          (*data_teardown) (void));
201 typedef struct {
202   gboolean      test_initialized;
203   gboolean      test_quick;     /* disable thorough tests */
204   gboolean      test_perf;      /* run performance tests */
205   gboolean      test_verbose;   /* extra info */
206   gboolean      test_quiet;     /* reduce output */
207 } GTestConfig;
208 GLIB_VAR const GTestConfig * const g_test_config_vars;
209
210 /* internal logging API */
211 typedef enum {
212   G_TEST_LOG_NONE,
213   G_TEST_LOG_ERROR,             /* s:msg */
214   G_TEST_LOG_START_BINARY,      /* s:binaryname s:seed */
215   G_TEST_LOG_LIST_CASE,         /* s:testpath */
216   G_TEST_LOG_SKIP_CASE,         /* s:testpath */
217   G_TEST_LOG_START_CASE,        /* s:testpath */
218   G_TEST_LOG_STOP_CASE,         /* d:status d:nforks d:elapsed */
219   G_TEST_LOG_MIN_RESULT,        /* s:blurb d:result */
220   G_TEST_LOG_MAX_RESULT,        /* s:blurb d:result */
221   G_TEST_LOG_MESSAGE            /* s:blurb */
222 } GTestLogType;
223
224 typedef struct {
225   GTestLogType  log_type;
226   guint         n_strings;
227   gchar       **strings; /* NULL terminated */
228   guint         n_nums;
229   long double  *nums;
230 } GTestLogMsg;
231 typedef struct {
232   /*< private >*/
233   GString     *data;
234   GSList      *msgs;
235 } GTestLogBuffer;
236
237 const char*     g_test_log_type_name    (GTestLogType    log_type);
238 GTestLogBuffer* g_test_log_buffer_new   (void);
239 void            g_test_log_buffer_free  (GTestLogBuffer *tbuffer);
240 void            g_test_log_buffer_push  (GTestLogBuffer *tbuffer,
241                                          guint           n_bytes,
242                                          const guint8   *bytes);
243 GTestLogMsg*    g_test_log_buffer_pop   (GTestLogBuffer *tbuffer);
244 void            g_test_log_msg_free     (GTestLogMsg    *tmsg);
245
246 G_END_DECLS
247
248 #endif /* __G_TEST_UTILS_H__ */