09008aa7900f424a707d415e7f4f89b9def04a0c
[apps/home/video-player.git] / test / hb-test.h
1 /*
2  * Copyright © 2011  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_TEST_H
28 #define HB_TEST_H
29
30 #include <config.h>
31
32 #include <hb-glib.h>
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 HB_BEGIN_DECLS
38
39 /* Just in case */
40 #undef G_DISABLE_ASSERT
41
42
43 /* Misc */
44
45 /* This is too ugly to be public API, but quite handy. */
46 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
47                                   ((const char *) s)[1], \
48                                   ((const char *) s)[2], \
49                                   ((const char *) s)[3]))
50
51
52 static inline const char *
53 srcdir (void)
54 {
55   static const char *s;
56
57   if (!s) {
58     s = getenv ("srcdir");
59
60 #ifdef SRCDIR
61     if (!s || !s[0])
62       s = SRCDIR;
63 #endif
64
65     if (!s || !s[0])
66       s = ".";
67   }
68
69   return s;
70 }
71
72
73 /* Helpers */
74
75 static inline void
76 hb_test_init (int *argc, char ***argv)
77 {
78   g_thread_init (NULL);
79   g_test_init (argc, argv, NULL);
80 }
81
82 static inline int
83 hb_test_run (void)
84 {
85   return g_test_run ();
86 }
87
88
89 /* Bugzilla helpers */
90
91 static inline void
92 hb_test_bug (const char *uri_base, unsigned int number)
93 {
94   char *s = g_strdup_printf ("%u", number);
95
96   g_test_bug_base (uri_base);
97   g_test_bug (s);
98
99   g_free (s);
100 }
101
102 static inline void
103 hb_test_bug_freedesktop (unsigned int number)
104 {
105   hb_test_bug ("http://bugs.freedesktop.org/", number);
106 }
107
108 static inline void
109 hb_test_bug_gnome (unsigned int number)
110 {
111   hb_test_bug ("http://bugzilla.gnome.org/", number);
112 }
113
114 static inline void
115 hb_test_bug_mozilla (unsigned int number)
116 {
117   hb_test_bug ("http://bugzilla.mozilla.org/", number);
118 }
119
120 static inline void
121 hb_test_bug_redhat (unsigned int number)
122 {
123   hb_test_bug ("http://bugzilla.redhat.com/", number);
124 }
125
126
127 /* Wrap glib test functions to simplify.  Should have been in glib already. */
128
129 /* Drops the "test_" prefix and converts '_' to '/'.
130  * Essentially builds test path from function name. */
131 static inline char *
132 hb_test_normalize_path (const char *path)
133 {
134   char *s, *p;
135
136   g_assert (0 == strncmp (path, "test_", 5));
137   path += 4;
138
139   s = g_strdup (path);
140   for (p = s; *p; p++)
141     if (*p == '_')
142       *p = '/';
143
144   return s;
145 }
146
147
148 #if GLIB_CHECK_VERSION(2,25,12)
149 typedef GTestFunc        hb_test_func_t;
150 typedef GTestDataFunc    hb_test_data_func_t;
151 typedef GTestFixtureFunc hb_test_fixture_func_t;
152 #else
153 typedef void (*hb_test_func_t)         (void);
154 typedef void (*hb_test_data_func_t)    (gconstpointer user_data);
155 typedef void (*hb_test_fixture_func_t) (gpointer      fixture,
156                                         gconstpointer user_data);
157 #endif
158
159 static inline void
160 hb_test_add_func (const char *test_path,
161                   hb_test_func_t   test_func)
162 {
163   char *normal_path = hb_test_normalize_path (test_path);
164   g_test_add_func (normal_path, test_func);
165   g_free (normal_path);
166 }
167 #define hb_test_add(Func) hb_test_add_func (#Func, Func)
168
169 static inline void
170 hb_test_add_func_flavor (const char *test_path,
171                          const char *flavor,
172                          hb_test_func_t   test_func)
173 {
174   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
175   hb_test_add_func (path, test_func);
176   g_free (path);
177 }
178 #define hb_test_add_flavor(Flavor, Func) hb_test_add_func (#Func, Flavor, Func)
179
180 static inline void
181 hb_test_add_data_func (const char          *test_path,
182                        gconstpointer        test_data,
183                        hb_test_data_func_t  test_func)
184 {
185   char *normal_path = hb_test_normalize_path (test_path);
186   g_test_add_data_func (normal_path, test_data, test_func);
187   g_free (normal_path);
188 }
189 #define hb_test_add_data(UserData, Func) hb_test_add_data_func (#Func, UserData, Func)
190
191 static inline void
192 hb_test_add_data_func_flavor (const char          *test_path,
193                               const char          *flavor,
194                               gconstpointer        test_data,
195                               hb_test_data_func_t  test_func)
196 {
197   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
198   hb_test_add_data_func (path, test_data, test_func);
199   g_free (path);
200 }
201 #define hb_test_add_data_flavor(UserData, Flavor, Func) hb_test_add_data_func_flavor (#Func, Flavor, UserData, Func)
202
203
204 static inline void
205 hb_test_add_vtable (const char             *test_path,
206                     gsize                   data_size,
207                     gconstpointer           test_data,
208                     hb_test_fixture_func_t  data_setup,
209                     hb_test_fixture_func_t  data_test,
210                     hb_test_fixture_func_t  data_teardown)
211 {
212   char *normal_path = hb_test_normalize_path (test_path);
213   g_test_add_vtable (normal_path, data_size, test_data, data_setup, data_test, data_teardown);
214   g_free (normal_path);
215 }
216 #define hb_test_add_fixture(FixturePrefix, UserData, Func) \
217 G_STMT_START { \
218   typedef G_PASTE (FixturePrefix, _t) Fixture; \
219   void (*add_vtable) (const char*, gsize, gconstpointer, \
220                       void (*) (Fixture*, gconstpointer), \
221                       void (*) (Fixture*, gconstpointer), \
222                       void (*) (Fixture*, gconstpointer)) \
223         = (void (*) (const gchar *, gsize, gconstpointer, \
224                      void (*) (Fixture*, gconstpointer), \
225                      void (*) (Fixture*, gconstpointer), \
226                      void (*) (Fixture*, gconstpointer))) hb_test_add_vtable; \
227   add_vtable (#Func, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
228               G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
229 } G_STMT_END
230
231 static inline void
232 hb_test_add_vtable_flavor (const char             *test_path,
233                            const char             *flavor,
234                            gsize                   data_size,
235                            gconstpointer           test_data,
236                            hb_test_fixture_func_t  data_setup,
237                            hb_test_fixture_func_t  data_test,
238                            hb_test_fixture_func_t  data_teardown)
239 {
240   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
241   hb_test_add_vtable (path, data_size, test_data, data_setup, data_test, data_teardown);
242   g_free (path);
243 }
244 #define hb_test_add_fixture_flavor(FixturePrefix, UserData, Flavor, Func) \
245 G_STMT_START { \
246   typedef G_PASTE (FixturePrefix, _t) Fixture; \
247   void (*add_vtable) (const char*, const char *, gsize, gconstpointer, \
248                       void (*) (Fixture*, gconstpointer), \
249                       void (*) (Fixture*, gconstpointer), \
250                       void (*) (Fixture*, gconstpointer)) \
251         = (void (*) (const gchar *, const char *, gsize, gconstpointer, \
252                      void (*) (Fixture*, gconstpointer), \
253                      void (*) (Fixture*, gconstpointer), \
254                      void (*) (Fixture*, gconstpointer))) hb_test_add_vtable_flavor; \
255   add_vtable (#Func, Flavor, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
256               G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
257 } G_STMT_END
258
259
260 HB_END_DECLS
261
262 #endif /* HB_TEST_H */