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 /* This file tests types defined in 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'};
45 static const char *buffer_names[] = {
59 fixture_init (Fixture *fixture, gconstpointer user_data)
63 fixture->b = hb_buffer_create (0);
65 switch (GPOINTER_TO_INT (user_data)) {
69 case BUFFER_ONE_BY_ONE:
70 for (i = 1; i < G_N_ELEMENTS (utf32) - 1; i++)
71 hb_buffer_add (fixture->b, utf32[i], 1, i);
75 hb_buffer_add_utf32 (fixture->b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
79 hb_buffer_add_utf16 (fixture->b, utf16, G_N_ELEMENTS (utf16), 1, G_N_ELEMENTS (utf16) - 2);
83 hb_buffer_add_utf8 (fixture->b, utf8, G_N_ELEMENTS (utf8), 1, G_N_ELEMENTS (utf8) - 2);
87 g_assert_not_reached ();
92 fixture_fini (Fixture *fixture, gconstpointer user_data)
94 hb_buffer_destroy (fixture->b);
99 test_buffer_properties (Fixture *fixture, gconstpointer user_data)
101 /* TODO check unicode_funcs */
103 g_assert (hb_buffer_get_direction (fixture->b) == HB_DIRECTION_INVALID);
104 g_assert (hb_buffer_get_script (fixture->b) == HB_SCRIPT_INVALID);
105 g_assert (hb_buffer_get_language (fixture->b) == NULL);
107 hb_buffer_set_direction (fixture->b, HB_DIRECTION_RTL);
108 g_assert (hb_buffer_get_direction (fixture->b) == HB_DIRECTION_RTL);
110 hb_buffer_set_script (fixture->b, HB_SCRIPT_ARABIC);
111 g_assert (hb_buffer_get_script (fixture->b) == HB_SCRIPT_ARABIC);
113 hb_buffer_set_language (fixture->b, hb_language_from_string ("fa"));
114 g_assert (hb_buffer_get_language (fixture->b) == hb_language_from_string ("Fa"));
118 test_buffer_contents (Fixture *fixture, gconstpointer user_data)
121 buffer_type_t buffer_type = GPOINTER_TO_INT (user_data);
122 hb_glyph_info_t *glyphs;
124 if (buffer_type == BUFFER_EMPTY) {
125 g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 0);
129 glyphs = hb_buffer_get_glyph_infos (fixture->b, &len);
130 g_assert_cmpint (len, ==, 5);
132 for (i = 0; i < len; i++) {
133 g_assert_cmphex (glyphs[i].mask, ==, 1);
134 g_assert_cmphex (glyphs[i].var1.u32, ==, 0);
135 g_assert_cmphex (glyphs[i].var2.u32, ==, 0);
138 for (i = 0; i < len; i++) {
139 unsigned int cluster;
142 if (buffer_type == BUFFER_UTF16)
144 else if (buffer_type == BUFFER_UTF8)
147 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
148 g_assert_cmphex (glyphs[i].cluster, ==, cluster);
151 /* reverse, test, and reverse back */
153 hb_buffer_reverse (fixture->b);
154 for (i = 0; i < len; i++)
155 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
157 hb_buffer_reverse (fixture->b);
158 for (i = 0; i < len; i++)
159 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
161 /* reverse_clusters works same as reverse for now since each codepoint is
162 * in its own cluster */
164 hb_buffer_reverse_clusters (fixture->b);
165 for (i = 0; i < len; i++)
166 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
168 hb_buffer_reverse_clusters (fixture->b);
169 for (i = 0; i < len; i++)
170 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
172 /* now form a cluster and test again */
173 glyphs[2].cluster = glyphs[1].cluster;
175 /* reverse, test, and reverse back */
177 hb_buffer_reverse (fixture->b);
178 for (i = 0; i < len; i++)
179 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
181 hb_buffer_reverse (fixture->b);
182 for (i = 0; i < len; i++)
183 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
185 /* reverse_clusters twice still should return the original string,
186 * but when applied once, the 1-2 cluster should be retained. */
188 hb_buffer_reverse_clusters (fixture->b);
189 for (i = 0; i < len; i++) {
190 unsigned int j = len-1-i;
195 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+j]);
198 hb_buffer_reverse_clusters (fixture->b);
199 for (i = 0; i < len; i++)
200 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
204 test_buffer_positions (Fixture *fixture, gconstpointer user_data)
207 hb_glyph_position_t *positions;
209 /* Without shaping, positions should all be zero */
210 positions = hb_buffer_get_glyph_positions (fixture->b, &len);
211 for (i = 0; i < len; i++) {
212 g_assert_cmpint (0, ==, positions[i].x_advance);
213 g_assert_cmpint (0, ==, positions[i].y_advance);
214 g_assert_cmpint (0, ==, positions[i].x_offset);
215 g_assert_cmpint (0, ==, positions[i].y_offset);
216 g_assert_cmpint (0, ==, positions[i].var.i32);
221 main (int argc, char **argv)
225 g_test_init (&argc, &argv, NULL);
227 for (i = 0; i < BUFFER_NUM_TYPES; i++) {
228 #define TEST_ADD(path, func) \
230 char *s = g_strdup_printf ("%s/%s", path, buffer_names[i]); \
231 g_test_add (s, Fixture, GINT_TO_POINTER (i), fixture_init, func, fixture_fini); \
234 TEST_ADD ("/buffer/properties", test_buffer_properties);
235 TEST_ADD ("/buffer/contents", test_buffer_contents);
236 TEST_ADD ("/buffer/positions", test_buffer_positions);
240 /* XXX test invalid UTF-8 / UTF-16 text input (also overlong sequences) */
241 /* XXX test pre_allocate(), allocation_successful(), and memory management */
242 /* XXX test buffer reset */
243 /* XXX test buffer set length */