test-unicode.c 18.4 KB
Newer Older
1
/*
2 3
 * Copyright © 2011  Codethink Limited
 * Copyright © 2011  Google, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Codethink Author(s): Ryan Lortie
26
 * Google Author(s): Behdad Esfahbod
27 28 29 30
 */

#include "hb-test.h"

31 32
/* Unit tests for hb-unicode.h */

33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#ifdef HAVE_GLIB
#include <hb-glib.h>
#endif
#ifdef HAVE_ICU
#include <hb-icu.h>
#endif


/* Check all properties */

/* Some of the following tables where adapted from glib/glib/tests/utf8-misc.c.
 * The license is compatible. */

typedef struct {
  hb_codepoint_t unicode;
  unsigned int   value;
} test_pair_t;

static const test_pair_t combining_class_tests[] =
53
{
B
Minor  
Behdad Esfahbod 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  {   0x0020, 0 },
  {   0x0334, 1 },
  {   0x093C, 7 },
  {   0x3099, 8 },
  {   0x094D, 9 },
  {   0x05B0, 10 },
  {   0x05B1, 11 },
  {   0x05B2, 12 },
  {   0x05B3, 13 },
  {   0x05B4, 14 },
  {   0x05B5, 15 },
  {   0x05B6, 16 },
  {   0x05B7, 17 },
  {   0x05B8, 18 },
  {   0x05B9, 19 },
  {   0x05BB, 20 },
  {   0x05BC, 21 },
  {   0x05BD, 22 },
  {   0x05BF, 23 },
  {   0x05C1, 24 },
  {   0x05C2, 25 },
  {   0xFB1E, 26 },
  {   0x064B, 27 },
  {   0x064C, 28 },
  {   0x064D, 29 },
79
  /* ... */
B
Minor  
Behdad Esfahbod 已提交
80 81 82 83 84 85 86 87 88
  {   0x05AE, 228 },
  {   0x0300, 230 },
  {   0x302C, 232 },
  {   0x0362, 233 },
  {   0x0360, 234 },
  {   0x1DCD, 234 },
  {   0x0345, 240 },

  { 0x111111, 0 }
89 90 91 92
};
static const test_pair_t combining_class_tests_more[] =
{
  /* Unicode-5.2 character additions */
B
Minor  
Behdad Esfahbod 已提交
93
  {   0xA8E0, 230 },
94

95
  /* Unicode-6.0 character additions */
B
Minor  
Behdad Esfahbod 已提交
96 97 98
  {   0x135D, 230 },

  { 0x111111, 0 }
99
};
100

101 102 103
static const test_pair_t eastasian_width_tests[] =
{
  /* Neutral */
B
Minor  
Behdad Esfahbod 已提交
104 105 106 107 108 109
  {   0x0000, 1 },
  {   0x0483, 1 },
  {   0x0641, 1 },
  {   0xFFFC, 1 },
  {  0x10000, 1 },
  {  0xE0001, 1 },
110 111

  /* Narrow */
B
Minor  
Behdad Esfahbod 已提交
112 113 114
  {   0x0020, 1 },
  {   0x0041, 1 },
  {   0x27E6, 1 },
115 116

  /* Halfwidth */
B
Minor  
Behdad Esfahbod 已提交
117 118 119 120
  {   0x20A9, 1 },
  {   0xFF61, 1 },
  {   0xFF69, 1 },
  {   0xFFEE, 1 },
121 122

  /* Ambiguous */
B
Minor  
Behdad Esfahbod 已提交
123 124 125 126 127
  {   0x00A1, 1 },
  {   0x00D8, 1 },
  {   0x02DD, 1 },
  {  0xE0100, 1 },
  { 0x100000, 1 },
128 129

  /* Fullwidth */
B
Minor  
Behdad Esfahbod 已提交
130 131
  {   0x3000, 2 },
  {   0xFF60, 2 },
132 133

  /* Wide */
B
Minor  
Behdad Esfahbod 已提交
134 135 136 137 138 139 140
  {   0x2329, 2 },
  {   0x3001, 2 },
  {   0xFE69, 2 },
  {  0x30000, 2 },
  {  0x3FFFD, 2 },

  { 0x111111, 1 }
141 142 143 144
};
static const test_pair_t eastasian_width_tests_more[] =
{
  /* Default Wide blocks */
B
Minor  
Behdad Esfahbod 已提交
145 146 147 148 149 150 151
  {   0x4DBF, 2 },
  {   0x9FFF, 2 },
  {   0xFAFF, 2 },
  {  0x2A6DF, 2 },
  {  0x2B73F, 2 },
  {  0x2B81F, 2 },
  {  0x2FA1F, 2 },
152 153 154

  /* Uniode-5.2 character additions */
  /* Wide */
B
Minor  
Behdad Esfahbod 已提交
155
  {   0x115F, 2 },
156 157 158

  /* Uniode-6.0 character additions */
  /* Wide */
B
Minor  
Behdad Esfahbod 已提交
159 160 161 162
  {  0x2B740, 2 },
  {  0x1B000, 2 },

  { 0x111111, 1 }
163 164 165 166
};

