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   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 
50 static void
test_types_direction(void)51 test_types_direction (void)
52 {
53   g_assert_cmpint ((signed) HB_DIRECTION_INVALID, ==, 0);
54   g_assert_cmpint (HB_DIRECTION_LTR, !=, 0);
55 
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));
60   g_assert (!HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_INVALID));
61 
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));
66   g_assert (!HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_INVALID));
67 
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));
72   g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_INVALID));
73 
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));
78   g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_INVALID));
79 
80   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 
87   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);
91   //g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_INVALID), ==, HB_DIRECTION_INVALID);
92 
93   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));
102 
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)));
108 }
109 
110 static void
test_types_tag(void)111 test_types_tag (void)
112 {
113   g_assert_cmphex (HB_TAG_NONE, ==, 0);
114 
115   g_assert_cmphex (HB_TAG ('a','B','c','D'), ==, 0x61426344);
116 
117   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);
127 
128   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);
131 }
132 
133 static void
test_types_script(void)134 test_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 
145   g_assert_cmpint (HB_SCRIPT_INVALID, ==, (hb_script_t) HB_TAG_NONE);
146   g_assert_cmphex (HB_SCRIPT_ARABIC, !=, HB_SCRIPT_LATIN);
147 
148   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));
152 
153   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));
158 
159   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. */
164   g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_string ("wWyZ", -1));
165   g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_iso15924_tag (wWyZ));
166   /* Otherwise, UNKNOWN should be returned. */
167   g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x123", -1));
168   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);
175   g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_OLD_ITALIC), ==, HB_DIRECTION_INVALID);
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