[test/unicode] Add testing of all unicode properties
[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 #ifdef HAVE_GLIB
35 #include <hb-glib.h>
36 #endif
37 #ifdef HAVE_ICU
38 #include <hb-icu.h>
39 #endif
40
41
42 /* Check all properties */
43
44 /* Some of the following tables where adapted from glib/glib/tests/utf8-misc.c.
45  * The license is compatible. */
46
47 typedef struct {
48   hb_codepoint_t unicode;
49   unsigned int   value;
50 } test_pair_t;
51
52 static const test_pair_t combining_class_tests[] =
53 {
54   {   0x111111, (unsigned int) 0 },
55
56   {   0x0020, (unsigned int) 0 },
57   {   0x0334, (unsigned int) 1 },
58   {   0x093C, (unsigned int) 7 },
59   {   0x3099, (unsigned int) 8 },
60   {   0x094D, (unsigned int) 9 },
61   {   0x05B0, (unsigned int) 10 },
62   {   0x05B1, (unsigned int) 11 },
63   {   0x05B2, (unsigned int) 12 },
64   {   0x05B3, (unsigned int) 13 },
65   {   0x05B4, (unsigned int) 14 },
66   {   0x05B5, (unsigned int) 15 },
67   {   0x05B6, (unsigned int) 16 },
68   {   0x05B7, (unsigned int) 17 },
69   {   0x05B8, (unsigned int) 18 },
70   {   0x05B9, (unsigned int) 19 },
71   {   0x05BB, (unsigned int) 20 },
72   {   0x05BC, (unsigned int) 21 },
73   {   0x05BD, (unsigned int) 22 },
74   {   0x05BF, (unsigned int) 23 },
75   {   0x05C1, (unsigned int) 24 },
76   {   0x05C2, (unsigned int) 25 },
77   {   0xFB1E, (unsigned int) 26 },
78   {   0x064B, (unsigned int) 27 },
79   {   0x064C, (unsigned int) 28 },
80   {   0x064D, (unsigned int) 29 },
81   /* ... */
82   {   0x05AE, (unsigned int) 228 },
83   {   0x0300, (unsigned int) 230 },
84   {   0x302C, (unsigned int) 232 },
85   {   0x0362, (unsigned int) 233 },
86   {   0x0360, (unsigned int) 234 },
87   {   0x1DCD, (unsigned int) 234 },
88   {   0x0345, (unsigned int) 240 },
89 };
90 static const test_pair_t combining_class_tests_more[] =
91 {
92   /* Unicode-5.2 character additions */
93   {   0xA8E0, (unsigned int) 230 },
94
95   /* Unicode-6.0 character additions */
96   {   0x135D, (unsigned int) 230 },
97 };
98
99 static const test_pair_t eastasian_width_tests[] =
100 {
101   {   0x111111, (unsigned int) 1 },
102
103   /* Neutral */
104   {   0x0000, (unsigned int) 1 },
105   {   0x0483, (unsigned int) 1 },
106   {   0x0641, (unsigned int) 1 },
107   {   0xFFFC, (unsigned int) 1 },
108   {   0x10000, (unsigned int) 1 },
109   {   0xE0001, (unsigned int) 1 },
110
111   /* Narrow */
112   {   0x0020, (unsigned int) 1 },
113   {   0x0041, (unsigned int) 1 },
114   {   0x27E6, (unsigned int) 1 },
115
116   /* Halfwidth */
117   {   0x20A9, (unsigned int) 1 },
118   {   0xFF61, (unsigned int) 1 },
119   {   0xFF69, (unsigned int) 1 },
120   {   0xFFEE, (unsigned int) 1 },
121
122   /* Ambiguous */
123   {   0x00A1, (unsigned int) 1 },
124   {   0x00D8, (unsigned int) 1 },
125   {   0x02DD, (unsigned int) 1 },
126   {   0xE0100, (unsigned int) 1 },
127   {   0x100000, (unsigned int) 1 },
128
129   /* Fullwidth */
130   {   0x3000, (unsigned int) 2 },
131   {   0xFF60, (unsigned int) 2 },
132
133   /* Wide */
134   {   0x2329, (unsigned int) 2 },
135   {   0x3001, (unsigned int) 2 },
136   {   0xFE69, (unsigned int) 2 },
137   {   0x30000, (unsigned int) 2 },
138   {   0x3FFFD, (unsigned int) 2 },
139 };
140 static const test_pair_t eastasian_width_tests_more[] =
141 {
142   /* Default Wide blocks */
143   {   0x4DBF, (unsigned int) 2 },
144   {   0x9FFF, (unsigned int) 2 },
145   {   0xFAFF, (unsigned int) 2 },
146   {   0x2A6DF, (unsigned int) 2 },
147   {   0x2B73F, (unsigned int) 2 },
148   {   0x2B81F, (unsigned int) 2 },
149   {   0x2FA1F, (unsigned int) 2 },
150
151   /* Uniode-5.2 character additions */
152   /* Wide */
153   {   0x115F, (unsigned int) 2 },
154
155   /* Uniode-6.0 character additions */
156   /* Wide */
157   {   0x2B740, (unsigned int) 2 },
158   {   0x1B000, (unsigned int) 2 },
159 };
160
161 static const test_pair_t general_category_tests[] =
162 {
163   { 0x111111, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED },
164
165   {   0x000D, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_CONTROL },
166   {   0x200E, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_FORMAT },
167   {   0x0378, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED },
168   {   0xE000, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE },
169   {   0xD800, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_SURROGATE },
170   {   0x0061, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER },
171   {   0x02B0, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER },
172   {   0x3400, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER },
173   {   0x01C5, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER },
174   {   0xFF21, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER },
175   {   0x0903, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_COMBINING_MARK },
176   {   0x20DD, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK },
177   {   0xA806, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK },
178   {   0xFF10, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER },
179   {   0x16EE, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER },
180   {   0x17F0, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER },
181   {   0x005F, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION },
182   {   0x058A, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION },
183   {   0x0F3B, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION },
184   {   0x2019, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION },
185   {   0x2018, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION },
186   {   0x2016, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION },
187   {   0x0F3A, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION },
188   {   0x20A0, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL },
189   {   0x309B, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL },
190   {   0xFB29, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL },
191   {   0x00A6, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL },
192   {   0x2028, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR },
193   {   0x2029, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR },
194   {   0x202F, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR }
195 };
196 static const test_pair_t general_category_tests_more[] =
197 {
198   /* Unicode-5.2 character additions */
199   {  0x1F131, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL },
200
201   /* Unicode-6.0 character additions */
202   {   0x0620, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER },
203 };
204
205 static const test_pair_t mirroring_tests[] =
206 {
207   { 0x111111, (unsigned int) 0x111111 },
208
209   /* Some characters that do NOT mirror */
210   {   0x0020, (unsigned int) 0x0020 },
211   {   0x0041, (unsigned int) 0x0041 },
212   {   0x00F0, (unsigned int) 0x00F0 },
213   {   0x27CC, (unsigned int) 0x27CC },
214   {  0xE01EF, (unsigned int) 0xE01EF },
215   {  0x1D7C3, (unsigned int) 0x1D7C3 },
216   { 0x100000, (unsigned int) 0x100000 },
217
218   /* Some characters that do mirror */
219   {   0x0029, (unsigned int) 0x0028 },
220   {   0x0028, (unsigned int) 0x0029 },
221   {   0x003E, (unsigned int) 0x003C },
222   {   0x003C, (unsigned int) 0x003E },
223   {   0x005D, (unsigned int) 0x005B },
224   {   0x005B, (unsigned int) 0x005D },
225   {   0x007D, (unsigned int) 0x007B },
226   {   0x007B, (unsigned int) 0x007D },
227   {   0x00BB, (unsigned int) 0x00AB },
228   {   0x00AB, (unsigned int) 0x00BB },
229   {   0x226B, (unsigned int) 0x226A },
230   {   0x226A, (unsigned int) 0x226B },
231   {   0x22F1, (unsigned int) 0x22F0 },
232   {   0x22F0, (unsigned int) 0x22F1 },
233   {   0xFF60, (unsigned int) 0xFF5F },
234   {   0xFF5F, (unsigned int) 0xFF60 },
235   {   0xFF63, (unsigned int) 0xFF62 },
236   {   0xFF62, (unsigned int) 0xFF63 },
237 };
238 static const test_pair_t mirroring_tests_more[] =
239 {
240   /* No new mirroring characters have been encoded in recent Unicode versions. */
241   {   0xFFFF, (unsigned int) 0xFFFF },
242 };
243
244 static const test_pair_t script_tests[] =
245 {
246   { 0x111111, (unsigned int) HB_SCRIPT_UNKNOWN },
247
248   {   0x002A, (unsigned int) HB_SCRIPT_COMMON },
249   {   0x0670, (unsigned int) HB_SCRIPT_INHERITED },
250   {   0x060D, (unsigned int) HB_SCRIPT_ARABIC },
251   {   0x0559, (unsigned int) HB_SCRIPT_ARMENIAN },
252   {   0x09CD, (unsigned int) HB_SCRIPT_BENGALI },
253   {   0x31B6, (unsigned int) HB_SCRIPT_BOPOMOFO },
254   {   0x13A2, (unsigned int) HB_SCRIPT_CHEROKEE },
255   {   0x2CFD, (unsigned int) HB_SCRIPT_COPTIC },
256   {   0x0482, (unsigned int) HB_SCRIPT_CYRILLIC },
257   {  0x10401, (unsigned int) HB_SCRIPT_DESERET },
258   {   0x094D, (unsigned int) HB_SCRIPT_DEVANAGARI },
259   {   0x1258, (unsigned int) HB_SCRIPT_ETHIOPIC },
260   {   0x10FC, (unsigned int) HB_SCRIPT_GEORGIAN },
261   {  0x10341, (unsigned int) HB_SCRIPT_GOTHIC },
262   {   0x0375, (unsigned int) HB_SCRIPT_GREEK },
263   {   0x0A83, (unsigned int) HB_SCRIPT_GUJARATI },
264   {   0x0A3C, (unsigned int) HB_SCRIPT_GURMUKHI },
265   {   0x3005, (unsigned int) HB_SCRIPT_HAN },
266   {   0x1100, (unsigned int) HB_SCRIPT_HANGUL },
267   {   0x05BF, (unsigned int) HB_SCRIPT_HEBREW },
268   {   0x309F, (unsigned int) HB_SCRIPT_HIRAGANA },
269   {   0x0CBC, (unsigned int) HB_SCRIPT_KANNADA },
270   {   0x30FF, (unsigned int) HB_SCRIPT_KATAKANA },
271   {   0x17DD, (unsigned int) HB_SCRIPT_KHMER },
272   {   0x0EDD, (unsigned int) HB_SCRIPT_LAO },
273   {   0x0061, (unsigned int) HB_SCRIPT_LATIN },
274   {   0x0D3D, (unsigned int) HB_SCRIPT_MALAYALAM },
275   {   0x1843, (unsigned int) HB_SCRIPT_MONGOLIAN },
276   {   0x1031, (unsigned int) HB_SCRIPT_MYANMAR },
277   {   0x169C, (unsigned int) HB_SCRIPT_OGHAM },
278   {  0x10322, (unsigned int) HB_SCRIPT_OLD_ITALIC },
279   {   0x0B3C, (unsigned int) HB_SCRIPT_ORIYA },
280   {   0x16EF, (unsigned int) HB_SCRIPT_RUNIC },
281   {   0x0DBD, (unsigned int) HB_SCRIPT_SINHALA },
282   {   0x0711, (unsigned int) HB_SCRIPT_SYRIAC },
283   {   0x0B82, (unsigned int) HB_SCRIPT_TAMIL },
284   {   0x0C03, (unsigned int) HB_SCRIPT_TELUGU },
285   {   0x07B1, (unsigned int) HB_SCRIPT_THAANA },
286   {   0x0E31, (unsigned int) HB_SCRIPT_THAI },
287   {   0x0FD4, (unsigned int) HB_SCRIPT_TIBETAN },
288   {   0x1401, (unsigned int) HB_SCRIPT_CANADIAN_ABORIGINAL },
289   {   0xA015, (unsigned int) HB_SCRIPT_YI },
290   {   0x1700, (unsigned int) HB_SCRIPT_TAGALOG },
291   {   0x1720, (unsigned int) HB_SCRIPT_HANUNOO },
292   {   0x1740, (unsigned int) HB_SCRIPT_BUHID },
293   {   0x1760, (unsigned int) HB_SCRIPT_TAGBANWA },
294
295   /* Unicode-4.0 additions */
296   {   0x2800, (unsigned int) HB_SCRIPT_BRAILLE },
297   {  0x10808, (unsigned int) HB_SCRIPT_CYPRIOT },
298   {   0x1932, (unsigned int) HB_SCRIPT_LIMBU },
299   {  0x10480, (unsigned int) HB_SCRIPT_OSMANYA },
300   {  0x10450, (unsigned int) HB_SCRIPT_SHAVIAN },
301   {  0x10000, (unsigned int) HB_SCRIPT_LINEAR_B },
302   {   0x1950, (unsigned int) HB_SCRIPT_TAI_LE },
303   {  0x1039F, (unsigned int) HB_SCRIPT_UGARITIC },
304
305   /* Unicode-4.1 additions */
306   {   0x1980, (unsigned int) HB_SCRIPT_NEW_TAI_LUE },
307   {   0x1A1F, (unsigned int) HB_SCRIPT_BUGINESE },
308   {   0x2C00, (unsigned int) HB_SCRIPT_GLAGOLITIC },
309   {   0x2D6F, (unsigned int) HB_SCRIPT_TIFINAGH },
310   {   0xA800, (unsigned int) HB_SCRIPT_SYLOTI_NAGRI },
311   {  0x103D0, (unsigned int) HB_SCRIPT_OLD_PERSIAN },
312   {  0x10A3F, (unsigned int) HB_SCRIPT_KHAROSHTHI },
313
314   /* Unicode-5.0 additions */
315   {   0x0378, (unsigned int) HB_SCRIPT_UNKNOWN },
316   {   0x1B04, (unsigned int) HB_SCRIPT_BALINESE },
317   {  0x12000, (unsigned int) HB_SCRIPT_CUNEIFORM },
318   {  0x10900, (unsigned int) HB_SCRIPT_PHOENICIAN },
319   {   0xA840, (unsigned int) HB_SCRIPT_PHAGS_PA },
320   {   0x07C0, (unsigned int) HB_SCRIPT_NKO },
321
322   /* Unicode-5.1 additions */
323   {   0xA900, (unsigned int) HB_SCRIPT_KAYAH_LI },
324   {   0x1C00, (unsigned int) HB_SCRIPT_LEPCHA },
325   {   0xA930, (unsigned int) HB_SCRIPT_REJANG },
326   {   0x1B80, (unsigned int) HB_SCRIPT_SUNDANESE },
327   {   0xA880, (unsigned int) HB_SCRIPT_SAURASHTRA },
328   {   0xAA00, (unsigned int) HB_SCRIPT_CHAM },
329   {   0x1C50, (unsigned int) HB_SCRIPT_OL_CHIKI },
330   {   0xA500, (unsigned int) HB_SCRIPT_VAI },
331   {  0x102A0, (unsigned int) HB_SCRIPT_CARIAN },
332   {  0x10280, (unsigned int) HB_SCRIPT_LYCIAN },
333   {  0x1093F, (unsigned int) HB_SCRIPT_LYDIAN },
334 };
335 static const test_pair_t script_tests_more[] =
336 {
337   /* Unicode-5.2 additions */
338   {  0x10B00, (unsigned int) HB_SCRIPT_AVESTAN },
339   {   0xA6A0, (unsigned int) HB_SCRIPT_BAMUM },
340   {  0x13000, (unsigned int) HB_SCRIPT_EGYPTIAN_HIEROGLYPHS },
341   {  0x10840, (unsigned int) HB_SCRIPT_IMPERIAL_ARAMAIC },
342   {  0x10B60, (unsigned int) HB_SCRIPT_INSCRIPTIONAL_PAHLAVI },
343   {  0x10B40, (unsigned int) HB_SCRIPT_INSCRIPTIONAL_PARTHIAN },
344   {   0xA980, (unsigned int) HB_SCRIPT_JAVANESE },
345   {  0x11082, (unsigned int) HB_SCRIPT_KAITHI },
346   {   0xA4D0, (unsigned int) HB_SCRIPT_LISU },
347   {   0xABE5, (unsigned int) HB_SCRIPT_MEETEI_MAYEK },
348   {  0x10A60, (unsigned int) HB_SCRIPT_OLD_SOUTH_ARABIAN },
349   {  0x10C00, (unsigned int) HB_SCRIPT_OLD_TURKIC },
350   {   0x0800, (unsigned int) HB_SCRIPT_SAMARITAN },
351   {   0x1A20, (unsigned int) HB_SCRIPT_TAI_THAM },
352   {   0xAA80, (unsigned int) HB_SCRIPT_TAI_VIET },
353
354   /* Unicode-6.0 additions */
355   {   0x1BC0, (unsigned int) HB_SCRIPT_BATAK },
356   {  0x11000, (unsigned int) HB_SCRIPT_BRAHMI },
357   {   0x0840, (unsigned int) HB_SCRIPT_MANDAIC },
358
359   /* Unicode-5.2 character additions */
360   {   0x1CED, (unsigned int) HB_SCRIPT_INHERITED },
361   {   0x1400, (unsigned int) HB_SCRIPT_CANADIAN_ABORIGINAL },
362 };
363
364
365 typedef unsigned int (*get_func_t)         (hb_unicode_funcs_t *ufuncs,
366                                             hb_codepoint_t      unicode,
367                                             void               *user_data);
368 typedef unsigned int (*func_setter_func_t) (hb_unicode_funcs_t *ufuncs,
369                                             get_func_t         *func,
370                                             void               *user_data,
371                                             hb_destroy_func_t   destroy);
372 typedef unsigned int (*getter_func_t)      (hb_unicode_funcs_t *ufuncs,
373                                             hb_codepoint_t      unicode);
374
375 typedef struct {
376   const char         *name;
377   func_setter_func_t  func_setter;
378   getter_func_t       getter;
379   const test_pair_t  *tests;
380   unsigned int        num_tests;
381   const test_pair_t  *tests_more;
382   unsigned int        num_tests_more;
383   unsigned int        default_value;
384 } property_t;
385
386 #define RETURNS_UNICODE_ITSELF ((unsigned int) -1)
387
388 #define PROPERTY(name, DEFAULT) \
389   { \
390     #name, \
391     (func_setter_func_t) hb_unicode_funcs_set_##name##_func, \
392     (getter_func_t) hb_unicode_get_##name, \
393     name##_tests, \
394     G_N_ELEMENTS (name##_tests), \
395     name##_tests_more, \
396     G_N_ELEMENTS (name##_tests_more), \
397     DEFAULT \
398   }
399 static const property_t properties[] =
400 {
401   PROPERTY (combining_class, 0),
402   PROPERTY (eastasian_width, 1),
403   PROPERTY (general_category, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER),
404   PROPERTY (mirroring, RETURNS_UNICODE_ITSELF),
405   PROPERTY (script, (unsigned int) HB_SCRIPT_UNKNOWN),
406 };
407 #undef PROPERTY
408
409 static void
410 test_unicode_properties (gconstpointer user_data)
411 {
412   hb_unicode_funcs_t *uf = (hb_unicode_funcs_t *) user_data;
413   unsigned int i, j;
414
415   g_assert (hb_unicode_funcs_is_immutable (uf));
416
417   for (i = 0; i < G_N_ELEMENTS (properties); i++) {
418     const property_t *p = &properties[i];
419     const test_pair_t *tests;
420
421     tests = p->tests;
422     for (j = 0; j < p->num_tests; j++)
423       g_assert_cmpint (p->getter (uf, tests[j].unicode), ==, tests[j].value);
424   }
425 }
426
427 static hb_codepoint_t
428 default_value (hb_codepoint_t default_value, hb_codepoint_t unicode)
429 {
430   return default_value == RETURNS_UNICODE_ITSELF ?  unicode : default_value;
431 }
432
433 static void
434 test_unicode_properties_nil (void)
435 {
436   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
437   unsigned int i, j;
438
439   g_assert (!hb_unicode_funcs_is_immutable (uf));
440
441   for (i = 0; i < G_N_ELEMENTS (properties); i++) {
442     const property_t *p = &properties[i];
443     const test_pair_t *tests;
444
445     tests = p->tests;
446     for (j = 0; j < p->num_tests; j++)
447       g_assert_cmpint (p->getter (uf, tests[j].unicode), ==, default_value (p->default_value, tests[j].unicode));
448   }
449
450   hb_unicode_funcs_destroy (uf);
451 }
452
453 #define MAGIC0 0x12345678
454 #define MAGIC1 0x76543210
455
456 typedef struct {
457   int value;
458   gboolean freed;
459 } data_t;
460
461 typedef struct {
462   data_t data[2];
463 } data_fixture_t;
464 static void
465 data_fixture_init (data_fixture_t *f, gconstpointer user_data)
466 {
467   f->data[0].value = MAGIC0;
468   f->data[1].value = MAGIC1;
469 }
470 static void
471 data_fixture_finish (data_fixture_t *f, gconstpointer user_data)
472 {
473 }
474
475 static void free_up (void *p)
476 {
477   data_t *data = (data_t *) p;
478
479   g_assert (data->value == MAGIC0 || data->value == MAGIC1);
480   g_assert (data->freed == FALSE);
481   data->freed = TRUE;
482 }
483
484 static hb_script_t
485 simple_get_script (hb_unicode_funcs_t *ufuncs,
486                    hb_codepoint_t      codepoint,
487                    void               *user_data)
488 {
489   data_t *data = (data_t *) user_data;
490
491   g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
492   g_assert (data->value == MAGIC0);
493   g_assert (data->freed == FALSE);
494
495   if ('a' <= codepoint && codepoint <= 'z')
496     return HB_SCRIPT_LATIN;
497   else
498     return HB_SCRIPT_UNKNOWN;
499 }
500
501 static hb_script_t
502 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
503                             hb_codepoint_t      codepoint,
504                             void               *user_data)
505 {
506   data_t *data = (data_t *) user_data;
507
508   g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
509   g_assert (data->value == MAGIC1);
510   g_assert (data->freed == FALSE);
511
512   if (codepoint == 'a') {
513     return HB_SCRIPT_ARABIC;
514   } else {
515     hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
516
517     return hb_unicode_get_script (parent, codepoint);
518   }
519 }
520
521 static void
522 test_unicode_custom (data_fixture_t *f, gconstpointer user_data)
523 {
524   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
525
526   hb_unicode_funcs_set_script_func (uf, simple_get_script,
527                                     &f->data[0], free_up);
528
529   g_assert_cmpint (hb_unicode_get_script (uf, 'a'), ==, HB_SCRIPT_LATIN);
530   g_assert_cmpint (hb_unicode_get_script (uf, '0'), ==, HB_SCRIPT_UNKNOWN);
531
532   g_assert (!hb_unicode_funcs_is_immutable (uf));
533   hb_unicode_funcs_make_immutable (uf);
534   g_assert (hb_unicode_funcs_is_immutable (uf));
535
536   /* Since uf is immutable now, the following setter should do nothing. */
537   hb_unicode_funcs_set_script_func (uf, a_is_for_arabic_get_script,
538                                     &f->data[1], free_up);
539
540   g_assert (!f->data[0].freed && !f->data[1].freed);
541   hb_unicode_funcs_destroy (uf);
542   g_assert (f->data[0].freed && !f->data[1].freed);
543 }
544
545 static void
546 test_unicode_subclassing_nil (data_fixture_t *f, gconstpointer user_data)
547 {
548   hb_unicode_funcs_t *uf, *aa;
549
550   uf = hb_unicode_funcs_create (NULL);
551
552   aa = hb_unicode_funcs_create (uf);
553
554   hb_unicode_funcs_destroy (uf);
555
556   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
557                                     &f->data[1], free_up);
558
559   g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
560   g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);
561
562   g_assert (!f->data[0].freed && !f->data[1].freed);
563   hb_unicode_funcs_destroy (aa);
564   g_assert (!f->data[0].freed && f->data[1].freed);
565 }
566
567 static void
568 test_unicode_subclassing_default (data_fixture_t *f, gconstpointer user_data)
569 {
570   hb_unicode_funcs_t *uf, *aa;
571
572   uf = hb_unicode_funcs_get_default ();
573   aa = hb_unicode_funcs_create (uf);
574
575   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
576                                     &f->data[1], free_up);
577
578   g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
579   g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
580
581   g_assert (!f->data[0].freed && !f->data[1].freed);
582   hb_unicode_funcs_destroy (aa);
583   g_assert (!f->data[0].freed && f->data[1].freed);
584 }
585
586 static void
587 test_unicode_subclassing_deep (data_fixture_t *f, gconstpointer user_data)
588 {
589   hb_unicode_funcs_t *uf, *aa;
590
591   uf = hb_unicode_funcs_create (NULL);
592
593   hb_unicode_funcs_set_script_func (uf, simple_get_script,
594                                     &f->data[0], free_up);
595
596   aa = hb_unicode_funcs_create (uf);
597
598   hb_unicode_funcs_destroy (uf);
599
600   /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
601   g_assert (!f->data[0].freed);
602
603   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
604                                     &f->data[1], free_up);
605
606   g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
607   g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
608   g_assert_cmpint (hb_unicode_get_script (aa, '0'), ==, HB_SCRIPT_UNKNOWN);
609
610   g_assert (!f->data[0].freed && !f->data[1].freed);
611   hb_unicode_funcs_destroy (aa);
612   g_assert (f->data[0].freed && f->data[1].freed);
613 }
614
615
616 int
617 main (int argc, char **argv)
618 {
619   hb_test_init (&argc, &argv);
620
621   hb_test_add (test_unicode_properties_nil);
622
623   hb_test_add_data_flavor (hb_unicode_funcs_get_default (), "default", test_unicode_properties);
624 #ifdef HAVE_GLIB
625   hb_test_add_data_flavor (hb_glib_get_unicode_funcs (),    "glib",    test_unicode_properties);
626 #endif
627 #ifdef HAVE_ICU
628   hb_test_add_data_flavor (hb_icu_get_unicode_funcs (),     "icu",    test_unicode_properties);
629 #endif
630
631   hb_test_add_fixture (data_fixture, NULL, test_unicode_custom);
632   hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_nil);
633   hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_default);
634   hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_deep);
635
636   /* XXX test icu ufuncs */
637   /* XXX test _more tests (warn?) */
638   /* XXX test chainup */
639   /* XXX test glib & icu two-way script conversion */
640
641   return hb_test_run ();
642 }