|
| 1 | +/* Copyright JS Foundation and other contributors, http://js.foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "jerryscript.h" |
| 17 | + |
| 18 | +#include "test-common.h" |
| 19 | + |
| 20 | +static bool |
| 21 | +strict_equals (jerry_value_t a, /**< the first string to compare */ |
| 22 | + jerry_value_t b) /**< the second string to compare */ |
| 23 | +{ |
| 24 | + const jerry_char_t is_equal_src[] = "var isEqual = function(a, b) { return (a === b); }; isEqual"; |
| 25 | + jerry_value_t is_equal_fn_val = jerry_eval (is_equal_src, sizeof (is_equal_src) - 1, JERRY_PARSE_NO_OPTS); |
| 26 | + TEST_ASSERT (!jerry_value_is_error (is_equal_fn_val)); |
| 27 | + jerry_value_t args[2] = {a, b}; |
| 28 | + jerry_value_t res = jerry_call_function (is_equal_fn_val, jerry_create_undefined (), args, 2); |
| 29 | + TEST_ASSERT (!jerry_value_is_error (res)); |
| 30 | + TEST_ASSERT (jerry_value_is_boolean (res)); |
| 31 | + bool is_strict_equal = jerry_get_boolean_value (res); |
| 32 | + jerry_release_value (res); |
| 33 | + jerry_release_value (is_equal_fn_val); |
| 34 | + return is_strict_equal; |
| 35 | +} /* strict_equals */ |
| 36 | + |
| 37 | +int |
| 38 | +main (void) |
| 39 | +{ |
| 40 | + jerry_size_t sz, utf8_sz, cesu8_sz; |
| 41 | + jerry_length_t cesu8_length, utf8_length; |
| 42 | + jerry_value_t args[2]; |
| 43 | + |
| 44 | + TEST_INIT (); |
| 45 | + jerry_init (JERRY_INIT_EMPTY); |
| 46 | + |
| 47 | + /* Test corner case for jerry_string_to_char_buffer */ |
| 48 | + args[0] = jerry_create_string ((jerry_char_t *) ""); |
| 49 | + sz = jerry_get_string_size (args[0]); |
| 50 | + TEST_ASSERT (sz == 0); |
| 51 | + jerry_release_value (args[0]); |
| 52 | + |
| 53 | + /* Test create_jerry_string_from_utf8 with 4-byte long unicode sequences, |
| 54 | + * test string: 'str: {DESERET CAPITAL LETTER LONG I}' |
| 55 | + */ |
| 56 | + args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x73\x74\x72\x3a \xf0\x90\x90\x80"); |
| 57 | + args[1] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80"); |
| 58 | + |
| 59 | + /* These sizes must be equal */ |
| 60 | + utf8_sz = jerry_get_string_size (args[0]); |
| 61 | + cesu8_sz = jerry_get_string_size (args[1]); |
| 62 | + |
| 63 | + char string_from_utf8[utf8_sz]; |
| 64 | + char string_from_cesu8[cesu8_sz]; |
| 65 | + |
| 66 | + jerry_string_to_char_buffer (args[0], (jerry_char_t *) string_from_utf8, utf8_sz); |
| 67 | + jerry_string_to_char_buffer (args[1], (jerry_char_t *) string_from_cesu8, cesu8_sz); |
| 68 | + |
| 69 | + TEST_ASSERT (utf8_sz == cesu8_sz); |
| 70 | + |
| 71 | + TEST_ASSERT (!strncmp (string_from_utf8, string_from_cesu8, utf8_sz)); |
| 72 | + jerry_release_value (args[0]); |
| 73 | + jerry_release_value (args[1]); |
| 74 | + |
| 75 | + /* Test jerry_string_to_utf8_char_buffer, test string: 'str: {DESERET CAPITAL LETTER LONG I}' */ |
| 76 | + args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x73\x74\x72\x3a \xf0\x90\x90\x80"); |
| 77 | + args[1] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80"); |
| 78 | + |
| 79 | + /* Test that the strings are equal / ensure hashes are equal */ |
| 80 | + TEST_ASSERT (strict_equals (args[0], args[1])); |
| 81 | + |
| 82 | + /* These sizes must be equal */ |
| 83 | + utf8_sz = jerry_get_utf8_string_size (args[0]); |
| 84 | + cesu8_sz = jerry_get_utf8_string_size (args[1]); |
| 85 | + |
| 86 | + TEST_ASSERT (utf8_sz == cesu8_sz); |
| 87 | + |
| 88 | + char string_from_utf8_string[utf8_sz]; |
| 89 | + char string_from_cesu8_string[cesu8_sz]; |
| 90 | + |
| 91 | + jerry_string_to_utf8_char_buffer (args[0], (jerry_char_t *) string_from_utf8_string, utf8_sz); |
| 92 | + jerry_string_to_utf8_char_buffer (args[1], (jerry_char_t *) string_from_cesu8_string, cesu8_sz); |
| 93 | + |
| 94 | + TEST_ASSERT (!strncmp (string_from_utf8_string, string_from_cesu8_string, utf8_sz)); |
| 95 | + jerry_release_value (args[0]); |
| 96 | + jerry_release_value (args[1]); |
| 97 | + |
| 98 | + /* Test string: 'str: {MATHEMATICAL FRAKTUR SMALL F}{MATHEMATICAL FRAKTUR SMALL G}' */ |
| 99 | + args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4"); |
| 100 | + |
| 101 | + cesu8_length = jerry_get_string_length (args[0]); |
| 102 | + utf8_length = jerry_get_utf8_string_length (args[0]); |
| 103 | + |
| 104 | + cesu8_sz = jerry_get_string_size (args[0]); |
| 105 | + utf8_sz = jerry_get_utf8_string_size (args[0]); |
| 106 | + |
| 107 | + TEST_ASSERT (cesu8_length == 10 && utf8_length == 8); |
| 108 | + TEST_ASSERT (cesu8_sz != utf8_sz); |
| 109 | + TEST_ASSERT (utf8_sz == 14 && cesu8_sz == 18); |
| 110 | + |
| 111 | + char test_string[utf8_sz]; |
| 112 | + |
| 113 | + TEST_ASSERT (jerry_string_to_utf8_char_buffer (args[0], (jerry_char_t *) test_string, utf8_sz) == 14); |
| 114 | + TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4", utf8_sz)); |
| 115 | + |
| 116 | + sz = jerry_substring_to_utf8_char_buffer (args[0], 0, utf8_length, (jerry_char_t *) test_string, utf8_sz); |
| 117 | + TEST_ASSERT (sz == 14); |
| 118 | + TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4", sz)); |
| 119 | + |
| 120 | + sz = jerry_substring_to_utf8_char_buffer (args[0], 0, utf8_length + 1, (jerry_char_t *) test_string, utf8_sz); |
| 121 | + TEST_ASSERT (sz == 14); |
| 122 | + TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4", sz)); |
| 123 | + |
| 124 | + sz = jerry_substring_to_utf8_char_buffer (args[0], utf8_length, 0, (jerry_char_t *) test_string, utf8_sz); |
| 125 | + TEST_ASSERT (sz == 0); |
| 126 | + |
| 127 | + sz = jerry_substring_to_utf8_char_buffer (args[0], 0, utf8_length, (jerry_char_t *) test_string, utf8_sz - 1); |
| 128 | + TEST_ASSERT (sz == 10); |
| 129 | + TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 ", sz)); |
| 130 | + |
| 131 | + sz = jerry_substring_to_utf8_char_buffer (args[0], 0, utf8_length - 1, (jerry_char_t *) test_string, utf8_sz); |
| 132 | + TEST_ASSERT (sz == 10); |
| 133 | + TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 ", sz)); |
| 134 | + |
| 135 | + sz = jerry_substring_to_utf8_char_buffer (args[0], |
| 136 | + utf8_length - 2, |
| 137 | + utf8_length - 1, |
| 138 | + (jerry_char_t *) test_string, |
| 139 | + utf8_sz); |
| 140 | + TEST_ASSERT (sz == 1); |
| 141 | + TEST_ASSERT (!strncmp (test_string, " ", sz)); |
| 142 | + |
| 143 | + sz = jerry_substring_to_utf8_char_buffer (args[0], |
| 144 | + utf8_length - 3, |
| 145 | + utf8_length - 2, |
| 146 | + (jerry_char_t *) test_string, |
| 147 | + utf8_sz); |
| 148 | + TEST_ASSERT (sz == 4); |
| 149 | + TEST_ASSERT (!strncmp (test_string, "\xf0\x9d\x94\xa3", sz)); |
| 150 | + |
| 151 | + jerry_release_value (args[0]); |
| 152 | + |
| 153 | + /* Test string: 'str: {DESERET CAPITAL LETTER LONG I}' */ |
| 154 | + args[0] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80"); |
| 155 | + |
| 156 | + cesu8_length = jerry_get_string_length (args[0]); |
| 157 | + utf8_length = jerry_get_utf8_string_length (args[0]); |
| 158 | + |
| 159 | + cesu8_sz = jerry_get_string_size (args[0]); |
| 160 | + utf8_sz = jerry_get_utf8_string_size (args[0]); |
| 161 | + |
| 162 | + TEST_ASSERT (cesu8_length == 7 && utf8_length == 6); |
| 163 | + TEST_ASSERT (cesu8_sz != utf8_sz); |
| 164 | + TEST_ASSERT (utf8_sz == 9 && cesu8_sz == 11); |
| 165 | + |
| 166 | + jerry_release_value (args[0]); |
| 167 | + |
| 168 | + /* Test string: 'price: 10{EURO SIGN}' */ |
| 169 | + args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x70\x72\x69\x63\x65\x3a \x31\x30\xe2\x82\xac"); |
| 170 | + |
| 171 | + cesu8_length = jerry_get_string_length (args[0]); |
| 172 | + utf8_length = jerry_get_utf8_string_length (args[0]); |
| 173 | + |
| 174 | + cesu8_sz = jerry_get_string_size (args[0]); |
| 175 | + utf8_sz = jerry_get_utf8_string_size (args[0]); |
| 176 | + |
| 177 | + TEST_ASSERT (cesu8_length == utf8_length); |
| 178 | + TEST_ASSERT (cesu8_length == 10); |
| 179 | + TEST_ASSERT (cesu8_sz == utf8_sz); |
| 180 | + TEST_ASSERT (utf8_sz == 12); |
| 181 | + jerry_release_value (args[0]); |
| 182 | + |
| 183 | + /* Test string: '3' */ |
| 184 | + { |
| 185 | + jerry_value_t test_str = jerry_create_string ((const jerry_char_t *) "3"); |
| 186 | + char result_string[1] = { 'E' }; |
| 187 | + jerry_size_t copied_utf8 = jerry_substring_to_utf8_char_buffer (test_str, |
| 188 | + 0, |
| 189 | + 1, |
| 190 | + (jerry_char_t *) result_string, |
| 191 | + sizeof (result_string)); |
| 192 | + TEST_ASSERT (copied_utf8 == 1); |
| 193 | + TEST_ASSERT (result_string[0] == '3'); |
| 194 | + |
| 195 | + result_string[0] = 'E'; |
| 196 | + jerry_size_t copied = jerry_substring_to_char_buffer (test_str, |
| 197 | + 0, |
| 198 | + 1, |
| 199 | + (jerry_char_t *) result_string, |
| 200 | + sizeof (result_string)); |
| 201 | + TEST_ASSERT (copied == 1); |
| 202 | + TEST_ASSERT (result_string[0] == '3'); |
| 203 | + |
| 204 | + jerry_release_value (test_str); |
| 205 | + } |
| 206 | + |
| 207 | + /* Test jerry_substring_to_char_buffer */ |
| 208 | + args[0] = jerry_create_string ((jerry_char_t *) "an ascii string"); |
| 209 | + |
| 210 | + /* Buffer size */ |
| 211 | + cesu8_sz = 5; |
| 212 | + |
| 213 | + char substring[cesu8_sz]; |
| 214 | + sz = jerry_substring_to_char_buffer (args[0], 3, 8, (jerry_char_t *) substring, cesu8_sz); |
| 215 | + TEST_ASSERT (sz == 5); |
| 216 | + TEST_ASSERT (!strncmp (substring, "ascii", sz)); |
| 217 | + |
| 218 | + /* Buffer size is 5, substring length is 11 => copied only the first 5 char */ |
| 219 | + sz = jerry_substring_to_char_buffer (args[0], 0, 11, (jerry_char_t *) substring, cesu8_sz); |
| 220 | + |
| 221 | + TEST_ASSERT (sz == 5); |
| 222 | + TEST_ASSERT (!strncmp (substring, "an as", sz)); |
| 223 | + |
| 224 | + /* Position of the first character is greater than the string length */ |
| 225 | + sz = jerry_substring_to_char_buffer (args[0], 16, 21, (jerry_char_t *) substring, cesu8_sz); |
| 226 | + TEST_ASSERT (sz == 0); |
| 227 | + |
| 228 | + sz = jerry_substring_to_char_buffer (args[0], 14, 15, (jerry_char_t *) substring, cesu8_sz); |
| 229 | + TEST_ASSERT (sz == 1); |
| 230 | + TEST_ASSERT (!strncmp (substring, "g", sz)); |
| 231 | + |
| 232 | + sz = jerry_substring_to_char_buffer (args[0], 0, 1, (jerry_char_t *) substring, cesu8_sz); |
| 233 | + TEST_ASSERT (sz == 1); |
| 234 | + TEST_ASSERT (!strncmp (substring, "a", sz)); |
| 235 | + |
| 236 | + cesu8_length = jerry_get_string_length (args[0]); |
| 237 | + cesu8_sz = jerry_get_string_size (args[0]); |
| 238 | + TEST_ASSERT (cesu8_length == 15); |
| 239 | + TEST_ASSERT (cesu8_length == cesu8_sz); |
| 240 | + |
| 241 | + sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) substring, cesu8_sz); |
| 242 | + TEST_ASSERT (sz = 15); |
| 243 | + TEST_ASSERT (!strncmp (substring, "an ascii string", sz)); |
| 244 | + |
| 245 | + jerry_release_value (args[0]); |
| 246 | + |
| 247 | + /* Test jerry_substring_to_char_buffer: '0101' */ |
| 248 | + args[0] = jerry_create_string ((jerry_char_t *) "0101"); |
| 249 | + cesu8_sz = jerry_get_string_size (args[0]); |
| 250 | + |
| 251 | + char number_substring[cesu8_sz]; |
| 252 | + |
| 253 | + sz = jerry_substring_to_char_buffer (args[0], 1, 3, (jerry_char_t *) number_substring, cesu8_sz); |
| 254 | + TEST_ASSERT (sz == 2); |
| 255 | + TEST_ASSERT (!strncmp (number_substring, "10", sz)); |
| 256 | + |
| 257 | + jerry_release_value (args[0]); |
| 258 | + |
| 259 | + /* Test jerry_substring_to_char_buffer: 'str: {greek zero sign}' */ |
| 260 | + args[0] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x80\xed\xb6\x8a"); |
| 261 | + cesu8_sz = jerry_get_string_size (args[0]); |
| 262 | + cesu8_length = jerry_get_string_length (args[0]); |
| 263 | + TEST_ASSERT (cesu8_sz == 11); |
| 264 | + TEST_ASSERT (cesu8_length = 7); |
| 265 | + |
| 266 | + char supl_substring[cesu8_sz]; |
| 267 | + |
| 268 | + sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) supl_substring, cesu8_sz); |
| 269 | + TEST_ASSERT (sz == 11); |
| 270 | + TEST_ASSERT (!strncmp (supl_substring, "\x73\x74\x72\x3a \xed\xa0\x80\xed\xb6\x8a", sz)); |
| 271 | + |
| 272 | + /* Decrease the buffer size => the low surrogate char will not fit into the buffer */ |
| 273 | + cesu8_sz -= 1; |
| 274 | + sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) supl_substring, cesu8_sz); |
| 275 | + TEST_ASSERT (sz == 8); |
| 276 | + TEST_ASSERT (!strncmp (supl_substring, "\x73\x74\x72\x3a \xed\xa0\x80", sz)); |
| 277 | + |
| 278 | + sz = jerry_substring_to_char_buffer (args[0], |
| 279 | + cesu8_length - 1, |
| 280 | + cesu8_length, |
| 281 | + (jerry_char_t *) supl_substring, |
| 282 | + cesu8_sz); |
| 283 | + TEST_ASSERT (sz == 3); |
| 284 | + TEST_ASSERT (!strncmp (supl_substring, "\xed\xb6\x8a", sz)); |
| 285 | + |
| 286 | + sz = jerry_substring_to_char_buffer (args[0], |
| 287 | + cesu8_length - 2, |
| 288 | + cesu8_length - 1, |
| 289 | + (jerry_char_t *) supl_substring, |
| 290 | + cesu8_sz); |
| 291 | + TEST_ASSERT (sz == 3); |
| 292 | + TEST_ASSERT (!strncmp (supl_substring, "\xed\xa0\x80", sz)); |
| 293 | + |
| 294 | + jerry_release_value (args[0]); |
| 295 | + |
| 296 | + jerry_cleanup (); |
| 297 | + |
| 298 | + return 0; |
| 299 | +} /* main */ |
0 commit comments