[test/buffer] Add initial utf-8 tests
[framework/uifw/harfbuzz.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 <hb-glib.h>
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 HB_BEGIN_DECLS
36
37 /* Just in case */
38 #undef G_DISABLE_ASSERT
39
40
41 /* Misc */
42
43 /* This is too ugly to be public API, but quite handy. */
44 #define HB_TAG_CHAR4(s)   (HB_TAG(((const char *) s)[0], \
45                                   ((const char *) s)[1], \
46                                   ((const char *) s)[2], \
47                                   ((const char *) s)[3]))
48
49
50 /* Helpers */
51
52 static inline void
53 hb_test_init (int *argc, char ***argv)
54 {
55   g_test_init (argc, argv, NULL);
56 }
57
58 static inline int
59 hb_test_run (void)
60 {
61   return g_test_run ();
62 }
63
64
65 /* Bugzilla helpers */
66
67 static inline void
68 hb_test_bug (const char *uri_base, unsigned int number)
69 {
70   char *s = g_strdup_printf ("%u", number);
71
72   g_test_bug_base (uri_base);
73   g_test_bug (s);
74
75   g_free (s);
76 }
77
78 static inline void
79 hb_test_bug_freedesktop (unsigned int number)
80 {
81   hb_test_bug ("http://bugs.freedesktop.org/", number);
82 }
83
84 static inline void
85 hb_test_bug_gnome (unsigned int number)
86 {
87   hb_test_bug ("http://bugzilla.gnome.org/", number);
88 }
89
90 static inline void
91 hb_test_bug_mozilla (unsigned int number)
92 {
93   hb_test_bug ("http://bugzilla.mozilla.org/", number);
94 }
95
96 static inline void
97 hb_test_bug_redhat (unsigned int number)
98 {
99   hb_test_bug ("http://bugzilla.redhat.com/", number);
100 }
101
102
103 /* Wrap glib test functions to simplify.  Should have been in glib already. */
104
105 /* Drops the "test_" prefix and converts '_' to '/'.
106  * Essentially builds test path from function name. */
107 static inline char *
108 hb_test_normalize_path (const char *path)
109 {
110   char *s, *p;
111
112   g_assert (0 == strncmp (path, "test_", 5));
113   path += 4;
114
115   s = g_strdup (path);
116   for (p = s; *p; p++)
117     if (*p == '_')
118       *p = '/';
119
120   return s;
121 }
122
123
124 static inline void
125 hb_test_add_func (const char *test_path,
126                   GTestFunc   test_func)
127 {
128   char *normal_path = hb_test_normalize_path (test_path);
129   g_test_add_func (normal_path, test_func);
130   g_free (normal_path);
131 }
132 #define hb_test_add(Func) hb_test_add_func (#Func, Func)
133
134 static inline void
135 hb_test_add_func_flavor (const char *test_path,
136                          const char *flavor,
137                          GTestFunc   test_func)
138 {
139   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
140   hb_test_add_func (path, test_func);
141   g_free (path);
142 }
143 #define hb_test_add_flavor(Flavor, Func) hb_test_add_func (#Func, Flavor, Func)
144
145 static inline void
146 hb_test_add_data_func (const char    *test_path,
147                        gconstpointer  test_data,
148                        GTestDataFunc  test_func)
149 {
150   char *normal_path = hb_test_normalize_path (test_path);
151   g_test_add_data_func (normal_path, test_data, test_func);
152   g_free (normal_path);
153 }
154 #define hb_test_add_data(Func, UserData) hb_test_add_data_func (#Func, UserData, Func)
155
156 static inline void
157 hb_test_add_data_func_flavor (const char    *test_path,
158                               const char    *flavor,
159                               gconstpointer  test_data,
160                               GTestDataFunc  test_func)
161 {
162   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
163   hb_test_add_data_func (path, test_data, test_func);
164   g_free (path);
165 }
166 #define hb_test_add_data_flavor(UserData, Flavor, Func) hb_test_add_data_func_flavor (#Func, Flavor, UserData, Func)
167
168
169 static inline void
170 hb_test_add_vtable (const char       *test_path,
171                     gsize             data_size,
172                     gconstpointer     test_data,
173                     GTestFixtureFunc  data_setup,
174                     GTestFixtureFunc  data_test,
175                     GTestFixtureFunc  data_teardown)
176 {
177   char *normal_path = hb_test_normalize_path (test_path);
178   g_test_add_vtable (normal_path, data_size, test_data, data_setup, data_test, data_teardown);
179   g_free (normal_path);
180 }
181 #define hb_test_add_fixture(FixturePrefix, UserData, Func) \
182 G_STMT_START { \
183   typedef G_PASTE (FixturePrefix, _t) Fixture; \
184   void (*add_vtable) (const char*, gsize, gconstpointer, \
185                       void (*) (Fixture*, gconstpointer), \
186                       void (*) (Fixture*, gconstpointer), \
187                       void (*) (Fixture*, gconstpointer)) \
188         = (void (*) (const gchar *, gsize, gconstpointer, \
189                      void (*) (Fixture*, gconstpointer), \
190                      void (*) (Fixture*, gconstpointer), \
191                      void (*) (Fixture*, gconstpointer))) hb_test_add_vtable; \
192   add_vtable (#Func, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
193               G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
194 } G_STMT_END
195
196 static inline void
197 hb_test_add_vtable_flavor (const char       *test_path,
198                            const char       *flavor,
199                            gsize             data_size,
200                            gconstpointer     test_data,
201                            GTestFixtureFunc  data_setup,
202                            GTestFixtureFunc  data_test,
203                            GTestFixtureFunc  data_teardown)
204 {
205   char *path = g_strdup_printf ("%s/%s", test_path, flavor);
206   hb_test_add_vtable (path, data_size, test_data, data_setup, data_test, data_teardown);
207   g_free (path);
208 }
209 #define hb_test_add_fixture_flavor(FixturePrefix, UserData, Flavor, Func) \
210 G_STMT_START { \
211   typedef G_PASTE (FixturePrefix, _t) Fixture; \
212   void (*add_vtable) (const char*, const char *, gsize, gconstpointer, \
213                       void (*) (Fixture*, gconstpointer), \
214                       void (*) (Fixture*, gconstpointer), \
215                       void (*) (Fixture*, gconstpointer)) \
216         = (void (*) (const gchar *, const char *, gsize, gconstpointer, \
217                      void (*) (Fixture*, gconstpointer), \
218                      void (*) (Fixture*, gconstpointer), \
219                      void (*) (Fixture*, gconstpointer))) hb_test_add_vtable_flavor; \
220   add_vtable (#Func, Flavor, sizeof (G_PASTE (FixturePrefix, _t)), UserData, \
221               G_PASTE (FixturePrefix, _init), Func, G_PASTE (FixturePrefix, _finish)); \
222 } G_STMT_END
223
224
225 HB_END_DECLS
226
227 #endif /* HB_TEST_H */