static const test_pair_t general_category_tests[] =
{
B
Minor  
Behdad Esfahbod 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  {   0x000D, HB_UNICODE_GENERAL_CATEGORY_CONTROL },
  {   0x200E, HB_UNICODE_GENERAL_CATEGORY_FORMAT },
  {   0x0378, HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED },
  {   0xE000, HB_UNICODE_GENERAL_CATEGORY_PRIVATE_USE },
  {   0xD800, HB_UNICODE_GENERAL_CATEGORY_SURROGATE },
  {   0x0061, HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER },
  {   0x02B0, HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER },
  {   0x3400, HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER },
  {   0x01C5, HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER },
  {   0xFF21, HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER },
  {   0x0903, HB_UNICODE_GENERAL_CATEGORY_COMBINING_MARK },
  {   0x20DD, HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK },
  {   0xA806, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK },
  {   0xFF10, HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER },
  {   0x16EE, HB_UNICODE_GENERAL_CATEGORY_LETTER_NUMBER },
  {   0x17F0, HB_UNICODE_GENERAL_CATEGORY_OTHER_NUMBER },
  {   0x005F, HB_UNICODE_GENERAL_CATEGORY_CONNECT_PUNCTUATION },
  {   0x058A, HB_UNICODE_GENERAL_CATEGORY_DASH_PUNCTUATION },
  {   0x0F3B, HB_UNICODE_GENERAL_CATEGORY_CLOSE_PUNCTUATION },
  {   0x2019, HB_UNICODE_GENERAL_CATEGORY_FINAL_PUNCTUATION },
  {   0x2018, HB_UNICODE_GENERAL_CATEGORY_INITIAL_PUNCTUATION },
  {   0x2016, HB_UNICODE_GENERAL_CATEGORY_OTHER_PUNCTUATION },
  {   0x0F3A, HB_UNICODE_GENERAL_CATEGORY_OPEN_PUNCTUATION },
  {   0x20A0, HB_UNICODE_GENERAL_CATEGORY_CURRENCY_SYMBOL },
  {   0x309B, HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL },
  {   0xFB29, HB_UNICODE_GENERAL_CATEGORY_MATH_SYMBOL },
  {   0x00A6, HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL },
  {   0x2028, HB_UNICODE_GENERAL_CATEGORY_LINE_SEPARATOR },
  {   0x2029, HB_UNICODE_GENERAL_CATEGORY_PARAGRAPH_SEPARATOR },
  {   0x202F, HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR },

  { 0x111111, HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED }
199 200 201 202
};
static const test_pair_t general_category_tests_more[] =
{
  /* Unicode-5.2 character additions */
B
Minor  
Behdad Esfahbod 已提交
203
  {  0x1F131, HB_UNICODE_GENERAL_CATEGORY_OTHER_SYMBOL },
204

205
  /* Unicode-6.0 character additions */
B
Minor  
Behdad Esfahbod 已提交
206 207 208
  {   0x0620, HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER },

  { 0x111111, HB_UNICODE_GENERAL_CATEGORY_UNASSIGNED }
209 210 211 212 213
};

