2 * Copyright © 2011 Codethink Limited
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Codethink Author(s): Ryan Lortie
29 /* This file tests the unicode virtual functions interface */
32 test_unicode_nil (void)
34 hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
36 g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_UNKNOWN);
38 hb_unicode_funcs_destroy (uf);
42 test_unicode_glib (void)
44 hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();
46 g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
50 test_unicode_default (void)
52 hb_unicode_funcs_t *uf = hb_unicode_funcs_get_default ();
54 g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
57 static gboolean freed0, freed1;
58 static int unique_pointer0[1];
59 static int unique_pointer1[1];
60 static void free_up (void *up)
62 if (up == unique_pointer0) {
65 } else if (up == unique_pointer1) {
69 g_assert_not_reached ();
74 simple_get_script (hb_unicode_funcs_t *ufuncs,
75 hb_codepoint_t codepoint,
78 g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
79 g_assert (unique_pointer0 == user_data);
81 if ('a' <= codepoint && codepoint <= 'z')
82 return HB_SCRIPT_LATIN;
84 return HB_SCRIPT_UNKNOWN;
88 test_unicode_custom (void)
90 hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
92 hb_unicode_funcs_set_script_func (uf, simple_get_script,
93 unique_pointer0, free_up);
95 g_assert_cmpint (hb_unicode_get_script (uf, 'a'), ==, HB_SCRIPT_LATIN);
96 g_assert_cmpint (hb_unicode_get_script (uf, '0'), ==, HB_SCRIPT_UNKNOWN);
98 g_assert (!freed0 && !freed1);
99 hb_unicode_funcs_destroy (uf);
100 g_assert (freed0 && !freed1);
106 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
107 hb_codepoint_t codepoint,
110 g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
111 g_assert (user_data == unique_pointer1);
113 if (codepoint == 'a') {
114 return HB_SCRIPT_ARABIC;
116 hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
118 return hb_unicode_get_script (parent, codepoint);
123 test_unicode_subclassing_nil (void)
125 hb_unicode_funcs_t *uf, *aa;
127 uf = hb_unicode_funcs_create (NULL);
129 aa = hb_unicode_funcs_create (uf);
131 hb_unicode_funcs_destroy (uf);
133 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
134 unique_pointer1, free_up);
136 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
137 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);
139 g_assert (!freed0 && !freed1);
140 hb_unicode_funcs_destroy (aa);
141 g_assert (!freed0 && freed1);
146 test_unicode_subclassing_glib (void)
148 hb_unicode_funcs_t *uf, *aa;
150 uf = hb_glib_get_unicode_funcs ();
151 aa = hb_unicode_funcs_create (uf);
153 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
154 unique_pointer1, free_up);
156 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
157 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
159 g_assert (!freed0 && !freed1);
160 hb_unicode_funcs_destroy (aa);
161 g_assert (!freed0 && freed1);
166 test_unicode_subclassing_deep (void)
168 hb_unicode_funcs_t *uf, *aa;
170 uf = hb_unicode_funcs_create (NULL);
172 hb_unicode_funcs_set_script_func (uf, simple_get_script,
173 unique_pointer0, free_up);
175 aa = hb_unicode_funcs_create (uf);
177 hb_unicode_funcs_destroy (uf);
179 /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
182 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
183 unique_pointer1, free_up);
185 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
186 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
187 g_assert_cmpint (hb_unicode_get_script (aa, '0'), ==, HB_SCRIPT_UNKNOWN);
189 g_assert (!freed0 && !freed1);
190 hb_unicode_funcs_destroy (aa);
191 g_assert (freed0 && freed1);
192 freed0 = freed1 = FALSE;
196 main (int argc, char **argv)
198 hb_test_init (&argc, &argv);
200 hb_test_add (test_unicode_nil);
201 hb_test_add (test_unicode_glib);
202 hb_test_add (test_unicode_default);
203 hb_test_add (test_unicode_custom);
204 hb_test_add (test_unicode_subclassing_nil);
205 hb_test_add (test_unicode_subclassing_glib);
206 hb_test_add (test_unicode_subclassing_deep);
208 /* XXX test all methods for their defaults and various (glib, icu, default) implementations. */
209 /* XXX test glib & icu two-way script conversion */
211 return hb_test_run ();