test-buffer.c 23.7 KB
Newer Older
B
Behdad Esfahbod 已提交
1
/*
B
Behdad Esfahbod 已提交
2
 * Copyright © 2011  Google, Inc.
B
Behdad Esfahbod 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  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.
 *
 * Google Author(s): Behdad Esfahbod
 */

#include "hb-test.h"

29
/* Unit tests for hb-buffer.h */
B
Behdad Esfahbod 已提交
30 31 32 33 34 35


static const char utf8[10] = "ab\360\240\200\200defg";
static const uint16_t utf16[8] = {'a', 'b', 0xD840, 0xDC00, 'd', 'e', 'f', 'g'};
static const uint32_t utf32[7] = {'a', 'b', 0x20000, 'd', 'e', 'f', 'g'};

36

B
Behdad Esfahbod 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
typedef enum {
  BUFFER_EMPTY,
  BUFFER_ONE_BY_ONE,
  BUFFER_UTF32,
  BUFFER_UTF16,
  BUFFER_UTF8,
  BUFFER_NUM_TYPES,
} buffer_type_t;

static const char *buffer_names[] = {
  "empty",
  "one-by-one",
  "utf32",
  "utf16",
  "utf8"
};

typedef struct
{
B
Behdad Esfahbod 已提交
56
  hb_buffer_t *buffer;
57
} fixture_t;
B
Behdad Esfahbod 已提交
58 59

static void
60
fixture_init (fixture_t *fixture, gconstpointer user_data)
B
Behdad Esfahbod 已提交
61
{
B
Behdad Esfahbod 已提交
62
  hb_buffer_t *b;
B
Behdad Esfahbod 已提交
63 64
  unsigned int i;

65
  b = fixture->buffer = hb_buffer_create ();
B
Behdad Esfahbod 已提交
66

B
Behdad Esfahbod 已提交
67 68
  switch (GPOINTER_TO_INT (user_data))
  {
B
Behdad Esfahbod 已提交
69 70 71 72 73
    case BUFFER_EMPTY:
      break;

    case BUFFER_ONE_BY_ONE:
      for (i = 1; i < G_N_ELEMENTS (utf32) - 1; i++)
B
Behdad Esfahbod 已提交
74
	hb_buffer_add (b, utf32[i], 1, i);
B
Behdad Esfahbod 已提交
75 76 77
      break;

    case BUFFER_UTF32:
B
Behdad Esfahbod 已提交
78
      hb_buffer_add_utf32 (b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
B
Behdad Esfahbod 已提交
79 80 81
      break;

    case BUFFER_UTF16:
B
Behdad Esfahbod 已提交
82
      hb_buffer_add_utf16 (b, utf16, G_N_ELEMENTS (utf16), 1, G_N_ELEMENTS (utf16) - 2);
B
Behdad Esfahbod 已提交
83 84 85
      break;

    case BUFFER_UTF8:
B
Behdad Esfahbod 已提交
86
      hb_buffer_add_utf8  (b, utf8,  G_N_ELEMENTS (utf8),  1, G_N_ELEMENTS (utf8)  - 2);
B
Behdad Esfahbod 已提交
87 88 89 90 91 92 93 94
      break;

    default:
      g_assert_not_reached ();
  }
}

static void
B
Behdad Esfahbod 已提交
95
fixture_finish (fixture_t *fixture, gconstpointer user_data)
B
Behdad Esfahbod 已提交
96
{
B
Behdad Esfahbod 已提交
97
  hb_buffer_destroy (fixture->buffer);
B
Behdad Esfahbod 已提交
98 99 100 101
}


static void
102
test_buffer_properties (fixture_t *fixture, gconstpointer user_data)
B
Behdad Esfahbod 已提交
103
{
B
Behdad Esfahbod 已提交
104
  hb_buffer_t *b = fixture->buffer;
105
  hb_unicode_funcs_t *ufuncs;
B
Behdad Esfahbod 已提交
106

107 108
  /* test default properties */

B
Behdad Esfahbod 已提交
109 110 111 112
  g_assert (hb_buffer_get_unicode_funcs (b) == hb_unicode_funcs_get_default ());
  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
  g_assert (hb_buffer_get_language (b) == NULL);
B
Behdad Esfahbod 已提交
113
  g_assert (hb_buffer_get_flags (b) == HB_BUFFER_FLAGS_DEFAULT);
B
Behdad Esfahbod 已提交
114

115 116 117

  /* test property changes are retained */
  ufuncs = hb_unicode_funcs_create (NULL);
B
Behdad Esfahbod 已提交
118
  hb_buffer_set_unicode_funcs (b, ufuncs);
119
  hb_unicode_funcs_destroy (ufuncs);
B
Behdad Esfahbod 已提交
120
  g_assert (hb_buffer_get_unicode_funcs (b) == ufuncs);
121

B
Behdad Esfahbod 已提交
122 123
  hb_buffer_set_direction (b, HB_DIRECTION_RTL);
  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_RTL);
B
Behdad Esfahbod 已提交
124

B
Behdad Esfahbod 已提交
125 126
  hb_buffer_set_script (b, HB_SCRIPT_ARABIC);
  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_ARABIC);