static const test_pair_t mirroring_tests[] =
{
  /* Some characters that do NOT mirror */
B
Minor  
Behdad Esfahbod 已提交
214 215 216 217 218 219 220
  {   0x0020, 0x0020 },
  {   0x0041, 0x0041 },
  {   0x00F0, 0x00F0 },
  {   0x27CC, 0x27CC },
  {  0xE01EF, 0xE01EF },
  {  0x1D7C3, 0x1D7C3 },
  { 0x100000, 0x100000 },
221 222

  /* Some characters that do mirror */
B
Minor  
Behdad Esfahbod 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  {   0x0029, 0x0028 },
  {   0x0028, 0x0029 },
  {   0x003E, 0x003C },
  {   0x003C, 0x003E },
  {   0x005D, 0x005B },
  {   0x005B, 0x005D },
  {   0x007D, 0x007B },
  {   0x007B, 0x007D },
  {   0x00BB, 0x00AB },
  {   0x00AB, 0x00BB },
  {   0x226B, 0x226A },
  {   0x226A, 0x226B },
  {   0x22F1, 0x22F0 },
  {   0x22F0, 0x22F1 },
  {   0xFF60, 0xFF5F },
  {   0xFF5F, 0xFF60 },
  {   0xFF63, 0xFF62 },
  {   0xFF62, 0xFF63 },

  { 0x111111, 0x111111 },
243 244 245 246
};
static const test_pair_t mirroring_tests_more[] =
{
  /* No new mirroring characters have been encoded in recent Unicode versions. */
B
Minor  
Behdad Esfahbod 已提交
247
  { 0x111111, 0x111111 }
248 249 250 251
};

