blob: e9fae1351052bcacea8513e6616752ee5f2594bb [file] [log] [blame]
Behdad Esfahbod9faa9802011-04-11 12:46:49 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2011 Google, Inc.
Behdad Esfahbod9faa9802011-04-11 12:46:49 -04003 *
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
Behdad Esfahbod6af9cff2011-04-29 12:00:38 -040029/* Unit tests for hb-common.h */
30
Behdad Esfahbod9faa9802011-04-11 12:46:49 -040031
32static void
33test_types_int (void)
34{
Behdad Esfahbod9faa9802011-04-11 12:46:49 -040035 g_assert_cmpint (sizeof (int8_t), ==, 1);
36 g_assert_cmpint (sizeof (uint8_t), ==, 1);
37 g_assert_cmpint (sizeof (int16_t), ==, 2);
38 g_assert_cmpint (sizeof (uint16_t), ==, 2);
39 g_assert_cmpint (sizeof (int32_t), ==, 4);
40 g_assert_cmpint (sizeof (uint32_t), ==, 4);
41 g_assert_cmpint (sizeof (int64_t), ==, 8);
42 g_assert_cmpint (sizeof (uint64_t), ==, 8);
43
44 g_assert_cmpint (sizeof (hb_codepoint_t), ==, 4);
45 g_assert_cmpint (sizeof (hb_position_t), ==, 4);
46 g_assert_cmpint (sizeof (hb_mask_t), ==, 4);
47 g_assert_cmpint (sizeof (hb_var_int_t), ==, 4);
48}
49
50static void
51test_types_direction (void)
52{
Behdad Esfahbod4bf90f62012-04-12 17:38:23 -040053 g_assert_cmpint ((signed) HB_DIRECTION_INVALID, ==, 0);
54 g_assert_cmpint (HB_DIRECTION_LTR, !=, 0);
Behdad Esfahbod9faa9802011-04-11 12:46:49 -040055
56 g_assert (HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_LTR));
57 g_assert (HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_RTL));
58 g_assert (!HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_TTB));
59 g_assert (!HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_BTT));
Behdad Esfahbod4bf90f62012-04-12 17:38:23 -040060 g_assert (!HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_INVALID));
Behdad Esfahbod9faa9802011-04-11 12:46:49 -040061
62 g_assert (!HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_LTR));
63 g_assert (!HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_RTL));
64 g_assert (HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_TTB));
65 g_assert (HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_BTT));
Behdad Esfahbod4bf90f62012-04-12 17:38:23 -040066 g_assert (!HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_INVALID));
Behdad Esfahbod9faa9802011-04-11 12:46:49 -040067
68 g_assert (HB_DIRECTION_IS_FORWARD (HB_DIRECTION_LTR));
69 g_assert (HB_DIRECTION_IS_FORWARD (HB_DIRECTION_TTB));
70 g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_RTL));
71 g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_BTT));
Behdad Esfahbod4bf90f62012-04-12 17:38:23 -040072 g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_INVALID));
Behdad Esfahbod9faa9802011-04-11 12:46:49 -040073
74 g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_LTR));
75 g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_TTB));
76 g_assert (HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_RTL));
77 g_assert (HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_BTT));
Behdad Esfahbod4bf90f62012-04-12 17:38:23 -040078 g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_INVALID));
Behdad Esfahbod9faa9802011-04-11 12:46:49 -040079
Behdad Esfahbodfe28b992012-04-14 19:19:26 -040080 g_assert (HB_DIRECTION_IS_VALID (HB_DIRECTION_LTR));
81 g_assert (HB_DIRECTION_IS_VALID (HB_DIRECTION_TTB));
82 g_assert (HB_DIRECTION_IS_VALID (HB_DIRECTION_RTL));
83 g_assert (HB_DIRECTION_IS_VALID (HB_DIRECTION_BTT));
84 g_assert (!HB_DIRECTION_IS_VALID (HB_DIRECTION_INVALID));
85 g_assert (!HB_DIRECTION_IS_VALID ((hb_direction_t) 0x12345678));
86
Behdad Esfahbod9faa9802011-04-11 12:46:49 -040087 g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_LTR), ==, HB_DIRECTION_RTL);
88 g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_RTL), ==, HB_DIRECTION_LTR);
89 g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_TTB), ==, HB_DIRECTION_BTT);
90 g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_BTT), ==, HB_DIRECTION_TTB);
Behdad Esfahbod4bf90f62012-04-12 17:38:23 -040091 //g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_INVALID), ==, HB_DIRECTION_INVALID);
Behdad Esfahbod39a840a2011-04-27 14:48:19 -040092
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +020093 g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string (NULL, -1));
94 g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string ("", -1));
95 g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string ("t", 0));
96 g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string ("x", -1));
97 g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("r", -1));
98 g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("rtl", -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 ("right-to-left", -1));
101 g_assert_cmpint (HB_DIRECTION_TTB, ==, hb_direction_from_string ("ttb", -1));
Behdad Esfahbod654f88f2011-05-27 03:38:46 -0400102
103 g_assert (0 == strcmp ("ltr", hb_direction_to_string (HB_DIRECTION_LTR)));
104 g_assert (0 == strcmp ("rtl", hb_direction_to_string (HB_DIRECTION_RTL)));
105 g_assert (0 == strcmp ("ttb", hb_direction_to_string (HB_DIRECTION_TTB)));
106 g_assert (0 == strcmp ("btt", hb_direction_to_string (HB_DIRECTION_BTT)));
107 g_assert (0 == strcmp ("invalid", hb_direction_to_string (HB_DIRECTION_INVALID)));
Behdad Esfahbod9faa9802011-04-11 12:46:49 -0400108}
109
110static void
111test_types_tag (void)
112{
113 g_assert_cmphex (HB_TAG_NONE, ==, 0);
Behdad Esfahbod7ff74012011-04-11 13:27:30 -0400114
Behdad Esfahbod9faa9802011-04-11 12:46:49 -0400115 g_assert_cmphex (HB_TAG ('a','B','c','D'), ==, 0x61426344);
116
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200117 g_assert_cmphex (hb_tag_from_string ("aBcDe", -1), ==, 0x61426344);
118 g_assert_cmphex (hb_tag_from_string ("aBcD", -1), ==, 0x61426344);
119 g_assert_cmphex (hb_tag_from_string ("aBc", -1), ==, 0x61426320);
120 g_assert_cmphex (hb_tag_from_string ("aB", -1), ==, 0x61422020);
121 g_assert_cmphex (hb_tag_from_string ("a", -1), ==, 0x61202020);
122 g_assert_cmphex (hb_tag_from_string ("aBcDe", 1), ==, 0x61202020);
123 g_assert_cmphex (hb_tag_from_string ("aBcDe", 2), ==, 0x61422020);
124 g_assert_cmphex (hb_tag_from_string ("aBcDe", 3), ==, 0x61426320);
125 g_assert_cmphex (hb_tag_from_string ("aBcDe", 4), ==, 0x61426344);
126 g_assert_cmphex (hb_tag_from_string ("aBcDe", 4), ==, 0x61426344);
Behdad Esfahbod7ff74012011-04-11 13:27:30 -0400127
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200128 g_assert_cmphex (hb_tag_from_string ("", -1), ==, HB_TAG_NONE);
129 g_assert_cmphex (hb_tag_from_string ("x", 0), ==, HB_TAG_NONE);
130 g_assert_cmphex (hb_tag_from_string (NULL, -1), ==, HB_TAG_NONE);
Behdad Esfahbod9faa9802011-04-11 12:46:49 -0400131}
132
Behdad Esfahboddb60c962011-04-11 16:17:02 -0400133static void
134test_types_script (void)
135{
136 hb_tag_t arab = HB_TAG_CHAR4 ("arab");
137 hb_tag_t Arab = HB_TAG_CHAR4 ("Arab");
138 hb_tag_t ARAB = HB_TAG_CHAR4 ("ARAB");
139
140 hb_tag_t wWyZ = HB_TAG_CHAR4 ("wWyZ");
141 hb_tag_t Wwyz = HB_TAG_CHAR4 ("Wwyz");
142
143 hb_tag_t x123 = HB_TAG_CHAR4 ("x123");
144
Behdad Esfahbod39a840a2011-04-27 14:48:19 -0400145 g_assert_cmpint (HB_SCRIPT_INVALID, ==, (hb_script_t) HB_TAG_NONE);
Behdad Esfahboddb60c962011-04-11 16:17:02 -0400146 g_assert_cmphex (HB_SCRIPT_ARABIC, !=, HB_SCRIPT_LATIN);
147
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200148 g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string (NULL, -1));
149 g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string ("", -1));
150 g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string ("x", 0));
151 g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x", -1));
Behdad Esfahbod39a840a2011-04-27 14:48:19 -0400152
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200153 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("arab", -1));
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 ("Arabic", 6));
157 g_assert_cmphex (HB_SCRIPT_ARABIC, !=, hb_script_from_string ("Arabic", 3));
Behdad Esfahbod39a840a2011-04-27 14:48:19 -0400158
Behdad Esfahboddb60c962011-04-11 16:17:02 -0400159 g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (arab));
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
163 /* Arbitrary tags that look like may be valid ISO 15924 should be preserved. */
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200164 g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_string ("wWyZ", -1));
Behdad Esfahboddb60c962011-04-11 16:17:02 -0400165 g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_iso15924_tag (wWyZ));
166 /* Otherwise, UNKNOWN should be returned. */
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200167 g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x123", -1));
Behdad Esfahboddb60c962011-04-11 16:17:02 -0400168 g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_iso15924_tag (x123));
169
170 g_assert_cmphex (hb_script_to_iso15924_tag (HB_SCRIPT_ARABIC), ==, Arab);
171 g_assert_cmphex (hb_script_to_iso15924_tag (hb_script_from_iso15924_tag (wWyZ)), ==, Wwyz);
172
173 g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_LATIN), ==, HB_DIRECTION_LTR);
174 g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_ARABIC), ==, HB_DIRECTION_RTL);
Behdad Esfahbodf673cfb2018-05-07 13:58:32 -0700175 g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_OLD_ITALIC), ==, HB_DIRECTION_INVALID);
Behdad Esfahboddb60c962011-04-11 16:17:02 -0400176 g_assert_cmpint (hb_script_get_horizontal_direction (hb_script_from_iso15924_tag (wWyZ)), ==, HB_DIRECTION_LTR);
177}
178
Behdad Esfahbod09125572011-04-11 17:49:33 -0400179static void
180test_types_language (void)
181{
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200182 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);
Behdad Esfahbod09125572011-04-11 17:49:33 -0400186
Behdad Esfahbod1a64f6e2011-05-13 22:55:32 -0400187 g_assert (HB_LANGUAGE_INVALID == NULL);
188
Behdad Esfahbod09125572011-04-11 17:49:33 -0400189 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 */
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200197 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));
Behdad Esfahbod09125572011-04-11 17:49:33 -0400200
Behdad Esfahbod4c9fe882011-08-26 09:18:53 +0200201 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));
Behdad Esfahbod1a64f6e2011-05-13 22:55:32 -0400205 g_assert (NULL == hb_language_to_string (HB_LANGUAGE_INVALID));
Behdad Esfahbod34fb5522011-05-06 00:04:28 -0400206
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. */
Behdad Esfahbod1a64f6e2011-05-13 22:55:32 -0400210 g_assert (HB_LANGUAGE_INVALID != hb_language_get_default ());
Behdad Esfahbod09125572011-04-11 17:49:33 -0400211}
212
Behdad Esfahbod9faa9802011-04-11 12:46:49 -0400213int
Behdad Esfahbod9385caa2011-04-11 19:43:51 -0400214main (int argc, char **argv)
Behdad Esfahbod9faa9802011-04-11 12:46:49 -0400215{
Behdad Esfahbodaafe3952011-04-28 17:10:44 -0400216 hb_test_init (&argc, &argv);
Behdad Esfahbod9faa9802011-04-11 12:46:49 -0400217
Behdad Esfahbodaafe3952011-04-28 17:10:44 -0400218 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);
Behdad Esfahbod9faa9802011-04-11 12:46:49 -0400223
Behdad Esfahbodaafe3952011-04-28 17:10:44 -0400224 return hb_test_run();
Behdad Esfahbod9faa9802011-04-11 12:46:49 -0400225}