2 * Copyright © 2011 Codethink Limited
3 * Copyright © 2011 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Codethink Author(s): Ryan Lortie
26 * Google Author(s): Behdad Esfahbod
31 /* Unit tests for hb-unicode.h */
35 test_unicode_nil (void)
37 hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
39 g_assert (!hb_unicode_funcs_is_immutable (uf));
41 g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_UNKNOWN);
43 hb_unicode_funcs_destroy (uf);
47 test_unicode_glib (void)
49 hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();
51 g_assert (hb_unicode_funcs_is_immutable (uf));
53 g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
57 test_unicode_default (void)
59 hb_unicode_funcs_t *uf = hb_unicode_funcs_get_default ();
61 g_assert (hb_unicode_funcs_is_immutable (uf));
63 g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
67 #define MAGIC0 0x12345678
68 #define MAGIC1 0x76543210
79 data_fixture_init (data_fixture_t *f, gconstpointer user_data)
81 f->data[0].value = MAGIC0;
82 f->data[1].value = MAGIC1;
85 data_fixture_finish (data_fixture_t *f, gconstpointer user_data)
89 static void free_up (void *p)
91 data_t *data = (data_t *) p;
93 g_assert (data->value == MAGIC0 || data->value == MAGIC1);
94 g_assert (data->freed == FALSE);
99 simple_get_script (hb_unicode_funcs_t *ufuncs,
100 hb_codepoint_t codepoint,
103 data_t *data = (data_t *) user_data;
105 g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
106 g_assert (data->value == MAGIC0);
107 g_assert (data->freed == FALSE);
109 if ('a' <= codepoint && codepoint <= 'z')
110 return HB_SCRIPT_LATIN;
112 return HB_SCRIPT_UNKNOWN;
116 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
117 hb_codepoint_t codepoint,
120 data_t *data = (data_t *) user_data;
122 g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
123 g_assert (data->value == MAGIC1);
124 g_assert (data->freed == FALSE);
126 if (codepoint == 'a') {
127 return HB_SCRIPT_ARABIC;
129 hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
131 return hb_unicode_get_script (parent, codepoint);
136 test_unicode_custom (data_fixture_t *f, gconstpointer user_data)
138 hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
140 hb_unicode_funcs_set_script_func (uf, simple_get_script,
141 &f->data[0], free_up);
143 g_assert_cmpint (hb_unicode_get_script (uf, 'a'), ==, HB_SCRIPT_LATIN);
144 g_assert_cmpint (hb_unicode_get_script (uf, '0'), ==, HB_SCRIPT_UNKNOWN);
146 g_assert (!hb_unicode_funcs_is_immutable (uf));
147 hb_unicode_funcs_make_immutable (uf);
148 g_assert (hb_unicode_funcs_is_immutable (uf));
150 /* Since uf is immutable now, the following setter should do nothing. */
151 hb_unicode_funcs_set_script_func (uf, a_is_for_arabic_get_script,
152 &f->data[1], free_up);
154 g_assert (!f->data[0].freed && !f->data[1].freed);
155 hb_unicode_funcs_destroy (uf);
156 g_assert (f->data[0].freed && !f->data[1].freed);
160 test_unicode_subclassing_nil (data_fixture_t *f, gconstpointer user_data)
162 hb_unicode_funcs_t *uf, *aa;
164 uf = hb_unicode_funcs_create (NULL);
166 aa = hb_unicode_funcs_create (uf);
168 hb_unicode_funcs_destroy (uf);
170 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
171 &f->data[1], free_up);
173 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
174 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);
176 g_assert (!f->data[0].freed && !f->data[1].freed);
177 hb_unicode_funcs_destroy (aa);
178 g_assert (!f->data[0].freed && f->data[1].freed);
182 test_unicode_subclassing_default (data_fixture_t *f, gconstpointer user_data)
184 hb_unicode_funcs_t *uf, *aa;
186 uf = hb_unicode_funcs_get_default ();
187 aa = hb_unicode_funcs_create (uf);
189 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
190 &f->data[1], free_up);
192 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
193 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
195 g_assert (!f->data[0].freed && !f->data[1].freed);
196 hb_unicode_funcs_destroy (aa);
197 g_assert (!f->data[0].freed && f->data[1].freed);
201 test_unicode_subclassing_deep (data_fixture_t *f, gconstpointer user_data)
203 hb_unicode_funcs_t *uf, *aa;
205 uf = hb_unicode_funcs_create (NULL);
207 hb_unicode_funcs_set_script_func (uf, simple_get_script,
208 &f->data[0], free_up);
210 aa = hb_unicode_funcs_create (uf);
212 hb_unicode_funcs_destroy (uf);
214 /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
215 g_assert (!f->data[0].freed);
217 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
218 &f->data[1], free_up);
220 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
221 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
222 g_assert_cmpint (hb_unicode_get_script (aa, '0'), ==, HB_SCRIPT_UNKNOWN);
224 g_assert (!f->data[0].freed && !f->data[1].freed);
225 hb_unicode_funcs_destroy (aa);
226 g_assert (f->data[0].freed && f->data[1].freed);
231 main (int argc, char **argv)
233 hb_test_init (&argc, &argv);
235 hb_test_add (test_unicode_nil);
236 hb_test_add (test_unicode_glib);
237 hb_test_add (test_unicode_default);
239 hb_test_add_fixture (data_fixture, NULL, test_unicode_custom);
240 hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_nil);
241 hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_default);
242 hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_deep);
244 /* XXX test all methods for their defaults and various (glib, icu, default) implementations. */
245 /* XXX test glib & icu two-way script conversion */
247 return hb_test_run ();