static const test_pair_t script_tests[] =
{
B
Minor  
Behdad Esfahbod 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  {   0x002A, HB_SCRIPT_COMMON },
  {   0x0670, HB_SCRIPT_INHERITED },
  {   0x060D, HB_SCRIPT_ARABIC },
  {   0x0559, HB_SCRIPT_ARMENIAN },
  {   0x09CD, HB_SCRIPT_BENGALI },
  {   0x31B6, HB_SCRIPT_BOPOMOFO },
  {   0x13A2, HB_SCRIPT_CHEROKEE },
  {   0x2CFD, HB_SCRIPT_COPTIC },
  {   0x0482, HB_SCRIPT_CYRILLIC },
  {  0x10401, HB_SCRIPT_DESERET },
  {   0x094D, HB_SCRIPT_DEVANAGARI },
  {   0x1258, HB_SCRIPT_ETHIOPIC },
  {   0x10FC, HB_SCRIPT_GEORGIAN },
  {  0x10341, HB_SCRIPT_GOTHIC },
  {   0x0375, HB_SCRIPT_GREEK },
  {   0x0A83, HB_SCRIPT_GUJARATI },
  {   0x0A3C, HB_SCRIPT_GURMUKHI },
  {   0x3005, HB_SCRIPT_HAN },
  {   0x1100, HB_SCRIPT_HANGUL },
  {   0x05BF, HB_SCRIPT_HEBREW },
  {   0x309F, HB_SCRIPT_HIRAGANA },
  {   0x0CBC, HB_SCRIPT_KANNADA },
  {   0x30FF, HB_SCRIPT_KATAKANA },
  {   0x17DD, HB_SCRIPT_KHMER },
  {   0x0EDD, HB_SCRIPT_LAO },
  {   0x0061, HB_SCRIPT_LATIN },
  {   0x0D3D, HB_SCRIPT_MALAYALAM },
  {   0x1843, HB_SCRIPT_MONGOLIAN },
  {   0x1031, HB_SCRIPT_MYANMAR },
  {   0x169C, HB_SCRIPT_OGHAM },
  {  0x10322, HB_SCRIPT_OLD_ITALIC },
  {   0x0B3C, HB_SCRIPT_ORIYA },
  {   0x16EF, HB_SCRIPT_RUNIC },
  {   0x0DBD, HB_SCRIPT_SINHALA },
  {   0x0711, HB_SCRIPT_SYRIAC },
  {   0x0B82, HB_SCRIPT_TAMIL },
  {   0x0C03, HB_SCRIPT_TELUGU },
  {   0x07B1, HB_SCRIPT_THAANA },
  {   0x0E31, HB_SCRIPT_THAI },
  {   0x0FD4, HB_SCRIPT_TIBETAN },
  {   0x1401, HB_SCRIPT_CANADIAN_ABORIGINAL },
  {   0xA015, HB_SCRIPT_YI },
  {   0x1700, HB_SCRIPT_TAGALOG },
  {   0x1720, HB_SCRIPT_HANUNOO },
  {   0x1740, HB_SCRIPT_BUHID },
  {   0x1760, HB_SCRIPT_TAGBANWA },
298 299

  /* Unicode-4.0 additions */
B
Minor  
Behdad Esfahbod 已提交
300 301 302 303 304 305 306 307
  {   0x2800, HB_SCRIPT_BRAILLE },
  {  0x10808, HB_SCRIPT_CYPRIOT },
  {   0x1932, HB_SCRIPT_LIMBU },
  {  0x10480, HB_SCRIPT_OSMANYA },
  {  0x10450, HB_SCRIPT_SHAVIAN },
  {  0x10000, HB_SCRIPT_LINEAR_B },
  {   0x1950, HB_SCRIPT_TAI_LE },
  {  0x1039F, HB_SCRIPT_UGARITIC },
308 309

  /* Unicode-4.1 additions */
B
Minor  
Behdad Esfahbod 已提交
310 311 312 313 314 315 316
  {   0x1980, HB_SCRIPT_NEW_TAI_LUE },
  {   0x1A1F, HB_SCRIPT_BUGINESE },
  {   0x2C00, HB_SCRIPT_GLAGOLITIC },
  {   0x2D6F, HB_SCRIPT_TIFINAGH },
  {   0xA800, HB_SCRIPT_SYLOTI_NAGRI },
  {  0x103D0, HB_SCRIPT_OLD_PERSIAN },
  {  0x10A3F, HB_SCRIPT_KHAROSHTHI },
317 318

  /* Unicode-5.0 additions */
B
Minor  
Behdad Esfahbod 已提交
319 320 321 322 323 324
  {   0x0378, HB_SCRIPT_UNKNOWN },
  {   0x1B04, HB_SCRIPT_BALINESE },
  {  0x12000, HB_SCRIPT_CUNEIFORM },
  {  0x10900, HB_SCRIPT_PHOENICIAN },
  {   0xA840, HB_SCRIPT_PHAGS_PA },
  {   0x07C0, HB_SCRIPT_NKO },
325 326

  /* Unicode-5.1 additions */
B
Minor  
Behdad Esfahbod 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339
  {   0xA900, HB_SCRIPT_KAYAH_LI },
  {   0x1C00, HB_SCRIPT_LEPCHA },
  {   0xA930, HB_SCRIPT_REJANG },
  {   0x1B80, HB_SCRIPT_SUNDANESE },
  {   0xA880, HB_SCRIPT_SAURASHTRA },
  {   0xAA00, HB_SCRIPT_CHAM },
  {   0x1C50, HB_SCRIPT_OL_CHIKI },
  {   0xA500, HB_SCRIPT_VAI },
  {  0x102A0, HB_SCRIPT_CARIAN },
  {  0x10280, HB_SCRIPT_LYCIAN },
  {  0x1093F, HB_SCRIPT_LYDIAN },

  { 0x111111, HB_SCRIPT_UNKNOWN }
340 341 342 343
};
static const test_pair_t script_tests_more[] =
{
  /* Unicode-5.2 additions */
B
Minor  
Behdad Esfahbod 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  {  0x10B00, HB_SCRIPT_AVESTAN },
  {   0xA6A0, HB_SCRIPT_BAMUM },
  {  0x13000, HB_SCRIPT_EGYPTIAN_HIEROGLYPHS },
  {  0x10840, HB_SCRIPT_IMPERIAL_ARAMAIC },
  {  0x10B60, HB_SCRIPT_INSCRIPTIONAL_PAHLAVI },
  {  0x10B40, HB_SCRIPT_INSCRIPTIONAL_PARTHIAN },
  {   0xA980, HB_SCRIPT_JAVANESE },
  {  0x11082, HB_SCRIPT_KAITHI },
  {   0xA4D0, HB_SCRIPT_LISU },
  {   0xABE5, HB_SCRIPT_MEETEI_MAYEK },
  {  0x10A60, HB_SCRIPT_OLD_SOUTH_ARABIAN },
  {  0x10C00, HB_SCRIPT_OLD_TURKIC },
  {   0x0800, HB_SCRIPT_SAMARITAN },
  {   0x1A20, HB_SCRIPT_TAI_THAM },
  {   0xAA80, HB_SCRIPT_TAI_VIET },
359 360

  /* Unicode-6.0 additions */
B
Minor  
Behdad Esfahbod 已提交
361 362 363
  {   0x1BC0, HB_SCRIPT_BATAK },
  {  0x11000, HB_SCRIPT_BRAHMI },
  {   0x0840, HB_SCRIPT_MANDAIC },
364 365

  /* Unicode-5.2 character additions */
B
Minor  
Behdad Esfahbod 已提交
366 367 368 369
  {   0x1CED, HB_SCRIPT_INHERITED },
  {   0x1400, HB_SCRIPT_CANADIAN_ABORIGINAL },

  { 0x111111, HB_SCRIPT_UNKNOWN }
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
};


