2.0_alpha
[apps/home/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 /* Unit tests for hb-glib.h */
33 /* Unit tests for hb-icu.h */
34
35
36 #ifdef HAVE_GLIB
37 #include <hb-glib.h>
38 #endif
39 #ifdef HAVE_ICU
40 #include <hb-icu.h>
41 #endif
42
43
44 /* Some useful stuff */
45
46 #define MAGIC0 0x12345678
47 #define MAGIC1 0x76543210
48
49 typedef struct {
50   int value;
51   gboolean freed;
52 } data_t;
53
54 static void free_up (void *p)
55 {
56   data_t *data = (data_t *) p;
57
58   g_assert (data->value == MAGIC0 || data->value == MAGIC1);
59   g_assert (!data->freed);
60   data->freed = TRUE;
61 }
62
63 static hb_script_t
64 simple_get_script (hb_unicode_funcs_t *ufuncs,
65                    hb_codepoint_t      codepoint,
66                    void               *user_data)
67 {
68   data_t *data = (data_t *) user_data;
69
70   g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
71   g_assert_cmphex (data->value, ==, MAGIC0);
72   g_assert (!data->freed);
73
74   if ('a' <= codepoint && codepoint <= 'z')
75     return HB_SCRIPT_LATIN;
76   else
77     return HB_SCRIPT_UNKNOWN;
78 }
79
80 static hb_script_t
81 a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
82                             hb_codepoint_t      codepoint,
83                             void               *user_data)
84 {
85   data_t *data = (data_t *) user_data;
86
87   g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
88   g_assert_cmphex (data->value, ==, MAGIC1);
89   g_assert (!data->freed);
90
91   if (codepoint == 'a') {
92     return HB_SCRIPT_ARABIC;
93   } else {
94     hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);
95
96     return hb_unicode_script (parent, codepoint);
97   }
98 }
99
100
101
102 /* Check all properties */
103
104 /* Some of the following tables where adapted from glib/glib/tests/utf8-misc.c.
105  * The license is compatible. */
106
107 typedef struct {
108   hb_codepoint_t unicode;
109   unsigned int   value;
110 } test_pair_t;
111
112 static const test_pair_t combining_class_tests[] =
113 {
114   {   0x0020, 0 },
115   {   0x0334, 1 },
116   {   0x093C, 7 },
117   {   0x3099, 8 },
118   {   0x094D, 9 },
119   {   0x05B0, 10 },
120   {   0x05B1, 11 },
121   {   0x05B2, 12 },
122   {   0x05B3, 13 },
123   {   0x05B4, 14 },
124   {   0x05B5, 15 },
125   {   0x05B6, 16 },
126   {   0x05B7, 17 },
127   {   0x05B8, 18 },
128   {   0x05B9, 19 },
129   {   0x05BB, 20 },
130   {   0x05BC, 21 },
131   {   0x05BD, 22 },
132   {   0x05BF, 23 },
133   {   0x05C1, 24 },
134   {   0x05C2, 25 },
135   {   0xFB1E, 26 },
136   {   0x064B, 27 },
137   {   0x064C, 28 },
138   {   0x064D, 29 },
139   /* ... */
140   {   0x05AE, 228 },
141   {   0x0300, 230 },
142   {   0x302C, 232 },
143   {   0x0362, 233 },
144   {   0x0360, 234 },
145   {   0x1DCD, 234 },
146   {   0x0345, 240 },
147
148   { 0x111111, 0 }
149 };
150 static const test_pair_t combining_class_tests_more[] =
151 {
152   /* Unicode-5.2 character additions */
153   {   0xA8E0, 230 },
154
155   /* Unicode-6.0 character additions */
156   {   0x135D, 230 },
157
158   { 0x111111, 0 }
159 };
160
161 static const test_pair_t eastasian_width_tests[] =
162 {
163   /* Neutral */
164   {   0x0000, 1 },
165   {   0x0483, 1 },
166   {   0x0641, 1 },
167   {   0xFFFC, 1 },
168   {  0x10000, 1 },
169   {  0xE0001, 1 },
170
171   /* Narrow */
172   {   0x0020, 1 },
173   {   0x0041, 1 },
174   {   0x27E6, 1 },
175
176   /* Halfwidth */
177   {   0x20A9, 1 },
178   {   0xFF61, 1 },
179   {   0xFF69, 1 },
180   {   0xFFEE, 1 },
181
182   /* Ambiguous */
183   {   0x00A1, 1 },
184   {   0x00D8, 1 },
185   {   0x02DD, 1 },
186   {  0xE0100, 1 },
187   { 0x100000, 1 },
188
189   /* Fullwidth */
190   {   0x3000, 2 },
191   {   0xFF60, 2 },
192
193   /* Wide */
194   {   0x2329, 2 },
195   {   0x3001, 2 },
196   {   0xFE69, 2 },
197   {  0x30000, 2 },
198   {  0x3FFFD, 2 },
199
200   { 0x111111, 1 }
201 };
202 static const test_pair_t eastasian_width_tests_more[] =
203 {
204   /* Default Wide blocks */
205   {   0x4DBF, 2 },
206   {   0x9FFF, 2 },
207   {   0xFAFF, 2 },
208   {  0x2A6DF, 2 },
209   {  0x2B73F, 2 },
210   {  0x2B81F, 2 },
211   {  0x2FA1F, 2 },
212
213   /* Uniode-5.2 character additions */
214   /* Wide */
215   {   0x115F, 2 },
216
217   /* Uniode-6.0 character additions */
218   /* Wide */
219   {  0x2B740, 2 },
220   {  0x1B000, 2 },
221
222   { 0x111111, 1 }
223 };
224
225 static const test_pair_t general_category_tests[] =
226 {
227   {   0x000D, HB_UNICODE_GENERAL_CATEGORY_CONTROL },
228   {   0x200E, HB_UNICODE_GENERAL_CATEGORY_FORMAT },
229   {   0x0378, HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED },
230   {   0xE000, HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE },
231   {   0xD800, HB_UNICODE_GENERAL_CATEGORY_SURROGATE },
232   {   0x0061, HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER },
233   {   0x02B0, HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER },
234   {   0x3400, HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER },
235   {   0x01C5, HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER },
236   {   0xFF21, HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER },
237   {   0x0903, HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK },
238   {   0x20DD, HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK },
239   {   0xA806, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK },
240   {   0xFF10, HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER },
241   {   0x16EE, HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER },
242   {   0x17F0, HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER },
243   {   0x005F, HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION },
244   {   0x058A, HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION },
245   {   0x0F3B, HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION },
246   {   0x2019, HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION },
247   {   0x2018, HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION },
248   {   0x2016, HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION },
249   {   0x0F3A, HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION },
250   {   0x20A0, HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL },
251   {   0x309B, HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL },
252   {   0xFB29, HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL },
253   {   0x00A6, HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL },
254   {   0x2028, HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR },
255   {   0x2029, HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR },
256   {   0x202F, HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR },
257
258   { 0x111111, HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED }
259 };
260 static const test_pair_t general_category_tests_more[] =
261 {
262   /* Unicode-5.2 character additions */
263   {  0x1F131, HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL },
264
265   /* Unicode-6.0 character additions */
266   {   0x0620, HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER },
267
268   { 0x111111, HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED }
269 };
270
271 static const test_pair_t mirroring_tests[] =
272 {
273   /* Some characters that do NOT mirror */
274   {   0x0020, 0x0020 },
275   {   0x0041, 0x0041 },
276   {   0x00F0, 0x00F0 },
277   {   0x27CC, 0x27CC },
278   {  0xE01EF, 0xE01EF },
279   {  0x1D7C3, 0x1D7C3 },
280   { 0x100000, 0x100000 },
281
282   /* Some characters that do mirror */
283   {   0x0029, 0x0028 },
284   {   0x0028, 0x0029 },
285   {   0x003E, 0x003C },
286   {   0x003C, 0x003E },
287   {   0x005D, 0x005B },
288   {   0x005B, 0x005D },
289   {   0x007D, 0x007B },
290   {   0x007B, 0x007D },
291   {   0x00BB, 0x00AB },
292   {   0x00AB, 0x00BB },
293   {   0x226B, 0x226A },
294   {   0x226A, 0x226B },
295   {   0x22F1, 0x22F0 },
296   {   0x22F0, 0x22F1 },
297   {   0xFF60, 0xFF5F },
298   {   0xFF5F, 0xFF60 },
299   {   0xFF63, 0xFF62 },
300   {   0xFF62, 0xFF63 },
301
302   { 0x111111, 0x111111 },
303 };
304 static const test_pair_t mirroring_tests_more[] =
305 {
306   /* No new mirroring characters have been encoded in recent Unicode versions. */
307   { 0x111111, 0x111111 }
308 };
309
310 static const test_pair_t script_tests[] =
311 {
312   {   0x002A, HB_SCRIPT_COMMON },
313   {   0x0670, HB_SCRIPT_INHERITED },
314   {   0x060D, HB_SCRIPT_ARABIC },
315   {   0x0559, HB_SCRIPT_ARMENIAN },
316   {   0x09CD, HB_SCRIPT_BENGALI },
317   {   0x31B6, HB_SCRIPT_BOPOMOFO },
318   {   0x13A2, HB_SCRIPT_CHEROKEE },
319   {   0x2CFD, HB_SCRIPT_COPTIC },
320   {   0x0482, HB_SCRIPT_CYRILLIC },
321   {  0x10401, HB_SCRIPT_DESERET },
322   {   0x094D, HB_SCRIPT_DEVANAGARI },
323   {   0x1258, HB_SCRIPT_ETHIOPIC },
324   {   0x10FC, HB_SCRIPT_GEORGIAN },
325   {  0x10341, HB_SCRIPT_GOTHIC },
326   {   0x0375, HB_SCRIPT_GREEK },
327   {   0x0A83, HB_SCRIPT_GUJARATI },
328   {   0x0A3C, HB_SCRIPT_GURMUKHI },
329   {   0x3005, HB_SCRIPT_HAN },
330   {   0x1100, HB_SCRIPT_HANGUL },
331   {   0x05BF, HB_SCRIPT_HEBREW },
332   {   0x309F, HB_SCRIPT_HIRAGANA },
333   {   0x0CBC, HB_SCRIPT_KANNADA },
334   {   0x30FF, HB_SCRIPT_KATAKANA },
335   {   0x17DD, HB_SCRIPT_KHMER },
336   {   0x0EDD, HB_SCRIPT_LAO },
337   {   0x0061, HB_SCRIPT_LATIN },
338   {   0x0D3D, HB_SCRIPT_MALAYALAM },
339   {   0x1843, HB_SCRIPT_MONGOLIAN },
340   {   0x1031, HB_SCRIPT_MYANMAR },
341   {   0x169C, HB_SCRIPT_OGHAM },
342   {  0x10322, HB_SCRIPT_OLD_ITALIC },
343   {   0x0B3C, HB_SCRIPT_ORIYA },
344   {   0x16EF, HB_SCRIPT_RUNIC },
345   {   0x0DBD, HB_SCRIPT_SINHALA },
346   {   0x0711, HB_SCRIPT_SYRIAC },
347   {   0x0B82, HB_SCRIPT_TAMIL },
348   {   0x0C03, HB_SCRIPT_TELUGU },
349   {   0x07B1, HB_SCRIPT_THAANA },
350   {   0x0E31, HB_SCRIPT_THAI },
351   {   0x0FD4, HB_SCRIPT_TIBETAN },
352   {   0x1401, HB_SCRIPT_CANADIAN_ABORIGINAL },
353   {   0xA015, HB_SCRIPT_YI },
354   {   0x1700, HB_SCRIPT_TAGALOG },
355   {   0x1720, HB_SCRIPT_HANUNOO },
356   {   0x1740, HB_SCRIPT_BUHID },
357   {   0x1760, HB_SCRIPT_TAGBANWA },
358
359   /* Unicode-4.0 additions */
360   {   0x2800, HB_SCRIPT_BRAILLE },
361   {  0x10808, HB_SCRIPT_CYPRIOT },
362   {   0x1932, HB_SCRIPT_LIMBU },
363   {  0x10480, HB_SCRIPT_OSMANYA },
364   {  0x10450, HB_SCRIPT_SHAVIAN },
365   {  0x10000, HB_SCRIPT_LINEAR_B },
366   {   0x1950, HB_SCRIPT_TAI_LE },
367   {  0x1039F, HB_SCRIPT_UGARITIC },
368
369   /* Unicode-4.1 additions */
370   {   0x1980, HB_SCRIPT_NEW_TAI_LUE },
371   {   0x1A1F, HB_SCRIPT_BUGINESE },
372   {   0x2C00, HB_SCRIPT_GLAGOLITIC },
373   {   0x2D6F, HB_SCRIPT_TIFINAGH },
374   {   0xA800, HB_SCRIPT_SYLOTI_NAGRI },
375   {  0x103D0, HB_SCRIPT_OLD_PERSIAN },
376   {  0x10A3F, HB_SCRIPT_KHAROSHTHI },
377
378   /* Unicode-5.0 additions */
379   {   0x0378, HB_SCRIPT_UNKNOWN },
380   {   0x1B04, HB_SCRIPT_BALINESE },
381   {  0x12000, HB_SCRIPT_CUNEIFORM },
382   {  0x10900, HB_SCRIPT_PHOENICIAN },
383   {   0xA840, HB_SCRIPT_PHAGS_PA },
384   {   0x07C0, HB_SCRIPT_NKO },
385
386   /* Unicode-5.1 additions */
387   {   0xA900, HB_SCRIPT_KAYAH_LI },
388   {   0x1C00, HB_SCRIPT_LEPCHA },
389   {   0xA930, HB_SCRIPT_REJANG },
390   {   0x1B80, HB_SCRIPT_SUNDANESE },
391   {   0xA880, HB_SCRIPT_SAURASHTRA },
392   {   0xAA00, HB_SCRIPT_CHAM },
393   {   0x1C50, HB_SCRIPT_OL_CHIKI },
394   {   0xA500, HB_SCRIPT_VAI },
395   {  0x102A0, HB_SCRIPT_CARIAN },
396   {  0x10280, HB_SCRIPT_LYCIAN },
397   {  0x1093F, HB_SCRIPT_LYDIAN },
398
399   { 0x111111, HB_SCRIPT_UNKNOWN }
400 };
401 static const test_pair_t script_tests_more[] =
402 {
403   /* Unicode-5.2 additions */
404   {  0x10B00, HB_SCRIPT_AVESTAN },
405   {   0xA6A0, HB_SCRIPT_BAMUM },
406   {  0x13000, HB_SCRIPT_EGYPTIAN_HIEROGLYPHS },
407   {  0x10840, HB_SCRIPT_IMPERIAL_ARAMAIC },
408   {  0x10B60, HB_SCRIPT_INSCRIPTIONAL_PAHLAVI },
409   {  0x10B40, HB_SCRIPT_INSCRIPTIONAL_PARTHIAN },
410   {   0xA980, HB_SCRIPT_JAVANESE },
411   {  0x11082, HB_SCRIPT_KAITHI },
412   {   0xA4D0, HB_SCRIPT_LISU },
413   {   0xABE5, HB_SCRIPT_MEETEI_MAYEK },
414   {  0x10A60, HB_SCRIPT_OLD_SOUTH_ARABIAN },
415   {  0x10C00, HB_SCRIPT_OLD_TURKIC },
416   {   0x0800, HB_SCRIPT_SAMARITAN },
417   {   0x1A20, HB_SCRIPT_TAI_THAM },
418   {   0xAA80, HB_SCRIPT_TAI_VIET },
419
420   /* Unicode-6.0 additions */
421   {   0x1BC0, HB_SCRIPT_BATAK },
422   {  0x11000, HB_SCRIPT_BRAHMI },
423   {   0x0840, HB_SCRIPT_MANDAIC },
424
425   /* Unicode-5.2 character additions */
426   {   0x1CED, HB_SCRIPT_INHERITED },
427   {   0x1400, HB_SCRIPT_CANADIAN_ABORIGINAL },
428
429   { 0x111111, HB_SCRIPT_UNKNOWN }
430 };
431
432
433 typedef unsigned int (*get_func_t)         (hb_unicode_funcs_t *ufuncs,
434                                             hb_codepoint_t      unicode,
435                                             void               *user_data);
436 typedef unsigned int (*func_setter_func_t) (hb_unicode_funcs_t *ufuncs,
437                                             get_func_t          func,
438                                             void               *user_data,
439                                             hb_destroy_func_t   destroy);
440 typedef unsigned int (*getter_func_t)      (hb_unicode_funcs_t *ufuncs,
441                                             hb_codepoint_t      unicode);
442
443 typedef struct {
444   const char         *name;
445   func_setter_func_t  func_setter;
446   getter_func_t       getter;
447   const test_pair_t  *tests;
448   unsigned int        num_tests;
449   const test_pair_t  *tests_more;
450   unsigned int        num_tests_more;
451   unsigned int        default_value;
452 } property_t;
453
454 #define RETURNS_UNICODE_ITSELF ((unsigned int) -1)
455
456 #define PROPERTY(name, DEFAULT) \
457   { \
458     #name, \
459     (func_setter_func_t) hb_unicode_funcs_set_##name##_func, \
460     (getter_func_t) hb_unicode_##name, \
461     name##_tests, \
462     G_N_ELEMENTS (name##_tests), \
463     name##_tests_more, \
464     G_N_ELEMENTS (name##_tests_more), \
465     DEFAULT \
466   }
467 static const property_t properties[] =
468 {
469   PROPERTY (combining_class, 0),
470   PROPERTY (eastasian_width, 1),
471   PROPERTY (general_category, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER),
472   PROPERTY (mirroring, RETURNS_UNICODE_ITSELF),
473   PROPERTY (script, (unsigned int) HB_SCRIPT_UNKNOWN)
474 };
475 #undef PROPERTY
476
477 static void
478 test_unicode_properties (gconstpointer user_data)
479 {
480   hb_unicode_funcs_t *uf = (hb_unicode_funcs_t *) user_data;
481   unsigned int i, j;
482   gboolean failed = TRUE;
483
484   g_assert (hb_unicode_funcs_is_immutable (uf));
485   g_assert (hb_unicode_funcs_get_parent (uf));
486
487   for (i = 0; i < G_N_ELEMENTS (properties); i++) {
488     const property_t *p = &properties[i];
489     const test_pair_t *tests;
490
491     g_test_message ("Testing property %s", p->name);
492     tests = p->tests;
493     for (j = 0; j < p->num_tests; j++) {
494       g_test_message ("Test %s #%d: U+%04X", p->name, j, tests[j].unicode);
495       g_assert_cmphex (p->getter (uf, tests[j].unicode), ==, tests[j].value);
496     }
497     /* These tests are from Unicode 5.2 onward and older glib/ICU
498      * don't get them right.  Just warn instead of assert. */
499     tests = p->tests_more;
500     for (j = 0; j < p->num_tests_more; j++) {
501       g_test_message ("Test %s more #%d: U+%04X", p->name, j, tests[j].unicode);
502       if (p->getter (uf, tests[j].unicode) != tests[j].value) {
503         g_test_message ("Soft fail: Received %x, expected %x", p->getter (uf, tests[j].unicode), tests[j].value);
504         failed = TRUE;
505       }
506     }
507   }
508
509   if (failed)
510     g_test_message ("Some property tests failed.  You probably have an old version of one of the libraries used.");
511 }
512
513 static hb_codepoint_t
514 default_value (hb_codepoint_t _default_value, hb_codepoint_t unicode)
515 {
516   return _default_value == RETURNS_UNICODE_ITSELF ?  unicode : _default_value;
517 }
518
519 static void
520 _test_unicode_properties_nil (hb_unicode_funcs_t *uf)
521 {
522   unsigned int i, j;
523
524   for (i = 0; i < G_N_ELEMENTS (properties); i++) {
525     const property_t *p = &properties[i];
526     const test_pair_t *tests;
527
528     g_test_message ("Testing property %s", p->name);
529     tests = p->tests;
530     for (j = 0; j < p->num_tests; j++) {
531       g_test_message ("Test %s #%d: U+%04X", p->name, j, tests[j].unicode);
532       g_assert_cmphex (p->getter (uf, tests[j].unicode), ==, default_value (p->default_value, tests[j].unicode));
533     }
534     tests = p->tests_more;
535     for (j = 0; j < p->num_tests_more; j++) {
536       g_test_message ("Test %s more #%d: U+%04X", p->name, j, tests[j].unicode);
537       g_assert_cmphex (p->getter (uf, tests[j].unicode), ==, default_value (p->default_value, tests[j].unicode));
538     }
539   }
540 }
541
542 static void
543 test_unicode_properties_nil (void)
544 {
545   hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
546
547   g_assert (!hb_unicode_funcs_is_immutable (uf));
548   _test_unicode_properties_nil (uf);
549
550   hb_unicode_funcs_destroy (uf);
551 }
552
553 static void
554 test_unicode_properties_empty (void)
555 {
556   hb_unicode_funcs_t *uf = hb_unicode_funcs_get_empty ();
557
558   g_assert (uf);
559   g_assert (hb_unicode_funcs_is_immutable (uf));
560   _test_unicode_properties_nil (uf);
561 }
562
563
564 static void
565 test_unicode_chainup (void)
566 {
567   hb_unicode_funcs_t *uf, *uf2;
568
569   /* Chain-up to nil */
570
571   uf = hb_unicode_funcs_create (NULL);
572   g_assert (!hb_unicode_funcs_is_immutable (uf));
573
574   uf2 = hb_unicode_funcs_create (uf);
575   g_assert (hb_unicode_funcs_is_immutable (uf));
576   hb_unicode_funcs_destroy (uf);
577
578   g_assert (!hb_unicode_funcs_is_immutable (uf2));
579   _test_unicode_properties_nil (uf2);
580
581   hb_unicode_funcs_destroy (uf2);
582
583   /* Chain-up to default */
584
585   uf = hb_unicode_funcs_create (hb_unicode_funcs_get_default ());
586   g_assert (!hb_unicode_funcs_is_immutable (uf));
587
588   uf2 = hb_unicode_funcs_create (uf);
589   g_assert (hb_unicode_funcs_is_immutable (uf));
590   hb_unicode_funcs_destroy (uf);
591
592   g_assert (!hb_unicode_funcs_is_immutable (uf2));
593   hb_unicode_funcs_make_immutable (uf2);
594   test_unicode_properties (uf2);
595
596   hb_unicode_funcs_destroy (uf2);
597
598 }
599
600 static void
601 test_unicode_setters (void)
602 {
603   hb_unicode_funcs_t *uf;
604   unsigned int i;
605
606   /* This is cruel: we use script-returning functions to test all properties,
607    * but it works. */
608
609   for (i = 0; i < G_N_ELEMENTS (properties); i++) {
610     const property_t *p = &properties[i];
611     data_t data[2] = {{MAGIC0, FALSE}, {MAGIC1, FALSE}};
612
613     g_test_message ("Testing property %s", p->name);
614
615     uf = hb_unicode_funcs_create (NULL);
616     g_assert (!hb_unicode_funcs_is_immutable (uf));
617
618     p->func_setter (uf, (get_func_t) simple_get_script, &data[0], free_up);
619
620     g_assert_cmphex (p->getter (uf, 'a'), ==, HB_SCRIPT_LATIN);
621     g_assert_cmphex (p->getter (uf, '0'), ==, HB_SCRIPT_UNKNOWN);
622
623     p->func_setter (uf, (get_func_t) NULL, NULL, NULL);
624     g_assert (data[0].freed && !data[1].freed);
625
626     g_assert (!hb_unicode_funcs_is_immutable (uf));
627     hb_unicode_funcs_make_immutable (uf);
628     g_assert (hb_unicode_funcs_is_immutable (uf));
629
630     /* Since uf is immutable now, the following setter should do nothing. */
631     p->func_setter (uf, (get_func_t) a_is_for_arabic_get_script, &data[1], free_up);
632
633     g_assert (data[0].freed && !data[1].freed);
634     hb_unicode_funcs_destroy (uf);
635     g_assert (data[0].freed && !data[1].freed);
636   }
637 }
638
639
640
641 typedef struct {
642   data_t data[2];
643 } data_fixture_t;
644
645 static void
646 data_fixture_init (data_fixture_t *f, gconstpointer user_data)
647 {
648   f->data[0].value = MAGIC0;
649   f->data[1].value = MAGIC1;
650 }
651 static void
652 data_fixture_finish (data_fixture_t *f, gconstpointer user_data)
653 {
654 }
655
656 static void
657 test_unicode_subclassing_nil (data_fixture_t *f, gconstpointer user_data)
658 {
659   hb_unicode_funcs_t *uf, *aa;
660
661   uf = hb_unicode_funcs_create (NULL);
662
663   aa = hb_unicode_funcs_create (uf);
664
665   hb_unicode_funcs_destroy (uf);
666
667   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
668                                     &f->data[1], free_up);
669
670   g_assert_cmphex (hb_unicode_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
671   g_assert_cmphex (hb_unicode_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);
672
673   g_assert (!f->data[0].freed && !f->data[1].freed);
674   hb_unicode_funcs_destroy (aa);
675   g_assert (!f->data[0].freed && f->data[1].freed);
676 }
677
678 static void
679 test_unicode_subclassing_default (data_fixture_t *f, gconstpointer user_data)
680 {
681   hb_unicode_funcs_t *uf, *aa;
682
683   uf = hb_unicode_funcs_get_default ();
684   aa = hb_unicode_funcs_create (uf);
685
686   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
687                                     &f->data[1], free_up);
688
689   g_assert_cmphex (hb_unicode_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
690   g_assert_cmphex (hb_unicode_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
691
692   g_assert (!f->data[0].freed && !f->data[1].freed);
693   hb_unicode_funcs_destroy (aa);
694   g_assert (!f->data[0].freed && f->data[1].freed);
695 }
696
697 static void
698 test_unicode_subclassing_deep (data_fixture_t *f, gconstpointer user_data)
699 {
700   hb_unicode_funcs_t *uf, *aa;
701
702   uf = hb_unicode_funcs_create (NULL);
703
704   hb_unicode_funcs_set_script_func (uf, simple_get_script,
705                                     &f->data[0], free_up);
706
707   aa = hb_unicode_funcs_create (uf);
708
709   hb_unicode_funcs_destroy (uf);
710
711   /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
712   g_assert (!f->data[0].freed);
713
714   hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
715                                     &f->data[1], free_up);
716
717   g_assert_cmphex (hb_unicode_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
718   g_assert_cmphex (hb_unicode_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
719   g_assert_cmphex (hb_unicode_script (aa, '0'), ==, HB_SCRIPT_UNKNOWN);
720
721   g_assert (!f->data[0].freed && !f->data[1].freed);
722   hb_unicode_funcs_destroy (aa);
723   g_assert (f->data[0].freed && f->data[1].freed);
724 }
725
726
727 static hb_script_t
728 script_roundtrip_default (hb_script_t script)
729 {
730   return hb_script_from_iso15924_tag (hb_script_to_iso15924_tag (script));
731 }
732
733 #ifdef HAVE_GLIB
734 static hb_script_t
735 script_roundtrip_glib (hb_script_t script)
736 {
737   return hb_glib_script_to_script (hb_glib_script_from_script (script));
738 }
739 #endif
740
741 #ifdef HAVE_ICU
742 static hb_script_t
743 script_roundtrip_icu (hb_script_t script)
744 {
745   return hb_icu_script_to_script (hb_icu_script_from_script (script));
746 }
747 #endif
748
749 static void
750 test_unicode_script_roundtrip (gconstpointer user_data)
751 {
752   typedef hb_script_t (*roundtrip_func_t) (hb_script_t);
753   roundtrip_func_t roundtrip_func = (roundtrip_func_t) user_data;
754   unsigned int i;
755   gboolean failed = FALSE;
756
757   for (i = 0; i < G_N_ELEMENTS (script_tests); i++) {
758     const test_pair_t *test = &script_tests[i];
759     hb_script_t script = test->value;
760
761     g_test_message ("Test script roundtrip #%d: %x", i, script);
762     g_assert_cmphex (script, ==, roundtrip_func (script));
763   }
764   for (i = 0; i < G_N_ELEMENTS (script_tests_more); i++) {
765     const test_pair_t *test = &script_tests_more[i];
766     hb_script_t script = test->value;
767
768     g_test_message ("Test script roundtrip more #%d: %x", i, script);
769     if (script != roundtrip_func (script)) {
770       g_test_message ("Soft fail: Received %x, expected %x", roundtrip_func (script), script);
771       failed = TRUE;
772     }
773   }
774
775   g_assert_cmphex (HB_SCRIPT_INVALID, ==, roundtrip_func (HB_SCRIPT_INVALID));
776
777   if (failed)
778     g_test_message ("Some script roundtrip tests failed.  You probably have an old version of one of the libraries used.");
779 }
780
781
782 /* TODO test compose() and decompose() */
783 static void
784 test_unicode_normalization (gconstpointer user_data)
785 {
786   hb_unicode_funcs_t *uf = (hb_unicode_funcs_t *) user_data;
787   gunichar a, b, ab;
788
789
790   /* Test compose() */
791
792   /* Not composable */
793   g_assert (!hb_unicode_compose (uf, 0x0041, 0x0042, &ab) && ab == 0);
794   g_assert (!hb_unicode_compose (uf, 0x0041, 0, &ab) && ab == 0);
795   g_assert (!hb_unicode_compose (uf, 0x0066, 0x0069, &ab) && ab == 0);
796
797   /* Singletons should not compose */
798   g_assert (!hb_unicode_compose (uf, 0x212B, 0, &ab) && ab == 0);
799   g_assert (!hb_unicode_compose (uf, 0x00C5, 0, &ab) && ab == 0);
800   g_assert (!hb_unicode_compose (uf, 0x2126, 0, &ab) && ab == 0);
801   g_assert (!hb_unicode_compose (uf, 0x03A9, 0, &ab) && ab == 0);
802
803   /* Non-starter pairs should not compose */
804   g_assert (!hb_unicode_compose (uf, 0x0308, 0x0301, &ab) && ab == 0); /* !0x0344 */
805   g_assert (!hb_unicode_compose (uf, 0x0F71, 0x0F72, &ab) && ab == 0); /* !0x0F73 */
806
807   /* Pairs */
808   g_assert (hb_unicode_compose (uf, 0x0041, 0x030A, &ab) && ab == 0x00C5);
809   g_assert (hb_unicode_compose (uf, 0x006F, 0x0302, &ab) && ab == 0x00F4);
810   g_assert (hb_unicode_compose (uf, 0x1E63, 0x0307, &ab) && ab == 0x1E69);
811   g_assert (hb_unicode_compose (uf, 0x0073, 0x0323, &ab) && ab == 0x1E63);
812   g_assert (hb_unicode_compose (uf, 0x0064, 0x0307, &ab) && ab == 0x1E0B);
813   g_assert (hb_unicode_compose (uf, 0x0064, 0x0323, &ab) && ab == 0x1E0D);
814
815   /* Hangul */
816   g_assert (hb_unicode_compose (uf, 0xD4CC, 0x11B6, &ab) && ab == 0xD4DB);
817   g_assert (hb_unicode_compose (uf, 0x1111, 0x1171, &ab) && ab == 0xD4CC);
818   g_assert (hb_unicode_compose (uf, 0xCE20, 0x11B8, &ab) && ab == 0xCE31);
819   g_assert (hb_unicode_compose (uf, 0x110E, 0x1173, &ab) && ab == 0xCE20);
820
821
822   /* Test decompose() */
823
824   /* Not decomposable */
825   g_assert (!hb_unicode_decompose (uf, 0x0041, &a, &b) && a == 0x0041 && b == 0);
826   g_assert (!hb_unicode_decompose (uf, 0xFB01, &a, &b) && a == 0xFB01 && b == 0);
827
828   /* Singletons */
829   g_assert (hb_unicode_decompose (uf, 0x212B, &a, &b) && a == 0x00C5 && b == 0);
830   g_assert (hb_unicode_decompose (uf, 0x2126, &a, &b) && a == 0x03A9 && b == 0);
831
832   /* Non-starter pairs decompose, but not compose */
833   g_assert (hb_unicode_decompose (uf, 0x0344, &a, &b) && a == 0x0308 && b == 0x0301);
834   g_assert (hb_unicode_decompose (uf, 0x0F73, &a, &b) && a == 0x0F71 && b == 0x0F72);
835
836   /* Pairs */
837   g_assert (hb_unicode_decompose (uf, 0x00C5, &a, &b) && a == 0x0041 && b == 0x030A);
838   g_assert (hb_unicode_decompose (uf, 0x00F4, &a, &b) && a == 0x006F && b == 0x0302);
839   g_assert (hb_unicode_decompose (uf, 0x1E69, &a, &b) && a == 0x1E63 && b == 0x0307);
840   g_assert (hb_unicode_decompose (uf, 0x1E63, &a, &b) && a == 0x0073 && b == 0x0323);
841   g_assert (hb_unicode_decompose (uf, 0x1E0B, &a, &b) && a == 0x0064 && b == 0x0307);
842   g_assert (hb_unicode_decompose (uf, 0x1E0D, &a, &b) && a == 0x0064 && b == 0x0323);
843
844   /* Hangul */
845   g_assert (hb_unicode_decompose (uf, 0xD4DB, &a, &b) && a == 0xD4CC && b == 0x11B6);
846   g_assert (hb_unicode_decompose (uf, 0xD4CC, &a, &b) && a == 0x1111 && b == 0x1171);
847   g_assert (hb_unicode_decompose (uf, 0xCE31, &a, &b) && a == 0xCE20 && b == 0x11B8);
848   g_assert (hb_unicode_decompose (uf, 0xCE20, &a, &b) && a == 0x110E && b == 0x1173);
849
850 }
851
852
853
854 int
855 main (int argc, char **argv)
856 {
857   hb_test_init (&argc, &argv);
858
859   hb_test_add (test_unicode_properties_nil);
860   hb_test_add (test_unicode_properties_empty);
861
862   hb_test_add_data_flavor (hb_unicode_funcs_get_default (),          "default", test_unicode_properties);
863   hb_test_add_data_flavor (hb_unicode_funcs_get_default (),          "default", test_unicode_normalization);
864   hb_test_add_data_flavor ((gconstpointer) script_roundtrip_default, "default", test_unicode_script_roundtrip);
865 #ifdef HAVE_GLIB
866   hb_test_add_data_flavor (hb_glib_get_unicode_funcs (),             "glib",    test_unicode_properties);
867   hb_test_add_data_flavor (hb_glib_get_unicode_funcs (),             "glib",    test_unicode_normalization);
868   hb_test_add_data_flavor ((gconstpointer) script_roundtrip_glib,    "glib",    test_unicode_script_roundtrip);
869 #endif
870 #ifdef HAVE_ICU
871   hb_test_add_data_flavor (hb_icu_get_unicode_funcs (),              "icu",     test_unicode_properties);
872   hb_test_add_data_flavor (hb_icu_get_unicode_funcs (),              "icu",     test_unicode_normalization);
873   hb_test_add_data_flavor ((gconstpointer) script_roundtrip_icu,     "icu",     test_unicode_script_roundtrip);
874 #endif
875
876   hb_test_add (test_unicode_chainup);
877
878   hb_test_add (test_unicode_setters);
879
880   hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_nil);
881   hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_default);
882   hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_deep);
883
884   return hb_test_run ();
885 }