B
Behdad Esfahbod 已提交
127

128 129
  hb_buffer_set_language (b, hb_language_from_string ("fa", -1));
  g_assert (hb_buffer_get_language (b) == hb_language_from_string ("Fa", -1));
130

B
Behdad Esfahbod 已提交
131 132 133
  hb_buffer_set_flags (b, HB_BUFFER_FLAG_BOT);
  g_assert (hb_buffer_get_flags (b) == HB_BUFFER_FLAG_BOT);

134

B
Behdad Esfahbod 已提交
135 136 137 138 139 140 141 142 143

  /* test clear clears all properties but unicode_funcs */

  hb_buffer_clear (b);

  g_assert (hb_buffer_get_unicode_funcs (b) == ufuncs);
  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
  g_assert (hb_buffer_get_language (b) == NULL);
B
Behdad Esfahbod 已提交
144
  g_assert (hb_buffer_get_flags (b) == HB_BUFFER_FLAGS_DEFAULT);
B
Behdad Esfahbod 已提交
145 146 147 148 149 150 151 152 153 154 155 156


  /* test reset clears all properties */

  hb_buffer_set_direction (b, HB_DIRECTION_RTL);
  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_RTL);

  hb_buffer_set_script (b, HB_SCRIPT_ARABIC);
  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_ARABIC);

  hb_buffer_set_language (b, hb_language_from_string ("fa", -1));
  g_assert (hb_buffer_get_language (b) == hb_language_from_string ("Fa", -1));
157

B
Behdad Esfahbod 已提交
158 159 160
  hb_buffer_set_flags (b, HB_BUFFER_FLAG_BOT);
  g_assert (hb_buffer_get_flags (b) == HB_BUFFER_FLAG_BOT);

B
Behdad Esfahbod 已提交
161
  hb_buffer_reset (b);
162

B
Behdad Esfahbod 已提交
163 164 165 166
  g_assert (hb_buffer_get_unicode_funcs (b) == hb_unicode_funcs_get_default ());
  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
  g_assert (hb_buffer_get_language (b) == NULL);
B
Behdad Esfahbod 已提交
167
  g_assert (hb_buffer_get_flags (b) == HB_BUFFER_FLAGS_DEFAULT);
B
Behdad Esfahbod 已提交
168 169 170
}

static void
171
test_buffer_contents (fixture_t *fixture, gconstpointer user_data)
B
Behdad Esfahbod 已提交
172
{
B
Behdad Esfahbod 已提交
173
  hb_buffer_t *b = fixture->buffer;
174
  unsigned int i, len, len2;
B
Behdad Esfahbod 已提交
175 176 177 178
  buffer_type_t buffer_type = GPOINTER_TO_INT (user_data);
  hb_glyph_info_t *glyphs;

  if (buffer_type == BUFFER_EMPTY) {
B
Behdad Esfahbod 已提交
179
    g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
B
Behdad Esfahbod 已提交
180 181 182
    return;
  }

B
Behdad Esfahbod 已提交
183
  len = hb_buffer_get_length (b);
184
  hb_buffer_get_glyph_infos (b, NULL); /* test NULL */
B
Behdad Esfahbod 已提交
185
  glyphs = hb_buffer_get_glyph_infos (b, &len2);
186
  g_assert_cmpint (len, ==, len2);
B
Behdad Esfahbod 已提交
187 188
  g_assert_cmpint (len, ==, 5);

189 190 191 192 193 194
  for (i = 0; i < len; i++) {
    g_assert_cmphex (glyphs[i].mask,      ==, 1);
    g_assert_cmphex (glyphs[i].var1.u32,  ==, 0);
    g_assert_cmphex (glyphs[i].var2.u32,  ==, 0);
  }

B
Behdad Esfahbod 已提交
195 196 197 198 199 200 201 202 203 204 205 206
  for (i = 0; i < len; i++) {
    unsigned int cluster;
    cluster = 1+i;
    if (i >= 2) {
      if (buffer_type == BUFFER_UTF16)
	cluster++;
      else if (buffer_type == BUFFER_UTF8)
        cluster += 3;
    }
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
    g_assert_cmphex (glyphs[i].cluster,   ==, cluster);
  }
207 208 209

  /* reverse, test, and reverse back */

B
Behdad Esfahbod 已提交
210
  hb_buffer_reverse (b);
211 212 213
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);

B
Behdad Esfahbod 已提交
214
  hb_buffer_reverse (b);
215 216 217 218 219 220
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);

  /* reverse_clusters works same as reverse for now since each codepoint is
   * in its own cluster */