typedef unsigned int (*get_func_t)         (hb_unicode_funcs_t *ufuncs,
					    hb_codepoint_t      unicode,
					    void               *user_data);
typedef unsigned int (*func_setter_func_t) (hb_unicode_funcs_t *ufuncs,
					    get_func_t         *func,
					    void               *user_data,
					    hb_destroy_func_t   destroy);
typedef unsigned int (*getter_func_t)      (hb_unicode_funcs_t *ufuncs,
					    hb_codepoint_t      unicode);

typedef struct {
  const char         *name;
  func_setter_func_t  func_setter;
  getter_func_t       getter;
  const test_pair_t  *tests;
  unsigned int        num_tests;
  const test_pair_t  *tests_more;
  unsigned int        num_tests_more;
  unsigned int        default_value;
} property_t;

#define RETURNS_UNICODE_ITSELF ((unsigned int) -1)

#define PROPERTY(name, DEFAULT) \
  { \
    #name, \
    (func_setter_func_t) hb_unicode_funcs_set_##name##_func, \
    (getter_func_t) hb_unicode_get_##name, \
    name##_tests, \
    G_N_ELEMENTS (name##_tests), \
    name##_tests_more, \
    G_N_ELEMENTS (name##_tests_more), \
    DEFAULT \
  }
static const property_t properties[] =
{
  PROPERTY (combining_class, 0),
  PROPERTY (eastasian_width, 1),
  PROPERTY (general_category, (unsigned int) HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER),
  PROPERTY (mirroring, RETURNS_UNICODE_ITSELF),
  PROPERTY (script, (unsigned int) HB_SCRIPT_UNKNOWN),
};
#undef PROPERTY
416 417

static void
418
test_unicode_properties (gconstpointer user_data)
419
{
420 421
  hb_unicode_funcs_t *uf = (hb_unicode_funcs_t *) user_data;
  unsigned int i, j;
422

423 424
  g_assert (hb_unicode_funcs_is_immutable (uf));

425 426 427 428 429 430 431 432 433 434 435 436 437 438
  for (i = 0; i < G_N_ELEMENTS (properties); i++) {
    const property_t *p = &properties[i];
    const test_pair_t *tests;

    tests = p->tests;
    for (j = 0; j < p->num_tests; j++)
      g_assert_cmpint (p->getter (uf, tests[j].unicode), ==, tests[j].value);
  }
}

static hb_codepoint_t
default_value (hb_codepoint_t default_value, hb_codepoint_t unicode)
{
  return default_value == RETURNS_UNICODE_ITSELF ?  unicode : default_value;
439 440
}

441
static void
442
test_unicode_properties_nil (void)
443
{
444 445
  hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);
  unsigned int i, j;
446

447
  g_assert (!hb_unicode_funcs_is_immutable (uf));
448

449 450 451
  for (i = 0; i < G_N_ELEMENTS (properties); i++) {
    const property_t *p = &properties[i];
    const test_pair_t *tests;
452

453 454 455 456 457 458 459
    tests = p->tests;
    for (j = 0; j < p->num_tests; j++)
      g_assert_cmpint (p->getter (uf, tests[j].unicode), ==, default_value (p->default_value, tests[j].unicode));
  }

  hb_unicode_funcs_destroy (uf);
}
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

#define MAGIC0 0x12345678
#define MAGIC1 0x76543210

typedef struct {
  int value;
  gboolean freed;
} data_t;

typedef struct {
  data_t data[2];
} data_fixture_t;
static void
data_fixture_init (data_fixture_t *f, gconstpointer user_data)
{
  f->data[0].value = MAGIC0;
  f->data[1].value = MAGIC1;
}
static void
data_fixture_finish (data_fixture_t *f, gconstpointer user_data)
480
{
481 482 483 484 485 486 487 488 489
}

static void free_up (void *p)
{
  data_t *data = (data_t *) p;

  g_assert (data->value == MAGIC0 || data->value == MAGIC1);
  g_assert (data->freed == FALSE);
  data->freed = TRUE;
490 491 492 493 494 495 496
}

static hb_script_t
simple_get_script (hb_unicode_funcs_t *ufuncs,
                   hb_codepoint_t      codepoint,
                   void               *user_data)
{
497 498
  data_t *data = (data_t *) user_data;

499
  g_assert (hb_unicode_funcs_get_parent (ufuncs) == NULL);
500 501
  g_assert (data->value == MAGIC0);
  g_assert (data->freed == FALSE);
502 503 504 505 506 507 508 509 510 511 512 513

  if ('a' <= codepoint && codepoint <= 'z')
    return HB_SCRIPT_LATIN;
  else
    return HB_SCRIPT_UNKNOWN;
}

static hb_script_t
a_is_for_arabic_get_script (hb_unicode_funcs_t *ufuncs,
                            hb_codepoint_t      codepoint,
                            void               *user_data)
{
514 515
  data_t *data = (data_t *) user_data;

516
  g_assert (hb_unicode_funcs_get_parent (ufuncs) != NULL);
517 518
  g_assert (data->value == MAGIC1);
  g_assert (data->freed == FALSE);
519 520 521 522 523 524 525 526 527 528

  if (codepoint == 'a') {
    return HB_SCRIPT_ARABIC;
  } else {
    hb_unicode_funcs_t *parent = hb_unicode_funcs_get_parent (ufuncs);

    return hb_unicode_get_script (parent, codepoint);
  }
}

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
static void
test_unicode_custom (data_fixture_t *f, gconstpointer user_data)
{
  hb_unicode_funcs_t *uf = hb_unicode_funcs_create (NULL);

  hb_unicode_funcs_set_script_func (uf, simple_get_script,
                                    &f->data[0], free_up);

  g_assert_cmpint (hb_unicode_get_script (uf, 'a'), ==, HB_SCRIPT_LATIN);
  g_assert_cmpint (hb_unicode_get_script (uf, '0'), ==, HB_SCRIPT_UNKNOWN);

  g_assert (!hb_unicode_funcs_is_immutable (uf));
  hb_unicode_funcs_make_immutable (uf);
  g_assert (hb_unicode_funcs_is_immutable (uf));

  /* Since uf is immutable now, the following setter should do nothing. */
  hb_unicode_funcs_set_script_func (uf, a_is_for_arabic_get_script,
                                    &f->data[1], free_up);

  g_assert (!f->data[0].freed && !f->data[1].freed);
  hb_unicode_funcs_destroy (uf);
  g_assert (f->data[0].freed && !f->data[1].freed);
}

553
static void
554
test_unicode_subclassing_nil (data_fixture_t *f, gconstpointer user_data)
555
{
556 557 558 559 560 561
  hb_unicode_funcs_t *uf, *aa;

  uf = hb_unicode_funcs_create (NULL);

  aa = hb_unicode_funcs_create (uf);

562 563 564
  hb_unicode_funcs_destroy (uf);

  hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
565
                                    &f->data[1], free_up);
566 567 568 569

  g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
  g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_UNKNOWN);

