• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdio.h>
37 
38 HB_BEGIN_DECLS
39 
40 /* Just in case */
41 #undef G_DISABLE_ASSERT
42 
43 
44 /* Misc */
45 
46 /* This is too ugly to be public API, but quite handy. */
47 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
48 				  ((const char *) s)[1], \
49 				  ((const char *) s)[2], \
50 				  ((const char *) s)[3]))
51 
52 
53 static inline const char *
srcdir(void)54 srcdir (void)
55 {
56   static const char *s;
57 
58   if (!s) {
59     s = getenv ("srcdir");
60 
61 #ifdef SRCDIR
62     if (!s || !s[0])
63       s = SRCDIR;
64 #endif
65 
66     if (!s || !s[0])
67       s = ".";
68   }
69 
70   return s;
71 }
72 
73 
74 /* Helpers */
75 
76 static inline void
hb_test_init(int * argc,char *** argv)77 hb_test_init (int *argc, char ***argv)
78 {
79 #if !GLIB_CHECK_VERSION(2,32,0)
80   g_thread_init (NULL);
81 #endif
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 
92 /* Bugzilla helpers */
93 
94 static inline void
hb_test_bug(const char * uri_base,unsigned int number)95 hb_test_bug (const char *uri_base, unsigned int number)
96 {
97   char *s = g_strdup_printf ("%u", number);
98 
99   g_test_bug_base (uri_base);
100   g_test_bug (s);
101 
102   g_free (s);
103 }
104 
105 static inline void
hb_test_bug_freedesktop(unsigned int number)106 hb_test_bug_freedesktop (unsigned int number)
107 {
108   hb_test_bug ("http://bugs.freedesktop.org/", number);
109 }
110 
111 static inline void
hb_test_bug_gnome(unsigned int number)112 hb_test_bug_gnome (unsigned int number)
113 {
114   hb_test_bug ("http://bugzilla.gnome.org/", number);
115 }
116 
117 static inline void
hb_test_bug_mozilla(unsigned int number)118 hb_test_bug_mozilla (unsigned int number)
119 {
120   hb_test_bug ("http://bugzilla.mozilla.org/", number);
121 }
122 
123 static inline void
hb_test_bug_redhat(unsigned int number)124 hb_test_bug_redhat (unsigned int number)
125 {
126   hb_test_bug ("http://bugzilla.redhat.com/", number);
127 }
128 
129 
130 /* Wrap glib test functions to simplify.  Should have been in glib already. */
131 
132 /* Drops the "test_" prefix and converts '_' to '/'.
133  * Essentially builds test path from function name. */
134 static inline char *
hb_test_normalize_path(const char * path)135 hb_test_normalize_path (const char *path)
136 {
137   char *s, *p;
138 
139   g_assert (0 == strncmp (path, "test_", 5));
140   path += 4;
141 
142   s = g_strdup (path);
143   for (p = s; *p; p++)
144     if (*p == '_')
145       *p = '/';
146 
147   return s;
148 }
149 
150 
151 #if GLIB_CHECK_VERSION(2,25,12)
152 typedef GTestFunc        hb_test_func_t;
153 typedef GTestDataFunc    hb_test_data_func_t;
154 typedef GTestFixtureFunc hb_test_fixture_func_t;
155 #else
156 typedef void (*hb_test_func_t)         (void);
157 typedef void (*hb_test_data_func_t)    (gconstpointer user_data);
158 typedef void (*hb_test_fixture_func_t) (void);
159 #endif
160 
161 #if !GLIB_CHECK_VERSION(2,30,0)
162 #define g_test_fail() g_error("Test failed")
163 #endif
164 
165 static inline void
hb_test_add_func(const char * test_path,hb_test_func_t test_func)166 hb_test_add_func (const char *test_path,
167 		  hb_test_func_t   test_func)
168 {
169   char *normal_path = hb_test_normalize_path (test_path);
170   g_test_add_func (normal_path, test_func);
171   g_free (normal_path);
172 }
173 #define hb_test_add(Func) hb_test_add_func (#Func, Func)
174 
175 static inline void
hb_test_add_func_flavor(const char * test_path,const char * flavor,hb_test_func_t test_func)176 hb_test_add_func_flavor (const char *test_path,
177 			 const char *flavor,
178 			 hb_test_func_t   test_func)
179 {
180   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
181   hb_test_add_func (path, test_func);
182   g_free (path);
183 }
184 #define hb_test_add_flavor(Flavor, Func) hb_test_add_func (#Func, Flavor, Func)
185 
186 static inline void
hb_test_add_data_func(const char * test_path,gconstpointer test_data,hb_test_data_func_t test_func)187 hb_test_add_data_func (const char          *test_path,
188 		       gconstpointer        test_data,
189 		       hb_test_data_func_t  test_func)
190 {
191   char *normal_path = hb_test_normalize_path (test_path);
192   g_test_add_data_func (normal_path, test_data, test_func);
193   g_free (normal_path);
194 }
195 #define hb_test_add_data(UserData, Func) hb_test_add_data_func (#Func, UserData, Func)
196 
197 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)198 hb_test_add_data_func_flavor (const char          *test_path,
199 			      const char          *flavor,
200 			      gconstpointer        test_data,
201 			      hb_test_data_func_t  test_func)
202 {
203   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
204   hb_test_add_data_func (path, test_data, test_func);
205   g_free (path);
206 }
207 #define hb_test_add_data_flavor(UserData, Flavor, Func) hb_test_add_data_func_flavor (#Func, Flavor, UserData, Func)
208 
209 
210 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)211 hb_test_add_vtable (const char             *test_path,
212 		    gsize                   data_size,
213 		    gconstpointer           test_data,
214 		    hb_test_fixture_func_t  data_setup,
215 		    hb_test_fixture_func_t  data_test,
216 		    hb_test_fixture_func_t  data_teardown)
217 {
218   char *normal_path = hb_test_normalize_path (test_path);
219   g_test_add_vtable (normal_path, data_size, test_data, data_setup, data_test, data_teardown);
220   g_free (normal_path);
221 }
222 #define hb_test_add_fixture(FixturePrefix, UserData, Func) \
223 G_STMT_START { \
224   typedef G_PASTE (FixturePrefix, _t) Fixture; \
225   void (*add_vtable) (const char*, gsize, gconstpointer, \
226 		      void (*) (Fixture*, gconstpointer), \
227 		      void (*) (Fixture*, gconstpointer), \
228 		      void (*) (Fixture*, gconstpointer)) \
229 	= (void (*) (const gchar *, gsize, gconstpointer, \
230 		     void (*) (Fixture*, gconstpointer), \
231 		     void (*) (Fixture*, gconstpointer), \
232 		     void (*) (Fixture*, gconstpointer))) hb_test_add_vtable; \
233   add_vtable (#Func, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
234 	      G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
235 } G_STMT_END
236 
237 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)238 hb_test_add_vtable_flavor (const char             *test_path,
239 			   const char             *flavor,
240 			   gsize                   data_size,
241 			   gconstpointer           test_data,
242 			   hb_test_fixture_func_t  data_setup,
243 			   hb_test_fixture_func_t  data_test,
244 			   hb_test_fixture_func_t  data_teardown)
245 {
246   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
247   hb_test_add_vtable (path, data_size, test_data, data_setup, data_test, data_teardown);
248   g_free (path);
249 }
250 #define hb_test_add_fixture_flavor(FixturePrefix, UserData, Flavor, Func) \
251 G_STMT_START { \
252   typedef G_PASTE (FixturePrefix, _t) Fixture; \
253   void (*add_vtable) (const char*, const char *, gsize, gconstpointer, \
254 		      void (*) (Fixture*, gconstpointer), \
255 		      void (*) (Fixture*, gconstpointer), \
256 		      void (*) (Fixture*, gconstpointer)) \
257 	= (void (*) (const gchar *, const char *, gsize, gconstpointer, \
258 		     void (*) (Fixture*, gconstpointer), \
259 		     void (*) (Fixture*, gconstpointer), \
260 		     void (*) (Fixture*, gconstpointer))) hb_test_add_vtable_flavor; \
261   add_vtable (#Func, Flavor, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
262 	      G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
263 } G_STMT_END
264 
265 
266 HB_END_DECLS
267 
268 #endif /* HB_TEST_H */
269