B
Behdad Esfahbod 已提交
221
  hb_buffer_reverse_clusters (b);
222 223 224
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);

B
Behdad Esfahbod 已提交
225
  hb_buffer_reverse_clusters (b);
226 227 228 229 230 231 232 233
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);

  /* now form a cluster and test again */
  glyphs[2].cluster = glyphs[1].cluster;

  /* reverse, test, and reverse back */

B
Behdad Esfahbod 已提交
234
  hb_buffer_reverse (b);
235 236 237
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);

B
Behdad Esfahbod 已提交
238
  hb_buffer_reverse (b);
239 240 241 242 243 244
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);

  /* reverse_clusters twice still should return the original string,
   * but when applied once, the 1-2 cluster should be retained. */

B
Behdad Esfahbod 已提交
245
  hb_buffer_reverse_clusters (b);
246 247 248 249 250 251 252 253 254
  for (i = 0; i < len; i++) {
    unsigned int j = len-1-i;
    if (j == 1)
      j = 2;
    else if (j == 2)
      j = 1;
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+j]);
  }

B
Behdad Esfahbod 已提交
255
  hb_buffer_reverse_clusters (b);
256 257
  for (i = 0; i < len; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
258 259 260 261 262


  /* test setting length */

  /* enlarge */
B
Behdad Esfahbod 已提交
263 264 265
  g_assert (hb_buffer_set_length (b, 10));
  glyphs = hb_buffer_get_glyph_infos (b, NULL);
  g_assert_cmpint (hb_buffer_get_length (b), ==, 10);
266 267 268 269 270
  for (i = 0; i < 5; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
  for (i = 5; i < 10; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, 0);
  /* shrink */
B
Behdad Esfahbod 已提交
271 272 273
  g_assert (hb_buffer_set_length (b, 3));
  glyphs = hb_buffer_get_glyph_infos (b, NULL);
  g_assert_cmpint (hb_buffer_get_length (b), ==, 3);
274 275 276 277
  for (i = 0; i < 3; i++)
    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);


B
Behdad Esfahbod 已提交
278
  g_assert (hb_buffer_allocation_successful (b));
279 280


281 282
  /* test reset clears content */

B
Behdad Esfahbod 已提交
283 284
  hb_buffer_reset (b);
  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
B
Behdad Esfahbod 已提交
285 286 287
}

static void
288
test_buffer_positions (fixture_t *fixture, gconstpointer user_data)
B
Behdad Esfahbod 已提交
289
{
B
Behdad Esfahbod 已提交
290
  hb_buffer_t *b = fixture->buffer;
291
  unsigned int i, len, len2;
B
Behdad Esfahbod 已提交
292 293 294
  hb_glyph_position_t *positions;

  /* Without shaping, positions should all be zero */
B
Behdad Esfahbod 已提交
295
  len = hb_buffer_get_length (b);
296
  hb_buffer_get_glyph_positions (b, NULL); /* test NULL */
B
Behdad Esfahbod 已提交
297
  positions = hb_buffer_get_glyph_positions (b, &len2);
298
  g_assert_cmpint (len, ==, len2);
B
Behdad Esfahbod 已提交
299 300 301 302 303 304 305
  for (i = 0; i < len; i++) {
    g_assert_cmpint (0, ==, positions[i].x_advance);
    g_assert_cmpint (0, ==, positions[i].y_advance);
    g_assert_cmpint (0, ==, positions[i].x_offset);
    g_assert_cmpint (0, ==, positions[i].y_offset);
    g_assert_cmpint (0, ==, positions[i].var.i32);
  }
306 307

  /* test reset clears content */
B
Behdad Esfahbod 已提交
308 309
  hb_buffer_reset (b);
  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
B
Behdad Esfahbod 已提交
310 311
}

