1 /*
2 * Copyright JS Foundation and other contributors, http://js.foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ecma-globals.h"
18 #include "ecma-helpers.h"
19 #include "ecma-conversion.h"
20 #include "ecma-init-finalize.h"
21 #include "ecma-exceptions.h"
22 #include "jerryscript.h"
23 #include "jcontext.h"
24
25 #include "test-common.h"
26
27 /**
28 * Unit test's main function.
29 */
30 int
main(void)31 main (void)
32 {
33 TEST_INIT ();
34
35 jmem_init ();
36 ecma_init ();
37
38 uint32_t num;
39
40 ecma_value_t int_num = ecma_make_int32_value (123);
41
42 uint32_t result = ecma_op_to_length (int_num, &num);
43
44 ecma_free_value (int_num);
45
46 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
47 TEST_ASSERT (num == 123);
48
49 /* 1, 3 */
50 ecma_value_t error_throw = ecma_raise_type_error (ECMA_ERR_MSG ("I'm an error"));
51
52 result = ecma_op_to_length (error_throw, &num);
53
54 jcontext_release_exception ();
55
56 TEST_ASSERT (ECMA_IS_VALUE_ERROR (result));
57
58 /* zero */
59 ecma_value_t zero = ecma_make_int32_value (0);
60
61 result = ecma_op_to_length (zero, &num);
62
63 ecma_free_value (zero);
64
65 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
66 TEST_ASSERT (num == 0);
67
68 /* negative */
69 ecma_value_t negative = ecma_make_number_value (-26.5973f);
70
71 result = ecma_op_to_length (negative, &num);
72
73 ecma_free_value (negative);
74
75 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
76 #if ENABLED (JERRY_ES2015)
77 TEST_ASSERT (num == 0);
78 #else /* !ENABLED (JERRY_ES2015) */
79 TEST_ASSERT (num == 4294967270);
80 #endif /* ENABLED (JERRY_ES2015) */
81
82 /* +infinity */
83 ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false));
84
85 result = ecma_op_to_length (positive_infinity, &num);
86
87 ecma_free_value (positive_infinity);
88
89 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
90 #if ENABLED (JERRY_ES2015)
91 TEST_ASSERT (num == UINT32_MAX);
92 #else /* !ENABLED (JERRY_ES2015) */
93 TEST_ASSERT (num == 0);
94 #endif /* ENABLED (JERRY_ES2015) */
95
96 /* -infinity */
97 ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true));
98
99 result = ecma_op_to_length (negative_infinity, &num);
100
101 ecma_free_value (negative_infinity);
102
103 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
104 TEST_ASSERT (num == 0);
105
106 /* NaN */
107 ecma_value_t nan = ecma_make_nan_value ();
108
109 result = ecma_op_to_length (nan, &num);
110
111 ecma_free_value (nan);
112
113 TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
114 TEST_ASSERT (num == 0);
115
116 ecma_finalize ();
117 jmem_finalize ();
118
119 return 0;
120 } /* main */
121