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 extern "C"
17 {
18   #include "ecma-init-finalize.h"
19   #include "jmem.h"
20   #include "ecma-helpers.h"
21   #include "ecma-conversion.h"
22   #include "ecma-exceptions.h"
23   #include "jcontext.h"
24 }
25 
26 #include "jerryscript-port.h"
27 #include "jerryscript-port-default.h"
28 #include "ecma-globals.h"
29 #include "jerryscript.h"
30 #include <gtest/gtest.h>
31 #include "test-common.h"
32 
33 /**
34  * Unit test's main function.
35  */
36 class ToLengthTest : public testing::Test{
37 public:
SetUpTestCase()38     static void SetUpTestCase()
39     {
40         GTEST_LOG_(INFO) << "ToLengthTest SetUpTestCase";
41     }
42 
TearDownTestCase()43     static void TearDownTestCase()
44     {
45         GTEST_LOG_(INFO) << "ToLengthTest TearDownTestCase";
46     }
47 
48     void SetUp() override {}
49     void TearDown() override {}
50 
51 };
52 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size, void* cb_data)53 static void* context_alloc_fn(size_t size, void* cb_data)
54 {
55     (void)cb_data;
56     size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
57     return malloc(newSize);
58 }
HWTEST_F(ToLengthTest, Test001, testing::ext::TestSize.Level1)59 HWTEST_F(ToLengthTest, Test001, testing::ext::TestSize.Level1)
60 {
61   TEST_INIT ();
62   jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
63   jerry_port_default_set_current_context (ctx_p);
64   jmem_init ();
65   ecma_init ();
66 
67   uint32_t num;
68 
69   ecma_value_t int_num = ecma_make_int32_value (123);
70 
71   uint32_t result = ecma_op_to_length (int_num, &num);
72 
73   ecma_free_value (int_num);
74 
75   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
76   TEST_ASSERT (num == 123);
77 
78   /* 1, 3 */
79   ecma_value_t error_throw = ecma_raise_type_error (ECMA_ERR_MSG ("I'm an error"));
80 
81   result = ecma_op_to_length (error_throw, &num);
82 
83   jcontext_release_exception ();
84 
85   TEST_ASSERT (ECMA_IS_VALUE_ERROR (result));
86 
87   /* zero */
88   ecma_value_t zero = ecma_make_int32_value (0);
89 
90   result = ecma_op_to_length (zero, &num);
91 
92   ecma_free_value (zero);
93 
94   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
95   TEST_ASSERT (num == 0);
96 
97   /* negative */
98   ecma_value_t negative = ecma_make_number_value (-26.5973f);
99 
100   result = ecma_op_to_length (negative, &num);
101 
102   ecma_free_value (negative);
103 
104   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
105 #if ENABLED (JERRY_ES2015)
106   TEST_ASSERT (num == 4294967270);
107 #else /* !ENABLED (JERRY_ES2015) */
108   TEST_ASSERT (num == 0);
109 #endif /* ENABLED (JERRY_ES2015) */
110 
111   /* +infinity */
112   ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false));
113 
114   result = ecma_op_to_length (positive_infinity, &num);
115 
116   ecma_free_value (positive_infinity);
117 
118   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
119 #if ENABLED (JERRY_ES2015)
120   TEST_ASSERT (num == 0);
121 #else /* !ENABLED (JERRY_ES2015) */
122   TEST_ASSERT (num == UINT32_MAX);
123 #endif /* ENABLED (JERRY_ES2015) */
124 
125   /* -infinity */
126   ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true));
127 
128   result = ecma_op_to_length (negative_infinity, &num);
129 
130   ecma_free_value (negative_infinity);
131 
132   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
133   TEST_ASSERT (num == 0);
134 
135   /* NaN */
136   ecma_value_t nan = ecma_make_nan_value ();
137 
138   result = ecma_op_to_length (nan, &num);
139 
140   ecma_free_value (nan);
141 
142   TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result));
143   TEST_ASSERT (num == 0);
144 
145   ecma_finalize ();
146   jmem_finalize ();
147   free(ctx_p);
148   return;
149 }
150