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 hb_unicode_funcs_destroy (uf);
44 hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();
46 g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
49 static gboolean freed0, freed1;
50 static int unique_pointer0[1];
51 static int unique_pointer1[1];
52 static void free_up (void *up)
54 if (up == unique_pointer0) {
57 } else if (up == unique_pointer1) {
61 g_assert_not_reached ();
66 simple_get_script (hb_unicode_funcs_t *ufuncs,
67 hb_codepoint_t codepoint,
70 g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
71 g_assert (unique_pointer0 == user_data);
73 if ('a' <= codepoint && codepoint <= 'z')
74 return HB_SCRIPT_LATIN;
76 return HB_SCRIPT_UNKNOWN;
82 hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
84 hb_unicode_funcs_set_script_func (uf, simple_get_script,
85 unique_pointer0, free_up);
87 g_assert_cmpint (hb_unicode_get_script (uf, 'a'), ==, HB_SCRIPT_LATIN);
88 g_assert_cmpint (hb_unicode_get_script (uf, '0'), ==, HB_SCRIPT_UNKNOWN);
90 g_assert (!freed0 && !freed1);
91 hb_unicode_funcs_destroy (uf);
92 g_assert (freed0 && !freed1);
98 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
99 hb_codepoint_t codepoint,
102 g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
103 g_assert (user_data == unique_pointer1);
105 if (codepoint == 'a') {
106 return HB_SCRIPT_ARABIC;
108 hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
110 return hb_unicode_get_script (parent, codepoint);
115 test_subclassing_nil (void)
117 hb_unicode_funcs_t *uf, *aa;
119 uf = hb_unicode_funcs_create (NULL);
121 aa = hb_unicode_funcs_create (uf);
123 hb_unicode_funcs_destroy (uf);
125 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
126 unique_pointer1, free_up);
128 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
129 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);
131 g_assert (!freed0 && !freed1);
132 hb_unicode_funcs_destroy (aa);
133 g_assert (!freed0 && freed1);
138 test_subclassing_glib (void)
140 hb_unicode_funcs_t *uf, *aa;
142 uf = hb_glib_get_unicode_funcs ();
143 aa = hb_unicode_funcs_create (uf);
145 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
146 unique_pointer1, free_up);
148 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
149 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
151 g_assert (!freed0 && !freed1);
152 hb_unicode_funcs_destroy (aa);
153 g_assert (!freed0 && freed1);
158 test_subclassing_deep (void)
160 hb_unicode_funcs_t *uf, *aa;
162 uf = hb_unicode_funcs_create (NULL);
164 hb_unicode_funcs_set_script_func (uf, simple_get_script,
165 unique_pointer0, free_up);
167 aa = hb_unicode_funcs_create (uf);
169 hb_unicode_funcs_destroy (uf);
171 /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
174 hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
175 unique_pointer1, free_up);
177 g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
178 g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
179 g_assert_cmpint (hb_unicode_get_script (aa, '0'), ==, HB_SCRIPT_UNKNOWN);
181 g_assert (!freed0 && !freed1);
182 hb_unicode_funcs_destroy (aa);
183 g_assert (freed0 && freed1);
184 freed0 = freed1 = FALSE;
188 main (int argc, char **argv)
190 g_test_init (&argc, &argv, NULL);
192 g_test_add_func ("/unicode/nil", test_nil);
193 g_test_add_func ("/unicode/glib", test_glib);
194 g_test_add_func ("/unicode/custom", test_custom);
195 g_test_add_func ("/unicode/subclassing/nil", test_subclassing_nil);
196 g_test_add_func ("/unicode/subclassing/glib", test_subclassing_glib);
197 g_test_add_func ("/unicode/subclassing/deep", test_subclassing_deep);
199 /* XXX test all methods for their defaults and various (glib, icu) implementations. */
200 /* XXX test glib & icu two-way script conversion */
202 return g_test_run ();