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