312 313 314
static void
test_buffer_allocation (fixture_t *fixture, gconstpointer user_data)
{
B
Behdad Esfahbod 已提交
315 316 317
  hb_buffer_t *b = fixture->buffer;

  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
318

B
Behdad Esfahbod 已提交
319 320 321
  g_assert (hb_buffer_pre_allocate (b, 100));
  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
  g_assert (hb_buffer_allocation_successful (b));
322 323

  /* lets try a huge allocation, make sure it fails */
B
Behdad Esfahbod 已提交
324 325 326
  g_assert (!hb_buffer_pre_allocate (b, (unsigned int) -1));
  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
  g_assert (!hb_buffer_allocation_successful (b));
327 328

  /* small one again */
B
Behdad Esfahbod 已提交
329 330 331
  g_assert (hb_buffer_pre_allocate (b, 50));
  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
  g_assert (!hb_buffer_allocation_successful (b));
332

B
Behdad Esfahbod 已提交
333 334
  hb_buffer_reset (b);
  g_assert (hb_buffer_allocation_successful (b));
335 336

  /* all allocation and size  */
B
Behdad Esfahbod 已提交
337 338
  g_assert (!hb_buffer_pre_allocate (b, ((unsigned int) -1) / 20 + 1));
  g_assert (!hb_buffer_allocation_successful (b));
339

B
Behdad Esfahbod 已提交
340 341
  hb_buffer_reset (b);
  g_assert (hb_buffer_allocation_successful (b));
342 343

  /* technically, this one can actually pass on 64bit machines, but
344 345 346
   * I'm doubtful that any malloc allows 4GB allocations at a time.
   * But let's only enable it on a 32-bit machine. */
  if (sizeof (long) == 4) {
B
Behdad Esfahbod 已提交
347 348
    g_assert (!hb_buffer_pre_allocate (b, ((unsigned int) -1) / 20 - 1));
    g_assert (!hb_buffer_allocation_successful (b));
349
  }
350

B
Behdad Esfahbod 已提交
351 352
  hb_buffer_reset (b);
  g_assert (hb_buffer_allocation_successful (b));
353 354
}

355 356 357 358

typedef struct {
  const char utf8[8];
  const uint32_t codepoints[8];
B
Behdad Esfahbod 已提交
359
} utf8_conversion_test_t;
360 361

/* note: we skip the first and last byte when adding to buffer */
B
Behdad Esfahbod 已提交
362
static const utf8_conversion_test_t utf8_conversion_tests[] = {
363 364 365 366 367 368
  {"a\303\207", {-1}},
  {"a\303\207b", {0xC7}},
  {"ab\303cd", {'b', -1, 'c'}},
  {"ab\303\302\301cd", {'b', -1, -1, -1, 'c'}}
};

369
static void
B
Behdad Esfahbod 已提交
370
test_buffer_utf8_conversion (void)
371 372 373
{
  hb_buffer_t *b;
  hb_glyph_info_t *glyphs;
B
Behdad Esfahbod 已提交
374
  unsigned int bytes, chars, i, j, len;
375

376
  b = hb_buffer_create ();
377

B
Behdad Esfahbod 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  for (i = 0; i < G_N_ELEMENTS (utf8_conversion_tests); i++)
  {
    const utf8_conversion_test_t *test = &utf8_conversion_tests[i];
    char *escaped;

    escaped = g_strescape (test->utf8, NULL);
    g_test_message ("UTF-8 test #%d: %s", i, escaped);
    g_free (escaped);

    bytes = strlen (test->utf8);
    for (chars = 0; test->codepoints[chars]; chars++)
      ;

    hb_buffer_reset (b);
    hb_buffer_add_utf8 (b, test->utf8, bytes,  1, bytes - 2);

    glyphs = hb_buffer_get_glyph_infos (b, &len);
    g_assert_cmpint (len, ==, chars);
    for (j = 0; j < chars; j++)
      g_assert_cmphex (glyphs[j].codepoint, ==, test->codepoints[j]);
  }
399 400 401 402

  hb_buffer_destroy (b);
}

403

404 405 406 407 408

/* Following test table is adapted from glib/glib/tests/utf8-validate.c
 * with relicensing permission from Matthias Clasen. */

typedef struct {
B
Behdad Esfahbod 已提交
409
  const char *utf8;
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  int max_len;
  unsigned int offset;
  gboolean valid;
} utf8_validity_test_t;

