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