14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2022 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 <climits> 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/js_bigint.h" 194514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_ciusing namespace panda; 224514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_cinamespace panda::test { 254514f5e3Sopenharmony_ciclass JSBigintTest : public BaseTestWithScope<false> { 264514f5e3Sopenharmony_ci}; 274514f5e3Sopenharmony_ci 284514f5e3Sopenharmony_ci/** 294514f5e3Sopenharmony_ci * @tc.name: Compare 304514f5e3Sopenharmony_ci * @tc.desc: 314514f5e3Sopenharmony_ci * @tc.type: FUNC 324514f5e3Sopenharmony_ci * @tc.require: 334514f5e3Sopenharmony_ci */ 344514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Compare) 354514f5e3Sopenharmony_ci{ 364514f5e3Sopenharmony_ci CString str1 = "9007199254740991012345"; 374514f5e3Sopenharmony_ci CString str2 = "9007199254740991012345 "; 384514f5e3Sopenharmony_ci CString str3 = "-9007199254740991012345"; 394514f5e3Sopenharmony_ci CString str4 = "-9007199254740991012"; 404514f5e3Sopenharmony_ci JSHandle<BigInt> bigint1 = BigIntHelper::SetBigInt(thread, str1); 414514f5e3Sopenharmony_ci JSHandle<BigInt> bigint2 = BigIntHelper::SetBigInt(thread, str2); 424514f5e3Sopenharmony_ci JSHandle<BigInt> bigint3 = BigIntHelper::SetBigInt(thread, str3); 434514f5e3Sopenharmony_ci JSHandle<BigInt> bigint4 = BigIntHelper::SetBigInt(thread, str4); 444514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint1.GetTaggedValue(), bigint1.GetTaggedValue()), ComparisonResult::EQUAL); 454514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint3.GetTaggedValue(), bigint2.GetTaggedValue()), ComparisonResult::LESS); 464514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint1.GetTaggedValue(), bigint2.GetTaggedValue()), ComparisonResult::LESS); 474514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint2.GetTaggedValue(), bigint1.GetTaggedValue()), ComparisonResult::GREAT); 484514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint2.GetTaggedValue(), bigint3.GetTaggedValue()), ComparisonResult::GREAT); 494514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint3.GetTaggedValue(), bigint4.GetTaggedValue()), ComparisonResult::LESS); 504514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint4.GetTaggedValue(), bigint3.GetTaggedValue()), ComparisonResult::GREAT); 514514f5e3Sopenharmony_ci 524514f5e3Sopenharmony_ci JSHandle<BigInt> zero = BigInt::Uint32ToBigInt(thread, 0); 534514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(zero.GetTaggedValue(), bigint1.GetTaggedValue()), ComparisonResult::LESS); 544514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint1.GetTaggedValue(), zero.GetTaggedValue()), ComparisonResult::GREAT); 554514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(zero.GetTaggedValue(), zero.GetTaggedValue()), ComparisonResult::EQUAL); 564514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(zero.GetTaggedValue(), bigint3.GetTaggedValue()), ComparisonResult::GREAT); 574514f5e3Sopenharmony_ci EXPECT_EQ(BigInt::Compare(bigint3.GetTaggedValue(), zero.GetTaggedValue()), ComparisonResult::LESS); 584514f5e3Sopenharmony_ci} 594514f5e3Sopenharmony_ci 604514f5e3Sopenharmony_ci/** 614514f5e3Sopenharmony_ci * @tc.name: CreateBigint 624514f5e3Sopenharmony_ci * @tc.desc: 634514f5e3Sopenharmony_ci * @tc.type: FUNC 644514f5e3Sopenharmony_ci * @tc.require: 654514f5e3Sopenharmony_ci */ 664514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, CreateBigint) 674514f5e3Sopenharmony_ci{ 684514f5e3Sopenharmony_ci uint32_t size = 100; 694514f5e3Sopenharmony_ci JSHandle<BigInt> bigint = BigInt::CreateBigint(thread, size); 704514f5e3Sopenharmony_ci EXPECT_EQ(bigint->GetLength(), size); 714514f5e3Sopenharmony_ci} 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_ci/** 744514f5e3Sopenharmony_ci * @tc.name: Equal & SameValue & SameValueZero 754514f5e3Sopenharmony_ci * @tc.desc: 764514f5e3Sopenharmony_ci * @tc.type: FUNC 774514f5e3Sopenharmony_ci * @tc.require: 784514f5e3Sopenharmony_ci */ 794514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Equal_SameValue_SameValueZero) 804514f5e3Sopenharmony_ci{ 814514f5e3Sopenharmony_ci // The largest safe integer in JavaScript is in [-(2 ^ 53 - 1), 2 ^ 53 - 1]. 824514f5e3Sopenharmony_ci CString maxSafeIntStr = "9007199254740991"; 834514f5e3Sopenharmony_ci CString minSafeIntStr = "-9007199254740991"; 844514f5e3Sopenharmony_ci JSHandle<BigInt> maxSafeInt = BigIntHelper::SetBigInt(thread, maxSafeIntStr); 854514f5e3Sopenharmony_ci JSHandle<BigInt> minSafeInt = BigIntHelper::SetBigInt(thread, minSafeIntStr); 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ci // Compare two integers in the safe range. 884514f5e3Sopenharmony_ci JSHandle<BigInt> minusMinSafeInt = BigInt::UnaryMinus(thread, minSafeInt); 894514f5e3Sopenharmony_ci JSHandle<BigInt> minusMaxSafeInt = BigInt::UnaryMinus(thread, maxSafeInt); 904514f5e3Sopenharmony_ci bool result1 = BigInt::Equal(maxSafeInt.GetTaggedValue(), minSafeInt.GetTaggedValue()); 914514f5e3Sopenharmony_ci bool result2 = BigInt::SameValue(maxSafeInt.GetTaggedValue(), minSafeInt.GetTaggedValue()); 924514f5e3Sopenharmony_ci bool result3 = BigInt::SameValueZero(maxSafeInt.GetTaggedValue(), minSafeInt.GetTaggedValue()); 934514f5e3Sopenharmony_ci EXPECT_TRUE(!result1); 944514f5e3Sopenharmony_ci EXPECT_TRUE(!result2); 954514f5e3Sopenharmony_ci EXPECT_TRUE(!result3); 964514f5e3Sopenharmony_ci result1 = BigInt::Equal(maxSafeInt.GetTaggedValue(), minusMinSafeInt.GetTaggedValue()); 974514f5e3Sopenharmony_ci result2 = BigInt::SameValue(maxSafeInt.GetTaggedValue(), minusMinSafeInt.GetTaggedValue()); 984514f5e3Sopenharmony_ci result3 = BigInt::SameValueZero(maxSafeInt.GetTaggedValue(), minusMinSafeInt.GetTaggedValue()); 994514f5e3Sopenharmony_ci EXPECT_TRUE(result1); 1004514f5e3Sopenharmony_ci EXPECT_TRUE(result2); 1014514f5e3Sopenharmony_ci EXPECT_TRUE(result3); 1024514f5e3Sopenharmony_ci result1 = BigInt::Equal(minSafeInt.GetTaggedValue(), minusMaxSafeInt.GetTaggedValue()); 1034514f5e3Sopenharmony_ci result2 = BigInt::SameValue(minSafeInt.GetTaggedValue(), minusMaxSafeInt.GetTaggedValue()); 1044514f5e3Sopenharmony_ci result3 = BigInt::SameValueZero(minSafeInt.GetTaggedValue(), minusMaxSafeInt.GetTaggedValue()); 1054514f5e3Sopenharmony_ci EXPECT_TRUE(result1); 1064514f5e3Sopenharmony_ci EXPECT_TRUE(result2); 1074514f5e3Sopenharmony_ci EXPECT_TRUE(result3); 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_ci // Compare two integers outside the safe range. 1104514f5e3Sopenharmony_ci CString unsafeIntStr1 = maxSafeIntStr + "0123456789"; 1114514f5e3Sopenharmony_ci CString unsafeIntStr2 = minSafeIntStr + "0123456789"; 1124514f5e3Sopenharmony_ci JSHandle<BigInt> unsafeInt1 = BigIntHelper::SetBigInt(thread, unsafeIntStr1); 1134514f5e3Sopenharmony_ci JSHandle<BigInt> unsafeInt2 = BigIntHelper::SetBigInt(thread, unsafeIntStr2); 1144514f5e3Sopenharmony_ci JSHandle<BigInt> minusUnsafeInt1 = BigInt::UnaryMinus(thread, unsafeInt1); 1154514f5e3Sopenharmony_ci result1 = BigInt::Equal(unsafeInt2.GetTaggedValue(), minusUnsafeInt1.GetTaggedValue()); 1164514f5e3Sopenharmony_ci result2 = BigInt::SameValue(unsafeInt2.GetTaggedValue(), minusUnsafeInt1.GetTaggedValue()); 1174514f5e3Sopenharmony_ci result3 = BigInt::SameValueZero(unsafeInt2.GetTaggedValue(), minusUnsafeInt1.GetTaggedValue()); 1184514f5e3Sopenharmony_ci EXPECT_TRUE(result1); 1194514f5e3Sopenharmony_ci EXPECT_TRUE(result2); 1204514f5e3Sopenharmony_ci EXPECT_TRUE(result3); 1214514f5e3Sopenharmony_ci} 1224514f5e3Sopenharmony_ci 1234514f5e3Sopenharmony_ci/** 1244514f5e3Sopenharmony_ci * @tc.name: InitializationZero 1254514f5e3Sopenharmony_ci * @tc.desc: 1264514f5e3Sopenharmony_ci * @tc.type: FUNC 1274514f5e3Sopenharmony_ci * @tc.require: 1284514f5e3Sopenharmony_ci */ 1294514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, InitializationZero) 1304514f5e3Sopenharmony_ci{ 1314514f5e3Sopenharmony_ci CString maxSafeIntPlusOneStr = "9007199254740992"; 1324514f5e3Sopenharmony_ci JSHandle<BigInt> maxSafeIntPlusOne = BigIntHelper::SetBigInt(thread, maxSafeIntPlusOneStr); 1334514f5e3Sopenharmony_ci uint32_t size = maxSafeIntPlusOne->GetLength(); 1344514f5e3Sopenharmony_ci uint32_t countZero = 0; 1354514f5e3Sopenharmony_ci for (uint32_t i = 0; i < size; i++) { 1364514f5e3Sopenharmony_ci uint32_t digit = maxSafeIntPlusOne->GetDigit(i); 1374514f5e3Sopenharmony_ci if (digit == 0) { 1384514f5e3Sopenharmony_ci countZero++; 1394514f5e3Sopenharmony_ci } 1404514f5e3Sopenharmony_ci } 1414514f5e3Sopenharmony_ci EXPECT_NE(countZero, size); 1424514f5e3Sopenharmony_ci 1434514f5e3Sopenharmony_ci maxSafeIntPlusOne->InitializationZero(); 1444514f5e3Sopenharmony_ci for (uint32_t i = 0; i < size; i++) { 1454514f5e3Sopenharmony_ci uint32_t digit = maxSafeIntPlusOne->GetDigit(i); 1464514f5e3Sopenharmony_ci EXPECT_EQ(digit, 0U); 1474514f5e3Sopenharmony_ci } 1484514f5e3Sopenharmony_ci} 1494514f5e3Sopenharmony_ci 1504514f5e3Sopenharmony_ci/** 1514514f5e3Sopenharmony_ci * @tc.name: BitwiseOp & BitwiseAND & BitwiseXOR & BitwiseOR & BitwiseSubOne & BitwiseAddOne & BitwiseNOT 1524514f5e3Sopenharmony_ci * @tc.desc: 1534514f5e3Sopenharmony_ci * @tc.type: FUNC 1544514f5e3Sopenharmony_ci * @tc.require: 1554514f5e3Sopenharmony_ci */ 1564514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Bitwise_AND_XOR_OR_NOT_SubOne_AddOne) 1574514f5e3Sopenharmony_ci{ 1584514f5e3Sopenharmony_ci CString maxSafeIntStr = "11111111111111111111111111111111111111111111111111111"; // Binary: 2 ^ 53 - 1 1594514f5e3Sopenharmony_ci CString maxSafeIntPlusOneStr = "100000000000000000000000000000000000000000000000000000"; // Binary: 2 ^ 53 1604514f5e3Sopenharmony_ci CString bigintStr1 = "111111111111111111111111111111111111111111111111111111"; // Binary: 2 ^ 54 - 1 1614514f5e3Sopenharmony_ci CString bigintStr2 = "11011100"; 1624514f5e3Sopenharmony_ci JSHandle<BigInt> maxSafeInt = BigIntHelper::SetBigInt(thread, maxSafeIntStr, BigInt::BINARY); 1634514f5e3Sopenharmony_ci JSHandle<BigInt> maxSafeIntPlusOne = BigIntHelper::SetBigInt(thread, maxSafeIntPlusOneStr, BigInt::BINARY); 1644514f5e3Sopenharmony_ci JSHandle<BigInt> bigint1 = BigIntHelper::SetBigInt(thread, bigintStr1, BigInt::BINARY); 1654514f5e3Sopenharmony_ci JSHandle<BigInt> bigint2 = BigIntHelper::SetBigInt(thread, bigintStr2, BigInt::BINARY); 1664514f5e3Sopenharmony_ci JSHandle<BigInt> bigint3 = BigInt::UnaryMinus(thread, bigint2); 1674514f5e3Sopenharmony_ci JSHandle<BigInt> bigint4 = BigInt::UnaryMinus(thread, bigint1); 1684514f5e3Sopenharmony_ci // Bitwise AND operation 1694514f5e3Sopenharmony_ci JSHandle<BigInt> addOpRes = BigInt::BitwiseOp(thread, Operate::AND, maxSafeIntPlusOne, bigint1); 1704514f5e3Sopenharmony_ci JSHandle<BigInt> andRes = BigInt::BitwiseAND(thread, maxSafeIntPlusOne, bigint1); 1714514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(addOpRes.GetTaggedValue(), maxSafeIntPlusOne.GetTaggedValue())); 1724514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(andRes.GetTaggedValue(), maxSafeIntPlusOne.GetTaggedValue())); 1734514f5e3Sopenharmony_ci 1744514f5e3Sopenharmony_ci JSHandle<BigInt> addOpRes1 = BigInt::BitwiseOp(thread, Operate::AND, bigint1, bigint2); 1754514f5e3Sopenharmony_ci JSHandle<BigInt> andRes1 = BigInt::BitwiseAND(thread, bigint1, bigint2); 1764514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(addOpRes1.GetTaggedValue(), bigint2.GetTaggedValue())); 1774514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(andRes1.GetTaggedValue(), bigint2.GetTaggedValue())); 1784514f5e3Sopenharmony_ci 1794514f5e3Sopenharmony_ci JSHandle<BigInt> addOpRes2 = BigInt::BitwiseOp(thread, Operate::AND, bigint2, bigint1); 1804514f5e3Sopenharmony_ci JSHandle<BigInt> andRes2 = BigInt::BitwiseAND(thread, bigint2, bigint1); 1814514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(addOpRes2.GetTaggedValue(), bigint2.GetTaggedValue())); 1824514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(andRes2.GetTaggedValue(), bigint2.GetTaggedValue())); 1834514f5e3Sopenharmony_ci 1844514f5e3Sopenharmony_ci CString bigintStr4 = "111111111111111111111111111111111111111111111100100100"; 1854514f5e3Sopenharmony_ci JSHandle<BigInt> bigint = BigIntHelper::SetBigInt(thread, bigintStr4, BigInt::BINARY); 1864514f5e3Sopenharmony_ci JSHandle<BigInt> andRes3 = BigInt::BitwiseAND(thread, bigint3, bigint1); 1874514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(andRes3.GetTaggedValue(), bigint.GetTaggedValue())); 1884514f5e3Sopenharmony_ci 1894514f5e3Sopenharmony_ci JSHandle<BigInt> andRes4 = BigInt::BitwiseAND(thread, bigint1, bigint3); 1904514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(andRes4.GetTaggedValue(), bigint.GetTaggedValue())); 1914514f5e3Sopenharmony_ci 1924514f5e3Sopenharmony_ci CString bigintStr5 = "-1000000000000000000000000000000000000000000000000000000"; 1934514f5e3Sopenharmony_ci JSHandle<BigInt> bigint5 = BigIntHelper::SetBigInt(thread, bigintStr5, BigInt::BINARY); 1944514f5e3Sopenharmony_ci JSHandle<BigInt> andRes5 = BigInt::BitwiseAND(thread, bigint3, bigint4); 1954514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(andRes5.GetTaggedValue(), bigint5.GetTaggedValue())); 1964514f5e3Sopenharmony_ci 1974514f5e3Sopenharmony_ci CString bigintStr6 = "-1000000000000000000000000000000000000000000000000000000"; 1984514f5e3Sopenharmony_ci JSHandle<BigInt> bigint6 = BigIntHelper::SetBigInt(thread, bigintStr6, BigInt::BINARY); 1994514f5e3Sopenharmony_ci JSHandle<BigInt> andRes6 = BigInt::BitwiseAND(thread, bigint4, bigint3); 2004514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(andRes6.GetTaggedValue(), bigint6.GetTaggedValue())); 2014514f5e3Sopenharmony_ci 2024514f5e3Sopenharmony_ci // Bitwise OR operation 2034514f5e3Sopenharmony_ci JSHandle<BigInt> orOpRes = BigInt::BitwiseOp(thread, Operate::OR, maxSafeInt, maxSafeIntPlusOne); 2044514f5e3Sopenharmony_ci JSHandle<BigInt> orRes = BigInt::BitwiseOR(thread, maxSafeInt, maxSafeIntPlusOne); 2054514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(orOpRes.GetTaggedValue(), bigint1.GetTaggedValue())); 2064514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(orRes.GetTaggedValue(), bigint1.GetTaggedValue())); 2074514f5e3Sopenharmony_ci 2084514f5e3Sopenharmony_ci JSHandle<BigInt> orRes1 = BigInt::BitwiseOR(thread, bigint3, maxSafeIntPlusOne); 2094514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(orRes1.GetTaggedValue(), bigint3.GetTaggedValue())); 2104514f5e3Sopenharmony_ci 2114514f5e3Sopenharmony_ci JSHandle<BigInt> orRes2 = BigInt::BitwiseOR(thread, maxSafeIntPlusOne, bigint3); 2124514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(orRes2.GetTaggedValue(), bigint3.GetTaggedValue())); 2134514f5e3Sopenharmony_ci 2144514f5e3Sopenharmony_ci CString bigintStr7 = "-11011011"; 2154514f5e3Sopenharmony_ci JSHandle<BigInt> bigint7 = BigIntHelper::SetBigInt(thread, bigintStr7, BigInt::BINARY); 2164514f5e3Sopenharmony_ci JSHandle<BigInt> orRes3 = BigInt::BitwiseOR(thread, bigint3, bigint4); 2174514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(orRes3.GetTaggedValue(), bigint7.GetTaggedValue())); 2184514f5e3Sopenharmony_ci 2194514f5e3Sopenharmony_ci // Bitwise XOR operation 2204514f5e3Sopenharmony_ci JSHandle<BigInt> xorOpRes = BigInt::BitwiseOp(thread, Operate::XOR, maxSafeIntPlusOne, bigint1); 2214514f5e3Sopenharmony_ci JSHandle<BigInt> xorRes = BigInt::BitwiseXOR(thread, maxSafeIntPlusOne, bigint1); 2224514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(xorOpRes.GetTaggedValue(), maxSafeInt.GetTaggedValue())); 2234514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(xorRes.GetTaggedValue(), maxSafeInt.GetTaggedValue())); 2244514f5e3Sopenharmony_ci 2254514f5e3Sopenharmony_ci CString bigintStr8 = "-100000000000000000000000000000000000000000000011011100"; 2264514f5e3Sopenharmony_ci JSHandle<BigInt> bigint8 = BigIntHelper::SetBigInt(thread, bigintStr8, BigInt::BINARY); 2274514f5e3Sopenharmony_ci JSHandle<BigInt> xorRes1 = BigInt::BitwiseXOR(thread, bigint3, maxSafeIntPlusOne); 2284514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(xorRes1.GetTaggedValue(), bigint8.GetTaggedValue())); 2294514f5e3Sopenharmony_ci 2304514f5e3Sopenharmony_ci JSHandle<BigInt> xorRes2 = BigInt::BitwiseXOR(thread, maxSafeIntPlusOne, bigint3); 2314514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(xorRes2.GetTaggedValue(), bigint8.GetTaggedValue())); 2324514f5e3Sopenharmony_ci 2334514f5e3Sopenharmony_ci CString bigintStr9 = "111111111111111111111111111111111111111111111100100101"; 2344514f5e3Sopenharmony_ci JSHandle<BigInt> bigint9 = BigIntHelper::SetBigInt(thread, bigintStr9, BigInt::BINARY); 2354514f5e3Sopenharmony_ci JSHandle<BigInt> xorRes3 = BigInt::BitwiseXOR(thread, bigint3, bigint4); 2364514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(xorRes3.GetTaggedValue(), bigint9.GetTaggedValue())); 2374514f5e3Sopenharmony_ci 2384514f5e3Sopenharmony_ci // Bitwise NOT operation, include sign bits. 2394514f5e3Sopenharmony_ci JSHandle<BigInt> notRes1 = BigInt::BitwiseNOT(thread, maxSafeInt); 2404514f5e3Sopenharmony_ci JSHandle<BigInt> minusMaxSafeInt = BigInt::UnaryMinus(thread, maxSafeIntPlusOne); 2414514f5e3Sopenharmony_ci // ~x == -x-1 == -(x+1) 2424514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(notRes1.GetTaggedValue(), minusMaxSafeInt.GetTaggedValue())); 2434514f5e3Sopenharmony_ci JSHandle<BigInt> notRes2 = BigInt::BitwiseNOT(thread, minusMaxSafeInt); 2444514f5e3Sopenharmony_ci // ~(-x) == ~(~(x-1)) == x-1 2454514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(notRes2.GetTaggedValue(), maxSafeInt.GetTaggedValue())); 2464514f5e3Sopenharmony_ci 2474514f5e3Sopenharmony_ci // Bitwise sub one operation, include sign bits. 2484514f5e3Sopenharmony_ci uint32_t maxSize = maxSafeIntPlusOne->GetLength(); 2494514f5e3Sopenharmony_ci JSHandle<BigInt> subOneRes = BigInt::BitwiseSubOne(thread, maxSafeIntPlusOne, maxSize); 2504514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(subOneRes.GetTaggedValue(), maxSafeInt.GetTaggedValue())); 2514514f5e3Sopenharmony_ci 2524514f5e3Sopenharmony_ci // Bitwise add one operation, include sign bits. 2534514f5e3Sopenharmony_ci JSHandle<BigInt> addOneRes = BigInt::BitwiseAddOne(thread, maxSafeInt); 2544514f5e3Sopenharmony_ci JSHandle<BigInt> minusMaxSafePlusOneInt = BigInt::UnaryMinus(thread, maxSafeIntPlusOne); 2554514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(addOneRes.GetTaggedValue(), minusMaxSafePlusOneInt.GetTaggedValue())); 2564514f5e3Sopenharmony_ci 2574514f5e3Sopenharmony_ci JSHandle<BigInt> newBigint = BigInt::CreateBigint(thread, 2); 2584514f5e3Sopenharmony_ci newBigint->SetDigit(0, std::numeric_limits<uint32_t>::max()); 2594514f5e3Sopenharmony_ci newBigint->SetDigit(1, std::numeric_limits<uint32_t>::max()); 2604514f5e3Sopenharmony_ci JSHandle<BigInt> addOneRes1 = BigInt::BitwiseAddOne(thread, newBigint); 2614514f5e3Sopenharmony_ci addOneRes1->SetSign(false); 2624514f5e3Sopenharmony_ci JSHandle<BigInt> newBigint1 = BigInt::CreateBigint(thread, 3); 2634514f5e3Sopenharmony_ci newBigint1->SetDigit(0, 0); 2644514f5e3Sopenharmony_ci newBigint1->SetDigit(1, 0); 2654514f5e3Sopenharmony_ci newBigint1->SetDigit(2, 1); 2664514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(addOneRes1.GetTaggedValue(), newBigint1.GetTaggedValue())); 2674514f5e3Sopenharmony_ci} 2684514f5e3Sopenharmony_ci 2694514f5e3Sopenharmony_ci/** 2704514f5e3Sopenharmony_ci * @tc.name: ToString & ToStdString 2714514f5e3Sopenharmony_ci * @tc.desc: 2724514f5e3Sopenharmony_ci * @tc.type: FUNC 2734514f5e3Sopenharmony_ci * @tc.require: 2744514f5e3Sopenharmony_ci */ 2754514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, ToString_ToStdString) 2764514f5e3Sopenharmony_ci{ 2774514f5e3Sopenharmony_ci CString bigintStdStr1 = "111111111111111111111111111111111111111111111111111111"; // Binary: 2 ^ 54 - 1 2784514f5e3Sopenharmony_ci CString bigintStdStr2 = "1234567890987654321"; // Decimal 2794514f5e3Sopenharmony_ci JSHandle<BigInt> bigint1 = BigIntHelper::SetBigInt(thread, bigintStdStr1, BigInt::BINARY); 2804514f5e3Sopenharmony_ci JSHandle<BigInt> bigint2 = BigIntHelper::SetBigInt(thread, bigintStdStr2, BigInt::DECIMAL); 2814514f5e3Sopenharmony_ci 2824514f5e3Sopenharmony_ci JSHandle<EcmaString> bigintEcmaStrBin1 = BigInt::ToString(thread, bigint1, BigInt::BINARY); 2834514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrBin1).ToCString().c_str(), 2844514f5e3Sopenharmony_ci "111111111111111111111111111111111111111111111111111111"); 2854514f5e3Sopenharmony_ci JSHandle<EcmaString> bigintEcmaStrOct1 = BigInt::ToString(thread, bigint1, BigInt::OCTAL); 2864514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrOct1).ToCString().c_str(), "777777777777777777"); 2874514f5e3Sopenharmony_ci JSHandle<EcmaString> bigintEcmaStrDec1 = BigInt::ToString(thread, bigint1, BigInt::DECIMAL); 2884514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrDec1).ToCString().c_str(), "18014398509481983"); 2894514f5e3Sopenharmony_ci JSHandle<EcmaString> bigintEcmaStrHex1 = BigInt::ToString(thread, bigint1, BigInt::HEXADECIMAL); 2904514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrHex1).ToCString().c_str(), "3fffffffffffff"); 2914514f5e3Sopenharmony_ci 2924514f5e3Sopenharmony_ci JSHandle<EcmaString> bigintEcmaStrBin2 = BigInt::ToString(thread, bigint2, BigInt::BINARY); 2934514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrBin2).ToCString().c_str(), 2944514f5e3Sopenharmony_ci "1000100100010000100001111010010110001011011000001110010110001"); 2954514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrBin2).ToCString().c_str(), 2964514f5e3Sopenharmony_ci (bigint2->ToStdString(BigInt::BINARY)).c_str()); 2974514f5e3Sopenharmony_ci 2984514f5e3Sopenharmony_ci JSHandle<EcmaString> bigintEcmaStrOct2 = BigInt::ToString(thread, bigint2, BigInt::OCTAL); 2994514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrOct2).ToCString().c_str(), "104420417226133016261"); 3004514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrOct2).ToCString().c_str(), 3014514f5e3Sopenharmony_ci (bigint2->ToStdString(BigInt::OCTAL)).c_str()); 3024514f5e3Sopenharmony_ci 3034514f5e3Sopenharmony_ci JSHandle<EcmaString> bigintEcmaStrDec2 = BigInt::ToString(thread, bigint2, BigInt::DECIMAL); 3044514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrDec2).ToCString().c_str(), "1234567890987654321"); 3054514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrDec2).ToCString().c_str(), 3064514f5e3Sopenharmony_ci (bigint2->ToStdString(BigInt::DECIMAL)).c_str()); 3074514f5e3Sopenharmony_ci 3084514f5e3Sopenharmony_ci JSHandle<EcmaString> bigintEcmaStrHex2 = BigInt::ToString(thread, bigint2, BigInt::HEXADECIMAL); 3094514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrHex2).ToCString().c_str(), "112210f4b16c1cb1"); 3104514f5e3Sopenharmony_ci EXPECT_STREQ(EcmaStringAccessor(bigintEcmaStrHex2).ToCString().c_str(), 3114514f5e3Sopenharmony_ci (bigint2->ToStdString(BigInt::HEXADECIMAL)).c_str()); 3124514f5e3Sopenharmony_ci} 3134514f5e3Sopenharmony_ci 3144514f5e3Sopenharmony_ci/** 3154514f5e3Sopenharmony_ci * @tc.name: UnaryMinus 3164514f5e3Sopenharmony_ci * @tc.desc: 3174514f5e3Sopenharmony_ci * @tc.type: FUNC 3184514f5e3Sopenharmony_ci * @tc.require: 3194514f5e3Sopenharmony_ci */ 3204514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, UnaryMinus) 3214514f5e3Sopenharmony_ci{ 3224514f5e3Sopenharmony_ci CString maxSafeIntStr = "9007199254740991"; 3234514f5e3Sopenharmony_ci CString minSafeIntStr = "-9007199254740991"; 3244514f5e3Sopenharmony_ci CString maxSafeIntPlusOneStr = "9007199254740992"; 3254514f5e3Sopenharmony_ci CString minSafeIntSubOneStr = "-9007199254740992"; 3264514f5e3Sopenharmony_ci JSHandle<BigInt> maxSafeInt = BigIntHelper::SetBigInt(thread, maxSafeIntStr); 3274514f5e3Sopenharmony_ci JSHandle<BigInt> minSafeInt = BigIntHelper::SetBigInt(thread, minSafeIntStr); 3284514f5e3Sopenharmony_ci JSHandle<BigInt> maxSafeIntPlusOne = BigIntHelper::SetBigInt(thread, maxSafeIntPlusOneStr); 3294514f5e3Sopenharmony_ci JSHandle<BigInt> minSafeIntSubOne = BigIntHelper::SetBigInt(thread, minSafeIntSubOneStr); 3304514f5e3Sopenharmony_ci 3314514f5e3Sopenharmony_ci JSHandle<BigInt> minusRes1 = BigInt::UnaryMinus(thread, maxSafeInt); 3324514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(minusRes1.GetTaggedValue(), minSafeInt.GetTaggedValue())); 3334514f5e3Sopenharmony_ci JSHandle<BigInt> minusRes2 = BigInt::UnaryMinus(thread, minSafeInt); 3344514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(minusRes2.GetTaggedValue(), maxSafeInt.GetTaggedValue())); 3354514f5e3Sopenharmony_ci JSHandle<BigInt> minusRes3 = BigInt::UnaryMinus(thread, maxSafeIntPlusOne); 3364514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(minusRes3.GetTaggedValue(), minSafeIntSubOne.GetTaggedValue())); 3374514f5e3Sopenharmony_ci JSHandle<BigInt> minusRes4 = BigInt::UnaryMinus(thread, minSafeIntSubOne); 3384514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(minusRes4.GetTaggedValue(), maxSafeIntPlusOne.GetTaggedValue())); 3394514f5e3Sopenharmony_ci 3404514f5e3Sopenharmony_ci JSHandle<BigInt> zero = BigInt::Int32ToBigInt(thread, 0); 3414514f5e3Sopenharmony_ci JSHandle<BigInt> minusRes5 = BigInt::UnaryMinus(thread, zero); 3424514f5e3Sopenharmony_ci EXPECT_TRUE(!minusRes5->GetSign()); 3434514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(zero.GetTaggedValue(), minusRes5.GetTaggedValue())); 3444514f5e3Sopenharmony_ci} 3454514f5e3Sopenharmony_ci 3464514f5e3Sopenharmony_ci/** 3474514f5e3Sopenharmony_ci * @tc.name: Exponentiate & Multiply & Divide & Remainder 3484514f5e3Sopenharmony_ci * @tc.desc: 3494514f5e3Sopenharmony_ci * @tc.type: FUNC 3504514f5e3Sopenharmony_ci * @tc.require: 3514514f5e3Sopenharmony_ci */ 3524514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Exponentiate_Multiply_Divide_Remainder) 3534514f5e3Sopenharmony_ci{ 3544514f5e3Sopenharmony_ci CString baseBigintStr = "2"; 3554514f5e3Sopenharmony_ci CString expBigintStr1 = "53"; 3564514f5e3Sopenharmony_ci CString expBigintStr2 = "54"; 3574514f5e3Sopenharmony_ci CString resBigintStr1 = "9007199254740992"; // 2 ^ 53 3584514f5e3Sopenharmony_ci CString resBigintStr2 = "18014398509481984"; // 2 ^ 54 3594514f5e3Sopenharmony_ci CString resBigintStr3 = "162259276829213363391578010288128"; // 2 ^ 107 3604514f5e3Sopenharmony_ci CString resBigintStr4 = "162259276829213363391578010288182"; // 2 ^ 107 + 54 3614514f5e3Sopenharmony_ci JSHandle<BigInt> baseBigint = BigIntHelper::SetBigInt(thread, baseBigintStr); 3624514f5e3Sopenharmony_ci JSHandle<BigInt> expBigint1 = BigIntHelper::SetBigInt(thread, expBigintStr1); 3634514f5e3Sopenharmony_ci JSHandle<BigInt> expBigint2 = BigIntHelper::SetBigInt(thread, expBigintStr2); 3644514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigIntHelper::SetBigInt(thread, resBigintStr1); 3654514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigIntHelper::SetBigInt(thread, resBigintStr2); 3664514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigIntHelper::SetBigInt(thread, resBigintStr3); 3674514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint4 = BigIntHelper::SetBigInt(thread, resBigintStr4); 3684514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint5 = BigInt::Int32ToBigInt(thread, -1); 3694514f5e3Sopenharmony_ci JSHandle<BigInt> zero = BigInt::Int32ToBigInt(thread, 0); 3704514f5e3Sopenharmony_ci // Exponentiate 3714514f5e3Sopenharmony_ci JSHandle<BigInt> expRes1 = BigInt::Exponentiate(thread, baseBigint, expBigint1); 3724514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(expRes1.GetTaggedValue(), resBigint1.GetTaggedValue())); 3734514f5e3Sopenharmony_ci JSHandle<BigInt> expRes2 = BigInt::Exponentiate(thread, baseBigint, expBigint2); 3744514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(expRes2.GetTaggedValue(), resBigint2.GetTaggedValue())); 3754514f5e3Sopenharmony_ci JSHandle<BigInt> expRes3 = BigInt::Exponentiate(thread, baseBigint, resBigint5); 3764514f5e3Sopenharmony_ci EXPECT_TRUE(expRes3.GetTaggedValue().IsException()); 3774514f5e3Sopenharmony_ci thread->ClearException(); 3784514f5e3Sopenharmony_ci // Multiply 3794514f5e3Sopenharmony_ci JSHandle<BigInt> mulRes1 = BigInt::Multiply(thread, baseBigint, baseBigint); 3804514f5e3Sopenharmony_ci for (int32_t i = 0; i < atoi(expBigintStr1.c_str()) - 2; i++) { 3814514f5e3Sopenharmony_ci mulRes1 = BigInt::Multiply(thread, mulRes1, baseBigint); 3824514f5e3Sopenharmony_ci } 3834514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(mulRes1.GetTaggedValue(), resBigint1.GetTaggedValue())); 3844514f5e3Sopenharmony_ci JSHandle<BigInt> mulRes2 = BigInt::Multiply(thread, baseBigint, baseBigint); 3854514f5e3Sopenharmony_ci for (int32_t i = 0; i < atoi(expBigintStr2.c_str()) - 2; i++) { 3864514f5e3Sopenharmony_ci mulRes2 = BigInt::Multiply(thread, mulRes2, baseBigint); 3874514f5e3Sopenharmony_ci } 3884514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(mulRes2.GetTaggedValue(), resBigint2.GetTaggedValue())); 3894514f5e3Sopenharmony_ci JSHandle<BigInt> mulRes3 = BigInt::Multiply(thread, resBigint1, resBigint2); 3904514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(mulRes3.GetTaggedValue(), resBigint3.GetTaggedValue())); 3914514f5e3Sopenharmony_ci 3924514f5e3Sopenharmony_ci // Divide 3934514f5e3Sopenharmony_ci // The result has no remainder. 3944514f5e3Sopenharmony_ci JSHandle<BigInt> divRes1 = BigInt::Divide(thread, resBigint3, resBigint2); 3954514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(divRes1.GetTaggedValue(), resBigint1.GetTaggedValue())); 3964514f5e3Sopenharmony_ci JSHandle<BigInt> divRes2 = BigInt::Divide(thread, resBigint3, resBigint1); 3974514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(divRes2.GetTaggedValue(), resBigint2.GetTaggedValue())); 3984514f5e3Sopenharmony_ci // The result has a remainder. 3994514f5e3Sopenharmony_ci JSHandle<BigInt> divRes3 = BigInt::Divide(thread, resBigint4, resBigint1); 4004514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(divRes3.GetTaggedValue(), resBigint2.GetTaggedValue())); 4014514f5e3Sopenharmony_ci JSHandle<BigInt> divRes4 = BigInt::Divide(thread, resBigint4, resBigint2); 4024514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(divRes4.GetTaggedValue(), resBigint1.GetTaggedValue())); 4034514f5e3Sopenharmony_ci JSHandle<BigInt> divRes5 = BigInt::Divide(thread, baseBigint, zero); 4044514f5e3Sopenharmony_ci EXPECT_TRUE(divRes5.GetTaggedValue().IsException()); 4054514f5e3Sopenharmony_ci thread->ClearException(); 4064514f5e3Sopenharmony_ci JSHandle<BigInt> divRes6 = BigInt::Divide(thread, expBigint2, baseBigint); 4074514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes6 = BigInt::Int32ToBigInt(thread, 27); // 27 : Expected calculation results 4084514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(divRes6.GetTaggedValue(), expectRes6.GetTaggedValue())); 4094514f5e3Sopenharmony_ci JSHandle<BigInt> divRes7 = BigInt::Divide(thread, expBigint1, baseBigint); 4104514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes7 = BigInt::Int32ToBigInt(thread, 26); // 26 : Expected calculation results 4114514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(divRes7.GetTaggedValue(), expectRes7.GetTaggedValue())); 4124514f5e3Sopenharmony_ci 4134514f5e3Sopenharmony_ci // Remainder 4144514f5e3Sopenharmony_ci JSHandle<BigInt> remRes1 = BigInt::Remainder(thread, resBigint4, resBigint1); 4154514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(remRes1.GetTaggedValue(), expBigint2.GetTaggedValue())); 4164514f5e3Sopenharmony_ci JSHandle<BigInt> remRes2 = BigInt::Remainder(thread, resBigint4, resBigint2); 4174514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(remRes2.GetTaggedValue(), expBigint2.GetTaggedValue())); 4184514f5e3Sopenharmony_ci JSHandle<BigInt> remRes3 = BigInt::Remainder(thread, resBigint4, resBigint3); 4194514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(remRes3.GetTaggedValue(), expBigint2.GetTaggedValue())); 4204514f5e3Sopenharmony_ci JSHandle<BigInt> remRes4 = BigInt::Remainder(thread, baseBigint, zero); 4214514f5e3Sopenharmony_ci EXPECT_TRUE(remRes4.GetTaggedValue().IsException()); 4224514f5e3Sopenharmony_ci thread->ClearException(); 4234514f5e3Sopenharmony_ci JSHandle<BigInt> remRes5 = BigInt::Remainder(thread, expBigint2, baseBigint); 4244514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(remRes5.GetTaggedValue(), zero.GetTaggedValue())); 4254514f5e3Sopenharmony_ci JSHandle<BigInt> remRes6 = BigInt::Remainder(thread, expBigint1, baseBigint); 4264514f5e3Sopenharmony_ci JSHandle<BigInt> expect = BigInt::Int32ToBigInt(thread, 1); 4274514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(remRes6.GetTaggedValue(), expect.GetTaggedValue())); 4284514f5e3Sopenharmony_ci} 4294514f5e3Sopenharmony_ci 4304514f5e3Sopenharmony_ci/** 4314514f5e3Sopenharmony_ci * @tc.name: ToInt64 4324514f5e3Sopenharmony_ci * @tc.desc: 4334514f5e3Sopenharmony_ci * @tc.type: FUNC 4344514f5e3Sopenharmony_ci * @tc.require: 4354514f5e3Sopenharmony_ci */ 4364514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, ToInt64) 4374514f5e3Sopenharmony_ci{ 4384514f5e3Sopenharmony_ci CString resBigintStr1 = std::to_string(LLONG_MAX).c_str(); 4394514f5e3Sopenharmony_ci CString resBigintStr2 = std::to_string(LLONG_MIN).c_str(); 4404514f5e3Sopenharmony_ci CString resBigintStr3 = std::to_string(INT_MAX).c_str(); 4414514f5e3Sopenharmony_ci CString resBigintStr4 = std::to_string(INT_MIN).c_str(); 4424514f5e3Sopenharmony_ci CString resBigintStr5 = "0"; 4434514f5e3Sopenharmony_ci 4444514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigIntHelper::SetBigInt(thread, resBigintStr1); 4454514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigIntHelper::SetBigInt(thread, resBigintStr2); 4464514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigIntHelper::SetBigInt(thread, resBigintStr3); 4474514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint4 = BigIntHelper::SetBigInt(thread, resBigintStr4); 4484514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint5 = BigIntHelper::SetBigInt(thread, resBigintStr5); 4494514f5e3Sopenharmony_ci 4504514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint1->ToInt64() == LLONG_MAX); 4514514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint2->ToInt64() == LLONG_MIN); 4524514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint3->ToInt64() == INT_MAX); 4534514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint4->ToInt64() == INT_MIN); 4544514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint5->ToInt64() == 0); 4554514f5e3Sopenharmony_ci} 4564514f5e3Sopenharmony_ci 4574514f5e3Sopenharmony_ci/** 4584514f5e3Sopenharmony_ci * @tc.name: ToUint64 4594514f5e3Sopenharmony_ci * @tc.desc: 4604514f5e3Sopenharmony_ci * @tc.type: FUNC 4614514f5e3Sopenharmony_ci * @tc.require: 4624514f5e3Sopenharmony_ci */ 4634514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, ToUint64) 4644514f5e3Sopenharmony_ci{ 4654514f5e3Sopenharmony_ci CString resBigintStr1 = std::to_string(ULLONG_MAX).c_str(); 4664514f5e3Sopenharmony_ci CString resBigintStr2 = std::to_string(UINT_MAX).c_str(); 4674514f5e3Sopenharmony_ci CString resBigintStr3 = "0"; 4684514f5e3Sopenharmony_ci 4694514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigIntHelper::SetBigInt(thread, resBigintStr1); 4704514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigIntHelper::SetBigInt(thread, resBigintStr2); 4714514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigIntHelper::SetBigInt(thread, resBigintStr3); 4724514f5e3Sopenharmony_ci 4734514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint1->ToUint64() == ULLONG_MAX); 4744514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint2->ToUint64() == UINT_MAX); 4754514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint3->ToUint64() == 0); 4764514f5e3Sopenharmony_ci} 4774514f5e3Sopenharmony_ci 4784514f5e3Sopenharmony_ci/** 4794514f5e3Sopenharmony_ci * @tc.name: Int64ToBigInt 4804514f5e3Sopenharmony_ci * @tc.desc: 4814514f5e3Sopenharmony_ci * @tc.type: FUNC 4824514f5e3Sopenharmony_ci * @tc.require: 4834514f5e3Sopenharmony_ci */ 4844514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Int64ToBigInt) 4854514f5e3Sopenharmony_ci{ 4864514f5e3Sopenharmony_ci // JSHandle<BigInt> BigInt::Int64ToBigInt(JSThread *thread, const int64_t &number) 4874514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::Int64ToBigInt(thread, LLONG_MAX); 4884514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigInt::Int64ToBigInt(thread, LLONG_MIN); 4894514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigInt::Int64ToBigInt(thread, INT_MAX); 4904514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint4 = BigInt::Int64ToBigInt(thread, INT_MIN); 4914514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint5 = BigInt::Int64ToBigInt(thread, 0); 4924514f5e3Sopenharmony_ci 4934514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint1->ToInt64() == LLONG_MAX); 4944514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint2->ToInt64() == LLONG_MIN); 4954514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint3->ToInt64() == INT_MAX); 4964514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint4->ToInt64() == INT_MIN); 4974514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint5->ToInt64() == 0); 4984514f5e3Sopenharmony_ci} 4994514f5e3Sopenharmony_ci 5004514f5e3Sopenharmony_ci/** 5014514f5e3Sopenharmony_ci * @tc.name: Uint64ToBigInt 5024514f5e3Sopenharmony_ci * @tc.desc: 5034514f5e3Sopenharmony_ci * @tc.type: FUNC 5044514f5e3Sopenharmony_ci * @tc.require: 5054514f5e3Sopenharmony_ci */ 5064514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Uint64ToBigInt) 5074514f5e3Sopenharmony_ci{ 5084514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::Uint64ToBigInt(thread, ULLONG_MAX); 5094514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigInt::Uint64ToBigInt(thread, UINT_MAX); 5104514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigInt::Uint64ToBigInt(thread, 0); 5114514f5e3Sopenharmony_ci 5124514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint1->ToUint64() == ULLONG_MAX); 5134514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint2->ToUint64() == UINT_MAX); 5144514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint3->ToUint64() == 0); 5154514f5e3Sopenharmony_ci} 5164514f5e3Sopenharmony_ci 5174514f5e3Sopenharmony_civoid GetWordsArray(bool *signBit, size_t wordCount, uint64_t *words, JSHandle<BigInt> bigintVal) 5184514f5e3Sopenharmony_ci{ 5194514f5e3Sopenharmony_ci uint32_t len = bigintVal->GetLength(); 5204514f5e3Sopenharmony_ci uint32_t count = 0; 5214514f5e3Sopenharmony_ci uint32_t index = 0; 5224514f5e3Sopenharmony_ci for (; index < wordCount - 1; ++index) { 5234514f5e3Sopenharmony_ci words[index] = static_cast<uint64_t>(bigintVal->GetDigit(count++)); 5244514f5e3Sopenharmony_ci words[index] |= static_cast<uint64_t>(bigintVal->GetDigit(count++)) << 32; // 32 : int32_t bits 5254514f5e3Sopenharmony_ci } 5264514f5e3Sopenharmony_ci if (len % 2 == 0) { // 2 : len is odd or even 5274514f5e3Sopenharmony_ci words[index] = static_cast<uint64_t>(bigintVal->GetDigit(count++)); 5284514f5e3Sopenharmony_ci words[index] |= static_cast<uint64_t>(bigintVal->GetDigit(count++)) << 32; // 32 : int32_t bits 5294514f5e3Sopenharmony_ci } else { 5304514f5e3Sopenharmony_ci words[index] = static_cast<uint64_t>(bigintVal->GetDigit(count++)); 5314514f5e3Sopenharmony_ci } 5324514f5e3Sopenharmony_ci *signBit = bigintVal->GetSign(); 5334514f5e3Sopenharmony_ci} 5344514f5e3Sopenharmony_ci 5354514f5e3Sopenharmony_ci/** 5364514f5e3Sopenharmony_ci * @tc.name: CreateBigWords 5374514f5e3Sopenharmony_ci * @tc.desc: 5384514f5e3Sopenharmony_ci * @tc.type: FUNC 5394514f5e3Sopenharmony_ci * @tc.require: 5404514f5e3Sopenharmony_ci */ 5414514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, CreateBigWords) 5424514f5e3Sopenharmony_ci{ 5434514f5e3Sopenharmony_ci size_t wordCount = 4; 5444514f5e3Sopenharmony_ci uint64_t words[] = { 0xFFFFFFFFFFFFFFFF, 34ULL, 56ULL, 0xFFFFFFFFFFFFFFFF }; 5454514f5e3Sopenharmony_ci JSHandle<BigInt> bigintFalse = BigInt::CreateBigWords(thread, false, wordCount, words); 5464514f5e3Sopenharmony_ci bool sign = true; 5474514f5e3Sopenharmony_ci uint64_t wordsOut[] = { 0ULL, 0ULL, 0ULL, 0ULL }; 5484514f5e3Sopenharmony_ci GetWordsArray(&sign, wordCount, wordsOut, bigintFalse); 5494514f5e3Sopenharmony_ci EXPECT_TRUE(sign == false); 5504514f5e3Sopenharmony_ci for (size_t i = 0; i < wordCount; i++) { 5514514f5e3Sopenharmony_ci EXPECT_TRUE(words[i] == wordsOut[i]); 5524514f5e3Sopenharmony_ci } 5534514f5e3Sopenharmony_ci 5544514f5e3Sopenharmony_ci JSHandle<BigInt> bigintTrue = BigInt::CreateBigWords(thread, true, wordCount, words); 5554514f5e3Sopenharmony_ci GetWordsArray(&sign, wordCount, wordsOut, bigintTrue); 5564514f5e3Sopenharmony_ci EXPECT_TRUE(sign == true); 5574514f5e3Sopenharmony_ci for (size_t i = 0; i < wordCount; i++) { 5584514f5e3Sopenharmony_ci EXPECT_TRUE(words[i] == wordsOut[i]); 5594514f5e3Sopenharmony_ci } 5604514f5e3Sopenharmony_ci 5614514f5e3Sopenharmony_ci size_t wordCount1 = 5; 5624514f5e3Sopenharmony_ci uint64_t words1[] = { 12ULL, 34ULL, 56ULL, 78ULL, 90ULL }; 5634514f5e3Sopenharmony_ci JSHandle<BigInt> bigintFalse1 = BigInt::CreateBigWords(thread, false, wordCount1, words1); 5644514f5e3Sopenharmony_ci 5654514f5e3Sopenharmony_ci bool sign1 = true; 5664514f5e3Sopenharmony_ci uint64_t wordsOut1[] = { 0ULL, 0ULL, 0ULL, 0ULL, 0ULL }; 5674514f5e3Sopenharmony_ci GetWordsArray(&sign1, wordCount1, wordsOut1, bigintFalse1); 5684514f5e3Sopenharmony_ci EXPECT_TRUE(sign1 == false); 5694514f5e3Sopenharmony_ci for (size_t i = 0; i < wordCount1; i++) { 5704514f5e3Sopenharmony_ci EXPECT_TRUE(words1[i] == wordsOut1[i]); 5714514f5e3Sopenharmony_ci } 5724514f5e3Sopenharmony_ci 5734514f5e3Sopenharmony_ci size_t wordCount2 = 0; 5744514f5e3Sopenharmony_ci uint64_t words2[10] = { 0 }; 5754514f5e3Sopenharmony_ci JSHandle<BigInt> bigint1 = BigInt::CreateBigWords(thread, true, wordCount2, words2); 5764514f5e3Sopenharmony_ci EXPECT_TRUE(bigint1->IsZero()); 5774514f5e3Sopenharmony_ci 5784514f5e3Sopenharmony_ci BigInt::CreateBigWords(thread, true, INT_MAX, words2); 5794514f5e3Sopenharmony_ci EXPECT_TRUE(thread->HasPendingException()); 5804514f5e3Sopenharmony_ci thread->ClearException(); 5814514f5e3Sopenharmony_ci} 5824514f5e3Sopenharmony_ci 5834514f5e3Sopenharmony_ci/** 5844514f5e3Sopenharmony_ci * @tc.name: GetUint64MaxBigInt GetInt64MaxBigInt 5854514f5e3Sopenharmony_ci * @tc.desc: 5864514f5e3Sopenharmony_ci * @tc.type: FUNC 5874514f5e3Sopenharmony_ci * @tc.require: 5884514f5e3Sopenharmony_ci */ 5894514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, GetUint64MaxBigint_GetInt64MaxBigint) 5904514f5e3Sopenharmony_ci{ 5914514f5e3Sopenharmony_ci JSHandle<BigInt> exponent = BigInt::Int32ToBigInt(thread, 64); // 64 : bits 5924514f5e3Sopenharmony_ci JSHandle<BigInt> exponentone = BigInt::Int32ToBigInt(thread, 63); // 63 : bits 5934514f5e3Sopenharmony_ci JSHandle<BigInt> base = BigInt::Int32ToBigInt(thread, 2); // 2 : base value 5944514f5e3Sopenharmony_ci JSHandle<BigInt> uint64MaxBigint1 = BigInt::Exponentiate(thread, base, exponent); 5954514f5e3Sopenharmony_ci JSHandle<BigInt> uint64MaxBigint2 = BigInt::GetUint64MaxBigInt(thread); 5964514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(uint64MaxBigint1.GetTaggedValue(), uint64MaxBigint2.GetTaggedValue())); 5974514f5e3Sopenharmony_ci JSHandle<BigInt> int64MaxBigint1 = BigInt::Exponentiate(thread, base, exponentone); 5984514f5e3Sopenharmony_ci JSHandle<BigInt> int64MaxBigint2 = BigInt::GetInt64MaxBigInt(thread); 5994514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(int64MaxBigint1.GetTaggedValue(), int64MaxBigint2.GetTaggedValue())); 6004514f5e3Sopenharmony_ci} 6014514f5e3Sopenharmony_ci 6024514f5e3Sopenharmony_ci/** 6034514f5e3Sopenharmony_ci * @tc.name: Int32ToBigInt 6044514f5e3Sopenharmony_ci * @tc.desc: 6054514f5e3Sopenharmony_ci * @tc.type: FUNC 6064514f5e3Sopenharmony_ci * @tc.require: 6074514f5e3Sopenharmony_ci */ 6084514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Int32ToBigInt) 6094514f5e3Sopenharmony_ci{ 6104514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::Int32ToBigInt(thread, std::numeric_limits<int32_t>::max()); 6114514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigInt::Int32ToBigInt(thread, std::numeric_limits<int32_t>::min()); 6124514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigInt::Int32ToBigInt(thread, 0); 6134514f5e3Sopenharmony_ci 6144514f5e3Sopenharmony_ci EXPECT_TRUE(static_cast<int32_t>(resBigint1->GetDigit(0)) == std::numeric_limits<int32_t>::max()); 6154514f5e3Sopenharmony_ci EXPECT_FALSE(resBigint1->GetSign()); 6164514f5e3Sopenharmony_ci EXPECT_TRUE(static_cast<int32_t>(resBigint2->GetDigit(0)) == std::numeric_limits<int32_t>::min()); 6174514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint2->GetSign()); 6184514f5e3Sopenharmony_ci EXPECT_TRUE(static_cast<int32_t>(resBigint3->GetDigit(0)) == 0); 6194514f5e3Sopenharmony_ci EXPECT_FALSE(resBigint3->GetSign()); 6204514f5e3Sopenharmony_ci} 6214514f5e3Sopenharmony_ci 6224514f5e3Sopenharmony_ci/** 6234514f5e3Sopenharmony_ci * @tc.name: Uint32ToBigInt 6244514f5e3Sopenharmony_ci * @tc.desc: 6254514f5e3Sopenharmony_ci * @tc.type: FUNC 6264514f5e3Sopenharmony_ci * @tc.require: 6274514f5e3Sopenharmony_ci */ 6284514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Uint32ToBigInt) 6294514f5e3Sopenharmony_ci{ 6304514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::Uint32ToBigInt(thread, std::numeric_limits<uint32_t>::max()); 6314514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigInt::Uint32ToBigInt(thread, std::numeric_limits<uint32_t>::min()); 6324514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigInt::Uint32ToBigInt(thread, 0); 6334514f5e3Sopenharmony_ci 6344514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint1->GetDigit(0) == std::numeric_limits<uint32_t>::max()); 6354514f5e3Sopenharmony_ci EXPECT_FALSE(resBigint1->GetSign()); 6364514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint2->GetDigit(0) == std::numeric_limits<uint32_t>::min()); 6374514f5e3Sopenharmony_ci EXPECT_FALSE(resBigint2->GetSign()); 6384514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint3->GetDigit(0) == 0); 6394514f5e3Sopenharmony_ci EXPECT_FALSE(resBigint3->GetSign()); 6404514f5e3Sopenharmony_ci} 6414514f5e3Sopenharmony_ci 6424514f5e3Sopenharmony_ci/** 6434514f5e3Sopenharmony_ci * @tc.name: BigIntToInt64 6444514f5e3Sopenharmony_ci * @tc.desc: 6454514f5e3Sopenharmony_ci * @tc.type: FUNC 6464514f5e3Sopenharmony_ci * @tc.require: 6474514f5e3Sopenharmony_ci */ 6484514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, BigIntToInt64) 6494514f5e3Sopenharmony_ci{ 6504514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::Int64ToBigInt(thread, LLONG_MAX); 6514514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigInt::Int64ToBigInt(thread, LLONG_MIN); 6524514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigInt::Int64ToBigInt(thread, INT_MAX); 6534514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint4 = BigInt::Int64ToBigInt(thread, INT_MIN); 6544514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint5 = BigInt::Int64ToBigInt(thread, 0); 6554514f5e3Sopenharmony_ci int64_t cValue = 0; 6564514f5e3Sopenharmony_ci bool lossless = false; 6574514f5e3Sopenharmony_ci BigInt::BigIntToInt64(thread, JSHandle<JSTaggedValue>(resBigint1), &cValue, &lossless); 6584514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == LLONG_MAX); 6594514f5e3Sopenharmony_ci EXPECT_TRUE(lossless); 6604514f5e3Sopenharmony_ci BigInt::BigIntToInt64(thread, JSHandle<JSTaggedValue>(resBigint2), &cValue, &lossless); 6614514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == LLONG_MIN); 6624514f5e3Sopenharmony_ci EXPECT_TRUE(lossless); 6634514f5e3Sopenharmony_ci BigInt::BigIntToInt64(thread, JSHandle<JSTaggedValue>(resBigint3), &cValue, &lossless); 6644514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == INT_MAX); 6654514f5e3Sopenharmony_ci EXPECT_TRUE(lossless); 6664514f5e3Sopenharmony_ci BigInt::BigIntToInt64(thread, JSHandle<JSTaggedValue>(resBigint4), &cValue, &lossless); 6674514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == INT_MIN); 6684514f5e3Sopenharmony_ci EXPECT_TRUE(lossless); 6694514f5e3Sopenharmony_ci BigInt::BigIntToInt64(thread, JSHandle<JSTaggedValue>(resBigint5), &cValue, &lossless); 6704514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == 0); 6714514f5e3Sopenharmony_ci EXPECT_TRUE(lossless); 6724514f5e3Sopenharmony_ci 6734514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint6 = BigInt::CreateBigint(thread, 3); // 3 : bigint length 6744514f5e3Sopenharmony_ci resBigint6->SetDigit(0, 0); 6754514f5e3Sopenharmony_ci resBigint6->SetDigit(1, 0); 6764514f5e3Sopenharmony_ci resBigint6->SetDigit(2, 1); // 2 : index 6774514f5e3Sopenharmony_ci lossless = false; 6784514f5e3Sopenharmony_ci BigInt::BigIntToInt64(thread, JSHandle<JSTaggedValue>(resBigint6), &cValue, &lossless); 6794514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == 0); 6804514f5e3Sopenharmony_ci EXPECT_TRUE(!lossless); 6814514f5e3Sopenharmony_ci 6824514f5e3Sopenharmony_ci resBigint6->SetSign(true); 6834514f5e3Sopenharmony_ci resBigint6->SetDigit(2, 2); // 2 : index 6844514f5e3Sopenharmony_ci BigInt::BigIntToInt64(thread, JSHandle<JSTaggedValue>(resBigint6), &cValue, &lossless); 6854514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == 0); 6864514f5e3Sopenharmony_ci EXPECT_TRUE(!lossless); 6874514f5e3Sopenharmony_ci} 6884514f5e3Sopenharmony_ci 6894514f5e3Sopenharmony_ci/** 6904514f5e3Sopenharmony_ci * @tc.name: BigIntToUint64 6914514f5e3Sopenharmony_ci * @tc.desc: 6924514f5e3Sopenharmony_ci * @tc.type: FUNC 6934514f5e3Sopenharmony_ci * @tc.require: 6944514f5e3Sopenharmony_ci */ 6954514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, BigIntToUint64) 6964514f5e3Sopenharmony_ci{ 6974514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::Uint64ToBigInt(thread, ULLONG_MAX); 6984514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigInt::Uint64ToBigInt(thread, UINT_MAX); 6994514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint3 = BigInt::Uint64ToBigInt(thread, 0); 7004514f5e3Sopenharmony_ci 7014514f5e3Sopenharmony_ci uint64_t cValue = 0; 7024514f5e3Sopenharmony_ci bool lossless = false; 7034514f5e3Sopenharmony_ci BigInt::BigIntToUint64(thread, JSHandle<JSTaggedValue>(resBigint1), &cValue, &lossless); 7044514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == ULLONG_MAX); 7054514f5e3Sopenharmony_ci EXPECT_TRUE(lossless); 7064514f5e3Sopenharmony_ci BigInt::BigIntToUint64(thread, JSHandle<JSTaggedValue>(resBigint2), &cValue, &lossless); 7074514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == UINT_MAX); 7084514f5e3Sopenharmony_ci EXPECT_TRUE(lossless); 7094514f5e3Sopenharmony_ci BigInt::BigIntToUint64(thread, JSHandle<JSTaggedValue>(resBigint3), &cValue, &lossless); 7104514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == 0); 7114514f5e3Sopenharmony_ci EXPECT_TRUE(lossless); 7124514f5e3Sopenharmony_ci 7134514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint4 = BigInt::CreateBigint(thread, 3); // 3 : bigint length 7144514f5e3Sopenharmony_ci resBigint4->SetDigit(0, 0); 7154514f5e3Sopenharmony_ci resBigint4->SetDigit(1, 0); 7164514f5e3Sopenharmony_ci resBigint4->SetDigit(2, 1); // 2 : index 7174514f5e3Sopenharmony_ci lossless = false; 7184514f5e3Sopenharmony_ci BigInt::BigIntToUint64(thread, JSHandle<JSTaggedValue>(resBigint4), &cValue, &lossless); 7194514f5e3Sopenharmony_ci EXPECT_TRUE(cValue == 0); 7204514f5e3Sopenharmony_ci EXPECT_TRUE(!lossless); 7214514f5e3Sopenharmony_ci} 7224514f5e3Sopenharmony_ci 7234514f5e3Sopenharmony_ci/** 7244514f5e3Sopenharmony_ci * @tc.name: Add 7254514f5e3Sopenharmony_ci * @tc.desc: 7264514f5e3Sopenharmony_ci * @tc.type: FUNC 7274514f5e3Sopenharmony_ci * @tc.require: 7284514f5e3Sopenharmony_ci */ 7294514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Add) 7304514f5e3Sopenharmony_ci{ 7314514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint = BigInt::CreateBigint(thread, 2); // 2 : bigint length 7324514f5e3Sopenharmony_ci resBigint->SetDigit(0, 0); 7334514f5e3Sopenharmony_ci resBigint->SetDigit(1, 1); 7344514f5e3Sopenharmony_ci resBigint->SetSign(true); 7354514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::CreateBigint(thread, 2); // 2 : bigint length 7364514f5e3Sopenharmony_ci resBigint1->SetDigit(0, 1); 7374514f5e3Sopenharmony_ci resBigint1->SetDigit(1, 1); 7384514f5e3Sopenharmony_ci 7394514f5e3Sopenharmony_ci JSHandle<BigInt> addres = BigInt::Add(thread, resBigint, resBigint1); 7404514f5e3Sopenharmony_ci EXPECT_TRUE(addres->GetLength() == 1); 7414514f5e3Sopenharmony_ci EXPECT_TRUE(addres->GetDigit(0) == 1); 7424514f5e3Sopenharmony_ci JSHandle<BigInt> addres1 = BigInt::Add(thread, resBigint1, resBigint); 7434514f5e3Sopenharmony_ci EXPECT_TRUE(addres1->GetLength() == 1); 7444514f5e3Sopenharmony_ci EXPECT_TRUE(addres1->GetDigit(0) == 1); 7454514f5e3Sopenharmony_ci 7464514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigInt::Int32ToBigInt(thread, 1); 7474514f5e3Sopenharmony_ci JSHandle<BigInt> addres2 = BigInt::Add(thread, resBigint2, resBigint); 7484514f5e3Sopenharmony_ci EXPECT_TRUE(addres2->GetLength() == 1); 7494514f5e3Sopenharmony_ci EXPECT_TRUE(addres2->GetSign()); 7504514f5e3Sopenharmony_ci EXPECT_TRUE(addres2->GetDigit(0) == std::numeric_limits<uint32_t>::max()); 7514514f5e3Sopenharmony_ci 7524514f5e3Sopenharmony_ci JSHandle<BigInt> addres3 = BigInt::Add(thread, resBigint2, resBigint1); 7534514f5e3Sopenharmony_ci EXPECT_TRUE(addres3->GetLength() == 2); // 2 : bigint length 7544514f5e3Sopenharmony_ci EXPECT_TRUE(!addres3->GetSign()); 7554514f5e3Sopenharmony_ci EXPECT_TRUE(addres3->GetDigit(0) == 2); // 2 : digit value 7564514f5e3Sopenharmony_ci} 7574514f5e3Sopenharmony_ci 7584514f5e3Sopenharmony_ci/** 7594514f5e3Sopenharmony_ci * @tc.name: Subtract 7604514f5e3Sopenharmony_ci * @tc.desc: 7614514f5e3Sopenharmony_ci * @tc.type: FUNC 7624514f5e3Sopenharmony_ci * @tc.require: 7634514f5e3Sopenharmony_ci */ 7644514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, Subtract) 7654514f5e3Sopenharmony_ci{ 7664514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint = BigInt::CreateBigint(thread, 2); // 2 : bigint length 7674514f5e3Sopenharmony_ci resBigint->SetDigit(0, 0); 7684514f5e3Sopenharmony_ci resBigint->SetDigit(1, 1); 7694514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::CreateBigint(thread, 2); // 2 : bigint length 7704514f5e3Sopenharmony_ci resBigint1->SetDigit(0, 1); 7714514f5e3Sopenharmony_ci resBigint1->SetDigit(1, 1); 7724514f5e3Sopenharmony_ci 7734514f5e3Sopenharmony_ci JSHandle<BigInt> addres = BigInt::Subtract(thread, resBigint1, resBigint); 7744514f5e3Sopenharmony_ci EXPECT_TRUE(addres->GetLength() == 1); 7754514f5e3Sopenharmony_ci EXPECT_TRUE(addres->GetDigit(0) == 1); 7764514f5e3Sopenharmony_ci JSHandle<BigInt> addres1 = BigInt::Subtract(thread, resBigint1, resBigint); 7774514f5e3Sopenharmony_ci EXPECT_TRUE(addres1->GetLength() == 1); 7784514f5e3Sopenharmony_ci EXPECT_TRUE(addres1->GetDigit(0) == 1); 7794514f5e3Sopenharmony_ci 7804514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint2 = BigInt::Int32ToBigInt(thread, -1); 7814514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint2->GetSign()); 7824514f5e3Sopenharmony_ci JSHandle<BigInt> addres2 = BigInt::Subtract(thread, resBigint, resBigint2); 7834514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(addres2.GetTaggedValue(), resBigint1.GetTaggedValue())); 7844514f5e3Sopenharmony_ci} 7854514f5e3Sopenharmony_ci 7864514f5e3Sopenharmony_ci/** 7874514f5e3Sopenharmony_ci * @tc.name: BigintSubOne 7884514f5e3Sopenharmony_ci * @tc.desc: 7894514f5e3Sopenharmony_ci * @tc.type: FUNC 7904514f5e3Sopenharmony_ci * @tc.require: 7914514f5e3Sopenharmony_ci */ 7924514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, BigintSubOne) 7934514f5e3Sopenharmony_ci{ 7944514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint = BigInt::Int32ToBigInt(thread, -1); 7954514f5e3Sopenharmony_ci EXPECT_TRUE(resBigint->GetSign()); 7964514f5e3Sopenharmony_ci JSHandle<BigInt> addres = BigInt::BigintSubOne(thread, resBigint); 7974514f5e3Sopenharmony_ci EXPECT_TRUE(addres->GetSign()); 7984514f5e3Sopenharmony_ci EXPECT_TRUE(addres->GetDigit(0) == 2); 7994514f5e3Sopenharmony_ci 8004514f5e3Sopenharmony_ci JSHandle<BigInt> resBigint1 = BigInt::Int32ToBigInt(thread, 1); 8014514f5e3Sopenharmony_ci JSHandle<BigInt> addres1 = BigInt::BigintSubOne(thread, resBigint1); 8024514f5e3Sopenharmony_ci EXPECT_TRUE(addres1->GetDigit(0) == 0); 8034514f5e3Sopenharmony_ci} 8044514f5e3Sopenharmony_ci 8054514f5e3Sopenharmony_ci/** 8064514f5e3Sopenharmony_ci * @tc.name: SignedRightShift 8074514f5e3Sopenharmony_ci * @tc.desc: 8084514f5e3Sopenharmony_ci * @tc.type: FUNC 8094514f5e3Sopenharmony_ci * @tc.require: 8104514f5e3Sopenharmony_ci */ 8114514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, SignedRightShift) 8124514f5e3Sopenharmony_ci{ 8134514f5e3Sopenharmony_ci // the left operand is a positive number 8144514f5e3Sopenharmony_ci CString bigintStr1 = "111111011011110111111101111111101111111111110111111111110111111011"; 8154514f5e3Sopenharmony_ci JSHandle<BigInt> bigint1 = BigIntHelper::SetBigInt(thread, bigintStr1, BigInt::BINARY); 8164514f5e3Sopenharmony_ci JSHandle<BigInt> shift1 = BigInt::Int32ToBigInt(thread, 20); // 20 : shiftBits 8174514f5e3Sopenharmony_ci JSHandle<BigInt> res1 = BigInt::SignedRightShift(thread, bigint1, shift1); 8184514f5e3Sopenharmony_ci 8194514f5e3Sopenharmony_ci CString expectResStr1 = "1111110110111101111111011111111011111111111101"; 8204514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes1 = BigIntHelper::SetBigInt(thread, expectResStr1, BigInt::BINARY); 8214514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res1.GetTaggedValue(), expectRes1.GetTaggedValue())); 8224514f5e3Sopenharmony_ci 8234514f5e3Sopenharmony_ci JSHandle<BigInt> shift2 = BigInt::Int32ToBigInt(thread, 0); 8244514f5e3Sopenharmony_ci JSHandle<BigInt> res2 = BigInt::SignedRightShift(thread, bigint1, shift2); 8254514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res2.GetTaggedValue(), bigint1.GetTaggedValue())); 8264514f5e3Sopenharmony_ci 8274514f5e3Sopenharmony_ci JSHandle<BigInt> res3 = BigInt::SignedRightShift(thread, shift2, bigint1); 8284514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res3.GetTaggedValue(), shift2.GetTaggedValue())); 8294514f5e3Sopenharmony_ci 8304514f5e3Sopenharmony_ci JSHandle<BigInt> shift3 = BigInt::Int32ToBigInt(thread, -33); // -33 : shiftBits 8314514f5e3Sopenharmony_ci JSHandle<BigInt> res4 = BigInt::SignedRightShift(thread, bigint1, shift3); 8324514f5e3Sopenharmony_ci CString expectResStr4 = 8334514f5e3Sopenharmony_ci "111111011011110111111101111111101111111111110111111111110111111011000000000000000000000000000000000"; 8344514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes4 = BigIntHelper::SetBigInt(thread, expectResStr4, BigInt::BINARY); 8354514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res4.GetTaggedValue(), expectRes4.GetTaggedValue())); 8364514f5e3Sopenharmony_ci 8374514f5e3Sopenharmony_ci // left operand is negative number 8384514f5e3Sopenharmony_ci JSHandle<BigInt> bigint2 = BigInt::UnaryMinus(thread, bigint1); 8394514f5e3Sopenharmony_ci 8404514f5e3Sopenharmony_ci CString expectResStr5 = 8414514f5e3Sopenharmony_ci "-1111110110111101111111011111111011111111111110"; 8424514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes5 = BigIntHelper::SetBigInt(thread, expectResStr5, BigInt::BINARY); 8434514f5e3Sopenharmony_ci JSHandle<BigInt> res5 = BigInt::SignedRightShift(thread, bigint2, shift1); 8444514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res5.GetTaggedValue(), expectRes5.GetTaggedValue())); 8454514f5e3Sopenharmony_ci 8464514f5e3Sopenharmony_ci CString expectResStr6 = 8474514f5e3Sopenharmony_ci "-111111011011110111111101111111101111111111110111111111110111111011000000000000000000000000000000000"; 8484514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes6 = BigIntHelper::SetBigInt(thread, expectResStr6, BigInt::BINARY); 8494514f5e3Sopenharmony_ci JSHandle<BigInt> res6 = BigInt::SignedRightShift(thread, bigint2, shift3); 8504514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res6.GetTaggedValue(), expectRes6.GetTaggedValue())); 8514514f5e3Sopenharmony_ci 8524514f5e3Sopenharmony_ci JSHandle<BigInt> res7 = BigInt::SignedRightShift(thread, shift3, bigint1); 8534514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes7 = BigInt::Int32ToBigInt(thread, -1); 8544514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res7.GetTaggedValue(), expectRes7.GetTaggedValue())); 8554514f5e3Sopenharmony_ci 8564514f5e3Sopenharmony_ci JSHandle<BigInt> shift4 = BigInt::Int32ToBigInt(thread, 65); // 65 : shiftBits 8574514f5e3Sopenharmony_ci JSHandle<BigInt> res8 = BigInt::SignedRightShift(thread, shift3, shift4); 8584514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res8.GetTaggedValue(), expectRes7.GetTaggedValue())); 8594514f5e3Sopenharmony_ci} 8604514f5e3Sopenharmony_ci 8614514f5e3Sopenharmony_ci/** 8624514f5e3Sopenharmony_ci * @tc.name: LeftShift 8634514f5e3Sopenharmony_ci * @tc.desc: 8644514f5e3Sopenharmony_ci * @tc.type: FUNC 8654514f5e3Sopenharmony_ci * @tc.require: 8664514f5e3Sopenharmony_ci */ 8674514f5e3Sopenharmony_ciHWTEST_F_L0(JSBigintTest, LeftShift) 8684514f5e3Sopenharmony_ci{ 8694514f5e3Sopenharmony_ci // the left operand is a positive number 8704514f5e3Sopenharmony_ci CString bigintStr1 = "111111011011110111111101111111101111111111110111111111110111111011"; 8714514f5e3Sopenharmony_ci JSHandle<BigInt> bigint1 = BigIntHelper::SetBigInt(thread, bigintStr1, BigInt::BINARY); 8724514f5e3Sopenharmony_ci JSHandle<BigInt> shift1 = BigInt::Int32ToBigInt(thread, 20); // 20 : shiftBits 8734514f5e3Sopenharmony_ci JSHandle<BigInt> res1 = BigInt::LeftShift(thread, bigint1, shift1); 8744514f5e3Sopenharmony_ci 8754514f5e3Sopenharmony_ci CString expectResStr1 = 8764514f5e3Sopenharmony_ci "11111101101111011111110111111110111111111111011111111111011111101100000000000000000000"; 8774514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes1 = BigIntHelper::SetBigInt(thread, expectResStr1, BigInt::BINARY); 8784514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res1.GetTaggedValue(), expectRes1.GetTaggedValue())); 8794514f5e3Sopenharmony_ci 8804514f5e3Sopenharmony_ci JSHandle<BigInt> shift2 = BigInt::Int32ToBigInt(thread, 0); 8814514f5e3Sopenharmony_ci JSHandle<BigInt> res2 = BigInt::LeftShift(thread, bigint1, shift2); 8824514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res2.GetTaggedValue(), bigint1.GetTaggedValue())); 8834514f5e3Sopenharmony_ci 8844514f5e3Sopenharmony_ci JSHandle<BigInt> shift3 = BigInt::Int32ToBigInt(thread, -33); // -33 : shiftBits 8854514f5e3Sopenharmony_ci JSHandle<BigInt> res4 = BigInt::LeftShift(thread, bigint1, shift3); 8864514f5e3Sopenharmony_ci CString expectResStr4 = "111111011011110111111101111111101"; 8874514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes4 = BigIntHelper::SetBigInt(thread, expectResStr4, BigInt::BINARY); 8884514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res4.GetTaggedValue(), expectRes4.GetTaggedValue())); 8894514f5e3Sopenharmony_ci 8904514f5e3Sopenharmony_ci // left operand is negative number 8914514f5e3Sopenharmony_ci JSHandle<BigInt> bigint2 = BigInt::UnaryMinus(thread, bigint1); 8924514f5e3Sopenharmony_ci 8934514f5e3Sopenharmony_ci CString expectResStr5 = 8944514f5e3Sopenharmony_ci "-11111101101111011111110111111110111111111111011111111111011111101100000000000000000000"; 8954514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes5 = BigIntHelper::SetBigInt(thread, expectResStr5, BigInt::BINARY); 8964514f5e3Sopenharmony_ci JSHandle<BigInt> res5 = BigInt::LeftShift(thread, bigint2, shift1); 8974514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res5.GetTaggedValue(), expectRes5.GetTaggedValue())); 8984514f5e3Sopenharmony_ci 8994514f5e3Sopenharmony_ci CString expectResStr6 = "-111111011011110111111101111111110"; 9004514f5e3Sopenharmony_ci JSHandle<BigInt> expectRes6 = BigIntHelper::SetBigInt(thread, expectResStr6, BigInt::BINARY); 9014514f5e3Sopenharmony_ci JSHandle<BigInt> res6 = BigInt::LeftShift(thread, bigint2, shift3); 9024514f5e3Sopenharmony_ci EXPECT_TRUE(BigInt::Equal(res6.GetTaggedValue(), expectRes6.GetTaggedValue())); 9034514f5e3Sopenharmony_ci} 9044514f5e3Sopenharmony_ci 9054514f5e3Sopenharmony_ci} // namespace panda::test