static const utf8_validity_test_t utf8_validity_tests[] = {
  /* some tests to check max_len handling */
  /* length 1 */
  { "abcde", -1, 5, TRUE },
  { "abcde", 3, 3, TRUE },
  { "abcde", 5, 5, TRUE },
  /* length 2 */
  { "\xc2\xa9\xc2\xa9\xc2\xa9", -1, 6, TRUE },
  { "\xc2\xa9\xc2\xa9\xc2\xa9",  1, 0, FALSE },
  { "\xc2\xa9\xc2\xa9\xc2\xa9",  2, 2, TRUE },
  { "\xc2\xa9\xc2\xa9\xc2\xa9",  3, 2, FALSE },
  { "\xc2\xa9\xc2\xa9\xc2\xa9",  4, 4, TRUE },
  { "\xc2\xa9\xc2\xa9\xc2\xa9",  5, 4, FALSE },
  { "\xc2\xa9\xc2\xa9\xc2\xa9",  6, 6, TRUE },
  /* length 3 */
  { "\xe2\x89\xa0\xe2\x89\xa0", -1, 6, TRUE },
  { "\xe2\x89\xa0\xe2\x89\xa0",  1, 0, FALSE },
  { "\xe2\x89\xa0\xe2\x89\xa0",  2, 0, FALSE },
  { "\xe2\x89\xa0\xe2\x89\xa0",  3, 3, TRUE },
  { "\xe2\x89\xa0\xe2\x89\xa0",  4, 3, FALSE },
  { "\xe2\x89\xa0\xe2\x89\xa0",  5, 3, FALSE },
  { "\xe2\x89\xa0\xe2\x89\xa0",  6, 6, TRUE },

  /* examples from http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt */
  /* greek 'kosme' */
  { "\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5", -1, 11, TRUE },
  /* first sequence of each length */
  { "\x00", -1, 0, TRUE },
  { "\xc2\x80", -1, 2, TRUE },
  { "\xe0\xa0\x80", -1, 3, TRUE },
  { "\xf0\x90\x80\x80", -1, 4, TRUE },
  { "\xf8\x88\x80\x80\x80", -1, 0, FALSE },
  { "\xfc\x84\x80\x80\x80\x80", -1, 0, FALSE },
  /* last sequence of each length */
  { "\x7f", -1, 1, TRUE },
  { "\xdf\xbf", -1, 2, TRUE },
  { "\xef\xbf\xbf", -1, 0, TRUE },
  { "\xf7\xbf\xbf\xbf", -1, 0, TRUE },
  { "\xfb\xbf\xbf\xbf\xbf", -1, 0, FALSE },
  { "\xfd\xbf\xbf\xbf\xbf\xbf", -1, 0, FALSE },
  /* other boundary conditions */
  { "\xed\x9f\xbf", -1, 3, TRUE },
  { "\xee\x80\x80", -1, 3, TRUE },
  { "\xef\xbf\xbd", -1, 3, TRUE },
  { "\xf4\x8f\xbf\xbf", -1, 0, TRUE },
  /* malformed sequences */
  /* continuation bytes */
  { "\x80", -1, 0, FALSE },
  { "\xbf", -1, 0, FALSE },
  { "\x80\xbf", -1, 0, FALSE },
  { "\x80\xbf\x80", -1, 0, FALSE },
  { "\x80\xbf\x80\xbf", -1, 0, FALSE },
  { "\x80\xbf\x80\xbf\x80", -1, 0, FALSE },
  { "\x80\xbf\x80\xbf\x80\xbf", -1, 0, FALSE },
  { "\x80\xbf\x80\xbf\x80\xbf\x80", -1, 0, FALSE },

  /* all possible continuation byte */
  { "\x80", -1, 0, FALSE },
  { "\x81", -1, 0, FALSE },
  { "\x82", -1, 0, FALSE },
  { "\x83", -1, 0, FALSE },
  { "\x84", -1, 0, FALSE },
  { "\x85", -1, 0, FALSE },
  { "\x86", -1, 0, FALSE },
  { "\x87", -1, 0, FALSE },
  { "\x88", -1, 0, FALSE },
  { "\x89", -1, 0, FALSE },
  { "\x8a", -1, 0, FALSE },
  { "\x8b", -1, 0, FALSE },
  { "\x8c", -1, 0, FALSE },
  { "\x8d", -1, 0, FALSE },
  { "\x8e", -1, 0, FALSE },
  { "\x8f", -1, 0, FALSE },
  { "\x90", -1, 0, FALSE },
  { "\x91", -1, 0, FALSE },
  { "\x92", -1, 0, FALSE },
  { "\x93", -1, 0, FALSE },
  { "\x94", -1, 0, FALSE },
  { "\x95", -1, 0, FALSE },
  { "\x96", -1, 0, FALSE },
  { "\x97", -1, 0, FALSE },
  { "\x98", -1, 0, FALSE },
  { "\x99", -1, 0, FALSE },
  { "\x9a", -1, 0, FALSE },
  { "\x9b", -1, 0, FALSE },
  { "\x9c", -1, 0, FALSE },
  { "\x9d", -1, 0, FALSE },
  { "\x9e", -1, 0, FALSE },
  { "\x9f", -1, 0, FALSE },
  { "\xa0", -1, 0, FALSE },
  { "\xa1", -1, 0, FALSE },
  { "\xa2", -1, 0, FALSE },
  { "\xa3", -1, 0, FALSE },
  { "\xa4", -1, 0, FALSE },
  { "\xa5", -1, 0, FALSE },
  { "\xa6", -1, 0, FALSE },
  { "\xa7", -1, 0, FALSE },
  { "\xa8", -1, 0, FALSE },
  { "\xa9", -1, 0, FALSE },
  { "\xaa", -1, 0, FALSE },
  { "\xab", -1, 0, FALSE },
  { "\xac", -1, 0, FALSE },
  { "\xad", -1, 0, FALSE },
  { "\xae", -1, 0, FALSE },
  { "\xaf", -1, 0, FALSE },
  { "\xb0", -1, 0, FALSE },
  { "\xb1", -1, 0, FALSE },
  { "\xb2", -1, 0, FALSE },
  { "\xb3", -1, 0, FALSE },
  { "\xb4", -1, 0, FALSE },
  { "\xb5", -1, 0, FALSE },
  { "\xb6", -1, 0, FALSE },
  { "\xb7", -1, 0, FALSE },
  { "\xb8", -1, 0, FALSE },
  { "\xb9", -1, 0, FALSE },
  { "\xba", -1, 0, FALSE },
  { "\xbb", -1, 0, FALSE },
  { "\xbc", -1, 0, FALSE },
  { "\xbd", -1, 0, FALSE },
  { "\xbe", -1, 0, FALSE },
  { "\xbf", -1, 0, FALSE },
  /* lone start characters */
  { "\xc0\x20", -1, 0, FALSE },
  { "\xc1\x20", -1, 0, FALSE },
  { "\xc2\x20", -1, 0, FALSE },
  { "\xc3\x20", -1, 0, FALSE },
  { "\xc4\x20", -1, 0, FALSE },
  { "\xc5\x20", -1, 0, FALSE },
  { "\xc6\x20", -1, 0, FALSE },
  { "\xc7\x20", -1, 0, FALSE },
  { "\xc8\x20", -1, 0, FALSE },
  { "\xc9\x20", -1, 0, FALSE },
  { "\xca\x20", -1, 0, FALSE },
  { "\xcb\x20", -1, 0, FALSE },
  { "\xcc\x20", -1, 0, FALSE },
  { "\xcd\x20", -1, 0, FALSE },
  { "\xce\x20", -1, 0, FALSE },
  { "\xcf\x20", -1, 0, FALSE },
  { "\xd0\x20", -1, 0, FALSE },
  { "\xd1\x20", -1, 0, FALSE },
  { "\xd2\x20", -1, 0, FALSE },
  { "\xd3\x20", -1, 0, FALSE },
  { "\xd4\x20", -1, 0, FALSE },
  { "\xd5\x20", -1, 0, FALSE },
  { "\xd6\x20", -1, 0, FALSE },
  { "\xd7\x20", -1, 0, FALSE },
  { "\xd8\x20", -1, 0, FALSE },
  { "\xd9\x20", -1, 0, FALSE },
  { "\xda\x20", -1, 0, FALSE },
  { "\xdb\x20", -1, 0, FALSE },
  { "\xdc\x20", -1, 0, FALSE },
  { "\xdd\x20", -1, 0, FALSE },
  { "\xde\x20", -1, 0, FALSE },
  { "\xdf\x20", -1, 0, FALSE },
  { "\xe0\x20", -1, 0, FALSE },
  { "\xe1\x20", -1, 0, FALSE },
  { "\xe2\x20", -1, 0, FALSE },
  { "\xe3\x20", -1, 0, FALSE },
  { "\xe4\x20", -1, 0, FALSE },
  { "\xe5\x20", -1, 0, FALSE },
  { "\xe6\x20", -1, 0, FALSE },
  { "\xe7\x20", -1, 0, FALSE },
  { "\xe8\x20", -1, 0, FALSE },
  { "\xe9\x20", -1, 0, FALSE },
  { "\xea\x20", -1, 0, FALSE },
  { "\xeb\x20", -1, 0, FALSE },
  { "\xec\x20", -1, 0, FALSE },
  { "\xed\x20", -1, 0, FALSE },
  { "\xee\x20", -1, 0, FALSE },
  { "\xef\x20", -1, 0, FALSE },
  { "\xf0\x20", -1, 0, FALSE },
  { "\xf1\x20", -1, 0, FALSE },
  { "\xf2\x20", -1, 0, FALSE },
  { "\xf3\x20", -1, 0, FALSE },
  { "\xf4\x20", -1, 0, FALSE },
  { "\xf5\x20", -1, 0, FALSE },
  { "\xf6\x20", -1, 0, FALSE },
  { "\xf7\x20", -1, 0, FALSE },
  { "\xf8\x20", -1, 0, FALSE },
  { "\xf9\x20", -1, 0, FALSE },
  { "\xfa\x20", -1, 0, FALSE },
  { "\xfb\x20", -1, 0, FALSE },
  { "\xfc\x20", -1, 0, FALSE },
  { "\xfd\x20", -1, 0, FALSE },
  /* missing continuation bytes */
  { "\x20\xc0", -1, 1, FALSE },
  { "\x20\xe0\x80", -1, 1, FALSE },
  { "\x20\xf0\x80\x80", -1, 1, FALSE },
  { "\x20\xf8\x80\x80\x80", -1, 1, FALSE },
  { "\x20\xfc\x80\x80\x80\x80", -1, 1, FALSE },
  { "\x20\xdf", -1, 1, FALSE },
  { "\x20\xef\xbf", -1, 1, FALSE },
  { "\x20\xf7\xbf\xbf", -1, 1, FALSE },
  { "\x20\xfb\xbf\xbf\xbf", -1, 1, FALSE },
  { "\x20\xfd\xbf\xbf\xbf\xbf", -1, 1, FALSE },
  /* impossible bytes */
  { "\x20\xfe\x20", -1, 1, FALSE },
  { "\x20\xff\x20", -1, 1, FALSE },
#if 0
  /* XXX fix these, or document that we don't detect them? */
  /* overlong sequences */
  { "\x20\xc0\xaf\x20", -1, 1, FALSE },
  { "\x20\xe0\x80\xaf\x20", -1, 1, FALSE },
  { "\x20\xf0\x80\x80\xaf\x20", -1, 1, FALSE },
  { "\x20\xf8\x80\x80\x80\xaf\x20", -1, 1, FALSE },
  { "\x20\xfc\x80\x80\x80\x80\xaf\x20", -1, 1, FALSE },
  { "\x20\xc1\xbf\x20", -1, 1, FALSE },
  { "\x20\xe0\x9f\xbf\x20", -1, 1, FALSE },
  { "\x20\xf0\x8f\xbf\xbf\x20", -1, 1, FALSE },
  { "\x20\xf8\x87\xbf\xbf\xbf\x20", -1, 1, FALSE },
  { "\x20\xfc\x83\xbf\xbf\xbf\xbf\x20", -1, 1, FALSE },
  { "\x20\xc0\x80\x20", -1, 1, FALSE },
  { "\x20\xe0\x80\x80\x20", -1, 1, FALSE },
  { "\x20\xf0\x80\x80\x80\x20", -1, 1, FALSE },
  { "\x20\xf8\x80\x80\x80\x80\x20", -1, 1, FALSE },
  { "\x20\xfc\x80\x80\x80\x80\x80\x20", -1, 1, FALSE },
  /* illegal code positions */
  { "\x20\xed\xa0\x80\x20", -1, 1, FALSE },
  { "\x20\xed\xad\xbf\x20", -1, 1, FALSE },
  { "\x20\xed\xae\x80\x20", -1, 1, FALSE },
  { "\x20\xed\xaf\xbf\x20", -1, 1, FALSE },
  { "\x20\xed\xb0\x80\x20", -1, 1, FALSE },
  { "\x20\xed\xbe\x80\x20", -1, 1, FALSE },
  { "\x20\xed\xbf\xbf\x20", -1, 1, FALSE },
  { "\x20\xed\xa0\x80\xed\xb0\x80\x20", -1, 1, FALSE },
  { "\x20\xed\xa0\x80\xed\xbf\xbf\x20", -1, 1, FALSE },
  { "\x20\xed\xad\xbf\xed\xb0\x80\x20", -1, 1, FALSE },
  { "\x20\xed\xad\xbf\xed\xbf\xbf\x20", -1, 1, FALSE },
  { "\x20\xed\xae\x80\xed\xb0\x80\x20", -1, 1, FALSE },
  { "\x20\xed\xae\x80\xed\xbf\xbf\x20", -1, 1, FALSE },
  { "\x20\xed\xaf\xbf\xed\xb0\x80\x20", -1, 1, FALSE },
  { "\x20\xed\xaf\xbf\xed\xbf\xbf\x20", -1, 1, FALSE },
  { "\x20\xef\xbf\xbe\x20", -1, 1, FALSE },
  { "\x20\xef\xbf\xbf\x20", -1, 1, FALSE },
#endif
  { "", -1, 0, TRUE }
};

