Renamed gtestframework to gtestutils.
[platform/upstream/glib.git] / glib / gtestutils.h
1 /* GLib testing utilities
2  * Copyright (C) 2007 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #ifndef __G_TEST_UTILS_H__
20 #define __G_TEST_UTILS_H__
21
22 #include <glib.h>
23
24 G_BEGIN_DECLS;
25
26 typedef struct GTestCase  GTestCase;
27 typedef struct GTestSuite GTestSuite;
28
29 /* assertion API */
30 #define g_assert_cmpstr(s1, cmp, s2)    do { const char *__s1 = (s1), *__s2 = (s2); \
31                                              if (g_strcmp0 (__s1, __s2) cmp 0) ; else \
32                                                g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
33                                                  #s1 " " #cmp " " #s2, __s1, #cmp, __s2); } while (0)
34 #define g_assert_cmpint(n1, cmp, n2)    do { gint64 __n1 = (n1), __n2 = (n2); \
35                                              if (__n1 cmp __n2) ; else \
36                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
37                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
38 #define g_assert_cmpuint(n1, cmp, n2)   do { guint64 __n1 = (n1), __n2 = (n2); \
39                                              if (__n1 cmp __n2) ; else \
40                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
41                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'i'); } while (0)
42 #define g_assert_cmphex(n1, cmp, n2)    do { guint64 __n1 = (n1), __n2 = (n2); \
43                                              if (__n1 cmp __n2) ; else \
44                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
45                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'x'); } while (0)
46 #define g_assert_cmpfloat(n1,cmp,n2)    do { long double __n1 = (n1), __n2 = (n2); \
47                                              if (__n1 cmp __n2) ; else \
48                                                g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
49                                                  #n1 " " #cmp " " #n2, __n1, #cmp, __n2, 'f'); } while (0)
50 int     g_strcmp0                       (const char     *str1,
51                                          const char     *str2);
52 //      g_assert(condition)             /*...*/
53 //      g_assert_not_reached()          /*...*/
54
55 /* report performance results */
56 void    g_test_minimized_result         (double          minimized_quantity,
57                                          const char     *format,
58                                          ...) G_GNUC_PRINTF (2, 3);
59 void    g_test_maximized_result         (double          maximized_quantity,
60                                          const char     *format,
61                                          ...) G_GNUC_PRINTF (2, 3);
62
63 /* initialize testing framework */
64 void    g_test_init                     (int            *argc,
65                                          char         ***argv,
66                                          ...);
67 /* query testing framework config */
68 #define g_test_quick()                  (g_test_config_vars->test_quick)
69 #define g_test_perf()                   (g_test_config_vars->test_perf)
70 #define g_test_verbose()                (g_test_config_vars->test_verbose)
71 #define g_test_quiet()                  (g_test_config_vars->test_quiet)
72 /* run all tests under toplevel suite (path: /) */
73 int     g_test_run                      (void);
74 /* hook up a simple test function under test path */
75 void    g_test_add_func                 (const char     *testpath,
76                                          void          (*test_func) (void));
77 /* hook up a test with fixture under test path */
78 #define g_test_add(testpath, Fixture, fsetup, ftest, fteardown) \
79                                         ((void (*) (const char*,        \
80                                                     gsize,              \
81                                                     void (*) (Fixture*),   \
82                                                     void (*) (Fixture*),   \
83                                                     void (*) (Fixture*)))  \
84                                          (void*) g_test_add_vtable) \
85                                           (testpath, sizeof (Fixture), fsetup, ftest, fteardown)
86 /* add test messages to the test report */
87 void    g_test_message                  (const char *format,
88                                          ...) G_GNUC_PRINTF (1, 2);
89 void    g_test_bug_base                 (const char *uri_pattern);
90 void    g_test_bug                      (const char *bug_uri_snippet);
91 /* measure test timings */
92 void    g_test_timer_start              (void);
93 double  g_test_timer_elapsed            (void); // elapsed seconds
94 double  g_test_timer_last               (void); // repeat last elapsed() result
95
96 /* automatically g_free or g_object_unref upon teardown */
97 void    g_test_queue_free               (gpointer gfree_pointer);
98 void    g_test_queue_destroy            (GDestroyNotify destroy_func,
99                                          gpointer       destroy_data);
100 #define g_test_queue_unref(gobject)     g_test_queue_destroy (g_object_unref, gobject)
101
102 /* test traps are guards used around forked tests */
103 typedef enum {
104   G_TEST_TRAP_SILENCE_STDOUT    = 1 << 7,
105   G_TEST_TRAP_SILENCE_STDERR    = 1 << 8,
106   G_TEST_TRAP_INHERIT_STDIN     = 1 << 9,
107 } GTestTrapFlags;
108 gboolean g_test_trap_fork               (guint64              usec_timeout,
109                                          GTestTrapFlags       test_trap_flags);
110 gboolean g_test_trap_has_passed         (void);
111 gboolean g_test_trap_reached_timeout    (void);
112 #define  g_test_trap_assert_passed()            g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 1, 0, 0, 0)
113 #define  g_test_trap_assert_failed()            g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 1, 0, 0)
114 #define  g_test_trap_assert_stdout(soutpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0, soutpattern, 0)
115 #define  g_test_trap_assert_stderr(serrpattern) g_test_trap_assertions (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, 0, 0, 0, serrpattern)
116
117 /* provide seed-able random numbers for tests */
118 #define  g_test_rand_bit()              (0 != (g_test_rand_int() & (1 << 15)))
119 gint32   g_test_rand_int                (void);
120 gint32   g_test_rand_int_range          (gint32          begin,
121                                          gint32          end);
122 double   g_test_rand_double             (void);
123 double   g_test_rand_double_range       (double          range_start,
124                                          double          range_end);
125
126 /* semi-internal API */
127 GTestCase*    g_test_create_case        (const char     *test_name,
128                                          gsize           data_size,
129                                          void          (*data_setup) (void),
130                                          void          (*data_test) (void),
131                                          void          (*data_teardown) (void));
132 GTestSuite*   g_test_create_suite       (const char     *suite_name);
133 GTestSuite*   g_test_get_root           (void);
134 void          g_test_suite_add          (GTestSuite     *suite,
135                                          GTestCase      *test_case);
136 void          g_test_suite_add_suite    (GTestSuite     *suite,
137                                          GTestSuite     *nestedsuite);
138 int           g_test_run_suite          (GTestSuite     *suite);
139
140 /* internal ABI */
141 void    g_test_trap_assertions          (const char     *domain,
142                                          const char     *file,
143                                          int             line,
144                                          const char     *func,
145                                          gboolean        must_pass,
146                                          gboolean        must_fail,
147                                          const char     *stdout_pattern,
148                                          const char     *stderr_pattern);
149 void    g_assertion_message             (const char     *domain,
150                                          const char     *file,
151                                          int             line,
152                                          const char     *func,
153                                          const char     *message);
154 void    g_assertion_message_expr        (const char     *domain,
155                                          const char     *file,
156                                          int             line,
157                                          const char     *func,
158                                          const char     *expr);
159 void    g_assertion_message_cmpstr      (const char     *domain,
160                                          const char     *file,
161                                          int             line,
162                                          const char     *func,
163                                          const char     *expr,
164                                          const char     *arg1,
165                                          const char     *cmp,
166                                          const char     *arg2);
167 void    g_assertion_message_cmpnum      (const char     *domain,
168                                          const char     *file,
169                                          int             line,
170                                          const char     *func,
171                                          const char     *expr,
172                                          long double     arg1,
173                                          const char     *cmp,
174                                          long double     arg2,
175                                          char            numtype);
176 void    g_test_add_vtable               (const char     *testpath,
177                                          gsize           data_size,
178                                          void          (*data_setup)    (void),
179                                          void          (*data_test)     (void),
180                                          void          (*data_teardown) (void));
181 typedef struct {
182   gboolean      test_quick;     /* disable thorough tests */
183   gboolean      test_perf;      /* run performance tests */
184   gboolean      test_verbose;   /* extra info */
185   gboolean      test_quiet;     /* reduce output */
186 } GTestConfig;
187 GLIB_VAR const GTestConfig *g_test_config_vars;
188
189 /* internal logging API */
190 typedef enum {
191   G_TEST_LOG_NONE,
192   G_TEST_LOG_ERROR,             // s:msg
193   G_TEST_LOG_START_BINARY,      // s:binaryname s:seed
194   G_TEST_LOG_LIST_CASE,         // s:testpath
195   G_TEST_LOG_SKIP_CASE,         // s:testpath
196   G_TEST_LOG_START_CASE,        // s:testpath
197   G_TEST_LOG_STOP_CASE,         // d:status d:nforks d:elapsed
198   G_TEST_LOG_MIN_RESULT,        // s:blurb d:result
199   G_TEST_LOG_MAX_RESULT,        // s:blurb d:result
200   G_TEST_LOG_MESSAGE,           // s:blurb
201 } GTestLogType;
202
203 typedef struct {
204   GTestLogType  log_type;
205   guint         n_strings;
206   gchar       **strings; // NULL terminated
207   guint         n_nums;
208   long double  *nums;
209 } GTestLogMsg;
210 typedef struct {
211   /*< private >*/
212   GString     *data;
213   GSList      *msgs;
214 } GTestLogBuffer;
215
216 const char*     g_test_log_type_name    (GTestLogType    log_type);
217 GTestLogBuffer* g_test_log_buffer_new   (void);
218 void            g_test_log_buffer_free  (GTestLogBuffer *tbuffer);
219 void            g_test_log_buffer_push  (GTestLogBuffer *tbuffer,
220                                          guint           n_bytes,
221                                          const guint8   *bytes);
222 GTestLogMsg*    g_test_log_buffer_pop   (GTestLogBuffer *tbuffer);
223 void            g_test_log_msg_free     (GTestLogMsg    *tmsg);
224
225 G_END_DECLS;
226
227 #endif /* __G_TEST_UTILS_H__ */