• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
strict_equals(jerry_value_t a,jerry_value_t b)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
main(void)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   JERRY_VLA (char, string_from_utf8, utf8_sz);
64   JERRY_VLA (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 && utf8_sz > 0);
87 
88   JERRY_VLA (char, string_from_utf8_string, utf8_sz);
89   JERRY_VLA (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   JERRY_VLA (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   JERRY_VLA (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   JERRY_VLA (char, fullstring, cesu8_sz);
242   sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) fullstring, cesu8_sz);
243   TEST_ASSERT (sz == 15);
244   TEST_ASSERT (!strncmp (fullstring, "an ascii string", sz));
245 
246   jerry_release_value (args[0]);
247 
248   /* Test jerry_substring_to_char_buffer: '0101' */
249   args[0] = jerry_create_string ((jerry_char_t *) "0101");
250   cesu8_sz = jerry_get_string_size (args[0]);
251 
252   JERRY_VLA (char, number_substring, cesu8_sz);
253 
254   sz = jerry_substring_to_char_buffer (args[0], 1, 3, (jerry_char_t *) number_substring, cesu8_sz);
255   TEST_ASSERT (sz == 2);
256   TEST_ASSERT (!strncmp (number_substring, "10", sz));
257 
258   jerry_release_value (args[0]);
259 
260   /* Test jerry_substring_to_char_buffer: 'str: {greek zero sign}' */
261   args[0] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x80\xed\xb6\x8a");
262   cesu8_sz = jerry_get_string_size (args[0]);
263   cesu8_length = jerry_get_string_length (args[0]);
264   TEST_ASSERT (cesu8_sz == 11);
265   TEST_ASSERT (cesu8_length = 7);
266 
267   JERRY_VLA (char, supl_substring, cesu8_sz);
268 
269   sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) supl_substring, cesu8_sz);
270   TEST_ASSERT (sz == 11);
271   TEST_ASSERT (!strncmp (supl_substring, "\x73\x74\x72\x3a \xed\xa0\x80\xed\xb6\x8a", sz));
272 
273   /* Decrease the buffer size => the low surrogate char will not fit into the buffer */
274   cesu8_sz -= 1;
275   sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) supl_substring, cesu8_sz);
276   TEST_ASSERT (sz == 8);
277   TEST_ASSERT (!strncmp (supl_substring, "\x73\x74\x72\x3a \xed\xa0\x80", sz));
278 
279   sz = jerry_substring_to_char_buffer (args[0],
280                                        cesu8_length - 1,
281                                        cesu8_length,
282                                        (jerry_char_t *) supl_substring,
283                                        cesu8_sz);
284   TEST_ASSERT (sz == 3);
285   TEST_ASSERT (!strncmp (supl_substring, "\xed\xb6\x8a", sz));
286 
287   sz = jerry_substring_to_char_buffer (args[0],
288                                        cesu8_length - 2,
289                                        cesu8_length - 1,
290                                        (jerry_char_t *) supl_substring,
291                                        cesu8_sz);
292   TEST_ASSERT (sz == 3);
293   TEST_ASSERT (!strncmp (supl_substring, "\xed\xa0\x80", sz));
294 
295   jerry_release_value (args[0]);
296 
297   jerry_cleanup ();
298 
299   return 0;
300 } /* main */
301