2 * Copyright (C) 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 */
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 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
39 hb_unicode_funcs_destroy (uf);
45 hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();
47 g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
49 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 0);
50 hb_unicode_funcs_destroy (uf);
51 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 0);
54 static gboolean freed0, freed1;
55 static int unique_pointer0[1];
56 static int unique_pointer1[1];
57 static void free_up (void *up)
59 if (up == unique_pointer0) {
62 } else if (up == unique_pointer1) {
66 g_assert_not_reached ();
71 simple_get_script (hb_unicode_funcs_t *ufuncs,
72 hb_codepoint_t codepoint,
75 g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
76 g_assert (unique_pointer0 == user_data);
78 if ('a' <= codepoint && codepoint <= 'z')
79 return HB_SCRIPT_LATIN;
81 return HB_SCRIPT_UNKNOWN;
87 hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
89 hb_unicode_funcs_set_script_func (uf, simple_get_script,
90 unique_pointer0, free_up);
92 g_assert_cmpint (hb_unicode_get_script (uf, 'a'), ==, HB_SCRIPT_LATIN);
93 g_assert_cmpint (hb_unicode_get_script (uf, '0'), ==, HB_SCRIPT_UNKNOWN);
95 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
96 g_assert (!freed0 && !freed1);
97 hb_unicode_funcs_destroy (uf);
98 g_assert (freed0 && !freed1);
104 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
105 hb_codepoint_t codepoint,
108 g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
109 g_assert (user_data == unique_pointer1);
111 if (codepoint == 'a') {
112 return HB_SCRIPT_ARABIC;
114 hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
116 return hb_unicode_get_script (parent, codepoint);
121 test_subclassing_nil (void)
123 hb_unicode_funcs_t *uf, *aa;
125 uf = hb_unicode_funcs_create (NULL);
126 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
128 aa = hb_unicode_funcs_create (uf);
129 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 2);
131 hb_unicode_funcs_destroy (uf);
132 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
134 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
135 unique_pointer1, free_up);
137 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
138 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);
140 g_assert_cmpint (hb_unicode_funcs_get_reference_count (aa), ==, 1);
141 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
142 g_assert (!freed0 && !freed1);
143 hb_unicode_funcs_destroy (aa);
144 g_assert (!freed0 && freed1);
149 test_subclassing_glib (void)
151 hb_unicode_funcs_t *uf, *aa;
153 uf = hb_glib_get_unicode_funcs ();
154 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 0);
156 aa = hb_unicode_funcs_create (uf);
157 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 0);
159 hb_unicode_funcs_destroy (uf);
160 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 0);
162 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
163 unique_pointer1, free_up);
165 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
166 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
168 g_assert_cmpint (hb_unicode_funcs_get_reference_count (aa), ==, 1);
169 g_assert (!freed0 && !freed1);
170 hb_unicode_funcs_destroy (aa);
171 g_assert (!freed0 && freed1);
176 test_subclassing_deep (void)
178 hb_unicode_funcs_t *uf, *aa;
180 uf = hb_unicode_funcs_create (NULL);
181 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
183 hb_unicode_funcs_set_script_func (uf, simple_get_script,
184 unique_pointer0, free_up);
186 aa = hb_unicode_funcs_create (uf);
187 g_assert_cmpint (hb_unicode_funcs_get_reference_count (aa), ==, 1);
188 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 2);
190 hb_unicode_funcs_destroy (uf);
191 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
193 /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
196 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
197 unique_pointer1, free_up);
199 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
200 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
201 g_assert_cmpint (hb_unicode_get_script (aa, '0'), ==, HB_SCRIPT_UNKNOWN);
203 g_assert_cmpint (hb_unicode_funcs_get_reference_count (aa), ==, 1);
204 g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
205 g_assert (!freed0 && !freed1);
206 hb_unicode_funcs_destroy (aa);
207 g_assert (freed0 && freed1);
208 freed0 = freed1 = FALSE;
212 main (int argc, char **argv)
214 g_test_init (&argc, &argv, NULL);
216 g_test_add_func ("/unicode/nil", test_nil);
217 g_test_add_func ("/unicode/glib", test_glib);
218 g_test_add_func ("/unicode/custom", test_custom);
219 g_test_add_func ("/unicode/subclassing/nil", test_subclassing_nil);
220 g_test_add_func ("/unicode/subclassing/glib", test_subclassing_glib);
221 g_test_add_func ("/unicode/subclassing/deep", test_subclassing_deep);
223 /* XXX test all methods for their defaults and various (glib, icu) implementations. */
224 /* XXX test glib & icu two-way script conversion */
226 return g_test_run ();