Update Copyright headers
[apps/home/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     unsigned int cluster;
134     cluster = 1+i;
135     if (i >= 2) {
136       if (buffer_type == BUFFER_UTF16)
137         cluster++;
138       else if (buffer_type == BUFFER_UTF8)
139         cluster += 3;
140     }
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);
146   }
147 }
148
149 static void
150 test_buffer_positions (Fixture *fixture, gconstpointer user_data)
151 {
152   unsigned int i, len;
153   hb_glyph_position_t *positions;
154
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);
163   }
164 }
165
166 int
167 main (int argc, char **argv)
168 {
169   int i;
170
171   g_test_init (&argc, &argv, NULL);
172
173   for (i = 0; i < BUFFER_NUM_TYPES; i++) {
174 #define TEST_ADD(path, func) \
175     G_STMT_START { \
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); \
178       g_free (s); \
179     } G_STMT_END
180     TEST_ADD ("/buffer/properties", test_buffer_properties);
181     TEST_ADD ("/buffer/contents", test_buffer_contents);
182     TEST_ADD ("/buffer/positions", test_buffer_positions);
183 #undef TEST_ADD
184   }
185
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 */
191
192   return g_test_run();
193 }