Imported Upstream version 0.9.3
[platform/upstream/harfbuzz.git] / test / api / test-buffer.c
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
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'};
35
36
37 typedef 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
46 static const char *buffer_names[] = {
47   "empty",
48   "one-by-one",
49   "utf32",
50   "utf16",
51   "utf8"
52 };
53
54 typedef struct
55 {
56   hb_buffer_t *buffer;
57 } fixture_t;
58
59 static void
60 fixture_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
94 static void
95 fixture_finish (fixture_t *fixture, gconstpointer user_data)
96 {
97   hb_buffer_destroy (fixture->buffer);
98 }
99
100
101 static void
102 test_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   /* test reset clears properties */
132
133   hb_buffer_reset (b);
134
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);
139 }
140
141 static void
142 test_buffer_contents (fixture_t *fixture, gconstpointer user_data)
143 {
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;
148
149   if (buffer_type == BUFFER_EMPTY) {
150     g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
151     return;
152   }
153
154   len = hb_buffer_get_length (b);
155   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);
159
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);
164   }
165
166   for (i = 0; i < len; i++) {
167     unsigned int cluster;
168     cluster = 1+i;
169     if (i >= 2) {
170       if (buffer_type == BUFFER_UTF16)
171         cluster++;
172       else if (buffer_type == BUFFER_UTF8)
173         cluster += 3;
174     }
175     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
176     g_assert_cmphex (glyphs[i].cluster,   ==, cluster);
177   }
178
179   /* reverse, test, and reverse back */
180
181   hb_buffer_reverse (b);
182   for (i = 0; i < len; i++)
183     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
184
185   hb_buffer_reverse (b);
186   for (i = 0; i < len; i++)
187     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
188
189   /* reverse_clusters works same as reverse for now since each codepoint is
190    * in its own cluster */
191
192   hb_buffer_reverse_clusters (b);
193   for (i = 0; i < len; i++)
194     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
195
196   hb_buffer_reverse_clusters (b);
197   for (i = 0; i < len; i++)
198     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
199
200   /* now form a cluster and test again */
201   glyphs[2].cluster = glyphs[1].cluster;
202
203   /* reverse, test, and reverse back */
204
205   hb_buffer_reverse (b);
206   for (i = 0; i < len; i++)
207     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
208
209   hb_buffer_reverse (b);
210   for (i = 0; i < len; i++)
211     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
212
213   /* reverse_clusters twice still should return the original string,
214    * but when applied once, the 1-2 cluster should be retained. */
215
216   hb_buffer_reverse_clusters (b);
217   for (i = 0; i < len; i++) {
218     unsigned int j = len-1-i;
219     if (j == 1)
220       j = 2;
221     else if (j == 2)
222       j = 1;
223     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+j]);
224   }
225
226   hb_buffer_reverse_clusters (b);
227   for (i = 0; i < len; i++)
228     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
229
230
231   /* test setting length */
232
233   /* enlarge */
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);
241   /* shrink */
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]);
247
248
249   g_assert (hb_buffer_allocation_successful (b));
250
251
252   /* test reset clears content */
253
254   hb_buffer_reset (b);
255   g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
256 }
257
258 static void
259 test_buffer_positions (fixture_t *fixture, gconstpointer user_data)
260 {
261   hb_buffer_t *b = fixture->buffer;
262   unsigned int i, len, len2;
263   hb_glyph_position_t *positions;
264
265   /* Without shaping, positions should all be zero */
266   len = hb_buffer_get_length (b);
267   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);
276   }
277
278   /* test reset clears content */
279   hb_buffer_reset (b);
280   g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
281 }
282
283 static void
284 test_buffer_allocation (fixture_t *fixture, gconstpointer user_data)
285 {
286   hb_buffer_t *b = fixture->buffer;
287
288   g_assert_cmpint (hb_buffer_get_length (b), ==, 0);
289
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));
293
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));
298
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));
303
304   hb_buffer_reset (b);
305   g_assert (hb_buffer_allocation_successful (b));
306
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));
310
311   hb_buffer_reset (b);
312   g_assert (hb_buffer_allocation_successful (b));
313
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));
320   }
321
322   hb_buffer_reset (b);
323   g_assert (hb_buffer_allocation_successful (b));
324 }
325
326
327 typedef struct {
328   const char utf8[8];
329   const uint32_t codepoints[8];
330 } utf8_conversion_test_t;
331
332 /* note: we skip the first and last byte when adding to buffer */
333 static const utf8_conversion_test_t utf8_conversion_tests[] = {
334   {"a\303\207", {-1}},
335   {"a\303\207b", {0xC7}},
336   {"ab\303cd", {'b', -1, 'c'}},
337   {"ab\303\302\301cd", {'b', -1, -1, -1, 'c'}}
338 };
339
340 static void
341 test_buffer_utf8_conversion (void)
342 {
343   hb_buffer_t *b;
344   hb_glyph_info_t *glyphs;
345   unsigned int bytes, chars, i, j, len;
346
347   b = hb_buffer_create ();
348
349   for (i = 0; i < G_N_ELEMENTS (utf8_conversion_tests); i++)
350   {
351     const utf8_conversion_test_t *test = &utf8_conversion_tests[i];
352     char *escaped;
353
354     escaped = g_strescape (test->utf8, NULL);
355     g_test_message ("UTF-8 test #%d: %s", i, escaped);
356     g_free (escaped);
357
358     bytes = strlen (test->utf8);
359     for (chars = 0; test->codepoints[chars]; chars++)
360       ;
361
362     hb_buffer_reset (b);
363     hb_buffer_add_utf8 (b, test->utf8, bytes,  1, bytes - 2);
364
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]);
369   }
370
371   hb_buffer_destroy (b);
372 }
373
374
375
376 /* Following test table is adapted from glib/glib/tests/utf8-validate.c
377  * with relicensing permission from Matthias Clasen. */
378
379 typedef struct {
380   const char *utf8;
381   int max_len;
382   unsigned int offset;
383   gboolean valid;
384 } utf8_validity_test_t;
385
386 static const utf8_validity_test_t utf8_validity_tests[] = {
387   /* some tests to check max_len handling */
388   /* length 1 */
389   { "abcde", -1, 5, TRUE },
390   { "abcde", 3, 3, TRUE },
391   { "abcde", 5, 5, TRUE },
392   /* length 2 */
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 },
400   /* length 3 */
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 },
408
409   /* examples from http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt */
410   /* greek 'kosme' */
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 },
441
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 },
584 #if 0
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 },
620 #endif
621   { "", -1, 0, TRUE }
622 };
623
624 static void
625 test_buffer_utf8_validity (void)
626 {
627   hb_buffer_t *b;
628   unsigned int i;
629
630   b = hb_buffer_create ();
631
632   for (i = 0; i < G_N_ELEMENTS (utf8_validity_tests); i++)
633   {
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;
637     char *escaped;
638
639     escaped = g_strescape (test->utf8, NULL);
640     g_test_message ("UTF-8 test #%d: %s", i, escaped);
641     g_free (escaped);
642
643     text_bytes = strlen (test->utf8);
644     if (test->max_len == -1)
645       segment_bytes = text_bytes;
646     else
647       segment_bytes = test->max_len;
648
649     hb_buffer_reset (b);
650     hb_buffer_add_utf8 (b, test->utf8, text_bytes,  0, segment_bytes);
651
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)
655         break;
656
657     g_assert (test->valid ? j == len : j < len);
658     if (!test->valid)
659       g_assert (glyphs[j].cluster == test->offset);
660   }
661
662   hb_buffer_destroy (b);
663 }
664
665
666 typedef struct {
667   const uint16_t utf16[8];
668   const uint32_t codepoints[8];
669 } utf16_conversion_test_t;
670
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}},
678   {{0x41, 0x61}, {}}
679 };
680
681 static void
682 test_buffer_utf16_conversion (void)
683 {
684   hb_buffer_t *b;
685   unsigned int i;
686
687   b = hb_buffer_create ();
688
689   for (i = 0; i < G_N_ELEMENTS (utf16_conversion_tests); i++)
690   {
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;
694
695     g_test_message ("UTF-16 test #%d", i);
696
697     for (u_len = 0; test->utf16[u_len]; u_len++)
698       ;
699     for (chars = 0; test->codepoints[chars]; chars++)
700       ;
701
702     hb_buffer_reset (b);
703     hb_buffer_add_utf16 (b, test->utf16, u_len,  1, u_len - 2);
704
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]);
709   }
710
711   hb_buffer_destroy (b);
712 }
713
714 static void
715 test_empty (hb_buffer_t *b)
716 {
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));
720 }
721
722 static void
723 test_buffer_empty (void)
724 {
725   hb_buffer_t *b = hb_buffer_get_empty ();
726
727   g_assert (hb_buffer_get_empty ());
728   g_assert (hb_buffer_get_empty () == b);
729
730   g_assert (!hb_buffer_allocation_successful (b));
731
732   test_empty (b);
733
734   hb_buffer_add_utf32 (b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
735
736   test_empty (b);
737
738   hb_buffer_reverse (b);
739   hb_buffer_reverse_clusters (b);
740
741   g_assert (!hb_buffer_set_length (b, 10));
742
743   test_empty (b);
744
745   g_assert (hb_buffer_set_length (b, 0));
746
747   test_empty (b);
748
749   g_assert (!hb_buffer_allocation_successful (b));
750
751   hb_buffer_reset (b);
752
753   test_empty (b);
754
755   g_assert (!hb_buffer_allocation_successful (b));
756 }
757
758 int
759 main (int argc, char **argv)
760 {
761   unsigned int i;
762
763   hb_test_init (&argc, &argv);
764
765   for (i = 0; i < BUFFER_NUM_TYPES; i++)
766   {
767     const void *buffer_type = GINT_TO_POINTER (i);
768     const char *buffer_name = buffer_names[i];
769
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);
773   }
774
775   hb_test_add_fixture (fixture, GINT_TO_POINTER (BUFFER_EMPTY), test_buffer_allocation);
776
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);
781
782   return hb_test_run();
783 }