d5cbfc7825a1f73d7c9a0a55cabcc90cf297631a
[framework/uifw/harfbuzz.git] / test / test-common.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 /* Unit tests for hb-common.h */
30
31
32 static 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
52 test_types_direction (void)
53 {
54   g_assert_cmpint ((signed) HB_DIRECTION_INVALID, ==, -1);
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
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
67   g_assert (HB_DIRECTION_IS_FORWARD (HB_DIRECTION_LTR));
68   g_assert (HB_DIRECTION_IS_FORWARD (HB_DIRECTION_TTB));
69   g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_RTL));
70   g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_BTT));
71
72   g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_LTR));
73   g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_TTB));
74   g_assert (HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_RTL));
75   g_assert (HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_BTT));
76
77   g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_LTR), ==, HB_DIRECTION_RTL);
78   g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_RTL), ==, HB_DIRECTION_LTR);
79   g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_TTB), ==, HB_DIRECTION_BTT);
80   g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_BTT), ==, HB_DIRECTION_TTB);
81
82   g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string (NULL));
83   g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string (""));
84   g_assert_cmpint (HB_DIRECTION_INVALID, ==, hb_direction_from_string ("x"));
85   g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("r"));
86   g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("rtl"));
87   g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("RtL"));
88   g_assert_cmpint (HB_DIRECTION_RTL, ==, hb_direction_from_string ("right-to-left"));
89   g_assert_cmpint (HB_DIRECTION_TTB, ==, hb_direction_from_string ("ttb"));
90
91   g_assert (0 == strcmp ("ltr", hb_direction_to_string (HB_DIRECTION_LTR)));
92   g_assert (0 == strcmp ("rtl", hb_direction_to_string (HB_DIRECTION_RTL)));
93   g_assert (0 == strcmp ("ttb", hb_direction_to_string (HB_DIRECTION_TTB)));
94   g_assert (0 == strcmp ("btt", hb_direction_to_string (HB_DIRECTION_BTT)));
95   g_assert (0 == strcmp ("invalid", hb_direction_to_string (HB_DIRECTION_INVALID)));
96 }
97
98 static void
99 test_types_tag (void)
100 {
101   g_assert_cmphex (HB_TAG_NONE, ==, 0);
102
103   g_assert_cmphex (HB_TAG ('a','B','c','D'), ==, 0x61426344);
104
105   g_assert_cmphex (hb_tag_from_string ("aBcDe"), ==, 0x61426344);
106   g_assert_cmphex (hb_tag_from_string ("aBcD"),  ==, 0x61426344);
107   g_assert_cmphex (hb_tag_from_string ("aBc"),   ==, 0x61426320);
108   g_assert_cmphex (hb_tag_from_string ("aB"),    ==, 0x61422020);
109   g_assert_cmphex (hb_tag_from_string ("a"),     ==, 0x61202020);
110
111   g_assert_cmphex (hb_tag_from_string (""),      ==, HB_TAG_NONE);
112   g_assert_cmphex (hb_tag_from_string (NULL),    ==, HB_TAG_NONE);
113 }
114
115 static void
116 test_types_script (void)
117 {
118   hb_tag_t arab = HB_TAG_CHAR4 ("arab");
119   hb_tag_t Arab = HB_TAG_CHAR4 ("Arab");
120   hb_tag_t ARAB = HB_TAG_CHAR4 ("ARAB");
121
122   hb_tag_t wWyZ = HB_TAG_CHAR4 ("wWyZ");
123   hb_tag_t Wwyz = HB_TAG_CHAR4 ("Wwyz");
124
125   hb_tag_t x123 = HB_TAG_CHAR4 ("x123");
126
127   g_assert_cmpint (HB_SCRIPT_INVALID, ==, (hb_script_t) HB_TAG_NONE);
128   g_assert_cmphex (HB_SCRIPT_ARABIC, !=, HB_SCRIPT_LATIN);
129
130   g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string (NULL));
131   g_assert_cmphex (HB_SCRIPT_INVALID, ==, hb_script_from_string (""));
132   g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x"));
133
134   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("arab"));
135   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("Arab"));
136   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_string ("ARAB"));
137
138   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (arab));
139   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (Arab));
140   g_assert_cmphex (HB_SCRIPT_ARABIC, ==, hb_script_from_iso15924_tag (ARAB));
141
142   /* Arbitrary tags that look like may be valid ISO 15924 should be preserved. */
143   g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_string ("wWyZ"));
144   g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_iso15924_tag (wWyZ));
145   /* Otherwise, UNKNOWN should be returned. */
146   g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x123"));
147   g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_iso15924_tag (x123));
148
149   g_assert_cmphex (hb_script_to_iso15924_tag (HB_SCRIPT_ARABIC), ==, Arab);
150   g_assert_cmphex (hb_script_to_iso15924_tag (hb_script_from_iso15924_tag (wWyZ)), ==, Wwyz);
151
152   g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_LATIN), ==, HB_DIRECTION_LTR);
153   g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_ARABIC), ==, HB_DIRECTION_RTL);
154   g_assert_cmpint (hb_script_get_horizontal_direction (hb_script_from_iso15924_tag (wWyZ)), ==, HB_DIRECTION_LTR);
155 }
156
157 static void
158 test_types_language (void)
159 {
160   hb_language_t fa = hb_language_from_string ("fa");
161   hb_language_t fa_IR = hb_language_from_string ("fa_IR");
162   hb_language_t fa_ir = hb_language_from_string ("fa-ir");
163   hb_language_t en = hb_language_from_string ("en");
164
165   g_assert (HB_LANGUAGE_INVALID == NULL);
166
167   g_assert (fa != NULL);
168   g_assert (fa_IR != NULL);
169   g_assert (fa_IR == fa_ir);
170
171   g_assert (en != NULL);
172   g_assert (en != fa);
173
174   /* Test recall */
175   g_assert (en == hb_language_from_string ("en"));
176   g_assert (en == hb_language_from_string ("eN"));
177
178   g_assert (HB_LANGUAGE_INVALID == hb_language_from_string (NULL));
179   g_assert (HB_LANGUAGE_INVALID == hb_language_from_string (""));
180   g_assert (NULL == hb_language_to_string (HB_LANGUAGE_INVALID));
181
182   /* Not sure how to test this better.  Setting env vars
183    * here doesn't sound like the right approach, and I'm
184    * not sure that it even works. */
185   g_assert (HB_LANGUAGE_INVALID != hb_language_get_default ());
186 }
187
188 int
189 main (int argc, char **argv)
190 {
191   hb_test_init (&argc, &argv);
192
193   hb_test_add (test_types_int);
194   hb_test_add (test_types_direction);
195   hb_test_add (test_types_tag);
196   hb_test_add (test_types_script);
197   hb_test_add (test_types_language);
198
199   return hb_test_run();
200 }