[test/buffer] Test reverse() and reverse_clusters()
[profile/ivi/org.tizen.video-player.git] / test / 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 /* This file tests types defined in 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 typedef enum {
37   BUFFER_EMPTY,
38   BUFFER_ONE_BY_ONE,
39   BUFFER_UTF32,
40   BUFFER_UTF16,
41   BUFFER_UTF8,
42   BUFFER_NUM_TYPES,
43 } buffer_type_t;
44
45 static const char *buffer_names[] = {
46   "empty",
47   "one-by-one",
48   "utf32",
49   "utf16",
50   "utf8"
51 };
52
53 typedef struct
54 {
55   hb_buffer_t *b;
56 } Fixture;
57
58 static void
59 fixture_init (Fixture *fixture, gconstpointer user_data)
60 {
61   unsigned int i;
62
63   fixture->b = hb_buffer_create (0);
64
65   switch (GPOINTER_TO_INT (user_data)) {
66     case BUFFER_EMPTY:
67       break;
68
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);
72       break;
73
74     case BUFFER_UTF32:
75       hb_buffer_add_utf32 (fixture->b, utf32, G_N_ELEMENTS (utf32), 1, G_N_ELEMENTS (utf32) - 2);
76       break;
77
78     case BUFFER_UTF16:
79       hb_buffer_add_utf16 (fixture->b, utf16, G_N_ELEMENTS (utf16), 1, G_N_ELEMENTS (utf16) - 2);
80       break;
81
82     case BUFFER_UTF8:
83       hb_buffer_add_utf8  (fixture->b, utf8,  G_N_ELEMENTS (utf8),  1, G_N_ELEMENTS (utf8)  - 2);
84       break;
85
86     default:
87       g_assert_not_reached ();
88   }
89 }
90
91 static void
92 fixture_fini (Fixture *fixture, gconstpointer user_data)
93 {
94   hb_buffer_destroy (fixture->b);
95 }
96
97
98 static void
99 test_buffer_properties (Fixture *fixture, gconstpointer user_data)
100 {
101   /* TODO check unicode_funcs */
102
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);
106
107   hb_buffer_set_direction (fixture->b, HB_DIRECTION_RTL);
108   g_assert (hb_buffer_get_direction (fixture->b) == HB_DIRECTION_RTL);
109
110   hb_buffer_set_script (fixture->b, HB_SCRIPT_ARABIC);
111   g_assert (hb_buffer_get_script (fixture->b) == HB_SCRIPT_ARABIC);
112
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"));
115 }
116
117 static void
118 test_buffer_contents (Fixture *fixture, gconstpointer user_data)
119 {
120   unsigned int i, len;
121   buffer_type_t buffer_type = GPOINTER_TO_INT (user_data);
122   hb_glyph_info_t *glyphs;
123
124   if (buffer_type == BUFFER_EMPTY) {
125     g_assert_cmpint (hb_buffer_get_length (fixture->b), ==, 0);
126     return;
127   }
128
129   glyphs = hb_buffer_get_glyph_infos (fixture->b, &len);
130   g_assert_cmpint (len, ==, 5);
131
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);
136   }
137
138   for (i = 0; i < len; i++) {
139     unsigned int cluster;
140     cluster = 1+i;
141     if (i >= 2) {
142       if (buffer_type == BUFFER_UTF16)
143         cluster++;
144       else if (buffer_type == BUFFER_UTF8)
145         cluster += 3;
146     }
147     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
148     g_assert_cmphex (glyphs[i].cluster,   ==, cluster);
149   }
150
151   /* reverse, test, and reverse back */
152
153   hb_buffer_reverse (fixture->b);
154   for (i = 0; i < len; i++)
155     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
156
157   hb_buffer_reverse (fixture->b);
158   for (i = 0; i < len; i++)
159     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
160
161   /* reverse_clusters works same as reverse for now since each codepoint is
162    * in its own cluster */
163
164   hb_buffer_reverse_clusters (fixture->b);
165   for (i = 0; i < len; i++)
166     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
167
168   hb_buffer_reverse_clusters (fixture->b);
169   for (i = 0; i < len; i++)
170     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
171
172   /* now form a cluster and test again */
173   glyphs[2].cluster = glyphs[1].cluster;
174
175   /* reverse, test, and reverse back */
176
177   hb_buffer_reverse (fixture->b);
178   for (i = 0; i < len; i++)
179     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[len-i]);
180
181   hb_buffer_reverse (fixture->b);
182   for (i = 0; i < len; i++)
183     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
184
185   /* reverse_clusters twice still should return the original string,
186    * but when applied once, the 1-2 cluster should be retained. */
187
188   hb_buffer_reverse_clusters (fixture->b);
189   for (i = 0; i < len; i++) {
190     unsigned int j = len-1-i;
191     if (j == 1)
192       j = 2;
193     else if (j == 2)
194       j = 1;
195     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+j]);
196   }
197
198   hb_buffer_reverse_clusters (fixture->b);
199   for (i = 0; i < len; i++)
200     g_assert_cmphex (glyphs[i].codepoint, ==, utf32[1+i]);
201 }
202
203 static void
204 test_buffer_positions (Fixture *fixture, gconstpointer user_data)
205 {
206   unsigned int i, len;
207   hb_glyph_position_t *positions;
208
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);
217   }
218 }
219
220 int
221 main (int argc, char **argv)
222 {
223   int i;
224
225   g_test_init (&argc, &argv, NULL);
226
227   for (i = 0; i < BUFFER_NUM_TYPES; i++) {
228 #define TEST_ADD(path, func) \
229     G_STMT_START { \
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); \
232       g_free (s); \
233     } G_STMT_END
234     TEST_ADD ("/buffer/properties", test_buffer_properties);
235     TEST_ADD ("/buffer/contents", test_buffer_contents);
236     TEST_ADD ("/buffer/positions", test_buffer_positions);
237 #undef TEST_ADD
238   }
239
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 */
244
245   return g_test_run();
246 }