static void
B
Behdad Esfahbod 已提交
654
test_buffer_utf8_validity (void)
655 656
{
  hb_buffer_t *b;
B
Behdad Esfahbod 已提交
657
  unsigned int i;
658

659
  b = hb_buffer_create ();
660

B
Behdad Esfahbod 已提交
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
  for (i = 0; i < G_N_ELEMENTS (utf8_validity_tests); i++)
  {
    const utf8_validity_test_t *test = &utf8_validity_tests[i];
    unsigned int text_bytes, segment_bytes, j, len;
    hb_glyph_info_t *glyphs;
    char *escaped;

    escaped = g_strescape (test->utf8, NULL);
    g_test_message ("UTF-8 test #%d: %s", i, escaped);
    g_free (escaped);

    text_bytes = strlen (test->utf8);
    if (test->max_len == -1)
      segment_bytes = text_bytes;
    else
      segment_bytes = test->max_len;

    hb_buffer_reset (b);
    hb_buffer_add_utf8 (b, test->utf8, text_bytes,  0, segment_bytes);

    glyphs = hb_buffer_get_glyph_infos (b, &len);
    for (j = 0; j < len; j++)
      if (glyphs[j].codepoint == (hb_codepoint_t) -1)
	break;

    g_assert (test->valid ? j == len : j < len);
    if (!test->valid)
      g_assert (glyphs[j].cluster == test->offset);
  }
690 691 692 693 694

  hb_buffer_destroy (b);
}


