[test/buffer] Add UTF-16 tests
[apps/home/video-player.git] / test / test-unicode.c
1 /*
2  * Copyright © 2011 Codethink Limited
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  * Codethink Author(s): Ryan Lortie
25  */
26
27 #include "hb-test.h"
28
29 /* This file tests the unicode virtual functions interface */
30
31 static void
32 test_unicode_nil (void)
33 {
34   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
35
36   g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_UNKNOWN);
37
38   hb_unicode_funcs_destroy (uf);
39 }
40
41 static void
42 test_unicode_glib (void)
43 {
44   hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();
45
46   g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
47 }
48
49 static void
50 test_unicode_default (void)
51 {
52   hb_unicode_funcs_t *uf = hb_unicode_funcs_get_default ();
53
54   g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
55 }
56
57 static gboolean freed0, freed1;
58 static int unique_pointer0[1];
59 static int unique_pointer1[1];
60 static void free_up (void *up)
61 {
62   if (up == unique_pointer0) {
63     g_assert (!freed0);
64     freed0 = TRUE;
65   } else if (up == unique_pointer1) {
66     g_assert (!freed1);
67     freed1 = TRUE;
68   } else {
69     g_assert_not_reached ();
70   }
71 }
72
73 static hb_script_t
74 simple_get_script (hb_unicode_funcs_t *ufuncs,
75                    hb_codepoint_t      codepoint,
76                    void               *user_data)
77 {
78   g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
79   g_assert (unique_pointer0 == user_data);
80
81   if ('a' <= codepoint && codepoint <= 'z')
82     return HB_SCRIPT_LATIN;
83   else
84     return HB_SCRIPT_UNKNOWN;
85 }
86
87 static void
88 test_unicode_custom (void)
89 {
90   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
91
92   hb_unicode_funcs_set_script_func (uf, simple_get_script,
93                                     unique_pointer0, free_up);
94
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);
97
98   g_assert (!freed0 && !freed1);
99   hb_unicode_funcs_destroy (uf);
100   g_assert (freed0 && !freed1);
101   freed0 = FALSE;
102 }
103
104
105 static hb_script_t
106 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
107                             hb_codepoint_t      codepoint,
108                             void               *user_data)
109 {
110   g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
111   g_assert (user_data == unique_pointer1);
112
113   if (codepoint == 'a') {
114     return HB_SCRIPT_ARABIC;
115   } else {
116     hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
117
118     return hb_unicode_get_script (parent, codepoint);
119   }
120 }
121
122 static void
123 test_unicode_subclassing_nil (void)
124 {
125   hb_unicode_funcs_t *uf, *aa;
126
127   uf = hb_unicode_funcs_create (NULL);
128
129   aa = hb_unicode_funcs_create (uf);
130
131   hb_unicode_funcs_destroy (uf);
132
133   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
134                                     unique_pointer1, free_up);
135
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);
138
139   g_assert (!freed0 && !freed1);
140   hb_unicode_funcs_destroy (aa);
141   g_assert (!freed0 && freed1);
142   freed1 = FALSE;
143 }
144
145 static void
146 test_unicode_subclassing_glib (void)
147 {
148   hb_unicode_funcs_t *uf, *aa;
149
150   uf = hb_glib_get_unicode_funcs ();
151   aa = hb_unicode_funcs_create (uf);
152
153   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
154                                     unique_pointer1, free_up);
155
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);
158
159   g_assert (!freed0 && !freed1);
160   hb_unicode_funcs_destroy (aa);
161   g_assert (!freed0 && freed1);
162   freed1 = FALSE;
163 }
164
165 static void
166 test_unicode_subclassing_deep (void)
167 {
168   hb_unicode_funcs_t *uf, *aa;
169
170   uf = hb_unicode_funcs_create (NULL);
171
172   hb_unicode_funcs_set_script_func (uf, simple_get_script,
173                                     unique_pointer0, free_up);
174
175   aa = hb_unicode_funcs_create (uf);
176
177   hb_unicode_funcs_destroy (uf);
178
179   /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
180   g_assert (!freed0);
181
182   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
183                                     unique_pointer1, free_up);
184
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);
188
189   g_assert (!freed0 && !freed1);
190   hb_unicode_funcs_destroy (aa);
191   g_assert (freed0 && freed1);
192   freed0 = freed1 = FALSE;
193 }
194
195 int
196 main (int argc, char **argv)
197 {
198   hb_test_init (&argc, &argv);
199
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);
207
208   /* XXX test all methods for their defaults and various (glib, icu, default) implementations. */
209   /* XXX test glib & icu two-way script conversion */
210
211   return hb_test_run ();
212 }