[test/unicode] Test is/make_immutable()
[profile/ivi/org.tizen.video-player.git] / test / test-unicode.c
1 /*
2  * Copyright © 2011  Codethink Limited
3  * Copyright © 2011  Google, Inc.
4  *
5  *  This is part of HarfBuzz, a text shaping library.
6  *
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.
12  *
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
17  * DAMAGE.
18  *
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.
24  *
25  * Codethink Author(s): Ryan Lortie
26  * Google Author(s): Behdad Esfahbod
27  */
28
29 #include "hb-test.h"
30
31 /* Unit tests for hb-unicode.h */
32
33
34 static void
35 test_unicode_nil (void)
36 {
37   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
38
39   g_assert (!hb_unicode_funcs_is_immutable (uf));
40
41   g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_UNKNOWN);
42
43   hb_unicode_funcs_destroy (uf);
44 }
45
46 static void
47 test_unicode_glib (void)
48 {
49   hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();
50
51   g_assert (hb_unicode_funcs_is_immutable (uf));
52
53   g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
54 }
55
56 static void
57 test_unicode_default (void)
58 {
59   hb_unicode_funcs_t *uf = hb_unicode_funcs_get_default ();
60
61   g_assert (hb_unicode_funcs_is_immutable (uf));
62
63   g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
64 }
65
66
67 #define MAGIC0 0x12345678
68 #define MAGIC1 0x76543210
69
70 typedef struct {
71   int value;
72   gboolean freed;
73 } data_t;
74
75 typedef struct {
76   data_t data[2];
77 } data_fixture_t;
78 static void
79 data_fixture_init (data_fixture_t *f, gconstpointer user_data)
80 {
81   f->data[0].value = MAGIC0;
82   f->data[1].value = MAGIC1;
83 }
84 static void
85 data_fixture_finish (data_fixture_t *f, gconstpointer user_data)
86 {
87 }
88
89 static void free_up (void *p)
90 {
91   data_t *data = (data_t *) p;
92
93   g_assert (data->value == MAGIC0 || data->value == MAGIC1);
94   g_assert (data->freed == FALSE);
95   data->freed = TRUE;
96 }
97
98 static hb_script_t
99 simple_get_script (hb_unicode_funcs_t *ufuncs,
100                    hb_codepoint_t      codepoint,
101                    void               *user_data)
102 {
103   data_t *data = (data_t *) user_data;
104
105   g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
106   g_assert (data->value == MAGIC0);
107   g_assert (data->freed == FALSE);
108
109   if ('a' <= codepoint && codepoint <= 'z')
110     return HB_SCRIPT_LATIN;
111   else
112     return HB_SCRIPT_UNKNOWN;
113 }
114
115 static hb_script_t
116 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
117                             hb_codepoint_t      codepoint,
118                             void               *user_data)
119 {
120   data_t *data = (data_t *) user_data;
121
122   g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
123   g_assert (data->value == MAGIC1);
124   g_assert (data->freed == FALSE);
125
126   if (codepoint == 'a') {
127     return HB_SCRIPT_ARABIC;
128   } else {
129     hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
130
131     return hb_unicode_get_script (parent, codepoint);
132   }
133 }
134
135 static void
136 test_unicode_custom (data_fixture_t *f, gconstpointer user_data)
137 {
138   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
139
140   hb_unicode_funcs_set_script_func (uf, simple_get_script,
141                                     &f->data[0], free_up);
142
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);
145
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));
149
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);
153
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);
157 }
158
159 static void
160 test_unicode_subclassing_nil (data_fixture_t *f, gconstpointer user_data)
161 {
162   hb_unicode_funcs_t *uf, *aa;
163
164   uf = hb_unicode_funcs_create (NULL);
165
166   aa = hb_unicode_funcs_create (uf);
167
168   hb_unicode_funcs_destroy (uf);
169
170   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
171                                     &f->data[1], free_up);
172
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);
175
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);
179 }
180
181 static void
182 test_unicode_subclassing_default (data_fixture_t *f, gconstpointer user_data)
183 {
184   hb_unicode_funcs_t *uf, *aa;
185
186   uf = hb_unicode_funcs_get_default ();
187   aa = hb_unicode_funcs_create (uf);
188
189   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
190                                     &f->data[1], free_up);
191
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);
194
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);
198 }
199
200 static void
201 test_unicode_subclassing_deep (data_fixture_t *f, gconstpointer user_data)
202 {
203   hb_unicode_funcs_t *uf, *aa;
204
205   uf = hb_unicode_funcs_create (NULL);
206
207   hb_unicode_funcs_set_script_func (uf, simple_get_script,
208                                     &f->data[0], free_up);
209
210   aa = hb_unicode_funcs_create (uf);
211
212   hb_unicode_funcs_destroy (uf);
213
214   /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
215   g_assert (!f->data[0].freed);
216
217   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
218                                     &f->data[1], free_up);
219
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);
223
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);
227 }
228
229
230 int
231 main (int argc, char **argv)
232 {
233   hb_test_init (&argc, &argv);
234
235   hb_test_add (test_unicode_nil);
236   hb_test_add (test_unicode_glib);
237   hb_test_add (test_unicode_default);
238
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);
243
244   /* XXX test all methods for their defaults and various (glib, icu, default) implementations. */
245   /* XXX test glib & icu two-way script conversion */
246
247   return hb_test_run ();
248 }