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-common.h */
30
31
32 static void
test_types_int(void)33 test_types_int (void)
34 {
35 /* We already ASSERT_STATIC these in hb-private.h, but anyway */
36 g_assert_cmpint (sizeof (int8_t), ==, 1);
37 g_assert_cmpint (sizeof (uint8_t), ==, 1);
38 g_assert_cmpint (sizeof (int16_t), ==, 2);
39 g_assert_cmpint (sizeof (uint16_t), ==, 2);
40 g_assert_cmpint (sizeof (int32_t), ==, 4);
41 g_assert_cmpint (sizeof (uint32_t), ==, 4);
42 g_assert_cmpint (sizeof (int64_t), ==, 8);
43 g_assert_cmpint (sizeof (uint64_t), ==, 8);
44
45 g_assert_cmpint (sizeof (hb_codepoint_t), ==, 4);
46 g_assert_cmpint (sizeof (hb_position_t), ==, 4);
47 g_assert_cmpint (sizeof (hb_mask_t), ==, 4);
48 g_assert_cmpint (sizeof (hb_var_int_t), ==, 4);
49 }
50
51 static void
test_types_direction(void)52 test_types_direction (void)
53 {
54 g_assert_cmpint ((signed) HB_DIRECTION_INVALID, ==, 0);
55 g_assert_cmpint (HB_DIRECTION_LTR, !=, 0);
56
57 g_assert (HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_LTR));
58 g_assert (HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_RTL));
59 g_assert (!HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_TTB));
60 g_assert (!HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_BTT));
61 g_assert (!HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_INVALID));
62
63 g_assert (!HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_LTR));
64 g_assert (!HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_RTL));
65 g_assert (HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_TTB));
66 g_assert (HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_BTT));
67 g_assert (!HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_INVALID));
68
69 g_assert (HB_DIRECTION_IS_FORWARD (HB_DIRECTION_LTR));
70 g_assert (HB_DIRECTION_IS_FORWARD (HB_DIRECTION_TTB));
71 g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_RTL));
72 g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_BTT));
73 g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_INVALID));
74
75 g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_LTR));
76 g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_TTB));
77 g_assert (HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_RTL));
78 g_assert (HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_BTT));
79 g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_INVALID));
80
81 g_assert (HB_DIRECTION_IS_VALID (HB_DIRECTION_LTR));
82 g_assert (HB_DIRECTION_IS_VALID (HB_DIRECTION_TTB));
83 g_assert (HB_DIRECTION_IS_VALID (HB_DIRECTION_RTL));
84 g_assert (HB_DIRECTION_IS_VALID (HB_DIRECTION_BTT));
85 g_assert (!HB_DIRECTION_IS_VALID (HB_DIRECTION_INVALID));
86 g_assert (!HB_DIRECTION_IS_VALID ((hb_direction_t) 0x12345678));
87
88 g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_LTR), ==, HB_DIRECTION_RTL);
89 g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_RTL), ==, HB_DIRECTION_LTR);
90 g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_TTB), ==, HB_DIRECTION_BTT);
91 g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_BTT), ==, HB_DIRECTION_TTB);
92 //g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_INVALID), ==, HB_DIRECTION_INVALID);
93
94 g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string (NULL, -1));
95 g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string ("", -1));
96 g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string ("t", 0));
97 g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string ("x", -1));
98 g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("r", -1));
99 g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("rtl", -1));
100 g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("RtL", -1));
101 g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("right-to-left", -1));
102 g_assert_cmpint (HB_DIRECTION_TTB, ==, hb_direction_from_string ("ttb", -1));
103
104 g_assert (0 == strcmp ("ltr", hb_direction_to_string (HB_DIRECTION_LTR)));
105 g_assert (0 == strcmp ("rtl", hb_direction_to_string (HB_DIRECTION_RTL)));
106 g_assert (0 == strcmp ("ttb", hb_direction_to_string (HB_DIRECTION_TTB)));
107 g_assert (0 == strcmp ("btt", hb_direction_to_string (HB_DIRECTION_BTT)));
108 g_assert (0 == strcmp ("invalid", hb_direction_to_string (HB_DIRECTION_INVALID)));
109 }
110
111 static void
test_types_tag(void)112 test_types_tag (void)
113 {
114 g_assert_cmphex (HB_TAG_NONE, ==, 0);
115
116 g_assert_cmphex (HB_TAG ('a','B','c','D'), ==, 0x61426344);
117
118 g_assert_cmphex (hb_tag_from_string ("aBcDe", -1), ==, 0x61426344);
119 g_assert_cmphex (hb_tag_from_string ("aBcD", -1), ==, 0x61426344);
120 g_assert_cmphex (hb_tag_from_string ("aBc", -1), ==, 0x61426320);
121 g_assert_cmphex (hb_tag_from_string ("aB", -1), ==, 0x61422020);
122 g_assert_cmphex (hb_tag_from_string ("a", -1), ==, 0x61202020);
123 g_assert_cmphex (hb_tag_from_string ("aBcDe", 1), ==, 0x61202020);
124 g_assert_cmphex (hb_tag_from_string ("aBcDe", 2), ==, 0x61422020);
125 g_assert_cmphex (hb_tag_from_string ("aBcDe", 3), ==, 0x61426320);
126 g_assert_cmphex (hb_tag_from_string ("aBcDe", 4), ==, 0x61426344);
127 g_assert_cmphex (hb_tag_from_string ("aBcDe", 4), ==, 0x61426344);
128
129 g_assert_cmphex (hb_tag_from_string ("", -1), ==, HB_TAG_NONE);
130 g_assert_cmphex (hb_tag_from_string ("x", 0), ==, HB_TAG_NONE);
131 g_assert_cmphex (hb_tag_from_string (NULL, -1), ==, HB_TAG_NONE);
132 }
133
134 static void
test_types_script(void)135 test_types_script (void)
136 {
137 hb_tag_t arab = HB_TAG_CHAR4 ("arab");
138 hb_tag_t Arab = HB_TAG_CHAR4 ("Arab");
139 hb_tag_t ARAB = HB_TAG_CHAR4 ("ARAB");
140
141 hb_tag_t wWyZ = HB_TAG_CHAR4 ("wWyZ");
142 hb_tag_t Wwyz = HB_TAG_CHAR4 ("Wwyz");
143
144 hb_tag_t x123 = HB_TAG_CHAR4 ("x123");
145
146 g_assert_cmpint (HB_SCRIPT_INVALID, ==, (hb_script_t) HB_TAG_NONE);
147 g_assert_cmphex (HB_SCRIPT_ARABIC, !=, HB_SCRIPT_LATIN);
148
149 g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string (NULL, -1));
150 g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string ("", -1));
151 g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string ("x", 0));
152 g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x", -1));
153
154 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("arab", -1));
155 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("Arab", -1));
156 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("ARAB", -1));
157 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("Arabic", 6));
158 g_assert_cmphex (HB_SCRIPT_ARABIC, !=, hb_script_from_string ("Arabic", 3));
159
160 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (arab));
161 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (Arab));
162 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (ARAB));
163
164 /* Arbitrary tags that look like may be valid ISO 15924 should be preserved. */
165 g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_string ("wWyZ", -1));
166 g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_iso15924_tag (wWyZ));
167 /* Otherwise, UNKNOWN should be returned. */
168 g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x123", -1));
169 g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_iso15924_tag (x123));
170
171 g_assert_cmphex (hb_script_to_iso15924_tag (HB_SCRIPT_ARABIC), ==, Arab);
172 g_assert_cmphex (hb_script_to_iso15924_tag (hb_script_from_iso15924_tag (wWyZ)), ==, Wwyz);
173
174 g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_LATIN), ==, HB_DIRECTION_LTR);
175 g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_ARABIC), ==, HB_DIRECTION_RTL);
176 g_assert_cmpint (hb_script_get_horizontal_direction (hb_script_from_iso15924_tag (wWyZ)), ==, HB_DIRECTION_LTR);
177 }
178
179 static void
test_types_language(void)180 test_types_language (void)
181 {
182 hb_language_t fa = hb_language_from_string ("fa", -1);
183 hb_language_t fa_IR = hb_language_from_string ("fa_IR", -1);
184 hb_language_t fa_ir = hb_language_from_string ("fa-ir", -1);
185 hb_language_t en = hb_language_from_string ("en", -1);
186
187 g_assert (HB_LANGUAGE_INVALID == NULL);
188
189 g_assert (fa != NULL);
190 g_assert (fa_IR != NULL);
191 g_assert (fa_IR == fa_ir);
192
193 g_assert (en != NULL);
194 g_assert (en != fa);
195
196 /* Test recall */
197 g_assert (en == hb_language_from_string ("en", -1));
198 g_assert (en == hb_language_from_string ("eN", -1));
199 g_assert (en == hb_language_from_string ("Enx", 2));
200
201 g_assert (HB_LANGUAGE_INVALID == hb_language_from_string (NULL, -1));
202 g_assert (HB_LANGUAGE_INVALID == hb_language_from_string ("", -1));
203 g_assert (HB_LANGUAGE_INVALID == hb_language_from_string ("en", 0));
204 g_assert (HB_LANGUAGE_INVALID != hb_language_from_string ("en", 1));
205 g_assert (NULL == hb_language_to_string (HB_LANGUAGE_INVALID));
206
207 /* Not sure how to test this better. Setting env vars
208 * here doesn't sound like the right approach, and I'm
209 * not sure that it even works. */
210 g_assert (HB_LANGUAGE_INVALID != hb_language_get_default ());
211 }
212
213 int
main(int argc,char ** argv)214 main (int argc, char **argv)
215 {
216 hb_test_init (&argc, &argv);
217
218 hb_test_add (test_types_int);
219 hb_test_add (test_types_direction);
220 hb_test_add (test_types_tag);
221 hb_test_add (test_types_script);
222 hb_test_add (test_types_language);
223
224 return hb_test_run();
225 }
226