Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2011 Google, Inc. |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 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 | |
Behdad Esfahbod | 6af9cff | 2011-04-29 12:00:38 -0400 | [diff] [blame] | 29 | /* Unit tests for hb-common.h */ |
| 30 | |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 31 | |
| 32 | static void |
| 33 | test_types_int (void) |
| 34 | { |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 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 |
| 51 | test_types_direction (void) |
| 52 | { |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 53 | g_assert_cmpint ((signed) HB_DIRECTION_INVALID, ==, 0); |
| 54 | g_assert_cmpint (HB_DIRECTION_LTR, !=, 0); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 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)); |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 60 | g_assert (!HB_DIRECTION_IS_HORIZONTAL (HB_DIRECTION_INVALID)); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 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)); |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 66 | g_assert (!HB_DIRECTION_IS_VERTICAL (HB_DIRECTION_INVALID)); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 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)); |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 72 | g_assert (!HB_DIRECTION_IS_FORWARD (HB_DIRECTION_INVALID)); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 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)); |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 78 | g_assert (!HB_DIRECTION_IS_BACKWARD (HB_DIRECTION_INVALID)); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 79 | |
Behdad Esfahbod | fe28b99 | 2012-04-14 19:19:26 -0400 | [diff] [blame] | 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 | |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 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); |
Behdad Esfahbod | 4bf90f6 | 2012-04-12 17:38:23 -0400 | [diff] [blame] | 91 | //g_assert_cmpint (HB_DIRECTION_REVERSE (HB_DIRECTION_INVALID), ==, HB_DIRECTION_INVALID); |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 92 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 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)); |
Behdad Esfahbod | 654f88f | 2011-05-27 03:38:46 -0400 | [diff] [blame] | 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))); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void |
| 111 | test_types_tag (void) |
| 112 | { |
| 113 | g_assert_cmphex (HB_TAG_NONE, ==, 0); |
Behdad Esfahbod | 7ff7401 | 2011-04-11 13:27:30 -0400 | [diff] [blame] | 114 | |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 115 | g_assert_cmphex (HB_TAG ('a','B','c','D'), ==, 0x61426344); |
| 116 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 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); |
Behdad Esfahbod | 7ff7401 | 2011-04-11 13:27:30 -0400 | [diff] [blame] | 127 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 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); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Behdad Esfahbod | db60c96 | 2011-04-11 16:17:02 -0400 | [diff] [blame] | 133 | static 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 | |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 145 | g_assert_cmpint (HB_SCRIPT_INVALID, ==, (hb_script_t) HB_TAG_NONE); |
Behdad Esfahbod | db60c96 | 2011-04-11 16:17:02 -0400 | [diff] [blame] | 146 | g_assert_cmphex (HB_SCRIPT_ARABIC, !=, HB_SCRIPT_LATIN); |
| 147 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 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)); |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 152 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 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)); |
Behdad Esfahbod | 39a840a | 2011-04-27 14:48:19 -0400 | [diff] [blame] | 158 | |
Behdad Esfahbod | db60c96 | 2011-04-11 16:17:02 -0400 | [diff] [blame] | 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. */ |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 164 | g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_string ("wWyZ", -1)); |
Behdad Esfahbod | db60c96 | 2011-04-11 16:17:02 -0400 | [diff] [blame] | 165 | g_assert_cmphex (HB_SCRIPT_UNKNOWN, !=, hb_script_from_iso15924_tag (wWyZ)); |
| 166 | /* Otherwise, UNKNOWN should be returned. */ |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 167 | g_assert_cmphex (HB_SCRIPT_UNKNOWN, ==, hb_script_from_string ("x123", -1)); |
Behdad Esfahbod | db60c96 | 2011-04-11 16:17:02 -0400 | [diff] [blame] | 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); |
Behdad Esfahbod | f673cfb | 2018-05-07 13:58:32 -0700 | [diff] [blame] | 175 | g_assert_cmpint (hb_script_get_horizontal_direction (HB_SCRIPT_OLD_ITALIC), ==, HB_DIRECTION_INVALID); |
Behdad Esfahbod | db60c96 | 2011-04-11 16:17:02 -0400 | [diff] [blame] | 176 | g_assert_cmpint (hb_script_get_horizontal_direction (hb_script_from_iso15924_tag (wWyZ)), ==, HB_DIRECTION_LTR); |
| 177 | } |
| 178 | |
Behdad Esfahbod | 0912557 | 2011-04-11 17:49:33 -0400 | [diff] [blame] | 179 | static void |
| 180 | test_types_language (void) |
| 181 | { |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 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); |
Behdad Esfahbod | 0912557 | 2011-04-11 17:49:33 -0400 | [diff] [blame] | 186 | |
Behdad Esfahbod | 1a64f6e | 2011-05-13 22:55:32 -0400 | [diff] [blame] | 187 | g_assert (HB_LANGUAGE_INVALID == NULL); |
| 188 | |
Behdad Esfahbod | 0912557 | 2011-04-11 17:49:33 -0400 | [diff] [blame] | 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 */ |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 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)); |
Behdad Esfahbod | 0912557 | 2011-04-11 17:49:33 -0400 | [diff] [blame] | 200 | |
Behdad Esfahbod | 4c9fe88 | 2011-08-26 09:18:53 +0200 | [diff] [blame] | 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)); |
Behdad Esfahbod | 1a64f6e | 2011-05-13 22:55:32 -0400 | [diff] [blame] | 205 | g_assert (NULL == hb_language_to_string (HB_LANGUAGE_INVALID)); |
Behdad Esfahbod | 34fb552 | 2011-05-06 00:04:28 -0400 | [diff] [blame] | 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. */ |
Behdad Esfahbod | 1a64f6e | 2011-05-13 22:55:32 -0400 | [diff] [blame] | 210 | g_assert (HB_LANGUAGE_INVALID != hb_language_get_default ()); |
Behdad Esfahbod | 0912557 | 2011-04-11 17:49:33 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 213 | int |
Behdad Esfahbod | 9385caa | 2011-04-11 19:43:51 -0400 | [diff] [blame] | 214 | main (int argc, char **argv) |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 215 | { |
Behdad Esfahbod | aafe395 | 2011-04-28 17:10:44 -0400 | [diff] [blame] | 216 | hb_test_init (&argc, &argv); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 217 | |
Behdad Esfahbod | aafe395 | 2011-04-28 17:10:44 -0400 | [diff] [blame] | 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); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 223 | |
Behdad Esfahbod | aafe395 | 2011-04-28 17:10:44 -0400 | [diff] [blame] | 224 | return hb_test_run(); |
Behdad Esfahbod | 9faa980 | 2011-04-11 12:46:49 -0400 | [diff] [blame] | 225 | } |