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 #define T(op, lhs, rhs, res) \
21 { op, lhs, rhs, res }
22
23 typedef struct
24 {
25 jerry_binary_operation_t op;
26 jerry_value_t lhs;
27 jerry_value_t rhs;
28 bool expected;
29 } test_entry_t;
30
31 int
main(void)32 main (void)
33 {
34 TEST_INIT ();
35
36 jerry_init (JERRY_INIT_EMPTY);
37
38 jerry_value_t obj1 = jerry_eval ((const jerry_char_t *) "o={x:1};o", 9, JERRY_PARSE_NO_OPTS);
39 jerry_value_t obj2 = jerry_eval ((const jerry_char_t *) "o={x:1};o", 9, JERRY_PARSE_NO_OPTS);
40 jerry_value_t err1 = jerry_create_error (JERRY_ERROR_SYNTAX, (const jerry_char_t *) "error");
41
42 test_entry_t tests[] =
43 {
44 /* Testing strict equal comparison */
45 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_number (5.0), jerry_create_number (5.0), true),
46 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_number (3.1), jerry_create_number (10), false),
47 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_number (3.1), jerry_create_undefined (), false),
48 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_number (3.1), jerry_create_boolean (true), false),
49 T (JERRY_BIN_OP_STRICT_EQUAL,
50 jerry_create_string ((const jerry_char_t *) "example string"),
51 jerry_create_string ((const jerry_char_t *) "example string"),
52 true),
53 T (JERRY_BIN_OP_STRICT_EQUAL,
54 jerry_create_string ((const jerry_char_t *) "example string"),
55 jerry_create_undefined (),
56 false),
57 T (JERRY_BIN_OP_STRICT_EQUAL,
58 jerry_create_string ((const jerry_char_t *) "example string"),
59 jerry_create_null (),
60 false),
61 T (JERRY_BIN_OP_STRICT_EQUAL,
62 jerry_create_string ((const jerry_char_t *) "example string"),
63 jerry_create_number (5.0),
64 false),
65 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_undefined (), jerry_create_undefined (), true),
66 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_undefined (), jerry_create_null (), false),
67 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_null (), jerry_create_null (), true),
68 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_boolean (true), jerry_create_boolean (true), true),
69 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_boolean (true), jerry_create_boolean (false), false),
70 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_boolean (false), jerry_create_boolean (true), false),
71 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_boolean (false), jerry_create_boolean (false), true),
72 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_acquire_value (obj1), true),
73 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_acquire_value (obj2), false),
74 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj2), jerry_acquire_value (obj1), false),
75 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_null (), false),
76 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_undefined (), false),
77 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_boolean (true), false),
78 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_boolean (false), false),
79 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (obj1), jerry_create_number (5.0), false),
80 T (JERRY_BIN_OP_STRICT_EQUAL,
81 jerry_acquire_value (obj1),
82 jerry_create_string ((const jerry_char_t *) "example string"),
83 false),
84
85 /* Testing equal comparison */
86 T (JERRY_BIN_OP_EQUAL, jerry_create_number (5.0), jerry_create_number (5.0), true),
87 T (JERRY_BIN_OP_EQUAL, jerry_create_number (3.1), jerry_create_number (10), false),
88 T (JERRY_BIN_OP_EQUAL, jerry_create_number (3.1), jerry_create_undefined (), false),
89 T (JERRY_BIN_OP_EQUAL, jerry_create_number (3.1), jerry_create_boolean (true), false),
90 T (JERRY_BIN_OP_EQUAL,
91 jerry_create_string ((const jerry_char_t *) "example string"),
92 jerry_create_string ((const jerry_char_t *) "example string"),
93 true),
94 T (JERRY_BIN_OP_EQUAL,
95 jerry_create_string ((const jerry_char_t *) "example string"),
96 jerry_create_undefined (),
97 false),
98 T (JERRY_BIN_OP_EQUAL,
99 jerry_create_string ((const jerry_char_t *) "example string"),
100 jerry_create_null (),
101 false),
102 T (JERRY_BIN_OP_EQUAL,
103 jerry_create_string ((const jerry_char_t *) "example string"),
104 jerry_create_number (5.0),
105 false),
106 T (JERRY_BIN_OP_EQUAL, jerry_create_undefined (), jerry_create_undefined (), true),
107 T (JERRY_BIN_OP_EQUAL, jerry_create_undefined (), jerry_create_null (), true),
108 T (JERRY_BIN_OP_EQUAL, jerry_create_null (), jerry_create_null (), true),
109 T (JERRY_BIN_OP_EQUAL, jerry_create_boolean (true), jerry_create_boolean (true), true),
110 T (JERRY_BIN_OP_EQUAL, jerry_create_boolean (true), jerry_create_boolean (false), false),
111 T (JERRY_BIN_OP_EQUAL, jerry_create_boolean (false), jerry_create_boolean (true), false),
112 T (JERRY_BIN_OP_EQUAL, jerry_create_boolean (false), jerry_create_boolean (false), true),
113 T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_acquire_value (obj1), true),
114 T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_acquire_value (obj2), false),
115 T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj2), jerry_acquire_value (obj1), false),
116 T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_null (), false),
117 T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_undefined (), false),
118 T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_boolean (true), false),
119 T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_boolean (false), false),
120 T (JERRY_BIN_OP_EQUAL, jerry_acquire_value (obj1), jerry_create_number (5.0), false),
121 T (JERRY_BIN_OP_EQUAL,
122 jerry_acquire_value (obj1),
123 jerry_create_string ((const jerry_char_t *) "example string"),
124 false),
125
126 /* Testing less comparison */
127 T (JERRY_BIN_OP_LESS, jerry_create_number (5.0), jerry_create_number (5.0), false),
128 T (JERRY_BIN_OP_LESS, jerry_create_number (3.1), jerry_create_number (10), true),
129 T (JERRY_BIN_OP_LESS, jerry_create_number (3.1), jerry_create_undefined (), false),
130 T (JERRY_BIN_OP_LESS, jerry_create_number (3.1), jerry_create_boolean (true), false),
131 T (JERRY_BIN_OP_LESS,
132 jerry_create_string ((const jerry_char_t *) "1"),
133 jerry_create_string ((const jerry_char_t *) "2"),
134 true),
135 T (JERRY_BIN_OP_LESS,
136 jerry_create_string ((const jerry_char_t *) "1"),
137 jerry_create_undefined (),
138 false),
139 T (JERRY_BIN_OP_LESS,
140 jerry_create_string ((const jerry_char_t *) "1"),
141 jerry_create_null (),
142 false),
143 T (JERRY_BIN_OP_LESS,
144 jerry_create_string ((const jerry_char_t *) "1"),
145 jerry_create_number (5.0),
146 true),
147 T (JERRY_BIN_OP_LESS, jerry_create_undefined (), jerry_create_undefined (), false),
148 T (JERRY_BIN_OP_LESS, jerry_create_undefined (), jerry_create_null (), false),
149 T (JERRY_BIN_OP_LESS, jerry_create_null (), jerry_create_null (), false),
150 T (JERRY_BIN_OP_LESS, jerry_create_boolean (true), jerry_create_boolean (true), false),
151 T (JERRY_BIN_OP_LESS, jerry_create_boolean (true), jerry_create_boolean (false), false),
152 T (JERRY_BIN_OP_LESS, jerry_create_boolean (false), jerry_create_boolean (true), true),
153 T (JERRY_BIN_OP_LESS, jerry_create_boolean (false), jerry_create_boolean (false), false),
154
155 /* Testing less or equal comparison */
156 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (5.0), jerry_create_number (5.0), true),
157 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (5.1), jerry_create_number (5.0), false),
158 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (3.1), jerry_create_number (10), true),
159 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (3.1), jerry_create_undefined (), false),
160 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_number (3.1), jerry_create_boolean (true), false),
161 T (JERRY_BIN_OP_LESS_EQUAL,
162 jerry_create_string ((const jerry_char_t *) "1"),
163 jerry_create_string ((const jerry_char_t *) "2"),
164 true),
165 T (JERRY_BIN_OP_LESS_EQUAL,
166 jerry_create_string ((const jerry_char_t *) "1"),
167 jerry_create_string ((const jerry_char_t *) "1"),
168 true),
169 T (JERRY_BIN_OP_LESS_EQUAL,
170 jerry_create_string ((const jerry_char_t *) "1"),
171 jerry_create_undefined (),
172 false),
173 T (JERRY_BIN_OP_LESS_EQUAL,
174 jerry_create_string ((const jerry_char_t *) "1"),
175 jerry_create_null (),
176 false),
177 T (JERRY_BIN_OP_LESS_EQUAL,
178 jerry_create_string ((const jerry_char_t *) "1"),
179 jerry_create_number (5.0),
180 true),
181 T (JERRY_BIN_OP_LESS_EQUAL,
182 jerry_create_string ((const jerry_char_t *) "5.0"),
183 jerry_create_number (5.0),
184 true),
185 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_undefined (), jerry_create_undefined (), false),
186 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_undefined (), jerry_create_null (), false),
187 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_null (), jerry_create_null (), true),
188 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_boolean (true), jerry_create_boolean (true), true),
189 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_boolean (true), jerry_create_boolean (false), false),
190 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_boolean (false), jerry_create_boolean (true), true),
191 T (JERRY_BIN_OP_LESS_EQUAL, jerry_create_boolean (false), jerry_create_boolean (false), true),
192
193 /* Testing greater comparison */
194 T (JERRY_BIN_OP_GREATER, jerry_create_number (5.0), jerry_create_number (5.0), false),
195 T (JERRY_BIN_OP_GREATER, jerry_create_number (10), jerry_create_number (3.1), true),
196 T (JERRY_BIN_OP_GREATER, jerry_create_number (3.1), jerry_create_undefined (), false),
197 T (JERRY_BIN_OP_GREATER, jerry_create_number (3.1), jerry_create_boolean (true), true),
198 T (JERRY_BIN_OP_GREATER,
199 jerry_create_string ((const jerry_char_t *) "2"),
200 jerry_create_string ((const jerry_char_t *) "1"),
201 true),
202 T (JERRY_BIN_OP_GREATER,
203 jerry_create_string ((const jerry_char_t *) "1"),
204 jerry_create_string ((const jerry_char_t *) "2"),
205 false),
206 T (JERRY_BIN_OP_GREATER,
207 jerry_create_string ((const jerry_char_t *) "1"),
208 jerry_create_undefined (),
209 false),
210 T (JERRY_BIN_OP_GREATER,
211 jerry_create_string ((const jerry_char_t *) "1"),
212 jerry_create_null (),
213 true),
214 T (JERRY_BIN_OP_GREATER,
215 jerry_create_number (5.0),
216 jerry_create_string ((const jerry_char_t *) "1"),
217 true),
218 T (JERRY_BIN_OP_GREATER, jerry_create_undefined (), jerry_create_undefined (), false),
219 T (JERRY_BIN_OP_GREATER, jerry_create_undefined (), jerry_create_null (), false),
220 T (JERRY_BIN_OP_GREATER, jerry_create_null (), jerry_create_null (), false),
221 T (JERRY_BIN_OP_GREATER, jerry_create_boolean (true), jerry_create_boolean (true), false),
222 T (JERRY_BIN_OP_GREATER, jerry_create_boolean (true), jerry_create_boolean (false), true),
223 T (JERRY_BIN_OP_GREATER, jerry_create_boolean (false), jerry_create_boolean (true), false),
224 T (JERRY_BIN_OP_GREATER, jerry_create_boolean (false), jerry_create_boolean (false), false),
225
226 /* Testing greater or equal comparison */
227 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (5.0), jerry_create_number (5.0), true),
228 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (5.0), jerry_create_number (5.1), false),
229 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (10), jerry_create_number (3.1), true),
230 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (3.1), jerry_create_undefined (), false),
231 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_number (3.1), jerry_create_boolean (true), true),
232 T (JERRY_BIN_OP_GREATER_EQUAL,
233 jerry_create_string ((const jerry_char_t *) "2"),
234 jerry_create_string ((const jerry_char_t *) "1"),
235 true),
236 T (JERRY_BIN_OP_GREATER_EQUAL,
237 jerry_create_string ((const jerry_char_t *) "1"),
238 jerry_create_string ((const jerry_char_t *) "1"),
239 true),
240 T (JERRY_BIN_OP_GREATER_EQUAL,
241 jerry_create_string ((const jerry_char_t *) "1"),
242 jerry_create_undefined (),
243 false),
244 T (JERRY_BIN_OP_GREATER_EQUAL,
245 jerry_create_string ((const jerry_char_t *) "1"),
246 jerry_create_null (),
247 true),
248 T (JERRY_BIN_OP_GREATER_EQUAL,
249 jerry_create_number (5.0),
250 jerry_create_string ((const jerry_char_t *) "1"),
251 true),
252 T (JERRY_BIN_OP_GREATER_EQUAL,
253 jerry_create_string ((const jerry_char_t *) "5.0"),
254 jerry_create_number (5.0),
255 true),
256 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_undefined (), jerry_create_undefined (), false),
257 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_undefined (), jerry_create_null (), false),
258 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_null (), jerry_create_null (), true),
259 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_boolean (true), jerry_create_boolean (true), true),
260 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_boolean (true), jerry_create_boolean (false), true),
261 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_boolean (false), jerry_create_boolean (true), false),
262 T (JERRY_BIN_OP_GREATER_EQUAL, jerry_create_boolean (false), jerry_create_boolean (false), true),
263 };
264
265 for (uint32_t idx = 0; idx < sizeof (tests) / sizeof (test_entry_t); idx++)
266 {
267 jerry_value_t result = jerry_binary_operation (tests[idx].op, tests[idx].lhs, tests[idx].rhs);
268 TEST_ASSERT (!jerry_value_is_error (result));
269 TEST_ASSERT (jerry_get_boolean_value (result) == tests[idx].expected);
270 jerry_release_value (tests[idx].lhs);
271 jerry_release_value (tests[idx].rhs);
272 jerry_release_value (result);
273 }
274
275 test_entry_t error_tests[] =
276 {
277 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (err1), jerry_acquire_value (err1), true),
278 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_acquire_value (err1), jerry_create_undefined (), true),
279 T (JERRY_BIN_OP_STRICT_EQUAL, jerry_create_undefined (), jerry_acquire_value (err1), true),
280 };
281
282 for (uint32_t idx = 0; idx < sizeof (error_tests) / sizeof (test_entry_t); idx++)
283 {
284 jerry_value_t result = jerry_binary_operation (tests[idx].op, error_tests[idx].lhs, error_tests[idx].rhs);
285 TEST_ASSERT (jerry_value_is_error (result) == error_tests[idx].expected);
286 jerry_release_value (error_tests[idx].lhs);
287 jerry_release_value (error_tests[idx].rhs);
288 jerry_release_value (result);
289 }
290
291 jerry_release_value (obj1);
292 jerry_release_value (obj2);
293 jerry_release_value (err1);
294
295 jerry_cleanup ();
296
297 return 0;
298 } /* main */
299