14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_number.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include <cmath> 194514f5e3Sopenharmony_ci#include <iostream> 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ci#include "ecmascript/base/number_helper.h" 224514f5e3Sopenharmony_ci#include "ecmascript/base/string_helper.h" 234514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h" 244514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h" 254514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 264514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 274514f5e3Sopenharmony_ci#include "ecmascript/js_function.h" 284514f5e3Sopenharmony_ci#include "ecmascript/js_global_object.h" 294514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h" 304514f5e3Sopenharmony_ci#include "ecmascript/js_primitive_ref.h" 314514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h" 324514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 334514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 344514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 374514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 384514f5e3Sopenharmony_ci 394514f5e3Sopenharmony_cinamespace panda::test { 404514f5e3Sopenharmony_ciclass BuiltinsNumberTest : public BaseTestWithScope<false> { 414514f5e3Sopenharmony_ci}; 424514f5e3Sopenharmony_ci 434514f5e3Sopenharmony_ci// new Number(10) 444514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, NumberConstructor) 454514f5e3Sopenharmony_ci{ 464514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_ci JSHandle<JSFunction> number(env->GetNumberFunction()); 494514f5e3Sopenharmony_ci JSHandle<JSObject> globalObject(thread, env->GetGlobalObject()); 504514f5e3Sopenharmony_ci 514514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*number), 6); 524514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(number.GetTaggedValue()); 534514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue()); 544514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(5))); 554514f5e3Sopenharmony_ci 564514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 574514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::NumberConstructor(ecmaRuntimeCallInfo); 584514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 594514f5e3Sopenharmony_ci JSTaggedValue value(static_cast<JSTaggedType>(result.GetRawData())); 604514f5e3Sopenharmony_ci ASSERT_TRUE(value.IsECMAObject()); 614514f5e3Sopenharmony_ci JSPrimitiveRef *ref = JSPrimitiveRef::Cast(value.GetTaggedObject()); 624514f5e3Sopenharmony_ci ASSERT_EQ(ref->GetValue().GetDouble(), 5.0); 634514f5e3Sopenharmony_ci} 644514f5e3Sopenharmony_ci 654514f5e3Sopenharmony_ci// Number.isFinite(-10) 664514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsFinite) 674514f5e3Sopenharmony_ci{ 684514f5e3Sopenharmony_ci const double value = -10; 694514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 704514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 714514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 724514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(value))); 734514f5e3Sopenharmony_ci 744514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 754514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsFinite(ecmaRuntimeCallInfo); 764514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData()); 774514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 784514f5e3Sopenharmony_ci} 794514f5e3Sopenharmony_ci 804514f5e3Sopenharmony_ci// Number.isFinite(Number.MAX_VALUE) 814514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsFinite1) 824514f5e3Sopenharmony_ci{ 834514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 844514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 854514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 864514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(base::MAX_VALUE)); 874514f5e3Sopenharmony_ci 884514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 894514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsFinite(ecmaRuntimeCallInfo); 904514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData()); 914514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 924514f5e3Sopenharmony_ci} 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ci// Number.isFinite("helloworld") 954514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsFinite2) 964514f5e3Sopenharmony_ci{ 974514f5e3Sopenharmony_ci JSHandle<EcmaString> test = thread->GetEcmaVM()->GetFactory()->NewFromASCII("helloworld"); 984514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 994514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1004514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1014514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, test.GetTaggedValue()); 1024514f5e3Sopenharmony_ci 1034514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1044514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsFinite(ecmaRuntimeCallInfo); 1054514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData()); 1064514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1074514f5e3Sopenharmony_ci} 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_ci// Number.isFinite(NaN) 1104514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsFinite3) 1114514f5e3Sopenharmony_ci{ 1124514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1134514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1144514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1154514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(base::NAN_VALUE)); 1164514f5e3Sopenharmony_ci 1174514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1184514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsFinite(ecmaRuntimeCallInfo); 1194514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData()); 1204514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1214514f5e3Sopenharmony_ci} 1224514f5e3Sopenharmony_ci 1234514f5e3Sopenharmony_ci// Number.isFinite(Infinity) 1244514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsFinite4) 1254514f5e3Sopenharmony_ci{ 1264514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1274514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1284514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1294514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(base::POSITIVE_INFINITY)); 1304514f5e3Sopenharmony_ci 1314514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1324514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsFinite(ecmaRuntimeCallInfo); 1334514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData()); 1344514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1354514f5e3Sopenharmony_ci} 1364514f5e3Sopenharmony_ci 1374514f5e3Sopenharmony_ci// Number.isFinite(undefined) 1384514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsFinite5) 1394514f5e3Sopenharmony_ci{ 1404514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1414514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1424514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1434514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined()); 1444514f5e3Sopenharmony_ci 1454514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1464514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsFinite(ecmaRuntimeCallInfo); 1474514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData()); 1484514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1494514f5e3Sopenharmony_ci} 1504514f5e3Sopenharmony_ci 1514514f5e3Sopenharmony_ci// Number.isFinite(null) 1524514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsFinite6) 1534514f5e3Sopenharmony_ci{ 1544514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1554514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1564514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1574514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Null()); 1584514f5e3Sopenharmony_ci 1594514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1604514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsFinite(ecmaRuntimeCallInfo); 1614514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData()); 1624514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1634514f5e3Sopenharmony_ci} 1644514f5e3Sopenharmony_ci 1654514f5e3Sopenharmony_ci// Number.isInteger(0.1) 1664514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsInteger) 1674514f5e3Sopenharmony_ci{ 1684514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1694514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1704514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1714514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0.1)); 1724514f5e3Sopenharmony_ci 1734514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1744514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsInteger(ecmaRuntimeCallInfo); 1754514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData()); 1764514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1774514f5e3Sopenharmony_ci} 1784514f5e3Sopenharmony_ci 1794514f5e3Sopenharmony_ci// Number.isNaN(0.1) 1804514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsNaN) 1814514f5e3Sopenharmony_ci{ 1824514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1834514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1844514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 1854514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(0.1)); 1864514f5e3Sopenharmony_ci 1874514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1884514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::IsNaN(ecmaRuntimeCallInfo); 1894514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData()); 1904514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1914514f5e3Sopenharmony_ci} 1924514f5e3Sopenharmony_ci 1934514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, ToString1) 1944514f5e3Sopenharmony_ci{ 1954514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1964514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 1974514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 1984514f5e3Sopenharmony_ci 1994514f5e3Sopenharmony_ci // new Number(123.456).toString(7) 2004514f5e3Sopenharmony_ci JSHandle<JSFunction> numberObject(env->GetNumberFunction()); 2014514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(thread, JSTaggedValue(123.456)); 2024514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> number = thread->GetEcmaVM()->GetFactory()->NewJSPrimitiveRef(numberObject, value); 2034514f5e3Sopenharmony_ci 2044514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2054514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 2064514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(number.GetTaggedValue()); 2074514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(7.0)); 2084514f5e3Sopenharmony_ci 2094514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 2104514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsNumber::ToString(ecmaRuntimeCallInfo1); 2114514f5e3Sopenharmony_ci ASSERT_TRUE(result1.IsString()); 2124514f5e3Sopenharmony_ci JSHandle<EcmaString> res1(thread, reinterpret_cast<EcmaString *>(result1.GetRawData())); 2134514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult1 = factory->NewFromASCII("234.312256641535441"); 2144514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res1, *correctResult1)); 2154514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2164514f5e3Sopenharmony_ci 2174514f5e3Sopenharmony_ci // (15).toString(4) 2184514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2194514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined()); 2204514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetThis(JSTaggedValue(15)); 2214514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue(4)); 2224514f5e3Sopenharmony_ci 2234514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2); 2244514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsNumber::ToString(ecmaRuntimeCallInfo2); 2254514f5e3Sopenharmony_ci ASSERT_TRUE(result2.IsString()); 2264514f5e3Sopenharmony_ci JSHandle<EcmaString> res2(thread, reinterpret_cast<EcmaString *>(result2.GetRawData())); 2274514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult2 = factory->NewFromASCII("33"); 2284514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res2, *correctResult2)); 2294514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2304514f5e3Sopenharmony_ci 2314514f5e3Sopenharmony_ci // (5).toString(8) 2324514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2334514f5e3Sopenharmony_ci ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined()); 2344514f5e3Sopenharmony_ci ecmaRuntimeCallInfo3->SetThis(JSTaggedValue(5)); 2354514f5e3Sopenharmony_ci ecmaRuntimeCallInfo3->SetCallArg(0, JSTaggedValue(8)); 2364514f5e3Sopenharmony_ci 2374514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3); 2384514f5e3Sopenharmony_ci JSTaggedValue result3 = BuiltinsNumber::ToString(ecmaRuntimeCallInfo3); 2394514f5e3Sopenharmony_ci ASSERT_TRUE(result3.IsString()); 2404514f5e3Sopenharmony_ci JSHandle<EcmaString> res3(thread, reinterpret_cast<EcmaString *>(result3.GetRawData())); 2414514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult3 = factory->NewFromASCII("5"); 2424514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res3, *correctResult3)); 2434514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2444514f5e3Sopenharmony_ci 2454514f5e3Sopenharmony_ci // (0).toString(8) 2464514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo4 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2474514f5e3Sopenharmony_ci ecmaRuntimeCallInfo4->SetFunction(JSTaggedValue::Undefined()); 2484514f5e3Sopenharmony_ci ecmaRuntimeCallInfo4->SetThis(JSTaggedValue(0)); 2494514f5e3Sopenharmony_ci ecmaRuntimeCallInfo4->SetCallArg(0, JSTaggedValue(8)); 2504514f5e3Sopenharmony_ci 2514514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo4); 2524514f5e3Sopenharmony_ci JSTaggedValue result4 = BuiltinsNumber::ToString(ecmaRuntimeCallInfo4); 2534514f5e3Sopenharmony_ci ASSERT_TRUE(result4.IsString()); 2544514f5e3Sopenharmony_ci JSHandle<EcmaString> res4(thread, reinterpret_cast<EcmaString *>(result4.GetRawData())); 2554514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult4 = factory->NewFromASCII("0"); 2564514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res4, *correctResult4)); 2574514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2584514f5e3Sopenharmony_ci} 2594514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, ToString2) 2604514f5e3Sopenharmony_ci{ 2614514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2624514f5e3Sopenharmony_ci // (-50).toString(35) 2634514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2644514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 2654514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(JSTaggedValue(-50)); 2664514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, JSTaggedValue(35)); 2674514f5e3Sopenharmony_ci 2684514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 2694514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsNumber::ToString(ecmaRuntimeCallInfo1); 2704514f5e3Sopenharmony_ci ASSERT_TRUE(result1.IsString()); 2714514f5e3Sopenharmony_ci JSHandle<EcmaString> res1(thread, reinterpret_cast<EcmaString *>(result1.GetRawData())); 2724514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult1 = factory->NewFromASCII("-1f"); 2734514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res1, *correctResult1)); 2744514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2754514f5e3Sopenharmony_ci 2764514f5e3Sopenharmony_ci // (2).toString(2.5) 2774514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2784514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined()); 2794514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetThis(JSTaggedValue(2)); 2804514f5e3Sopenharmony_ci ecmaRuntimeCallInfo2->SetCallArg(0, JSTaggedValue(2.5)); 2814514f5e3Sopenharmony_ci 2824514f5e3Sopenharmony_ci prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2); 2834514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsNumber::ToString(ecmaRuntimeCallInfo2); 2844514f5e3Sopenharmony_ci ASSERT_TRUE(result2.IsString()); 2854514f5e3Sopenharmony_ci JSHandle<EcmaString> res2(thread, reinterpret_cast<EcmaString *>(result2.GetRawData())); 2864514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult2 = factory->NewFromASCII("10"); 2874514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res2, *correctResult2)); 2884514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2894514f5e3Sopenharmony_ci} 2904514f5e3Sopenharmony_ci 2914514f5e3Sopenharmony_ci// new Number(123.456).toExponential(5) 2924514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, IsExponential) 2934514f5e3Sopenharmony_ci{ 2944514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2954514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2964514f5e3Sopenharmony_ci 2974514f5e3Sopenharmony_ci JSHandle<JSFunction> numberObject(env->GetNumberFunction()); 2984514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(thread, JSTaggedValue(123.456)); 2994514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> number = factory->NewJSPrimitiveRef(numberObject, value); 3004514f5e3Sopenharmony_ci 3014514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3024514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3034514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(number.GetTaggedValue()); 3044514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(5.0)); 3054514f5e3Sopenharmony_ci 3064514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3074514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::ToExponential(ecmaRuntimeCallInfo); 3084514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3094514f5e3Sopenharmony_ci JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 3104514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult = factory->NewFromASCII("1.23456e+2"); 3114514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res, *correctResult)); 3124514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3134514f5e3Sopenharmony_ci} 3144514f5e3Sopenharmony_ci 3154514f5e3Sopenharmony_ci// new Number(123.456).toFixed(10) 3164514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, ToFixed) 3174514f5e3Sopenharmony_ci{ 3184514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3194514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 3204514f5e3Sopenharmony_ci 3214514f5e3Sopenharmony_ci JSHandle<JSFunction> numberObject(env->GetNumberFunction()); 3224514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(thread, JSTaggedValue(123.456)); 3234514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> number = factory->NewJSPrimitiveRef(numberObject, value); 3244514f5e3Sopenharmony_ci 3254514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3264514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3274514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(number.GetTaggedValue()); 3284514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(10.0)); 3294514f5e3Sopenharmony_ci 3304514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3314514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::ToFixed(ecmaRuntimeCallInfo); 3324514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3334514f5e3Sopenharmony_ci JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 3344514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult = factory->NewFromASCII("123.4560000000"); 3354514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res, *correctResult)); 3364514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3374514f5e3Sopenharmony_ci} 3384514f5e3Sopenharmony_ci 3394514f5e3Sopenharmony_ci// new Number(123.456).toFixed(30) 3404514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, ToFixed1) 3414514f5e3Sopenharmony_ci{ 3424514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3434514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 3444514f5e3Sopenharmony_ci 3454514f5e3Sopenharmony_ci JSHandle<JSFunction> numberObject(env->GetNumberFunction()); 3464514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(thread, JSTaggedValue(123.456)); 3474514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> number = factory->NewJSPrimitiveRef(numberObject, value); 3484514f5e3Sopenharmony_ci 3494514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3504514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3514514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(number.GetTaggedValue()); 3524514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(30.0)); 3534514f5e3Sopenharmony_ci 3544514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3554514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::ToFixed(ecmaRuntimeCallInfo); 3564514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3574514f5e3Sopenharmony_ci JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 3584514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult = factory->NewFromASCII("123.456000000000003069544618483633"); 3594514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res, *correctResult)); 3604514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3614514f5e3Sopenharmony_ci} 3624514f5e3Sopenharmony_ci 3634514f5e3Sopenharmony_ci// new Number(1e21).toFixed(20) 3644514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, ToFixed2) { 3654514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3664514f5e3Sopenharmony_ci auto ecmaVM = thread->GetEcmaVM(); 3674514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv(); 3684514f5e3Sopenharmony_ci 3694514f5e3Sopenharmony_ci JSHandle<JSFunction> numberObject(env->GetNumberFunction()); 3704514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1e21)); 3714514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> number = factory->NewJSPrimitiveRef(numberObject, value); 3724514f5e3Sopenharmony_ci 3734514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = 3744514f5e3Sopenharmony_ci TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3754514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3764514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(number.GetTaggedValue()); 3774514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(20.0)); 3784514f5e3Sopenharmony_ci 3794514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3804514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::ToFixed(ecmaRuntimeCallInfo); 3814514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 3824514f5e3Sopenharmony_ci JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 3834514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult = factory->NewFromASCII("1e+21"); 3844514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res, *correctResult)); 3854514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3864514f5e3Sopenharmony_ci} 3874514f5e3Sopenharmony_ci 3884514f5e3Sopenharmony_ci// new Number(123.456).toPrecision(8) 3894514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, ToPrecision) 3904514f5e3Sopenharmony_ci{ 3914514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3924514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 3934514f5e3Sopenharmony_ci 3944514f5e3Sopenharmony_ci JSHandle<JSFunction> numberObject(env->GetNumberFunction()); 3954514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value(thread, JSTaggedValue(123.456)); 3964514f5e3Sopenharmony_ci JSHandle<JSPrimitiveRef> number = factory->NewJSPrimitiveRef(numberObject, value); 3974514f5e3Sopenharmony_ci 3984514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3994514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 4004514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(number.GetTaggedValue()); 4014514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(8.0)); 4024514f5e3Sopenharmony_ci 4034514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4044514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::ToPrecision(ecmaRuntimeCallInfo); 4054514f5e3Sopenharmony_ci ASSERT_TRUE(result.IsString()); 4064514f5e3Sopenharmony_ci JSHandle<EcmaString> res(thread, reinterpret_cast<EcmaString *>(result.GetRawData())); 4074514f5e3Sopenharmony_ci JSHandle<EcmaString> correctResult = factory->NewFromASCII("123.45600"); 4084514f5e3Sopenharmony_ci ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*res, *correctResult)); 4094514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4104514f5e3Sopenharmony_ci} 4114514f5e3Sopenharmony_ci 4124514f5e3Sopenharmony_ci// Number.parseFloat(0x123) 4134514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, parseFloat) 4144514f5e3Sopenharmony_ci{ 4154514f5e3Sopenharmony_ci JSHandle<EcmaString> param = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0x123"); 4164514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 4174514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 4184514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 4194514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, param.GetTaggedValue()); 4204514f5e3Sopenharmony_ci 4214514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4224514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::ParseFloat(ecmaRuntimeCallInfo); 4234514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue(static_cast<double>(0)).GetRawData()); 4244514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4254514f5e3Sopenharmony_ci} 4264514f5e3Sopenharmony_ci 4274514f5e3Sopenharmony_ci// Number.parseFloat(0x123xx) 4284514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, parseFloat1) 4294514f5e3Sopenharmony_ci{ 4304514f5e3Sopenharmony_ci JSHandle<EcmaString> param = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0x123xx"); 4314514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 4324514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 4334514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 4344514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, param.GetTaggedValue()); 4354514f5e3Sopenharmony_ci 4364514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4374514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::ParseFloat(ecmaRuntimeCallInfo); 4384514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue(static_cast<double>(0)).GetRawData()); 4394514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4404514f5e3Sopenharmony_ci} 4414514f5e3Sopenharmony_ci 4424514f5e3Sopenharmony_ci// Number.parseInt(0x123) 4434514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, parseInt) 4444514f5e3Sopenharmony_ci{ 4454514f5e3Sopenharmony_ci const char *number = "0x123"; 4464514f5e3Sopenharmony_ci 4474514f5e3Sopenharmony_ci JSHandle<EcmaString> param = thread->GetEcmaVM()->GetFactory()->NewFromASCII(number); 4484514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 4494514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 4504514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 4514514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, param.GetTaggedValue()); 4524514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(16.0)); 4534514f5e3Sopenharmony_ci 4544514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4554514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsNumber::ParseInt(ecmaRuntimeCallInfo); 4564514f5e3Sopenharmony_ci ASSERT_EQ(result.GetRawData(), JSTaggedValue(static_cast<int>(291)).GetRawData()); 4574514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4584514f5e3Sopenharmony_ci} 4594514f5e3Sopenharmony_ci 4604514f5e3Sopenharmony_ci// testcases of StringToDouble flags 4614514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, StringToDoubleFlags) 4624514f5e3Sopenharmony_ci{ 4634514f5e3Sopenharmony_ci JSHandle<EcmaString> str; 4644514f5e3Sopenharmony_ci Span<const uint8_t> sp; 4654514f5e3Sopenharmony_ci CVector<uint8_t> buf; 4664514f5e3Sopenharmony_ci 4674514f5e3Sopenharmony_ci // flags of IGNORE_TRAILING 4684514f5e3Sopenharmony_ci 4694514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0a"); 4704514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4714514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::IGNORE_TRAILING), 0); 4724514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0b"); 4734514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4744514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::IGNORE_TRAILING), 0); 4754514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0o"); 4764514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4774514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::IGNORE_TRAILING), 0); 4784514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 00x"); 4794514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4804514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::IGNORE_TRAILING), 0); 4814514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 000.4_"); 4824514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4834514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::IGNORE_TRAILING), 0.4); 4844514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0010.s "); 4854514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4864514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::IGNORE_TRAILING), 10); 4874514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0010e2"); 4884514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4894514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::IGNORE_TRAILING), 1000); 4904514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0010e+3_0"); 4914514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4924514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::IGNORE_TRAILING), 10000); 4934514f5e3Sopenharmony_ci 4944514f5e3Sopenharmony_ci // flags of ALLOW_HEX 4954514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0x"); 4964514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 4974514f5e3Sopenharmony_ci ASSERT_TRUE(std::isnan(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_HEX))); 4984514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0x10 "); 4994514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5004514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_HEX), 16); 5014514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0x1g"); 5024514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5034514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_HEX + base::IGNORE_TRAILING), 1); 5044514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0xh"); 5054514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5064514f5e3Sopenharmony_ci ASSERT_TRUE(std::isnan( 5074514f5e3Sopenharmony_ci base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_HEX + base::IGNORE_TRAILING))); 5084514f5e3Sopenharmony_ci 5094514f5e3Sopenharmony_ci // flags of ALLOW_OCTAL 5104514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0O"); 5114514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5124514f5e3Sopenharmony_ci ASSERT_TRUE(std::isnan(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_OCTAL))); 5134514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0o10 "); 5144514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5154514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_OCTAL), 8); 5164514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0o1d"); 5174514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5184514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_OCTAL | base::IGNORE_TRAILING), 5194514f5e3Sopenharmony_ci 1); 5204514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0o8"); 5214514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5224514f5e3Sopenharmony_ci ASSERT_TRUE(std::isnan( 5234514f5e3Sopenharmony_ci base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_OCTAL | base::IGNORE_TRAILING))); 5244514f5e3Sopenharmony_ci 5254514f5e3Sopenharmony_ci // flags of ALLOW_BINARY 5264514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0b"); 5274514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5284514f5e3Sopenharmony_ci ASSERT_TRUE(std::isnan(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_BINARY))); 5294514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0b10 "); 5304514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5314514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_BINARY), 2); 5324514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0b1d"); 5334514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5344514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_BINARY | base::IGNORE_TRAILING), 5354514f5e3Sopenharmony_ci 1); 5364514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0b2"); 5374514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5384514f5e3Sopenharmony_ci ASSERT_TRUE(std::isnan( 5394514f5e3Sopenharmony_ci base::NumberHelper::StringToDouble(sp.begin(), sp.end(), 0, base::ALLOW_BINARY | base::IGNORE_TRAILING))); 5404514f5e3Sopenharmony_ci} 5414514f5e3Sopenharmony_ci 5424514f5e3Sopenharmony_ci// testcases of StringToDouble radix 5434514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, StringToDoubleRadix) 5444514f5e3Sopenharmony_ci{ 5454514f5e3Sopenharmony_ci JSHandle<EcmaString> str; 5464514f5e3Sopenharmony_ci Span<const uint8_t> sp; 5474514f5e3Sopenharmony_ci CVector<uint8_t> buf; 5484514f5e3Sopenharmony_ci int radix; 5494514f5e3Sopenharmony_ci 5504514f5e3Sopenharmony_ci radix = 0; // default 10 5514514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 5524514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5534514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 100); 5544514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100.3e2 "); 5554514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5564514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 10030); 5574514f5e3Sopenharmony_ci radix = 1; 5584514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0000 "); 5594514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5604514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 0); 5614514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0001 "); 5624514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5634514f5e3Sopenharmony_ci ASSERT_TRUE(std::isnan(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS))); 5644514f5e3Sopenharmony_ci radix = 2; 5654514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 5664514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5674514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 4); 5684514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 11 "); 5694514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5704514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 3); 5714514f5e3Sopenharmony_ci radix = 3; 5724514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 5734514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5744514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 9); 5754514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 21 "); 5764514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5774514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 7); 5784514f5e3Sopenharmony_ci radix = 4; 5794514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 5804514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5814514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 16); 5824514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 31 "); 5834514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5844514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 13); 5854514f5e3Sopenharmony_ci radix = 8; 5864514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 5874514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5884514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 64); 5894514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 71 "); 5904514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5914514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 57); 5924514f5e3Sopenharmony_ci radix = 10; 5934514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 5944514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5954514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 100); 5964514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 0020 "); 5974514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 5984514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 20); 5994514f5e3Sopenharmony_ci radix = 16; 6004514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 6014514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 6024514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 256); 6034514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 1e "); 6044514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 6054514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 30); 6064514f5e3Sopenharmony_ci radix = 18; 6074514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 6084514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 6094514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 324); 6104514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 1g "); 6114514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 6124514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 34); 6134514f5e3Sopenharmony_ci radix = 25; 6144514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 6154514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 6164514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 625); 6174514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 1g "); 6184514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 6194514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 41); 6204514f5e3Sopenharmony_ci radix = 36; 6214514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 100 "); 6224514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 6234514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 1296); 6244514f5e3Sopenharmony_ci str = thread->GetEcmaVM()->GetFactory()->NewFromASCII(" 1z "); 6254514f5e3Sopenharmony_ci sp = EcmaStringAccessor(str).ToUtf8Span(buf); 6264514f5e3Sopenharmony_ci ASSERT_EQ(base::NumberHelper::StringToDouble(sp.begin(), sp.end(), radix, base::NO_FLAGS), 71); 6274514f5e3Sopenharmony_ci} 6284514f5e3Sopenharmony_ci 6294514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsNumberTest, NumberToString) 6304514f5e3Sopenharmony_ci{ 6314514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 6324514f5e3Sopenharmony_ci JSHandle<EcmaString> res = factory->NewFromASCII("100"); 6334514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6344514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(100)), res), 0); 6354514f5e3Sopenharmony_ci res = factory->NewFromASCII("11223344"); 6364514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6374514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(11223344)), res), 0); 6384514f5e3Sopenharmony_ci res = factory->NewFromASCII("1234567890"); 6394514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6404514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(1234567890)), res), 0); 6414514f5e3Sopenharmony_ci res = factory->NewFromASCII("100"); 6424514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6434514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(100.0))), res), 0); 6444514f5e3Sopenharmony_ci res = factory->NewFromASCII("100.5"); 6454514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6464514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(100.5))), res), 0); 6474514f5e3Sopenharmony_ci res = factory->NewFromASCII("100.25"); 6484514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6494514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(100.25))), res), 0); 6504514f5e3Sopenharmony_ci res = factory->NewFromASCII("100.125"); 6514514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6524514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(100.125))), res), 0); 6534514f5e3Sopenharmony_ci res = factory->NewFromASCII("100.6125"); 6544514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6554514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(100.6125))), res), 0); 6564514f5e3Sopenharmony_ci res = factory->NewFromASCII("0.0006125"); 6574514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6584514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(0.0006125))), res), 0); 6594514f5e3Sopenharmony_ci res = factory->NewFromASCII("-0.0006125"); 6604514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6614514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(-0.0006125))), res), 0); 6624514f5e3Sopenharmony_ci res = factory->NewFromASCII("-1234567890.0006125"); 6634514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6644514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(-1234567890.0006125))), res), 0); 6654514f5e3Sopenharmony_ci res = factory->NewFromASCII("1234567890.0006125"); 6664514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6674514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(1234567890.0006125))), res), 0); 6684514f5e3Sopenharmony_ci res = factory->NewFromASCII("11234567890.000612"); 6694514f5e3Sopenharmony_ci ASSERT_EQ(EcmaStringAccessor::Compare(instance, 6704514f5e3Sopenharmony_ci base::NumberHelper::NumberToString(thread, JSTaggedValue(double(11234567890.0006125))), res), 0); 6714514f5e3Sopenharmony_ci} 6724514f5e3Sopenharmony_ci} // namespace panda::test 673