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 unsigned int cluster;
136 if (buffer_type == BUFFER_UTF16)
138 else if (buffer_type == BUFFER_UTF8)
141 g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
142 g_assert_cmphex (glyphs[i].cluster, ==, cluster);
143 g_assert_cmphex (glyphs[i].mask, ==, 1);
144 g_assert_cmphex (glyphs[i].var1.u32, ==, 0);
145 g_assert_cmphex (glyphs[i].var2.u32, ==, 0);
150 test_buffer_positions (Fixture *fixture, gconstpointer user_data)
153 hb_glyph_position_t *positions;
155 /* Without shaping, positions should all be zero */
156 positions = hb_buffer_get_glyph_positions (fixture->b, &len);
157 for (i = 0; i < len; i++) {
158 g_assert_cmpint (0, ==, positions[i].x_advance);
159 g_assert_cmpint (0, ==, positions[i].y_advance);
160 g_assert_cmpint (0, ==, positions[i].x_offset);
161 g_assert_cmpint (0, ==, positions[i].y_offset);
162 g_assert_cmpint (0, ==, positions[i].var.i32);
167 main (int argc, char **argv)
171 g_test_init (&argc, &argv, NULL);
173 for (i = 0; i < BUFFER_NUM_TYPES; i++) {
174 #define TEST_ADD(path, func) \
176 char *s = g_strdup_printf ("%s/%s", path, buffer_names[i]); \
177 g_test_add (s, Fixture, GINT_TO_POINTER (i), fixture_init, func, fixture_fini); \
180 TEST_ADD ("/buffer/properties", test_buffer_properties);
181 TEST_ADD ("/buffer/contents", test_buffer_contents);
182 TEST_ADD ("/buffer/positions", test_buffer_positions);
186 /* XXX test invalid UTF-8 / UTF-16 text input (also overlong sequences) */
187 /* XXX test reverse() and reverse_clusters() */
188 /* XXX test pre_allocate(), allocation_successful(), and memory management */
189 /* XXX test buffer reset */
190 /* XXX test buffer set length */