unicode: Cleanup implementation
[profile/ivi/org.tizen.video-player.git] / test / test-unicode.c
1 /*
2  * Copyright (C) 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_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   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
39   hb_unicode_funcs_destroy (uf);
40 }
41
42 static void
43 test_glib (void)
44 {
45   hb_unicode_funcs_t *uf = hb_glib_get_unicode_funcs ();
46
47   g_assert_cmpint (hb_unicode_get_script (uf, 'd'), ==, HB_SCRIPT_LATIN);
48
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);
52 }
53
54 static gboolean freed0, freed1;
55 static int unique_pointer0[1];
56 static int unique_pointer1[1];
57 static void free_up (void *up)
58 {
59   if (up == unique_pointer0) {
60     g_assert (!freed0);
61     freed0 = TRUE;
62   } else if (up == unique_pointer1) {
63     g_assert (!freed1);
64     freed1 = TRUE;
65   } else {
66     g_assert_not_reached ();
67   }
68 }
69
70 static hb_script_t
71 simple_get_script (hb_unicode_funcs_t *ufuncs,
72                    hb_codepoint_t      codepoint,
73                    void               *user_data)
74 {
75   g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
76   g_assert (unique_pointer0 == user_data);
77
78   if ('a' <= codepoint && codepoint <= 'z')
79     return HB_SCRIPT_LATIN;
80   else
81     return HB_SCRIPT_UNKNOWN;
82 }
83
84 static void
85 test_custom (void)
86 {
87   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
88
89   hb_unicode_funcs_set_script_func (uf, simple_get_script,
90                                     unique_pointer0, free_up);
91
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);
94
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);
99   freed0 = FALSE;
100 }
101
102
103 static hb_script_t
104 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
105                             hb_codepoint_t      codepoint,
106                             void               *user_data)
107 {
108   g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
109   g_assert (user_data == unique_pointer1);
110
111   if (codepoint == 'a') {
112     return HB_SCRIPT_ARABIC;
113   } else {
114     hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
115
116     return hb_unicode_get_script (parent, codepoint);
117   }
118 }
119
120 static void
121 test_subclassing_nil (void)
122 {
123   hb_unicode_funcs_t *uf, *aa;
124
125   uf = hb_unicode_funcs_create (NULL);
126   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
127
128   aa = hb_unicode_funcs_create (uf);
129   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 2);
130
131   hb_unicode_funcs_destroy (uf);
132   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
133
134   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
135                                     unique_pointer1, free_up);
136
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);
139
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);
145   freed1 = FALSE;
146 }
147
148 static void
149 test_subclassing_glib (void)
150 {
151   hb_unicode_funcs_t *uf, *aa;
152
153   uf = hb_glib_get_unicode_funcs ();
154   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 0);
155
156   aa = hb_unicode_funcs_create (uf);
157   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 0);
158
159   hb_unicode_funcs_destroy (uf);
160   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 0);
161
162   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
163                                     unique_pointer1, free_up);
164
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);
167
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);
172   freed1 = FALSE;
173 }
174
175 static void
176 test_subclassing_deep (void)
177 {
178   hb_unicode_funcs_t *uf, *aa;
179
180   uf = hb_unicode_funcs_create (NULL);
181   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
182
183   hb_unicode_funcs_set_script_func (uf, simple_get_script,
184                                     unique_pointer0, free_up);
185
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);
189
190   hb_unicode_funcs_destroy (uf);
191   g_assert_cmpint (hb_unicode_funcs_get_reference_count (uf), ==, 1);
192
193   /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
194   g_assert (!freed0);
195
196   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
197                                     unique_pointer1, free_up);
198
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);
202
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;
209 }
210
211 int
212 main (int argc, char **argv)
213 {
214   g_test_init (&argc, &argv, NULL);
215
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);
222
223   return g_test_run ();
224 }