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