570
  g_assert (!f->data[0].freed && !f->data[1].freed);
571
  hb_unicode_funcs_destroy (aa);
572
  g_assert (!f->data[0].freed && f->data[1].freed);
573 574 575
}

static void
576
test_unicode_subclassing_default (data_fixture_t *f, gconstpointer user_data)
577
{
578 579
  hb_unicode_funcs_t *uf, *aa;

580
  uf = hb_unicode_funcs_get_default ();
581
  aa = hb_unicode_funcs_create (uf);
582 583

  hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
584
                                    &f->data[1], free_up);
585 586 587 588

  g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
  g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);

589
  g_assert (!f->data[0].freed && !f->data[1].freed);
590
  hb_unicode_funcs_destroy (aa);
591
  g_assert (!f->data[0].freed && f->data[1].freed);
592 593 594
}

static void
595
test_unicode_subclassing_deep (data_fixture_t *f, gconstpointer user_data)
596
{
597 598 599
  hb_unicode_funcs_t *uf, *aa;

  uf = hb_unicode_funcs_create (NULL);
600 601

  hb_unicode_funcs_set_script_func (uf, simple_get_script,
602
                                    &f->data[0], free_up);
603

604
  aa = hb_unicode_funcs_create (uf);
605 606 607 608

  hb_unicode_funcs_destroy (uf);

  /* make sure the 'uf' didn't get freed, since 'aa' holds a ref */
609
  g_assert (!f->data[0].freed);
610 611

  hb_unicode_funcs_set_script_func (aa, a_is_for_arabic_get_script,
612
                                    &f->data[1], free_up);
613 614 615 616 617

  g_assert_cmpint (hb_unicode_get_script (aa, 'a'), ==, HB_SCRIPT_ARABIC);
  g_assert_cmpint (hb_unicode_get_script (aa, 'b'), ==, HB_SCRIPT_LATIN);
  g_assert_cmpint (hb_unicode_get_script (aa, '0'), ==, HB_SCRIPT_UNKNOWN);

618
  g_assert (!f->data[0].freed && !f->data[1].freed);
619
  hb_unicode_funcs_destroy (aa);
620
  g_assert (f->data[0].freed && f->data[1].freed);
621 622
}

623

624 625 626
int
main (int argc, char **argv)
{
B
Behdad Esfahbod 已提交
627
  hb_test_init (&argc, &argv);
628

629 630 631 632 633 634 635 636 637
  hb_test_add (test_unicode_properties_nil);

  hb_test_add_data_flavor (hb_unicode_funcs_get_default (), "default", test_unicode_properties);
#ifdef HAVE_GLIB
  hb_test_add_data_flavor (hb_glib_get_unicode_funcs (),    "glib",    test_unicode_properties);
#endif
#ifdef HAVE_ICU
  hb_test_add_data_flavor (hb_icu_get_unicode_funcs (),     "icu",    test_unicode_properties);
#endif
638 639 640 641 642

  hb_test_add_fixture (data_fixture, NULL, test_unicode_custom);
  hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_nil);
  hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_default);
  hb_test_add_fixture (data_fixture, NULL, test_unicode_subclassing_deep);
643

644 645 646
  /* XXX test icu ufuncs */
  /* XXX test _more tests (warn?) */
  /* XXX test chainup */
B
Behdad Esfahbod 已提交
647 648
  /* XXX test glib & icu two-way script conversion */

B
Behdad Esfahbod 已提交
649
  return hb_test_run ();
650
}