B
Behdad Esfahbod 已提交
695 696 697
typedef struct {
  const uint16_t utf16[8];
  const uint32_t codepoints[8];
B
Behdad Esfahbod 已提交
698
} utf16_conversion_test_t;
B
Behdad Esfahbod 已提交
699 700

/* note: we skip the first and last item from utf16 when adding to buffer */
B
Behdad Esfahbod 已提交
701
static const utf16_conversion_test_t utf16_conversion_tests[] = {
B
Behdad Esfahbod 已提交
702 703 704 705 706 707 708 709 710
  {{0x41, 0x004D, 0x0430, 0x4E8C, 0xD800, 0xDF02, 0x61} , {0x004D, 0x0430, 0x4E8C, 0x10302}},
  {{0x41, 0xD800, 0xDF02, 0x61}, {0x10302}},
  {{0x41, 0xD800, 0xDF02}, {-1}},
  {{0x41, 0x61, 0xD800, 0xDF02}, {0x61, -1}},
  {{0x41, 0xD800, 0x61, 0xDF02}, {-1, 0x61}},
  {{0x41, 0x61}, {}}
};

static void
B
Behdad Esfahbod 已提交
711
test_buffer_utf16_conversion (void)
B
Behdad Esfahbod 已提交
712 713
{
  hb_buffer_t *b;
B
Behdad Esfahbod 已提交
714
  unsigned int i;
B
Behdad Esfahbod 已提交
715

716
  b = hb_buffer_create ();
B
Behdad Esfahbod 已提交
717

B
Behdad Esfahbod 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
  for (i = 0; i < G_N_ELEMENTS (utf16_conversion_tests); i++)
  {
    const utf16_conversion_test_t *test = &utf16_conversion_tests[i];
    unsigned int u_len, chars, j, len;
    hb_glyph_info_t *glyphs;

    g_test_message ("UTF-16 test #%d", i);

    for (u_len = 0; test->utf16[u_len]; u_len++)
      ;
    for (chars = 0; test->codepoints[chars]; chars++)
      ;

    hb_buffer_reset (b);
    hb_buffer_add_utf16 (b, test->utf16, u_len,  1, u_len - 2);

    glyphs = hb_buffer_get_glyph_infos (b, &len);
    g_assert_cmpint (len, ==, chars);
    for (j = 0; j < chars; j++)
      g_assert_cmphex (glyphs[j].codepoint, ==, test->codepoints[j]);
  }
B
Behdad Esfahbod 已提交
739 740 741 742

  hb_buffer_destroy (b);
}

