test-buffer.c revision 82ecaff736e245e117d70b6ec1497508c6eb08d2
1/*
2 * Copyright © 2011  Google, Inc.
3 *
4 *  This is part of HarfBuzz, a text shaping library.
5 *
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.
11 *
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
16 * DAMAGE.
17 *
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.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
27#include "hb-test.h"
28
29/* Unit tests for hb-buffer.h */
30
31
32static const char utf8[10] = "ab\360\240\200\200defg";
33static const uint16_t utf16[8] = {'a', 'b', 0xD840, 0xDC00, 'd', 'e', 'f', 'g'};
34static const uint32_t utf32[7] = {'a', 'b', 0x20000, 'd', 'e', 'f', 'g'};
35
36
37typedef enum {
38  BUFFER_EMPTY,
39  BUFFER_ONE_BY_ONE,
40  BUFFER_UTF32,
41  BUFFER_UTF16,
42  BUFFER_UTF8,
43  BUFFER_NUM_TYPES,
44} buffer_type_t;
45
46static const char *buffer_names[] = {
47  "empty",
48  "one-by-one",
49  "utf32",
50  "utf16",
51  "utf8"
52};
53
54typedef struct
55{
56  hb_buffer_t *buffer;
57} fixture_t;
58
59static void
60fixture_init (fixture_t *fixture, gconstpointer user_data)
61{
62  hb_buffer_t *b;
63  unsigned int i;
64
65  b = fixture->buffer = hb_buffer_create ();
66
67  switch (GPOINTER_TO_INT (user_data))
68  {
69    case BUFFER_EMPTY:
70      break;
71
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);
75      break;
76
77    case BUFFER_UTF32:
78      hb_buffer_add_utf32 (b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
79      break;
80
81    case BUFFER_UTF16:
82      hb_buffer_add_utf16 (b, utf16, G_N_ELEMENTS (utf16), 1, G_N_ELEMENTS (utf16) - 2);
83      break;
84
85    case BUFFER_UTF8:
86      hb_buffer_add_utf8  (b, utf8,  G_N_ELEMENTS (utf8),  1, G_N_ELEMENTS (utf8)  - 2);
87      break;
88
89    default:
90      g_assert_not_reached ();
91  }
92}
93
94static void
95fixture_finish (fixture_t *fixture, gconstpointer user_data)
96{
97  hb_buffer_destroy (fixture->buffer);
98}
99
100
101static void
102test_buffer_properties (fixture_t *fixture, gconstpointer user_data)
103{
104  hb_buffer_t *b = fixture->buffer;
105  hb_unicode_funcs_t *ufuncs;
106
107  /* test default properties */
108
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);
113
114
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);
120
121  hb_buffer_set_direction (b, HB_DIRECTION_RTL);
122  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_RTL);
123
124  hb_buffer_set_script (b, HB_SCRIPT_ARABIC);
125  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_ARABIC);
126
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));
129
130
131
132  /* test clear clears all properties but unicode_funcs */
133
134  hb_buffer_clear (b);
135
136  g_assert (hb_buffer_get_unicode_funcs (b) == ufuncs);
137  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
138  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
139  g_assert (hb_buffer_get_language (b) == NULL);
140
141
142  /* test reset clears all properties */
143
144  hb_buffer_set_direction (b, HB_DIRECTION_RTL);
145  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_RTL);
146
147  hb_buffer_set_script (b, HB_SCRIPT_ARABIC);
148  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_ARABIC);
149
150  hb_buffer_set_language (b, hb_language_from_string ("fa", -1));
151  g_assert (hb_buffer_get_language (b) == hb_language_from_string ("Fa", -1));
152
153  hb_buffer_reset (b);
154
155  g_assert (hb_buffer_get_unicode_funcs (b) == hb_unicode_funcs_get_default ());
156  g_assert (hb_buffer_get_direction (b) == HB_DIRECTION_INVALID);
157  g_assert (hb_buffer_get_script (b) == HB_SCRIPT_INVALID);
158  g_assert (hb_buffer_get_language (b) == NULL);
159}
160
161static void
162test_buffer_contents (fixture_t *fixture, gconstpointer user_data)
163{
164  hb_buffer_t *b = fixture->buffer;
165  unsigned int i, len, len2;
166  buffer_type_t buffer_type = GPOINTER_TO_INT (user_data);
167  hb_glyph_info_t *glyphs;
168
169  if (buffer_type == BUFFER_EMPTY) {
170    g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
171    return;
172  }
173
174  len = hb_buffer_get_length (b);
175  hb_buffer_get_glyph_infos (b, NULL); /* test NULL */
176  glyphs = hb_buffer_get_glyph_infos (b, &len2);
177  g_assert_cmpint (len, ==, len2);
178  g_assert_cmpint (len, ==, 5);
179
180  for (i = 0; i < len; i++) {
181    g_assert_cmphex (glyphs[i].mask,      ==, 1);
182    g_assert_cmphex (glyphs[i].var1.u32,  ==, 0);
183    g_assert_cmphex (glyphs[i].var2.u32,  ==, 0);
184  }
185
186  for (i = 0; i < len; i++) {
187    unsigned int cluster;
188    cluster = 1+i;
189    if (i >= 2) {
190      if (buffer_type == BUFFER_UTF16)
191	cluster++;
192      else if (buffer_type == BUFFER_UTF8)
193        cluster += 3;
194    }
195    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
196    g_assert_cmphex (glyphs[i].cluster,   ==, cluster);
197  }
198
199  /* reverse, test, and reverse back */
200
201  hb_buffer_reverse (b);
202  for (i = 0; i < len; i++)
203    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
204
205  hb_buffer_reverse (b);
206  for (i = 0; i < len; i++)
207    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
208
209  /* reverse_clusters works same as reverse for now since each codepoint is
210   * in its own cluster */
211
212  hb_buffer_reverse_clusters (b);
213  for (i = 0; i < len; i++)
214    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
215
216  hb_buffer_reverse_clusters (b);
217  for (i = 0; i < len; i++)
218    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
219
220  /* now form a cluster and test again */
221  glyphs[2].cluster = glyphs[1].cluster;
222
223  /* reverse, test, and reverse back */
224
225  hb_buffer_reverse (b);
226  for (i = 0; i < len; i++)
227    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
228
229  hb_buffer_reverse (b);
230  for (i = 0; i < len; i++)
231    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
232
233  /* reverse_clusters twice still should return the original string,
234   * but when applied once, the 1-2 cluster should be retained. */
235
236  hb_buffer_reverse_clusters (b);
237  for (i = 0; i < len; i++) {
238    unsigned int j = len-1-i;
239    if (j == 1)
240      j = 2;
241    else if (j == 2)
242      j = 1;
243    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+j]);
244  }
245
246  hb_buffer_reverse_clusters (b);
247  for (i = 0; i < len; i++)
248    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
249
250
251  /* test setting length */
252
253  /* enlarge */
254  g_assert (hb_buffer_set_length (b, 10));
255  glyphs = hb_buffer_get_glyph_infos (b, NULL);
256  g_assert_cmpint (hb_buffer_get_length (b), ==, 10);
257  for (i = 0; i < 5; i++)
258    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
259  for (i = 5; i < 10; i++)
260    g_assert_cmphex (glyphs[i].codepoint, ==, 0);
261  /* shrink */
262  g_assert (hb_buffer_set_length (b, 3));
263  glyphs = hb_buffer_get_glyph_infos (b, NULL);
264  g_assert_cmpint (hb_buffer_get_length (b), ==, 3);
265  for (i = 0; i < 3; i++)
266    g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
267
268
269  g_assert (hb_buffer_allocation_successful (b));
270
271
272  /* test reset clears content */
273
274  hb_buffer_reset (b);
275  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
276}
277
278static void
279test_buffer_positions (fixture_t *fixture, gconstpointer user_data)
280{
281  hb_buffer_t *b = fixture->buffer;
282  unsigned int i, len, len2;
283  hb_glyph_position_t *positions;
284
285  /* Without shaping, positions should all be zero */
286  len = hb_buffer_get_length (b);
287  hb_buffer_get_glyph_positions (b, NULL); /* test NULL */
288  positions = hb_buffer_get_glyph_positions (b, &len2);
289  g_assert_cmpint (len, ==, len2);
290  for (i = 0; i < len; i++) {
291    g_assert_cmpint (0, ==, positions[i].x_advance);
292    g_assert_cmpint (0, ==, positions[i].y_advance);
293    g_assert_cmpint (0, ==, positions[i].x_offset);
294    g_assert_cmpint (0, ==, positions[i].y_offset);
295    g_assert_cmpint (0, ==, positions[i].var.i32);
296  }
297
298  /* test reset clears content */
299  hb_buffer_reset (b);
300  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
301}
302
303static void
304test_buffer_allocation (fixture_t *fixture, gconstpointer user_data)
305{
306  hb_buffer_t *b = fixture->buffer;
307
308  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
309
310  g_assert (hb_buffer_pre_allocate (b, 100));
311  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
312  g_assert (hb_buffer_allocation_successful (b));
313
314  /* lets try a huge allocation, make sure it fails */
315  g_assert (!hb_buffer_pre_allocate (b, (unsigned int) -1));
316  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
317  g_assert (!hb_buffer_allocation_successful (b));
318
319  /* small one again */
320  g_assert (hb_buffer_pre_allocate (b, 50));
321  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
322  g_assert (!hb_buffer_allocation_successful (b));
323
324  hb_buffer_reset (b);
325  g_assert (hb_buffer_allocation_successful (b));
326
327  /* all allocation and size  */
328  g_assert (!hb_buffer_pre_allocate (b, ((unsigned int) -1) / 20 + 1));
329  g_assert (!hb_buffer_allocation_successful (b));
330
331  hb_buffer_reset (b);
332  g_assert (hb_buffer_allocation_successful (b));
333
334  /* technically, this one can actually pass on 64bit machines, but
335   * I'm doubtful that any malloc allows 4GB allocations at a time.
336   * But let's only enable it on a 32-bit machine. */
337  if (sizeof (long) == 4) {
338    g_assert (!hb_buffer_pre_allocate (b, ((unsigned int) -1) / 20 - 1));
339    g_assert (!hb_buffer_allocation_successful (b));
340  }
341
342  hb_buffer_reset (b);
343  g_assert (hb_buffer_allocation_successful (b));
344}
345
346
347typedef struct {
348  const char utf8[8];
349  const uint32_t codepoints[8];
350} utf8_conversion_test_t;
351
352/* note: we skip the first and last byte when adding to buffer */
353static const utf8_conversion_test_t utf8_conversion_tests[] = {
354  {"a\303\207", {-1}},
355  {"a\303\207b", {0xC7}},
356  {"ab\303cd", {'b', -1, 'c'}},
357  {"ab\303\302\301cd", {'b', -1, -1, -1, 'c'}}
358};
359
360static void
361test_buffer_utf8_conversion (void)
362{
363  hb_buffer_t *b;
364  hb_glyph_info_t *glyphs;
365  unsigned int bytes, chars, i, j, len;
366
367  b = hb_buffer_create ();
368
369  for (i = 0; i < G_N_ELEMENTS (utf8_conversion_tests); i++)
370  {
371    const utf8_conversion_test_t *test = &utf8_conversion_tests[i];
372    char *escaped;
373
374    escaped = g_strescape (test->utf8, NULL);
375    g_test_message ("UTF-8 test #%d: %s", i, escaped);
376    g_free (escaped);
377
378    bytes = strlen (test->utf8);
379    for (chars = 0; test->codepoints[chars]; chars++)
380      ;
381
382    hb_buffer_reset (b);
383    hb_buffer_add_utf8 (b, test->utf8, bytes,  1, bytes - 2);
384
385    glyphs = hb_buffer_get_glyph_infos (b, &len);
386    g_assert_cmpint (len, ==, chars);
387    for (j = 0; j < chars; j++)
388      g_assert_cmphex (glyphs[j].codepoint, ==, test->codepoints[j]);
389  }
390
391  hb_buffer_destroy (b);
392}
393
394
395
396/* Following test table is adapted from glib/glib/tests/utf8-validate.c
397 * with relicensing permission from Matthias Clasen. */
398
399typedef struct {
400  const char *utf8;
401  int max_len;
402  unsigned int offset;
403  gboolean valid;
404} utf8_validity_test_t;
405
406static const utf8_validity_test_t utf8_validity_tests[] = {
407  /* some tests to check max_len handling */
408  /* length 1 */
409  { "abcde", -1, 5, TRUE },
410  { "abcde", 3, 3, TRUE },
411  { "abcde", 5, 5, TRUE },
412  /* length 2 */
413  { "\xc2\xa9\xc2\xa9\xc2\xa9", -1, 6, TRUE },
414  { "\xc2\xa9\xc2\xa9\xc2\xa9",  1, 0, FALSE },
415  { "\xc2\xa9\xc2\xa9\xc2\xa9",  2, 2, TRUE },
416  { "\xc2\xa9\xc2\xa9\xc2\xa9",  3, 2, FALSE },
417  { "\xc2\xa9\xc2\xa9\xc2\xa9",  4, 4, TRUE },
418  { "\xc2\xa9\xc2\xa9\xc2\xa9",  5, 4, FALSE },
419  { "\xc2\xa9\xc2\xa9\xc2\xa9",  6, 6, TRUE },
420  /* length 3 */
421  { "\xe2\x89\xa0\xe2\x89\xa0", -1, 6, TRUE },
422  { "\xe2\x89\xa0\xe2\x89\xa0",  1, 0, FALSE },
423  { "\xe2\x89\xa0\xe2\x89\xa0",  2, 0, FALSE },
424  { "\xe2\x89\xa0\xe2\x89\xa0",  3, 3, TRUE },
425  { "\xe2\x89\xa0\xe2\x89\xa0",  4, 3, FALSE },
426  { "\xe2\x89\xa0\xe2\x89\xa0",  5, 3, FALSE },
427  { "\xe2\x89\xa0\xe2\x89\xa0",  6, 6, TRUE },
428
429  /* examples from http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt */
430  /* greek 'kosme' */
431  { "\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5", -1, 11, TRUE },
432  /* first sequence of each length */
433  { "\x00", -1, 0, TRUE },
434  { "\xc2\x80", -1, 2, TRUE },
435  { "\xe0\xa0\x80", -1, 3, TRUE },
436  { "\xf0\x90\x80\x80", -1, 4, TRUE },
437  { "\xf8\x88\x80\x80\x80", -1, 0, FALSE },
438  { "\xfc\x84\x80\x80\x80\x80", -1, 0, FALSE },
439  /* last sequence of each length */
440  { "\x7f", -1, 1, TRUE },
441  { "\xdf\xbf", -1, 2, TRUE },
442  { "\xef\xbf\xbf", -1, 0, TRUE },
443  { "\xf7\xbf\xbf\xbf", -1, 0, TRUE },
444  { "\xfb\xbf\xbf\xbf\xbf", -1, 0, FALSE },
445  { "\xfd\xbf\xbf\xbf\xbf\xbf", -1, 0, FALSE },
446  /* other boundary conditions */
447  { "\xed\x9f\xbf", -1, 3, TRUE },
448  { "\xee\x80\x80", -1, 3, TRUE },
449  { "\xef\xbf\xbd", -1, 3, TRUE },
450  { "\xf4\x8f\xbf\xbf", -1, 0, TRUE },
451  /* malformed sequences */
452  /* continuation bytes */
453  { "\x80", -1, 0, FALSE },
454  { "\xbf", -1, 0, FALSE },
455  { "\x80\xbf", -1, 0, FALSE },
456  { "\x80\xbf\x80", -1, 0, FALSE },
457  { "\x80\xbf\x80\xbf", -1, 0, FALSE },
458  { "\x80\xbf\x80\xbf\x80", -1, 0, FALSE },
459  { "\x80\xbf\x80\xbf\x80\xbf", -1, 0, FALSE },
460  { "\x80\xbf\x80\xbf\x80\xbf\x80", -1, 0, FALSE },
461
462  /* all possible continuation byte */
463  { "\x80", -1, 0, FALSE },
464  { "\x81", -1, 0, FALSE },
465  { "\x82", -1, 0, FALSE },
466  { "\x83", -1, 0, FALSE },
467  { "\x84", -1, 0, FALSE },
468  { "\x85", -1, 0, FALSE },
469  { "\x86", -1, 0, FALSE },
470  { "\x87", -1, 0, FALSE },
471  { "\x88", -1, 0, FALSE },
472  { "\x89", -1, 0, FALSE },
473  { "\x8a", -1, 0, FALSE },
474  { "\x8b", -1, 0, FALSE },
475  { "\x8c", -1, 0, FALSE },
476  { "\x8d", -1, 0, FALSE },
477  { "\x8e", -1, 0, FALSE },
478  { "\x8f", -1, 0, FALSE },
479  { "\x90", -1, 0, FALSE },
480  { "\x91", -1, 0, FALSE },
481  { "\x92", -1, 0, FALSE },
482  { "\x93", -1, 0, FALSE },
483  { "\x94", -1, 0, FALSE },
484  { "\x95", -1, 0, FALSE },
485  { "\x96", -1, 0, FALSE },
486  { "\x97", -1, 0, FALSE },
487  { "\x98", -1, 0, FALSE },
488  { "\x99", -1, 0, FALSE },
489  { "\x9a", -1, 0, FALSE },
490  { "\x9b", -1, 0, FALSE },
491  { "\x9c", -1, 0, FALSE },
492  { "\x9d", -1, 0, FALSE },
493  { "\x9e", -1, 0, FALSE },
494  { "\x9f", -1, 0, FALSE },
495  { "\xa0", -1, 0, FALSE },
496  { "\xa1", -1, 0, FALSE },
497  { "\xa2", -1, 0, FALSE },
498  { "\xa3", -1, 0, FALSE },
499  { "\xa4", -1, 0, FALSE },
500  { "\xa5", -1, 0, FALSE },
501  { "\xa6", -1, 0, FALSE },
502  { "\xa7", -1, 0, FALSE },
503  { "\xa8", -1, 0, FALSE },
504  { "\xa9", -1, 0, FALSE },
505  { "\xaa", -1, 0, FALSE },
506  { "\xab", -1, 0, FALSE },
507  { "\xac", -1, 0, FALSE },
508  { "\xad", -1, 0, FALSE },
509  { "\xae", -1, 0, FALSE },
510  { "\xaf", -1, 0, FALSE },
511  { "\xb0", -1, 0, FALSE },
512  { "\xb1", -1, 0, FALSE },
513  { "\xb2", -1, 0, FALSE },
514  { "\xb3", -1, 0, FALSE },
515  { "\xb4", -1, 0, FALSE },
516  { "\xb5", -1, 0, FALSE },
517  { "\xb6", -1, 0, FALSE },
518  { "\xb7", -1, 0, FALSE },
519  { "\xb8", -1, 0, FALSE },
520  { "\xb9", -1, 0, FALSE },
521  { "\xba", -1, 0, FALSE },
522  { "\xbb", -1, 0, FALSE },
523  { "\xbc", -1, 0, FALSE },
524  { "\xbd", -1, 0, FALSE },
525  { "\xbe", -1, 0, FALSE },
526  { "\xbf", -1, 0, FALSE },
527  /* lone start characters */
528  { "\xc0\x20", -1, 0, FALSE },
529  { "\xc1\x20", -1, 0, FALSE },
530  { "\xc2\x20", -1, 0, FALSE },
531  { "\xc3\x20", -1, 0, FALSE },
532  { "\xc4\x20", -1, 0, FALSE },
533  { "\xc5\x20", -1, 0, FALSE },
534  { "\xc6\x20", -1, 0, FALSE },
535  { "\xc7\x20", -1, 0, FALSE },
536  { "\xc8\x20", -1, 0, FALSE },
537  { "\xc9\x20", -1, 0, FALSE },
538  { "\xca\x20", -1, 0, FALSE },
539  { "\xcb\x20", -1, 0, FALSE },
540  { "\xcc\x20", -1, 0, FALSE },
541  { "\xcd\x20", -1, 0, FALSE },
542  { "\xce\x20", -1, 0, FALSE },
543  { "\xcf\x20", -1, 0, FALSE },
544  { "\xd0\x20", -1, 0, FALSE },
545  { "\xd1\x20", -1, 0, FALSE },
546  { "\xd2\x20", -1, 0, FALSE },
547  { "\xd3\x20", -1, 0, FALSE },
548  { "\xd4\x20", -1, 0, FALSE },
549  { "\xd5\x20", -1, 0, FALSE },
550  { "\xd6\x20", -1, 0, FALSE },
551  { "\xd7\x20", -1, 0, FALSE },
552  { "\xd8\x20", -1, 0, FALSE },
553  { "\xd9\x20", -1, 0, FALSE },
554  { "\xda\x20", -1, 0, FALSE },
555  { "\xdb\x20", -1, 0, FALSE },
556  { "\xdc\x20", -1, 0, FALSE },
557  { "\xdd\x20", -1, 0, FALSE },
558  { "\xde\x20", -1, 0, FALSE },
559  { "\xdf\x20", -1, 0, FALSE },
560  { "\xe0\x20", -1, 0, FALSE },
561  { "\xe1\x20", -1, 0, FALSE },
562  { "\xe2\x20", -1, 0, FALSE },
563  { "\xe3\x20", -1, 0, FALSE },
564  { "\xe4\x20", -1, 0, FALSE },
565  { "\xe5\x20", -1, 0, FALSE },
566  { "\xe6\x20", -1, 0, FALSE },
567  { "\xe7\x20", -1, 0, FALSE },
568  { "\xe8\x20", -1, 0, FALSE },
569  { "\xe9\x20", -1, 0, FALSE },
570  { "\xea\x20", -1, 0, FALSE },
571  { "\xeb\x20", -1, 0, FALSE },
572  { "\xec\x20", -1, 0, FALSE },
573  { "\xed\x20", -1, 0, FALSE },
574  { "\xee\x20", -1, 0, FALSE },
575  { "\xef\x20", -1, 0, FALSE },
576  { "\xf0\x20", -1, 0, FALSE },
577  { "\xf1\x20", -1, 0, FALSE },
578  { "\xf2\x20", -1, 0, FALSE },
579  { "\xf3\x20", -1, 0, FALSE },
580  { "\xf4\x20", -1, 0, FALSE },
581  { "\xf5\x20", -1, 0, FALSE },
582  { "\xf6\x20", -1, 0, FALSE },
583  { "\xf7\x20", -1, 0, FALSE },
584  { "\xf8\x20", -1, 0, FALSE },
585  { "\xf9\x20", -1, 0, FALSE },
586  { "\xfa\x20", -1, 0, FALSE },
587  { "\xfb\x20", -1, 0, FALSE },
588  { "\xfc\x20", -1, 0, FALSE },
589  { "\xfd\x20", -1, 0, FALSE },
590  /* missing continuation bytes */
591  { "\x20\xc0", -1, 1, FALSE },
592  { "\x20\xe0\x80", -1, 1, FALSE },
593  { "\x20\xf0\x80\x80", -1, 1, FALSE },
594  { "\x20\xf8\x80\x80\x80", -1, 1, FALSE },
595  { "\x20\xfc\x80\x80\x80\x80", -1, 1, FALSE },
596  { "\x20\xdf", -1, 1, FALSE },
597  { "\x20\xef\xbf", -1, 1, FALSE },
598  { "\x20\xf7\xbf\xbf", -1, 1, FALSE },
599  { "\x20\xfb\xbf\xbf\xbf", -1, 1, FALSE },
600  { "\x20\xfd\xbf\xbf\xbf\xbf", -1, 1, FALSE },
601  /* impossible bytes */
602  { "\x20\xfe\x20", -1, 1, FALSE },
603  { "\x20\xff\x20", -1, 1, FALSE },
604#if 0
605  /* XXX fix these, or document that we don't detect them? */
606  /* overlong sequences */
607  { "\x20\xc0\xaf\x20", -1, 1, FALSE },
608  { "\x20\xe0\x80\xaf\x20", -1, 1, FALSE },
609  { "\x20\xf0\x80\x80\xaf\x20", -1, 1, FALSE },
610  { "\x20\xf8\x80\x80\x80\xaf\x20", -1, 1, FALSE },
611  { "\x20\xfc\x80\x80\x80\x80\xaf\x20", -1, 1, FALSE },
612  { "\x20\xc1\xbf\x20", -1, 1, FALSE },
613  { "\x20\xe0\x9f\xbf\x20", -1, 1, FALSE },
614  { "\x20\xf0\x8f\xbf\xbf\x20", -1, 1, FALSE },
615  { "\x20\xf8\x87\xbf\xbf\xbf\x20", -1, 1, FALSE },
616  { "\x20\xfc\x83\xbf\xbf\xbf\xbf\x20", -1, 1, FALSE },
617  { "\x20\xc0\x80\x20", -1, 1, FALSE },
618  { "\x20\xe0\x80\x80\x20", -1, 1, FALSE },
619  { "\x20\xf0\x80\x80\x80\x20", -1, 1, FALSE },
620  { "\x20\xf8\x80\x80\x80\x80\x20", -1, 1, FALSE },
621  { "\x20\xfc\x80\x80\x80\x80\x80\x20", -1, 1, FALSE },
622  /* illegal code positions */
623  { "\x20\xed\xa0\x80\x20", -1, 1, FALSE },
624  { "\x20\xed\xad\xbf\x20", -1, 1, FALSE },
625  { "\x20\xed\xae\x80\x20", -1, 1, FALSE },
626  { "\x20\xed\xaf\xbf\x20", -1, 1, FALSE },
627  { "\x20\xed\xb0\x80\x20", -1, 1, FALSE },
628  { "\x20\xed\xbe\x80\x20", -1, 1, FALSE },
629  { "\x20\xed\xbf\xbf\x20", -1, 1, FALSE },
630  { "\x20\xed\xa0\x80\xed\xb0\x80\x20", -1, 1, FALSE },
631  { "\x20\xed\xa0\x80\xed\xbf\xbf\x20", -1, 1, FALSE },
632  { "\x20\xed\xad\xbf\xed\xb0\x80\x20", -1, 1, FALSE },
633  { "\x20\xed\xad\xbf\xed\xbf\xbf\x20", -1, 1, FALSE },
634  { "\x20\xed\xae\x80\xed\xb0\x80\x20", -1, 1, FALSE },
635  { "\x20\xed\xae\x80\xed\xbf\xbf\x20", -1, 1, FALSE },
636  { "\x20\xed\xaf\xbf\xed\xb0\x80\x20", -1, 1, FALSE },
637  { "\x20\xed\xaf\xbf\xed\xbf\xbf\x20", -1, 1, FALSE },
638  { "\x20\xef\xbf\xbe\x20", -1, 1, FALSE },
639  { "\x20\xef\xbf\xbf\x20", -1, 1, FALSE },
640#endif
641  { "", -1, 0, TRUE }
642};
643
644static void
645test_buffer_utf8_validity (void)
646{
647  hb_buffer_t *b;
648  unsigned int i;
649
650  b = hb_buffer_create ();
651
652  for (i = 0; i < G_N_ELEMENTS (utf8_validity_tests); i++)
653  {
654    const utf8_validity_test_t *test = &utf8_validity_tests[i];
655    unsigned int text_bytes, segment_bytes, j, len;
656    hb_glyph_info_t *glyphs;
657    char *escaped;
658
659    escaped = g_strescape (test->utf8, NULL);
660    g_test_message ("UTF-8 test #%d: %s", i, escaped);
661    g_free (escaped);
662
663    text_bytes = strlen (test->utf8);
664    if (test->max_len == -1)
665      segment_bytes = text_bytes;
666    else
667      segment_bytes = test->max_len;
668
669    hb_buffer_reset (b);
670    hb_buffer_add_utf8 (b, test->utf8, text_bytes,  0, segment_bytes);
671
672    glyphs = hb_buffer_get_glyph_infos (b, &len);
673    for (j = 0; j < len; j++)
674      if (glyphs[j].codepoint == (hb_codepoint_t) -1)
675	break;
676
677    g_assert (test->valid ? j == len : j < len);
678    if (!test->valid)
679      g_assert (glyphs[j].cluster == test->offset);
680  }
681
682  hb_buffer_destroy (b);
683}
684
685
686typedef struct {
687  const uint16_t utf16[8];
688  const uint32_t codepoints[8];
689} utf16_conversion_test_t;
690
691/* note: we skip the first and last item from utf16 when adding to buffer */
692static const utf16_conversion_test_t utf16_conversion_tests[] = {
693  {{0x41, 0x004D, 0x0430, 0x4E8C, 0xD800, 0xDF02, 0x61} , {0x004D, 0x0430, 0x4E8C, 0x10302}},
694  {{0x41, 0xD800, 0xDF02, 0x61}, {0x10302}},
695  {{0x41, 0xD800, 0xDF02}, {-1}},
696  {{0x41, 0x61, 0xD800, 0xDF02}, {0x61, -1}},
697  {{0x41, 0xD800, 0x61, 0xDF02}, {-1, 0x61}},
698  {{0x41, 0x61}, {}}
699};
700
701static void
702test_buffer_utf16_conversion (void)
703{
704  hb_buffer_t *b;
705  unsigned int i;
706
707  b = hb_buffer_create ();
708
709  for (i = 0; i < G_N_ELEMENTS (utf16_conversion_tests); i++)
710  {
711    const utf16_conversion_test_t *test = &utf16_conversion_tests[i];
712    unsigned int u_len, chars, j, len;
713    hb_glyph_info_t *glyphs;
714
715    g_test_message ("UTF-16 test #%d", i);
716
717    for (u_len = 0; test->utf16[u_len]; u_len++)
718      ;
719    for (chars = 0; test->codepoints[chars]; chars++)
720      ;
721
722    hb_buffer_reset (b);
723    hb_buffer_add_utf16 (b, test->utf16, u_len,  1, u_len - 2);
724
725    glyphs = hb_buffer_get_glyph_infos (b, &len);
726    g_assert_cmpint (len, ==, chars);
727    for (j = 0; j < chars; j++)
728      g_assert_cmphex (glyphs[j].codepoint, ==, test->codepoints[j]);
729  }
730
731  hb_buffer_destroy (b);
732}
733
734static void
735test_empty (hb_buffer_t *b)
736{
737  g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
738  g_assert (!hb_buffer_get_glyph_infos (b, NULL));
739  g_assert (!hb_buffer_get_glyph_positions (b, NULL));
740}
741
742static void
743test_buffer_empty (void)
744{
745  hb_buffer_t *b = hb_buffer_get_empty ();
746
747  g_assert (hb_buffer_get_empty ());
748  g_assert (hb_buffer_get_empty () == b);
749
750  g_assert (!hb_buffer_allocation_successful (b));
751
752  test_empty (b);
753
754  hb_buffer_add_utf32 (b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
755
756  test_empty (b);
757
758  hb_buffer_reverse (b);
759  hb_buffer_reverse_clusters (b);
760
761  g_assert (!hb_buffer_set_length (b, 10));
762
763  test_empty (b);
764
765  g_assert (hb_buffer_set_length (b, 0));
766
767  test_empty (b);
768
769  g_assert (!hb_buffer_allocation_successful (b));
770
771  hb_buffer_reset (b);
772
773  test_empty (b);
774
775  g_assert (!hb_buffer_allocation_successful (b));
776}
777
778int
779main (int argc, char **argv)
780{
781  unsigned int i;
782
783  hb_test_init (&argc, &argv);
784
785  for (i = 0; i < BUFFER_NUM_TYPES; i++)
786  {
787    const void *buffer_type = GINT_TO_POINTER (i);
788    const char *buffer_name = buffer_names[i];
789
790    hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_properties);
791    hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_contents);
792    hb_test_add_fixture_flavor (fixture, buffer_type, buffer_name, test_buffer_positions);
793  }
794
795  hb_test_add_fixture (fixture, GINT_TO_POINTER (BUFFER_EMPTY), test_buffer_allocation);
796
797  hb_test_add (test_buffer_utf8_conversion);
798  hb_test_add (test_buffer_utf8_validity);
799  hb_test_add (test_buffer_utf16_conversion);
800  hb_test_add (test_buffer_empty);
801
802  return hb_test_run();
803}
804