2
0

test-common.c 14 KB

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