743 744 745 746 747 748 749 750
static void
test_empty (hb_buffer_t *b)
{
  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
  g_assert (!hb_buffer_get_glyph_infos (b, NULL));
  g_assert (!hb_buffer_get_glyph_positions (b, NULL));
}

751 752 753
static void
test_buffer_empty (void)
{
754 755
  hb_buffer_t *b = hb_buffer_get_empty ();

756
  g_assert (hb_buffer_get_empty ());
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
  g_assert (hb_buffer_get_empty () == b);

  g_assert (!hb_buffer_allocation_successful (b));

  test_empty (b);

  hb_buffer_add_utf32 (b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);

  test_empty (b);

  hb_buffer_reverse (b);
  hb_buffer_reverse_clusters (b);

  g_assert (!hb_buffer_set_length (b, 10));

  test_empty (b);

  g_assert (hb_buffer_set_length (b, 0));

  test_empty (b);

  g_assert (!hb_buffer_allocation_successful (b));

  hb_buffer_reset (b);

  test_empty (b);

  g_assert (!hb_buffer_allocation_successful (b));
785
}
B
Behdad Esfahbod 已提交
786

B
Behdad Esfahbod 已提交
787 788 789
int
main (int argc, char **argv)
{
790
  unsigned int i;
B
Behdad Esfahbod 已提交
791

B
Behdad Esfahbod 已提交
792 793 794 795 796 797 798 799 800 801
  hb_test_init (&argc, &argv);

  for (i = 0; i < BUFFER_NUM_TYPES; i++)
  {
    const void *buffer_type = GINT_TO_POINTER (i);
    const char *buffer_name = buffer_names[i];

    hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_properties);
    hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_contents);
    hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_positions);
B
Behdad Esfahbod 已提交
802 803
  }

B
Behdad Esfahbod 已提交
804
  hb_test_add_fixture (fixture, GINT_TO_POINTER (BUFFER_EMPTY), test_buffer_allocation);
805

B
Behdad Esfahbod 已提交
806 807 808
  hb_test_add (test_buffer_utf8_conversion);
  hb_test_add (test_buffer_utf8_validity);
  hb_test_add (test_buffer_utf16_conversion);
809
  hb_test_add (test_buffer_empty);
B
Behdad Esfahbod 已提交
810

B
Behdad Esfahbod 已提交
811
  return hb_test_run();
B
Behdad Esfahbod 已提交
812
}