Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / test / api / 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 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <hb-glib.h>
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39
40 HB_BEGIN_DECLS
41
42 /* Just in case */
43 #undef G_DISABLE_ASSERT
44
45
46 /* Misc */
47
48 /* This is too ugly to be public API, but quite handy. */
49 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
50                                   ((const char *) s)[1], \
51                                   ((const char *) s)[2], \
52                                   ((const char *) s)[3]))
53
54
55 static inline const char *
56 srcdir (void)
57 {
58   static const char *s;
59
60   if (!s) {
61     s = getenv ("srcdir");
62
63 #ifdef SRCDIR
64     if (!s || !s[0])
65       s = SRCDIR;
66 #endif
67
68     if (!s || !s[0])
69       s = ".";
70   }
71
72   return s;
73 }
74
75
76 /* Helpers */
77
78 static inline void
79 hb_test_init (int *argc, char ***argv)
80 {
81   g_test_init (argc, argv, NULL);
82 }
83
84 static inline int
85 hb_test_run (void)
86 {
87   return g_test_run ();
88 }
89
90
91 #if 0
92 /* Helpers for loading test fonts */
93 static inline hb_face_t *
94 hb_test_load_face (const char *path)
95 {
96   const char *font_data = NULL;
97   unsigned int len = 0;
98   hb_blob_t *blob = NULL;
99   hb_face_t *face = NULL;
100
101   FILE *f = fopen (path, "rb");
102   if (!f) {
103     perror (path);
104     exit (1);
105   }
106
107   fseek (f, 0, SEEK_END);
108   len = ftell (f);
109   fseek (f, 0, SEEK_SET);
110   font_data = (const char *) malloc (len);
111   if (!font_data) len = 0;
112   len = fread ((char *) font_data, 1, len, f);
113   fclose (f);
114
115   blob = hb_blob_create (font_data, len, HB_MEMORY_MODE_READONLY, 0, free);
116   face = hb_face_create (blob, 0 /* first face */);
117   hb_blob_destroy (blob);
118   return face;
119 }
120 #endif
121
122
123 /* Bugzilla helpers */
124
125 static inline void
126 hb_test_bug (const char *uri_base, unsigned int number)
127 {
128   char *s = g_strdup_printf ("%u", number);
129
130   g_test_bug_base (uri_base);
131   g_test_bug (s);
132
133   g_free (s);
134 }
135
136 static inline void
137 hb_test_bug_freedesktop (unsigned int number)
138 {
139   hb_test_bug ("http://bugs.freedesktop.org/", number);
140 }
141
142 static inline void
143 hb_test_bug_gnome (unsigned int number)
144 {
145   hb_test_bug ("http://bugzilla.gnome.org/", number);
146 }
147
148 static inline void
149 hb_test_bug_mozilla (unsigned int number)
150 {
151   hb_test_bug ("http://bugzilla.mozilla.org/", number);
152 }
153
154 static inline void
155 hb_test_bug_redhat (unsigned int number)
156 {
157   hb_test_bug ("http://bugzilla.redhat.com/", number);
158 }
159
160
161 /* Wrap glib test functions to simplify.  Should have been in glib already. */
162
163 /* Drops the "test_" prefix and converts '_' to '/'.
164  * Essentially builds test path from function name. */
165 static inline char *
166 hb_test_normalize_path (const char *path)
167 {
168   char *s, *p;
169
170   g_assert (0 == strncmp (path, "test_", 5));
171   path += 4;
172
173   s = g_strdup (path);
174   for (p = s; *p; p++)
175     if (*p == '_')
176       *p = '/';
177
178   return s;
179 }
180
181
182 #if GLIB_CHECK_VERSION(2,25,12)
183 typedef GTestFunc        hb_test_func_t;
184 typedef GTestDataFunc    hb_test_data_func_t;
185 typedef GTestFixtureFunc hb_test_fixture_func_t;
186 #else
187 typedef void (*hb_test_func_t)         (void);
188 typedef void (*hb_test_data_func_t)    (gconstpointer user_data);
189 typedef void (*hb_test_fixture_func_t) (void);
190 #endif
191
192 #if !GLIB_CHECK_VERSION(2,30,0)
193 #define g_test_fail() g_error("Test failed")
194 #endif
195 #ifndef g_assert_true
196 #define g_assert_true g_assert
197 #endif
198 #ifndef g_assert_cmpmem
199 #define g_assert_cmpmem(m1, l1, m2, l2) g_assert_true (l1 == l2 && memcmp (m1, m2, l1) == 0)
200 #endif
201
202 static inline void hb_test_assert_blobs_equal (hb_blob_t *expected_blob, hb_blob_t *actual_blob)
203 {
204   unsigned int expected_length, actual_length;
205   const char *raw_expected = hb_blob_get_data (expected_blob, &expected_length);
206   const char *raw_actual = hb_blob_get_data (actual_blob, &actual_length);
207   g_assert_cmpint(expected_length, ==, actual_length);
208   g_assert_cmpint(0, ==, memcmp(raw_expected, raw_actual, expected_length));
209 }
210
211 static inline void
212 hb_test_add_func (const char *test_path,
213                   hb_test_func_t   test_func)
214 {
215   char *normal_path = hb_test_normalize_path (test_path);
216   g_test_add_func (normal_path, test_func);
217   g_free (normal_path);
218 }
219 #define hb_test_add(Func) hb_test_add_func (#Func, Func)
220
221 static inline void
222 hb_test_add_func_flavor (const char *test_path,
223                          const char *flavor,
224                          hb_test_func_t   test_func)
225 {
226   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
227   hb_test_add_func (path, test_func);
228   g_free (path);
229 }
230 #define hb_test_add_flavor(Flavor, Func) hb_test_add_func (#Func, Flavor, Func)
231
232 static inline void
233 hb_test_add_data_func (const char          *test_path,
234                        gconstpointer        test_data,
235                        hb_test_data_func_t  test_func)
236 {
237   char *normal_path = hb_test_normalize_path (test_path);
238   g_test_add_data_func (normal_path, test_data, test_func);
239   g_free (normal_path);
240 }
241 #define hb_test_add_data(UserData, Func) hb_test_add_data_func (#Func, UserData, Func)
242
243 static inline void
244 hb_test_add_data_func_flavor (const char          *test_path,
245                               const char          *flavor,
246                               gconstpointer        test_data,
247                               hb_test_data_func_t  test_func)
248 {
249   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
250   hb_test_add_data_func (path, test_data, test_func);
251   g_free (path);
252 }
253 #define hb_test_add_data_flavor(UserData, Flavor, Func) hb_test_add_data_func_flavor (#Func, Flavor, UserData, Func)
254
255
256 static inline void
257 hb_test_add_vtable (const char             *test_path,
258                     gsize                   data_size,
259                     gconstpointer           test_data,
260                     hb_test_fixture_func_t  data_setup,
261                     hb_test_fixture_func_t  data_test,
262                     hb_test_fixture_func_t  data_teardown)
263 {
264   char *normal_path = hb_test_normalize_path (test_path);
265   g_test_add_vtable (normal_path, data_size, test_data, data_setup, data_test, data_teardown);
266   g_free (normal_path);
267 }
268 #define hb_test_add_fixture(FixturePrefix, UserData, Func) \
269 G_STMT_START { \
270   typedef G_PASTE (FixturePrefix, _t) Fixture; \
271   void (*add_vtable) (const char*, gsize, gconstpointer, \
272                       void (*) (Fixture*, gconstpointer), \
273                       void (*) (Fixture*, gconstpointer), \
274                       void (*) (Fixture*, gconstpointer)) \
275         = (void (*) (const gchar *, gsize, gconstpointer, \
276                      void (*) (Fixture*, gconstpointer), \
277                      void (*) (Fixture*, gconstpointer), \
278                      void (*) (Fixture*, gconstpointer))) hb_test_add_vtable; \
279   add_vtable (#Func, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
280               G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
281 } G_STMT_END
282
283 static inline void
284 hb_test_add_vtable_flavor (const char             *test_path,
285                            const char             *flavor,
286                            gsize                   data_size,
287                            gconstpointer           test_data,
288                            hb_test_fixture_func_t  data_setup,
289                            hb_test_fixture_func_t  data_test,
290                            hb_test_fixture_func_t  data_teardown)
291 {
292   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
293   hb_test_add_vtable (path, data_size, test_data, data_setup, data_test, data_teardown);
294   g_free (path);
295 }
296 #define hb_test_add_fixture_flavor(FixturePrefix, UserData, Flavor, Func) \
297 G_STMT_START { \
298   typedef G_PASTE (FixturePrefix, _t) Fixture; \
299   void (*add_vtable) (const char*, const char *, gsize, gconstpointer, \
300                       void (*) (Fixture*, gconstpointer), \
301                       void (*) (Fixture*, gconstpointer), \
302                       void (*) (Fixture*, gconstpointer)) \
303         = (void (*) (const gchar *, const char *, gsize, gconstpointer, \
304                      void (*) (Fixture*, gconstpointer), \
305                      void (*) (Fixture*, gconstpointer), \
306                      void (*) (Fixture*, gconstpointer))) hb_test_add_vtable_flavor; \
307   add_vtable (#Func, Flavor, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
308               G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
309 } G_STMT_END
310
311
312 HB_END_DECLS
313
314 #endif /* HB_TEST_H */