[API] Remove hb_*_get_reference_count()
[apps/home/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   hb_unicode_funcs_destroy (uf);
39 }
40
41 static void
42 test_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 gboolean freed0, freed1;
50 static int unique_pointer0[1];
51 static int unique_pointer1[1];
52 static void free_up (void *up)
53 {
54   if (up == unique_pointer0) {
55     g_assert (!freed0);
56     freed0 = TRUE;
57   } else if (up == unique_pointer1) {
58     g_assert (!freed1);
59     freed1 = TRUE;
60   } else {
61     g_assert_not_reached ();
62   }
63 }
64
65 static hb_script_t
66 simple_get_script (hb_unicode_funcs_t *ufuncs,
67                    hb_codepoint_t      codepoint,
68                    void               *user_data)
69 {
70   g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
71   g_assert (unique_pointer0 == user_data);
72
73   if ('a' <= codepoint && codepoint <= 'z')
74     return HB_SCRIPT_LATIN;
75   else
76     return HB_SCRIPT_UNKNOWN;
77 }
78
79 static void
80 test_custom (void)
81 {
82   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
83
84   hb_unicode_funcs_set_script_func (uf, simple_get_script,
85                                     unique_pointer0, free_up);
86
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);
89
90   g_assert (!freed0 && !freed1);
91   hb_unicode_funcs_destroy (uf);
92   g_assert (freed0 && !freed1);
93   freed0 = FALSE;
94 }
95
96
97 static hb_script_t
98 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
99                             hb_codepoint_t      codepoint,
100                             void               *user_data)
101 {
102   g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
103   g_assert (user_data == unique_pointer1);
104
105   if (codepoint == 'a') {
106     return HB_SCRIPT_ARABIC;
107   } else {
108     hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
109
110     return hb_unicode_get_script (parent, codepoint);
111   }
112 }
113
114 static void
115 test_subclassing_nil (void)
116 {
117   hb_unicode_funcs_t *uf, *aa;
118
119   uf = hb_unicode_funcs_create (NULL);
120
121   aa = hb_unicode_funcs_create (uf);
122
123   hb_unicode_funcs_destroy (uf);
124
125   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
126                                     unique_pointer1, free_up);
127
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);
130
131   g_assert (!freed0 && !freed1);
132   hb_unicode_funcs_destroy (aa);
133   g_assert (!freed0 && freed1);
134   freed1 = FALSE;
135 }
136
137 static void
138 test_subclassing_glib (void)
139 {
140   hb_unicode_funcs_t *uf, *aa;
141
142   uf = hb_glib_get_unicode_funcs ();
143   aa = hb_unicode_funcs_create (uf);
144
145   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
146                                     unique_pointer1, free_up);
147
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);
150
151   g_assert (!freed0 && !freed1);
152   hb_unicode_funcs_destroy (aa);
153   g_assert (!freed0 && freed1);
154   freed1 = FALSE;
155 }
156
157 static void
158 test_subclassing_deep (void)
159 {
160   hb_unicode_funcs_t *uf, *aa;
161
162   uf = hb_unicode_funcs_create (NULL);
163
164   hb_unicode_funcs_set_script_func (uf, simple_get_script,
165                                     unique_pointer0, free_up);
166
167   aa = hb_unicode_funcs_create (uf);
168
169   hb_unicode_funcs_destroy (uf);
170
171   /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
172   g_assert (!freed0);
173
174   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
175                                     unique_pointer1, free_up);
176
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);
180
181   g_assert (!freed0 && !freed1);
182   hb_unicode_funcs_destroy (aa);
183   g_assert (freed0 && freed1);
184   freed0 = freed1 = FALSE;
185 }
186
187 int
188 main (int argc, char **argv)
189 {
190   g_test_init (&argc, &argv, NULL);
191
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);
198
199   /* XXX test all methods for their defaults and various (glib, icu) implementations. */
200   /* XXX test glib & icu two-way script conversion */
201
202   return g_test_run ();
203 }