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 ToIntegerTest : public testing::Test{ 37425bb815Sopenharmony_cipublic: 38425bb815Sopenharmony_ci static void SetUpTestCase() 39425bb815Sopenharmony_ci { 40425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ToIntegerTest SetUpTestCase"; 41425bb815Sopenharmony_ci } 42425bb815Sopenharmony_ci 43425bb815Sopenharmony_ci static void TearDownTestCase() 44425bb815Sopenharmony_ci { 45425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ToIntegerTest TearDownTestCase"; 46425bb815Sopenharmony_ci } 47425bb815Sopenharmony_ci 48425bb815Sopenharmony_ci void SetUp() override {} 49425bb815Sopenharmony_ci void TearDown() override {} 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_ci}; 52425bb815Sopenharmony_ci 53425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 54425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 55425bb815Sopenharmony_ci{ 56425bb815Sopenharmony_ci (void)cb_data; 57425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 58425bb815Sopenharmony_ci return malloc(newSize); 59425bb815Sopenharmony_ci} 60425bb815Sopenharmony_ci 61425bb815Sopenharmony_ciHWTEST_F(ToIntegerTest, Test001, testing::ext::TestSize.Level1) 62425bb815Sopenharmony_ci{ 63425bb815Sopenharmony_ci TEST_INIT (); 64425bb815Sopenharmony_ci jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 65425bb815Sopenharmony_ci jerry_port_default_set_current_context (ctx_p); 66425bb815Sopenharmony_ci jmem_init (); 67425bb815Sopenharmony_ci ecma_init (); 68425bb815Sopenharmony_ci 69425bb815Sopenharmony_ci ecma_number_t num; 70425bb815Sopenharmony_ci 71425bb815Sopenharmony_ci ecma_value_t int_num = ecma_make_int32_value (123); 72425bb815Sopenharmony_ci 73425bb815Sopenharmony_ci ecma_number_t result = ecma_op_to_integer (int_num, &num); 74425bb815Sopenharmony_ci 75425bb815Sopenharmony_ci ecma_free_value (int_num); 76425bb815Sopenharmony_ci 77425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 78425bb815Sopenharmony_ci TEST_ASSERT (num == 123); 79425bb815Sopenharmony_ci 80425bb815Sopenharmony_ci /* 2 */ 81425bb815Sopenharmony_ci ecma_value_t error = ecma_raise_type_error (ECMA_ERR_MSG ("I am a neat little error message")); 82425bb815Sopenharmony_ci 83425bb815Sopenharmony_ci result = ecma_op_to_integer (error, &num); 84425bb815Sopenharmony_ci 85425bb815Sopenharmony_ci jcontext_release_exception (); 86425bb815Sopenharmony_ci 87425bb815Sopenharmony_ci TEST_ASSERT (ECMA_IS_VALUE_ERROR (result)); 88425bb815Sopenharmony_ci 89425bb815Sopenharmony_ci /* 3 */ 90425bb815Sopenharmony_ci ecma_value_t nan = ecma_make_nan_value (); 91425bb815Sopenharmony_ci 92425bb815Sopenharmony_ci result = ecma_op_to_integer (nan, &num); 93425bb815Sopenharmony_ci 94425bb815Sopenharmony_ci ecma_free_value (nan); 95425bb815Sopenharmony_ci 96425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 97425bb815Sopenharmony_ci TEST_ASSERT (num == 0); 98425bb815Sopenharmony_ci 99425bb815Sopenharmony_ci /* 4 */ 100425bb815Sopenharmony_ci /* -0 */ 101425bb815Sopenharmony_ci ecma_value_t negative_zero = ecma_make_number_value (-0.0f); 102425bb815Sopenharmony_ci 103425bb815Sopenharmony_ci result = ecma_op_to_integer (negative_zero, &num); 104425bb815Sopenharmony_ci 105425bb815Sopenharmony_ci ecma_free_value (negative_zero); 106425bb815Sopenharmony_ci 107425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 108425bb815Sopenharmony_ci TEST_ASSERT (1.0f / num == ecma_number_make_infinity (true)); 109425bb815Sopenharmony_ci 110425bb815Sopenharmony_ci /* +0 */ 111425bb815Sopenharmony_ci ecma_value_t positive_zero = ecma_make_number_value (+0.0f); 112425bb815Sopenharmony_ci 113425bb815Sopenharmony_ci result = ecma_op_to_integer (positive_zero, &num); 114425bb815Sopenharmony_ci 115425bb815Sopenharmony_ci ecma_free_value (positive_zero); 116425bb815Sopenharmony_ci 117425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 118425bb815Sopenharmony_ci TEST_ASSERT (1.0f / num == ecma_number_make_infinity (false)); 119425bb815Sopenharmony_ci 120425bb815Sopenharmony_ci /* -infinity */ 121425bb815Sopenharmony_ci ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true)); 122425bb815Sopenharmony_ci 123425bb815Sopenharmony_ci result = ecma_op_to_integer (negative_infinity, &num); 124425bb815Sopenharmony_ci 125425bb815Sopenharmony_ci ecma_free_value (negative_infinity); 126425bb815Sopenharmony_ci 127425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 128425bb815Sopenharmony_ci TEST_ASSERT (num == ecma_number_make_infinity (true)); 129425bb815Sopenharmony_ci 130425bb815Sopenharmony_ci /* +infinity */ 131425bb815Sopenharmony_ci ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false)); 132425bb815Sopenharmony_ci 133425bb815Sopenharmony_ci result = ecma_op_to_integer (positive_infinity, &num); 134425bb815Sopenharmony_ci 135425bb815Sopenharmony_ci ecma_free_value (positive_infinity); 136425bb815Sopenharmony_ci 137425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 138425bb815Sopenharmony_ci TEST_ASSERT (num == ecma_number_make_infinity (false)); 139425bb815Sopenharmony_ci 140425bb815Sopenharmony_ci /* 5 */ 141425bb815Sopenharmony_ci ecma_value_t floor = ecma_make_number_value (3.001f); 142425bb815Sopenharmony_ci 143425bb815Sopenharmony_ci result = ecma_op_to_integer (floor, &num); 144425bb815Sopenharmony_ci 145425bb815Sopenharmony_ci ecma_free_value (floor); 146425bb815Sopenharmony_ci 147425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 148425bb815Sopenharmony_ci TEST_ASSERT (num == 3); 149425bb815Sopenharmony_ci 150425bb815Sopenharmony_ci ecma_value_t floor2 = ecma_make_number_value (-26.5973); 151425bb815Sopenharmony_ci 152425bb815Sopenharmony_ci result = ecma_op_to_integer (floor2, &num); 153425bb815Sopenharmony_ci 154425bb815Sopenharmony_ci ecma_free_value (floor2); 155425bb815Sopenharmony_ci 156425bb815Sopenharmony_ci TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); 157425bb815Sopenharmony_ci TEST_ASSERT (num == -26); 158425bb815Sopenharmony_ci 159425bb815Sopenharmony_ci ecma_finalize (); 160425bb815Sopenharmony_ci jmem_finalize (); 161425bb815Sopenharmony_ci free(ctx_p); 162425bb815Sopenharmony_ci return; 163425bb815Sopenharmony_ci} 164