1425bb815Sopenharmony_ci/* 2425bb815Sopenharmony_ci * Copyright JS Foundation and other contributors, http://js.foundation 3425bb815Sopenharmony_ci * 4425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 5425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 6425bb815Sopenharmony_ci * You may obtain a copy of the License at 7425bb815Sopenharmony_ci * 8425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 9425bb815Sopenharmony_ci * 10425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 11425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 12425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 14425bb815Sopenharmony_ci * limitations under the License. 15425bb815Sopenharmony_ci */ 16425bb815Sopenharmony_ciextern "C" 17425bb815Sopenharmony_ci{ 18425bb815Sopenharmony_ci #include "ecma-init-finalize.h" 19425bb815Sopenharmony_ci #include "jmem.h" 20425bb815Sopenharmony_ci #include "ecma-helpers.h" 21425bb815Sopenharmony_ci #include "ecma-conversion.h" 22425bb815Sopenharmony_ci #include "ecma-exceptions.h" 23425bb815Sopenharmony_ci #include "jcontext.h" 24425bb815Sopenharmony_ci} 25425bb815Sopenharmony_ci 26425bb815Sopenharmony_ci#include "jerryscript-port.h" 27425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 28425bb815Sopenharmony_ci#include "ecma-globals.h" 29425bb815Sopenharmony_ci#include "jerryscript.h" 30425bb815Sopenharmony_ci#include <gtest/gtest.h> 31425bb815Sopenharmony_ci#include "test-common.h" 32425bb815Sopenharmony_ci 33425bb815Sopenharmony_ci/** 34425bb815Sopenharmony_ci * Unit test's main function. 35425bb815Sopenharmony_ci */ 36425bb815Sopenharmony_ciclass ToLengthTest : public testing::Test{ 37425bb815Sopenharmony_cipublic: 38425bb815Sopenharmony_ci static void SetUpTestCase() 39425bb815Sopenharmony_ci { 40425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ToLengthTest SetUpTestCase"; 41425bb815Sopenharmony_ci } 42425bb815Sopenharmony_ci 43425bb815Sopenharmony_ci static void TearDownTestCase() 44425bb815Sopenharmony_ci { 45425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ToLengthTest TearDownTestCase"; 46425bb815Sopenharmony_ci } 47425bb815Sopenharmony_ci 48425bb815Sopenharmony_ci void SetUp() override {} 49425bb815Sopenharmony_ci void TearDown() override {} 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_ci}; 52425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 53425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 54425bb815Sopenharmony_ci{ 55425bb815Sopenharmony_ci (void)cb_data; 56425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 57425bb815Sopenharmony_ci return malloc(newSize); 58425bb815Sopenharmony_ci} 59425bb815Sopenharmony_ciHWTEST_F(ToLengthTest, Test001, testing::ext::TestSize.Level1) 60425bb815Sopenharmony_ci{ 61425bb815Sopenharmony_ci TEST_INIT (); 62425bb815Sopenharmony_ci jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 63425bb815Sopenharmony_ci jerry_port_default_set_current_context (ctx_p); 64425bb815Sopenharmony_ci jmem_init (); 65425bb815Sopenharmony_ci ecma_init (); 66425bb815Sopenharmony_ci 67425bb815Sopenharmony_ci uint32_t num; 68425bb815Sopenharmony_ci 69425bb815Sopenharmony_ci ecma_value_t int_num = ecma_make_int32_value (123); 70425bb815Sopenharmony_ci 71425bb815Sopenharmony_ci uint32_t result = ecma_op_to_length (int_num, &num); 72425bb815Sopenharmony_ci 73425bb815Sopenharmony_ci ecma_free_value (int_num); 74425bb815Sopenharmony_ci 75425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 76425bb815Sopenharmony_ci TEST_ASSERT (num == 123); 77425bb815Sopenharmony_ci 78425bb815Sopenharmony_ci /* 1, 3 */ 79425bb815Sopenharmony_ci ecma_value_t error_throw = ecma_raise_type_error (ECMA_ERR_MSG ("I'm an error")); 80425bb815Sopenharmony_ci 81425bb815Sopenharmony_ci result = ecma_op_to_length (error_throw, &num); 82425bb815Sopenharmony_ci 83425bb815Sopenharmony_ci jcontext_release_exception (); 84425bb815Sopenharmony_ci 85425bb815Sopenharmony_ci TEST_ASSERT (ECMA_IS_VALUE_ERROR (result)); 86425bb815Sopenharmony_ci 87425bb815Sopenharmony_ci /* zero */ 88425bb815Sopenharmony_ci ecma_value_t zero = ecma_make_int32_value (0); 89425bb815Sopenharmony_ci 90425bb815Sopenharmony_ci result = ecma_op_to_length (zero, &num); 91425bb815Sopenharmony_ci 92425bb815Sopenharmony_ci ecma_free_value (zero); 93425bb815Sopenharmony_ci 94425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 95425bb815Sopenharmony_ci TEST_ASSERT (num == 0); 96425bb815Sopenharmony_ci 97425bb815Sopenharmony_ci /* negative */ 98425bb815Sopenharmony_ci ecma_value_t negative = ecma_make_number_value (-26.5973f); 99425bb815Sopenharmony_ci 100425bb815Sopenharmony_ci result = ecma_op_to_length (negative, &num); 101425bb815Sopenharmony_ci 102425bb815Sopenharmony_ci ecma_free_value (negative); 103425bb815Sopenharmony_ci 104425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 105425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 106425bb815Sopenharmony_ci TEST_ASSERT (num == 4294967270); 107425bb815Sopenharmony_ci#else /* !ENABLED (JERRY_ES2015) */ 108425bb815Sopenharmony_ci TEST_ASSERT (num == 0); 109425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 110425bb815Sopenharmony_ci 111425bb815Sopenharmony_ci /* +infinity */ 112425bb815Sopenharmony_ci ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false)); 113425bb815Sopenharmony_ci 114425bb815Sopenharmony_ci result = ecma_op_to_length (positive_infinity, &num); 115425bb815Sopenharmony_ci 116425bb815Sopenharmony_ci ecma_free_value (positive_infinity); 117425bb815Sopenharmony_ci 118425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 119425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015) 120425bb815Sopenharmony_ci TEST_ASSERT (num == 0); 121425bb815Sopenharmony_ci#else /* !ENABLED (JERRY_ES2015) */ 122425bb815Sopenharmony_ci TEST_ASSERT (num == UINT32_MAX); 123425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015) */ 124425bb815Sopenharmony_ci 125425bb815Sopenharmony_ci /* -infinity */ 126425bb815Sopenharmony_ci ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true)); 127425bb815Sopenharmony_ci 128425bb815Sopenharmony_ci result = ecma_op_to_length (negative_infinity, &num); 129425bb815Sopenharmony_ci 130425bb815Sopenharmony_ci ecma_free_value (negative_infinity); 131425bb815Sopenharmony_ci 132425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 133425bb815Sopenharmony_ci TEST_ASSERT (num == 0); 134425bb815Sopenharmony_ci 135425bb815Sopenharmony_ci /* NaN */ 136425bb815Sopenharmony_ci ecma_value_t nan = ecma_make_nan_value (); 137425bb815Sopenharmony_ci 138425bb815Sopenharmony_ci result = ecma_op_to_length (nan, &num); 139425bb815Sopenharmony_ci 140425bb815Sopenharmony_ci ecma_free_value (nan); 141425bb815Sopenharmony_ci 142425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 143425bb815Sopenharmony_ci TEST_ASSERT (num == 0); 144425bb815Sopenharmony_ci 145425bb815Sopenharmony_ci ecma_finalize (); 146425bb815Sopenharmony_ci jmem_finalize (); 147425bb815Sopenharmony_ci free(ctx_p); 148425bb815Sopenharmony_ci return; 149425bb815Sopenharmony_ci} 150