2 * Copyright © 2011 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
29 /* Unit tests for hb-buffer.h */
32 static const char utf8[10] = "ab\360\240\200\200defg";
33 static const uint16_t utf16[8] = {'a', 'b', 0xD840, 0xDC00, 'd', 'e', 'f', 'g'};
34 static const uint32_t utf32[7] = {'a', 'b', 0x20000, 'd', 'e', 'f', 'g'};
46 static const char *buffer_names[] = {
60 fixture_init (fixture_t *fixture, gconstpointer user_data)
65 b = fixture->buffer = hb_buffer_create ();
67 switch (GPOINTER_TO_INT (user_data))
72 case BUFFER_ONE_BY_ONE:
73 for (i = 1; i < G_N_ELEMENTS (utf32) - 1; i++)
74 hb_buffer_add (b, utf32[i], 1, i);
78 hb_buffer_add_utf32 (b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
82 hb_buffer_add_utf16 (b, utf16, G_N_ELEMENTS (utf16), 1, G_N_ELEMENTS (utf16) - 2);
86 hb_buffer_add_utf8 (b, utf8, G_N_ELEMENTS (utf8), 1, G_N_ELEMENTS (utf8) - 2);
90 g_assert_not_reached ();
95 fixture_finish (fixture_t *fixture, gconstpointer user_data)
97 hb_buffer_destroy (fixture->buffer);
102 test_buffer_properties (fixture_t *fixture, gconstpointer user_data)
104 hb_buffer_t *b = fixture->buffer;
105 hb_unicode_funcs_t *ufuncs;
107 /* test default properties */
109 g_assert (hb_buffer_get_unicode_funcs (b) == hb_unicode_funcs_get_default ());
110 g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
111 g_assert (hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
112 g_assert (hb_buffer_get_language (b) == NULL);
115 /* test property changes are retained */
116 ufuncs = hb_unicode_funcs_create (NULL);
117 hb_buffer_set_unicode_funcs (b, ufuncs);
118 hb_unicode_funcs_destroy (ufuncs);
119 g_assert (hb_buffer_get_unicode_funcs (b) == ufuncs);
121 hb_buffer_set_direction (b, HB_DIRECTION_RTL);
122 g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_RTL);
124 hb_buffer_set_script (b, HB_SCRIPT_ARABIC);
125 g_assert (hb_buffer_get_script (b) == HB_SCRIPT_ARABIC);
127 hb_buffer_set_language (b, hb_language_from_string ("fa", -1));
128 g_assert (hb_buffer_get_language (b) == hb_language_from_string ("Fa", -1));
131 /* test reset clears properties */
135 g_assert (hb_buffer_get_unicode_funcs (b) == hb_unicode_funcs_get_default ());
136 g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
137 g_assert (hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
138 g_assert (hb_buffer_get_language (b) == NULL);
142 test_buffer_contents (fixture_t *fixture, gconstpointer user_data)
144 hb_buffer_t *b = fixture->buffer;
145 unsigned int i, len, len2;
146 buffer_type_t buffer_type = GPOINTER_TO_INT (user_data);
147 hb_glyph_info_t *glyphs;
149 if (buffer_type == BUFFER_EMPTY) {
150 g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
154 len = hb_buffer_get_length (b);
155 glyphs = hb_buffer_get_glyph_infos (b, NULL); /* test NULL */
156 glyphs = hb_buffer_get_glyph_infos (b, &len2);
157 g_assert_cmpint (len, ==, len2);
158 g_assert_cmpint (len, ==, 5);
160 for (i = 0; i < len; i++) {
161 g_assert_cmphex (glyphs[i].mask, ==, 1);
162 g_assert_cmphex (glyphs[i].var1.u32, ==, 0);
163 g_assert_cmphex (glyphs[i].var2.u32, ==, 0);
166 for (i = 0; i < len; i++) {
167 unsigned int cluster;
170 if (buffer_type == BUFFER_UTF16)
172 else if (buffer_type == BUFFER_UTF8)
175 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
176 g_assert_cmphex (glyphs[i].cluster, ==, cluster);
179 /* reverse, test, and reverse back */
181 hb_buffer_reverse (b);
182 for (i = 0; i < len; i++)
183 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
185 hb_buffer_reverse (b);
186 for (i = 0; i < len; i++)
187 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
189 /* reverse_clusters works same as reverse for now since each codepoint is
190 * in its own cluster */
192 hb_buffer_reverse_clusters (b);
193 for (i = 0; i < len; i++)
194 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
196 hb_buffer_reverse_clusters (b);
197 for (i = 0; i < len; i++)
198 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
200 /* now form a cluster and test again */
201 glyphs[2].cluster = glyphs[1].cluster;
203 /* reverse, test, and reverse back */
205 hb_buffer_reverse (b);
206 for (i = 0; i < len; i++)
207 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
209 hb_buffer_reverse (b);
210 for (i = 0; i < len; i++)
211 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
213 /* reverse_clusters twice still should return the original string,
214 * but when applied once, the 1-2 cluster should be retained. */
216 hb_buffer_reverse_clusters (b);
217 for (i = 0; i < len; i++) {
218 unsigned int j = len-1-i;
223 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+j]);
226 hb_buffer_reverse_clusters (b);
227 for (i = 0; i < len; i++)
228 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
231 /* test setting length */
234 g_assert (hb_buffer_set_length (b, 10));
235 glyphs = hb_buffer_get_glyph_infos (b, NULL);
236 g_assert_cmpint (hb_buffer_get_length (b), ==, 10);
237 for (i = 0; i < 5; i++)
238 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
239 for (i = 5; i < 10; i++)
240 g_assert_cmphex (glyphs[i].codepoint, ==, 0);
242 g_assert (hb_buffer_set_length (b, 3));
243 glyphs = hb_buffer_get_glyph_infos (b, NULL);
244 g_assert_cmpint (hb_buffer_get_length (b), ==, 3);
245 for (i = 0; i < 3; i++)
246 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
249 g_assert (hb_buffer_allocation_successful (b));
252 /* test reset clears content */
255 g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
259 test_buffer_positions (fixture_t *fixture, gconstpointer user_data)
261 hb_buffer_t *b = fixture->buffer;
262 unsigned int i, len, len2;
263 hb_glyph_position_t *positions;
265 /* Without shaping, positions should all be zero */
266 len = hb_buffer_get_length (b);
267 positions = hb_buffer_get_glyph_positions (b, NULL); /* test NULL */
268 positions = hb_buffer_get_glyph_positions (b, &len2);
269 g_assert_cmpint (len, ==, len2);
270 for (i = 0; i < len; i++) {
271 g_assert_cmpint (0, ==, positions[i].x_advance);
272 g_assert_cmpint (0, ==, positions[i].y_advance);
273 g_assert_cmpint (0, ==, positions[i].x_offset);
274 g_assert_cmpint (0, ==, positions[i].y_offset);
275 g_assert_cmpint (0, ==, positions[i].var.i32);
278 /* test reset clears content */
280 g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
284 test_buffer_allocation (fixture_t *fixture, gconstpointer user_data)
286 hb_buffer_t *b = fixture->buffer;
288 g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
290 g_assert (hb_buffer_pre_allocate (b, 100));
291 g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
292 g_assert (hb_buffer_allocation_successful (b));
294 /* lets try a huge allocation, make sure it fails */
295 g_assert (!hb_buffer_pre_allocate (b, (unsigned int) -1));
296 g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
297 g_assert (!hb_buffer_allocation_successful (b));
299 /* small one again */
300 g_assert (hb_buffer_pre_allocate (b, 50));
301 g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
302 g_assert (!hb_buffer_allocation_successful (b));
305 g_assert (hb_buffer_allocation_successful (b));
307 /* all allocation and size */
308 g_assert (!hb_buffer_pre_allocate (b, ((unsigned int) -1) / 20 + 1));
309 g_assert (!hb_buffer_allocation_successful (b));
312 g_assert (hb_buffer_allocation_successful (b));
314 /* technically, this one can actually pass on 64bit machines, but
315 * I'm doubtful that any malloc allows 4GB allocations at a time.
316 * But let's only enable it on a 32-bit machine. */
317 if (sizeof (long) == 4) {
318 g_assert (!hb_buffer_pre_allocate (b, ((unsigned int) -1) / 20 - 1));
319 g_assert (!hb_buffer_allocation_successful (b));
323 g_assert (hb_buffer_allocation_successful (b));
329 const uint32_t codepoints[8];
330 } utf8_conversion_test_t;
332 /* note: we skip the first and last byte when adding to buffer */
333 static const utf8_conversion_test_t utf8_conversion_tests[] = {
335 {"a\303\207b", {0xC7}},
336 {"ab\303cd", {'b', -1, 'c'}},
337 {"ab\303\302\301cd", {'b', -1, -1, -1, 'c'}}
341 test_buffer_utf8_conversion (void)
344 hb_glyph_info_t *glyphs;
345 unsigned int bytes, chars, i, j, len;
347 b = hb_buffer_create ();
349 for (i = 0; i < G_N_ELEMENTS (utf8_conversion_tests); i++)
351 const utf8_conversion_test_t *test = &utf8_conversion_tests[i];
354 escaped = g_strescape (test->utf8, NULL);
355 g_test_message ("UTF-8 test #%d: %s", i, escaped);
358 bytes = strlen (test->utf8);
359 for (chars = 0; test->codepoints[chars]; chars++)
363 hb_buffer_add_utf8 (b, test->utf8, bytes, 1, bytes - 2);
365 glyphs = hb_buffer_get_glyph_infos (b, &len);
366 g_assert_cmpint (len, ==, chars);
367 for (j = 0; j < chars; j++)
368 g_assert_cmphex (glyphs[j].codepoint, ==, test->codepoints[j]);
371 hb_buffer_destroy (b);
376 /* Following test table is adapted from glib/glib/tests/utf8-validate.c
377 * with relicensing permission from Matthias Clasen. */
384 } utf8_validity_test_t;
386 static const utf8_validity_test_t utf8_validity_tests[] = {
387 /* some tests to check max_len handling */
389 { "abcde", -1, 5, TRUE },
390 { "abcde", 3, 3, TRUE },
391 { "abcde", 5, 5, TRUE },
393 { "\xc2\xa9\xc2\xa9\xc2\xa9", -1, 6, TRUE },
394 { "\xc2\xa9\xc2\xa9\xc2\xa9", 1, 0, FALSE },
395 { "\xc2\xa9\xc2\xa9\xc2\xa9", 2, 2, TRUE },
396 { "\xc2\xa9\xc2\xa9\xc2\xa9", 3, 2, FALSE },
397 { "\xc2\xa9\xc2\xa9\xc2\xa9", 4, 4, TRUE },
398 { "\xc2\xa9\xc2\xa9\xc2\xa9", 5, 4, FALSE },
399 { "\xc2\xa9\xc2\xa9\xc2\xa9", 6, 6, TRUE },
401 { "\xe2\x89\xa0\xe2\x89\xa0", -1, 6, TRUE },
402 { "\xe2\x89\xa0\xe2\x89\xa0", 1, 0, FALSE },
403 { "\xe2\x89\xa0\xe2\x89\xa0", 2, 0, FALSE },
404 { "\xe2\x89\xa0\xe2\x89\xa0", 3, 3, TRUE },
405 { "\xe2\x89\xa0\xe2\x89\xa0", 4, 3, FALSE },
406 { "\xe2\x89\xa0\xe2\x89\xa0", 5, 3, FALSE },
407 { "\xe2\x89\xa0\xe2\x89\xa0", 6, 6, TRUE },
409 /* examples from http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt */
411 { "\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5", -1, 11, TRUE },
412 /* first sequence of each length */
413 { "\x00", -1, 0, TRUE },
414 { "\xc2\x80", -1, 2, TRUE },
415 { "\xe0\xa0\x80", -1, 3, TRUE },
416 { "\xf0\x90\x80\x80", -1, 4, TRUE },
417 { "\xf8\x88\x80\x80\x80", -1, 0, FALSE },
418 { "\xfc\x84\x80\x80\x80\x80", -1, 0, FALSE },
419 /* last sequence of each length */
420 { "\x7f", -1, 1, TRUE },
421 { "\xdf\xbf", -1, 2, TRUE },
422 { "\xef\xbf\xbf", -1, 0, TRUE },
423 { "\xf7\xbf\xbf\xbf", -1, 0, TRUE },
424 { "\xfb\xbf\xbf\xbf\xbf", -1, 0, FALSE },
425 { "\xfd\xbf\xbf\xbf\xbf\xbf", -1, 0, FALSE },
426 /* other boundary conditions */
427 { "\xed\x9f\xbf", -1, 3, TRUE },
428 { "\xee\x80\x80", -1, 3, TRUE },
429 { "\xef\xbf\xbd", -1, 3, TRUE },
430 { "\xf4\x8f\xbf\xbf", -1, 0, TRUE },
431 /* malformed sequences */
432 /* continuation bytes */
433 { "\x80", -1, 0, FALSE },
434 { "\xbf", -1, 0, FALSE },
435 { "\x80\xbf", -1, 0, FALSE },
436 { "\x80\xbf\x80", -1, 0, FALSE },
437 { "\x80\xbf\x80\xbf", -1, 0, FALSE },
438 { "\x80\xbf\x80\xbf\x80", -1, 0, FALSE },
439 { "\x80\xbf\x80\xbf\x80\xbf", -1, 0, FALSE },
440 { "\x80\xbf\x80\xbf\x80\xbf\x80", -1, 0, FALSE },
442 /* all possible continuation byte */
443 { "\x80", -1, 0, FALSE },
444 { "\x81", -1, 0, FALSE },
445 { "\x82", -1, 0, FALSE },
446 { "\x83", -1, 0, FALSE },
447 { "\x84", -1, 0, FALSE },
448 { "\x85", -1, 0, FALSE },
449 { "\x86", -1, 0, FALSE },
450 { "\x87", -1, 0, FALSE },
451 { "\x88", -1, 0, FALSE },
452 { "\x89", -1, 0, FALSE },
453 { "\x8a", -1, 0, FALSE },
454 { "\x8b", -1, 0, FALSE },
455 { "\x8c", -1, 0, FALSE },
456 { "\x8d", -1, 0, FALSE },
457 { "\x8e", -1, 0, FALSE },
458 { "\x8f", -1, 0, FALSE },
459 { "\x90", -1, 0, FALSE },
460 { "\x91", -1, 0, FALSE },
461 { "\x92", -1, 0, FALSE },
462 { "\x93", -1, 0, FALSE },
463 { "\x94", -1, 0, FALSE },
464 { "\x95", -1, 0, FALSE },
465 { "\x96", -1, 0, FALSE },
466 { "\x97", -1, 0, FALSE },
467 { "\x98", -1, 0, FALSE },
468 { "\x99", -1, 0, FALSE },
469 { "\x9a", -1, 0, FALSE },
470 { "\x9b", -1, 0, FALSE },
471 { "\x9c", -1, 0, FALSE },
472 { "\x9d", -1, 0, FALSE },
473 { "\x9e", -1, 0, FALSE },
474 { "\x9f", -1, 0, FALSE },
475 { "\xa0", -1, 0, FALSE },
476 { "\xa1", -1, 0, FALSE },
477 { "\xa2", -1, 0, FALSE },
478 { "\xa3", -1, 0, FALSE },
479 { "\xa4", -1, 0, FALSE },
480 { "\xa5", -1, 0, FALSE },
481 { "\xa6", -1, 0, FALSE },
482 { "\xa7", -1, 0, FALSE },
483 { "\xa8", -1, 0, FALSE },
484 { "\xa9", -1, 0, FALSE },
485 { "\xaa", -1, 0, FALSE },
486 { "\xab", -1, 0, FALSE },
487 { "\xac", -1, 0, FALSE },
488 { "\xad", -1, 0, FALSE },
489 { "\xae", -1, 0, FALSE },
490 { "\xaf", -1, 0, FALSE },
491 { "\xb0", -1, 0, FALSE },
492 { "\xb1", -1, 0, FALSE },
493 { "\xb2", -1, 0, FALSE },
494 { "\xb3", -1, 0, FALSE },
495 { "\xb4", -1, 0, FALSE },
496 { "\xb5", -1, 0, FALSE },
497 { "\xb6", -1, 0, FALSE },
498 { "\xb7", -1, 0, FALSE },
499 { "\xb8", -1, 0, FALSE },
500 { "\xb9", -1, 0, FALSE },
501 { "\xba", -1, 0, FALSE },
502 { "\xbb", -1, 0, FALSE },
503 { "\xbc", -1, 0, FALSE },
504 { "\xbd", -1, 0, FALSE },
505 { "\xbe", -1, 0, FALSE },
506 { "\xbf", -1, 0, FALSE },
507 /* lone start characters */
508 { "\xc0\x20", -1, 0, FALSE },
509 { "\xc1\x20", -1, 0, FALSE },
510 { "\xc2\x20", -1, 0, FALSE },
511 { "\xc3\x20", -1, 0, FALSE },
512 { "\xc4\x20", -1, 0, FALSE },
513 { "\xc5\x20", -1, 0, FALSE },
514 { "\xc6\x20", -1, 0, FALSE },
515 { "\xc7\x20", -1, 0, FALSE },
516 { "\xc8\x20", -1, 0, FALSE },
517 { "\xc9\x20", -1, 0, FALSE },
518 { "\xca\x20", -1, 0, FALSE },
519 { "\xcb\x20", -1, 0, FALSE },
520 { "\xcc\x20", -1, 0, FALSE },
521 { "\xcd\x20", -1, 0, FALSE },
522 { "\xce\x20", -1, 0, FALSE },
523 { "\xcf\x20", -1, 0, FALSE },
524 { "\xd0\x20", -1, 0, FALSE },
525 { "\xd1\x20", -1, 0, FALSE },
526 { "\xd2\x20", -1, 0, FALSE },
527 { "\xd3\x20", -1, 0, FALSE },
528 { "\xd4\x20", -1, 0, FALSE },
529 { "\xd5\x20", -1, 0, FALSE },
530 { "\xd6\x20", -1, 0, FALSE },
531 { "\xd7\x20", -1, 0, FALSE },
532 { "\xd8\x20", -1, 0, FALSE },
533 { "\xd9\x20", -1, 0, FALSE },
534 { "\xda\x20", -1, 0, FALSE },
535 { "\xdb\x20", -1, 0, FALSE },
536 { "\xdc\x20", -1, 0, FALSE },
537 { "\xdd\x20", -1, 0, FALSE },
538 { "\xde\x20", -1, 0, FALSE },
539 { "\xdf\x20", -1, 0, FALSE },
540 { "\xe0\x20", -1, 0, FALSE },
541 { "\xe1\x20", -1, 0, FALSE },
542 { "\xe2\x20", -1, 0, FALSE },
543 { "\xe3\x20", -1, 0, FALSE },
544 { "\xe4\x20", -1, 0, FALSE },
545 { "\xe5\x20", -1, 0, FALSE },
546 { "\xe6\x20", -1, 0, FALSE },
547 { "\xe7\x20", -1, 0, FALSE },
548 { "\xe8\x20", -1, 0, FALSE },
549 { "\xe9\x20", -1, 0, FALSE },
550 { "\xea\x20", -1, 0, FALSE },
551 { "\xeb\x20", -1, 0, FALSE },
552 { "\xec\x20", -1, 0, FALSE },
553 { "\xed\x20", -1, 0, FALSE },
554 { "\xee\x20", -1, 0, FALSE },
555 { "\xef\x20", -1, 0, FALSE },
556 { "\xf0\x20", -1, 0, FALSE },
557 { "\xf1\x20", -1, 0, FALSE },
558 { "\xf2\x20", -1, 0, FALSE },
559 { "\xf3\x20", -1, 0, FALSE },
560 { "\xf4\x20", -1, 0, FALSE },
561 { "\xf5\x20", -1, 0, FALSE },
562 { "\xf6\x20", -1, 0, FALSE },
563 { "\xf7\x20", -1, 0, FALSE },
564 { "\xf8\x20", -1, 0, FALSE },
565 { "\xf9\x20", -1, 0, FALSE },
566 { "\xfa\x20", -1, 0, FALSE },
567 { "\xfb\x20", -1, 0, FALSE },
568 { "\xfc\x20", -1, 0, FALSE },
569 { "\xfd\x20", -1, 0, FALSE },
570 /* missing continuation bytes */
571 { "\x20\xc0", -1, 1, FALSE },
572 { "\x20\xe0\x80", -1, 1, FALSE },
573 { "\x20\xf0\x80\x80", -1, 1, FALSE },
574 { "\x20\xf8\x80\x80\x80", -1, 1, FALSE },
575 { "\x20\xfc\x80\x80\x80\x80", -1, 1, FALSE },
576 { "\x20\xdf", -1, 1, FALSE },
577 { "\x20\xef\xbf", -1, 1, FALSE },
578 { "\x20\xf7\xbf\xbf", -1, 1, FALSE },
579 { "\x20\xfb\xbf\xbf\xbf", -1, 1, FALSE },
580 { "\x20\xfd\xbf\xbf\xbf\xbf", -1, 1, FALSE },
581 /* impossible bytes */
582 { "\x20\xfe\x20", -1, 1, FALSE },
583 { "\x20\xff\x20", -1, 1, FALSE },
585 /* XXX fix these, or document that we don't detect them? */
586 /* overlong sequences */
587 { "\x20\xc0\xaf\x20", -1, 1, FALSE },
588 { "\x20\xe0\x80\xaf\x20", -1, 1, FALSE },
589 { "\x20\xf0\x80\x80\xaf\x20", -1, 1, FALSE },
590 { "\x20\xf8\x80\x80\x80\xaf\x20", -1, 1, FALSE },
591 { "\x20\xfc\x80\x80\x80\x80\xaf\x20", -1, 1, FALSE },
592 { "\x20\xc1\xbf\x20", -1, 1, FALSE },
593 { "\x20\xe0\x9f\xbf\x20", -1, 1, FALSE },
594 { "\x20\xf0\x8f\xbf\xbf\x20", -1, 1, FALSE },
595 { "\x20\xf8\x87\xbf\xbf\xbf\x20", -1, 1, FALSE },
596 { "\x20\xfc\x83\xbf\xbf\xbf\xbf\x20", -1, 1, FALSE },
597 { "\x20\xc0\x80\x20", -1, 1, FALSE },
598 { "\x20\xe0\x80\x80\x20", -1, 1, FALSE },
599 { "\x20\xf0\x80\x80\x80\x20", -1, 1, FALSE },
600 { "\x20\xf8\x80\x80\x80\x80\x20", -1, 1, FALSE },
601 { "\x20\xfc\x80\x80\x80\x80\x80\x20", -1, 1, FALSE },
602 /* illegal code positions */
603 { "\x20\xed\xa0\x80\x20", -1, 1, FALSE },
604 { "\x20\xed\xad\xbf\x20", -1, 1, FALSE },
605 { "\x20\xed\xae\x80\x20", -1, 1, FALSE },
606 { "\x20\xed\xaf\xbf\x20", -1, 1, FALSE },
607 { "\x20\xed\xb0\x80\x20", -1, 1, FALSE },
608 { "\x20\xed\xbe\x80\x20", -1, 1, FALSE },
609 { "\x20\xed\xbf\xbf\x20", -1, 1, FALSE },
610 { "\x20\xed\xa0\x80\xed\xb0\x80\x20", -1, 1, FALSE },
611 { "\x20\xed\xa0\x80\xed\xbf\xbf\x20", -1, 1, FALSE },
612 { "\x20\xed\xad\xbf\xed\xb0\x80\x20", -1, 1, FALSE },
613 { "\x20\xed\xad\xbf\xed\xbf\xbf\x20", -1, 1, FALSE },
614 { "\x20\xed\xae\x80\xed\xb0\x80\x20", -1, 1, FALSE },
615 { "\x20\xed\xae\x80\xed\xbf\xbf\x20", -1, 1, FALSE },
616 { "\x20\xed\xaf\xbf\xed\xb0\x80\x20", -1, 1, FALSE },
617 { "\x20\xed\xaf\xbf\xed\xbf\xbf\x20", -1, 1, FALSE },
618 { "\x20\xef\xbf\xbe\x20", -1, 1, FALSE },
619 { "\x20\xef\xbf\xbf\x20", -1, 1, FALSE },
625 test_buffer_utf8_validity (void)
630 b = hb_buffer_create ();
632 for (i = 0; i < G_N_ELEMENTS (utf8_validity_tests); i++)
634 const utf8_validity_test_t *test = &utf8_validity_tests[i];
635 unsigned int text_bytes, segment_bytes, j, len;
636 hb_glyph_info_t *glyphs;
639 escaped = g_strescape (test->utf8, NULL);
640 g_test_message ("UTF-8 test #%d: %s", i, escaped);
643 text_bytes = strlen (test->utf8);
644 if (test->max_len == -1)
645 segment_bytes = text_bytes;
647 segment_bytes = test->max_len;
650 hb_buffer_add_utf8 (b, test->utf8, text_bytes, 0, segment_bytes);
652 glyphs = hb_buffer_get_glyph_infos (b, &len);
653 for (j = 0; j < len; j++)
654 if (glyphs[j].codepoint == (hb_codepoint_t) -1)
657 g_assert (test->valid ? j == len : j < len);
659 g_assert (glyphs[j].cluster == test->offset);
662 hb_buffer_destroy (b);
667 const uint16_t utf16[8];
668 const uint32_t codepoints[8];
669 } utf16_conversion_test_t;
671 /* note: we skip the first and last item from utf16 when adding to buffer */
672 static const utf16_conversion_test_t utf16_conversion_tests[] = {
673 {{0x41, 0x004D, 0x0430, 0x4E8C, 0xD800, 0xDF02, 0x61} , {0x004D, 0x0430, 0x4E8C, 0x10302}},
674 {{0x41, 0xD800, 0xDF02, 0x61}, {0x10302}},
675 {{0x41, 0xD800, 0xDF02}, {-1}},
676 {{0x41, 0x61, 0xD800, 0xDF02}, {0x61, -1}},
677 {{0x41, 0xD800, 0x61, 0xDF02}, {-1, 0x61}},
682 test_buffer_utf16_conversion (void)
687 b = hb_buffer_create ();
689 for (i = 0; i < G_N_ELEMENTS (utf16_conversion_tests); i++)
691 const utf16_conversion_test_t *test = &utf16_conversion_tests[i];
692 unsigned int u_len, chars, j, len;
693 hb_glyph_info_t *glyphs;
695 g_test_message ("UTF-16 test #%d", i);
697 for (u_len = 0; test->utf16[u_len]; u_len++)
699 for (chars = 0; test->codepoints[chars]; chars++)
703 hb_buffer_add_utf16 (b, test->utf16, u_len, 1, u_len - 2);
705 glyphs = hb_buffer_get_glyph_infos (b, &len);
706 g_assert_cmpint (len, ==, chars);
707 for (j = 0; j < chars; j++)
708 g_assert_cmphex (glyphs[j].codepoint, ==, test->codepoints[j]);
711 hb_buffer_destroy (b);
715 test_empty (hb_buffer_t *b)
717 g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
718 g_assert (!hb_buffer_get_glyph_infos (b, NULL));
719 g_assert (!hb_buffer_get_glyph_positions (b, NULL));
723 test_buffer_empty (void)
725 hb_buffer_t *b = hb_buffer_get_empty ();
727 g_assert (hb_buffer_get_empty ());
728 g_assert (hb_buffer_get_empty () == b);
730 g_assert (!hb_buffer_allocation_successful (b));
734 hb_buffer_add_utf32 (b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
738 hb_buffer_reverse (b);
739 hb_buffer_reverse_clusters (b);
741 g_assert (!hb_buffer_set_length (b, 10));
745 g_assert (hb_buffer_set_length (b, 0));
749 g_assert (!hb_buffer_allocation_successful (b));
755 g_assert (!hb_buffer_allocation_successful (b));
759 main (int argc, char **argv)
763 hb_test_init (&argc, &argv);
765 for (i = 0; i < BUFFER_NUM_TYPES; i++)
767 const void *buffer_type = GINT_TO_POINTER (i);
768 const char *buffer_name = buffer_names[i];
770 hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_properties);
771 hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_contents);
772 hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_positions);
775 hb_test_add_fixture (fixture, GINT_TO_POINTER (BUFFER_EMPTY), test_buffer_allocation);
777 hb_test_add (test_buffer_utf8_conversion);
778 hb_test_add (test_buffer_utf8_validity);
779 hb_test_add (test_buffer_utf16_conversion);
780 hb_test_add (test_buffer_empty);
782 return hb_test_run();