14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2024 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#include "ecmascript/ecma_vm.h" 164514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 174514f5e3Sopenharmony_ci#include "ecmascript/js_map.h" 184514f5e3Sopenharmony_ci#include "ecmascript/js_array.h" 194514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h" 204514f5e3Sopenharmony_ci#include "ecmascript/js_iterator.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h" 224514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 234514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_async_generator.h" 244514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_shared_map.h" 254514f5e3Sopenharmony_ci#include "ecmascript/linked_hash_table.h" 264514f5e3Sopenharmony_ci#include "ecmascript/shared_objects/js_shared_map.h" 274514f5e3Sopenharmony_ci#include "ecmascript/shared_objects/js_shared_map_iterator.h" 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 304514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 314514f5e3Sopenharmony_ci 324514f5e3Sopenharmony_cinamespace panda::test { 334514f5e3Sopenharmony_ciusing BuiltinsSharedMap = ecmascript::builtins::BuiltinsSharedMap; 344514f5e3Sopenharmony_ci 354514f5e3Sopenharmony_ciclass BuiltinsSharedMapTest : public BaseTestWithScope<false> { 364514f5e3Sopenharmony_cipublic: 374514f5e3Sopenharmony_ci class TestClass : public base::BuiltinsBase { 384514f5e3Sopenharmony_ci public: 394514f5e3Sopenharmony_ci static JSTaggedValue TestFunc(EcmaRuntimeCallInfo *argv) 404514f5e3Sopenharmony_ci { 414514f5e3Sopenharmony_ci int num = GetCallArg(argv, 0)->GetInt(); 424514f5e3Sopenharmony_ci JSArray *jsArray = JSArray::Cast(GetThis(argv)->GetTaggedObject()); 434514f5e3Sopenharmony_ci int length = jsArray->GetArrayLength() + num; 444514f5e3Sopenharmony_ci jsArray->SetArrayLength(argv->GetThread(), length); 454514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 464514f5e3Sopenharmony_ci } 474514f5e3Sopenharmony_ci }; 484514f5e3Sopenharmony_ci}; 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ciJSSharedMap *CreateSBuiltinsMap(JSThread *thread) 514514f5e3Sopenharmony_ci{ 524514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 534514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetSBuiltininMapFunction()); 544514f5e3Sopenharmony_ci // 4 : test case 554514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 4); 564514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue()); 574514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 584514f5e3Sopenharmony_ci 594514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 604514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedMap::Constructor(ecmaRuntimeCallInfo); 614514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 624514f5e3Sopenharmony_ci 634514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsECMAObject()); 644514f5e3Sopenharmony_ci JSSharedMap *jsSMap = JSSharedMap::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData())); 654514f5e3Sopenharmony_ci return jsSMap; 664514f5e3Sopenharmony_ci} 674514f5e3Sopenharmony_ci 684514f5e3Sopenharmony_ci 694514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, CreateAndGetSize) 704514f5e3Sopenharmony_ci{ 714514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 724514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 734514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetSBuiltininMapFunction()); 744514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map(thread, CreateSBuiltinsMap(thread)); 754514f5e3Sopenharmony_ci 764514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 774514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 784514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue()); 794514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined()); 804514f5e3Sopenharmony_ci 814514f5e3Sopenharmony_ci { 824514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 834514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedMap::GetSize(ecmaRuntimeCallInfo); 844514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 854514f5e3Sopenharmony_ci 864514f5e3Sopenharmony_ci EXPECT_EQ(result.GetRawData(), JSTaggedValue(0).GetRawData()); 874514f5e3Sopenharmony_ci } 884514f5e3Sopenharmony_ci JSHandle<TaggedArray> array(factory->NewTaggedArray(5)); 894514f5e3Sopenharmony_ci for (int i = 0; i < 5; i++) { 904514f5e3Sopenharmony_ci JSHandle<TaggedArray> internalArray(factory->NewTaggedArray(2)); 914514f5e3Sopenharmony_ci internalArray->Set(thread, 0, JSTaggedValue(i)); 924514f5e3Sopenharmony_ci internalArray->Set(thread, 1, JSTaggedValue(i)); 934514f5e3Sopenharmony_ci auto arr = JSArray::CreateArrayFromList(thread, internalArray); 944514f5e3Sopenharmony_ci array->Set(thread, i, arr); 954514f5e3Sopenharmony_ci } 964514f5e3Sopenharmony_ci JSHandle<JSArray> values = JSArray::CreateArrayFromList(thread, array); 974514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 984514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(newTarget.GetTaggedValue()); 994514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(map.GetTaggedValue()); 1004514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, values.GetTaggedValue()); 1014514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetNewTarget(newTarget.GetTaggedValue()); 1024514f5e3Sopenharmony_ci { 1034514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 1044514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsSharedMap::Constructor(ecmaRuntimeCallInfo1); 1054514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1064514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map1(thread, JSSharedMap::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()))); 1074514f5e3Sopenharmony_ci EXPECT_EQ(JSSharedMap::GetSize(thread, map1), 5); 1084514f5e3Sopenharmony_ci } 1094514f5e3Sopenharmony_ci} 1104514f5e3Sopenharmony_ci 1114514f5e3Sopenharmony_ci 1124514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, GetKeyTest001) 1134514f5e3Sopenharmony_ci{ 1144514f5e3Sopenharmony_ci constexpr uint32_t NODE_NUMBERS = 8; 1154514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map(thread, CreateSBuiltinsMap(thread)); 1164514f5e3Sopenharmony_ci for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 1174514f5e3Sopenharmony_ci auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 1184514f5e3Sopenharmony_ci callInfo->SetFunction(JSTaggedValue::Undefined()); 1194514f5e3Sopenharmony_ci callInfo->SetThis(map.GetTaggedValue()); 1204514f5e3Sopenharmony_ci callInfo->SetCallArg(0, JSTaggedValue(i)); 1214514f5e3Sopenharmony_ci callInfo->SetCallArg(1, JSTaggedValue(i)); 1224514f5e3Sopenharmony_ci 1234514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo); 1244514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedMap::Set(callInfo); 1254514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1264514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSSharedMap()); 1274514f5e3Sopenharmony_ci JSHandle<JSSharedMap> jsSMap(thread, JSSharedMap::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData()))); 1284514f5e3Sopenharmony_ci EXPECT_EQ(JSSharedMap::GetSize(thread, jsSMap), i + 1); 1294514f5e3Sopenharmony_ci } 1304514f5e3Sopenharmony_ci JSTaggedValue result = JSSharedMap::GetKey(thread, map, 0); 1314514f5e3Sopenharmony_ci EXPECT_NE(result.GetRawData(), JSTaggedValue::Exception().GetRawData()); 1324514f5e3Sopenharmony_ci} 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, GetValueTest001) 1354514f5e3Sopenharmony_ci{ 1364514f5e3Sopenharmony_ci constexpr uint32_t NODE_NUMBERS = 8; 1374514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map(thread, CreateSBuiltinsMap(thread)); 1384514f5e3Sopenharmony_ci for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 1394514f5e3Sopenharmony_ci auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 1404514f5e3Sopenharmony_ci callInfo->SetFunction(JSTaggedValue::Undefined()); 1414514f5e3Sopenharmony_ci callInfo->SetThis(map.GetTaggedValue()); 1424514f5e3Sopenharmony_ci callInfo->SetCallArg(0, JSTaggedValue(i)); 1434514f5e3Sopenharmony_ci callInfo->SetCallArg(1, JSTaggedValue(i)); 1444514f5e3Sopenharmony_ci 1454514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo); 1464514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedMap::Set(callInfo); 1474514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1484514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSSharedMap()); 1494514f5e3Sopenharmony_ci JSHandle<JSSharedMap> jsSMap(thread, JSSharedMap::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData()))); 1504514f5e3Sopenharmony_ci EXPECT_EQ(JSSharedMap::GetSize(thread, jsSMap), i + 1); 1514514f5e3Sopenharmony_ci } 1524514f5e3Sopenharmony_ci JSTaggedValue result = JSSharedMap::GetValue(thread, map, 0); 1534514f5e3Sopenharmony_ci EXPECT_NE(result.GetRawData(), JSTaggedValue::Exception().GetRawData()); 1544514f5e3Sopenharmony_ci} 1554514f5e3Sopenharmony_ci 1564514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, DeleteTest001) 1574514f5e3Sopenharmony_ci{ 1584514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map(thread, CreateSBuiltinsMap(thread)); 1594514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(thread, JSTaggedValue(10)); 1604514f5e3Sopenharmony_ci bool result = JSSharedMap::Delete(thread, map, key); 1614514f5e3Sopenharmony_ci ASSERT_EQ(result, false); 1624514f5e3Sopenharmony_ci} 1634514f5e3Sopenharmony_ci 1644514f5e3Sopenharmony_cienum class AlgorithmType { 1654514f5e3Sopenharmony_ci SET, 1664514f5e3Sopenharmony_ci FOR_EACH, 1674514f5e3Sopenharmony_ci HAS, 1684514f5e3Sopenharmony_ci}; 1694514f5e3Sopenharmony_ci 1704514f5e3Sopenharmony_ciJSTaggedValue MapAlgorithm(JSThread *thread, JSTaggedValue thisArg, std::vector<JSTaggedValue>& args, 1714514f5e3Sopenharmony_ci int32_t argLen, AlgorithmType type) 1724514f5e3Sopenharmony_ci{ 1734514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argLen); 1744514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1754514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(thisArg); 1764514f5e3Sopenharmony_ci for (size_t i = 0; i < args.size(); i++) { 1774514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(i, args[i]); 1784514f5e3Sopenharmony_ci } 1794514f5e3Sopenharmony_ci auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1804514f5e3Sopenharmony_ci JSTaggedValue result; 1814514f5e3Sopenharmony_ci switch (type) { 1824514f5e3Sopenharmony_ci case AlgorithmType::SET: 1834514f5e3Sopenharmony_ci result = BuiltinsSharedMap::Set(ecmaRuntimeCallInfo); 1844514f5e3Sopenharmony_ci break; 1854514f5e3Sopenharmony_ci case AlgorithmType::FOR_EACH: 1864514f5e3Sopenharmony_ci result = BuiltinsSharedMap::ForEach(ecmaRuntimeCallInfo); 1874514f5e3Sopenharmony_ci break; 1884514f5e3Sopenharmony_ci case AlgorithmType::HAS: 1894514f5e3Sopenharmony_ci result = BuiltinsSharedMap::Has(ecmaRuntimeCallInfo); 1904514f5e3Sopenharmony_ci break; 1914514f5e3Sopenharmony_ci default: 1924514f5e3Sopenharmony_ci break; 1934514f5e3Sopenharmony_ci } 1944514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1954514f5e3Sopenharmony_ci return result; 1964514f5e3Sopenharmony_ci} 1974514f5e3Sopenharmony_ci 1984514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, SetAndHas) 1994514f5e3Sopenharmony_ci{ 2004514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2014514f5e3Sopenharmony_ci // create jsSMap 2024514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map(thread, CreateSBuiltinsMap(thread)); 2034514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromASCII("key")); 2044514f5e3Sopenharmony_ci 2054514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 2064514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 2074514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue()); 2084514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue()); 2094514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(1))); 2104514f5e3Sopenharmony_ci 2114514f5e3Sopenharmony_ci // JSSharedMap *jsSMap; 2124514f5e3Sopenharmony_ci { 2134514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 2144514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsSharedMap::Has(ecmaRuntimeCallInfo); 2154514f5e3Sopenharmony_ci 2164514f5e3Sopenharmony_ci EXPECT_EQ(result1.GetRawData(), JSTaggedValue::False().GetRawData()); 2174514f5e3Sopenharmony_ci 2184514f5e3Sopenharmony_ci // test Set() 2194514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsSharedMap::Set(ecmaRuntimeCallInfo); 2204514f5e3Sopenharmony_ci 2214514f5e3Sopenharmony_ci EXPECT_TRUE(result2.IsECMAObject()); 2224514f5e3Sopenharmony_ci JSHandle<JSSharedMap> jsSMap(thread, JSSharedMap::Cast(reinterpret_cast<TaggedObject *>(result2.GetRawData()))); 2234514f5e3Sopenharmony_ci EXPECT_EQ(JSSharedMap::GetSize(thread, jsSMap), 1); 2244514f5e3Sopenharmony_ci 2254514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{key.GetTaggedValue(), JSTaggedValue(static_cast<int32_t>(1))}; // 1:value 2264514f5e3Sopenharmony_ci auto result3 = MapAlgorithm(thread, jsSMap.GetTaggedValue(), args, 8, AlgorithmType::HAS); // 8: arg len 2274514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData()); 2284514f5e3Sopenharmony_ci } 2294514f5e3Sopenharmony_ci} 2304514f5e3Sopenharmony_ci 2314514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, SetAndGet) 2324514f5e3Sopenharmony_ci{ 2334514f5e3Sopenharmony_ci constexpr uint32_t NODE_NUMBERS = 8; 2344514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map(thread, CreateSBuiltinsMap(thread)); 2354514f5e3Sopenharmony_ci for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 2364514f5e3Sopenharmony_ci auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 2374514f5e3Sopenharmony_ci callInfo->SetFunction(JSTaggedValue::Undefined()); 2384514f5e3Sopenharmony_ci callInfo->SetThis(map.GetTaggedValue()); 2394514f5e3Sopenharmony_ci callInfo->SetCallArg(0, JSTaggedValue(i)); 2404514f5e3Sopenharmony_ci callInfo->SetCallArg(1, JSTaggedValue(i)); 2414514f5e3Sopenharmony_ci 2424514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo); 2434514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedMap::Set(callInfo); 2444514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2454514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSSharedMap()); 2464514f5e3Sopenharmony_ci JSHandle<JSSharedMap> jsSMap(thread, JSSharedMap::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData()))); 2474514f5e3Sopenharmony_ci EXPECT_EQ(JSSharedMap::GetSize(thread, jsSMap), i + 1); 2484514f5e3Sopenharmony_ci } 2494514f5e3Sopenharmony_ci 2504514f5e3Sopenharmony_ci for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 2514514f5e3Sopenharmony_ci auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2524514f5e3Sopenharmony_ci callInfo->SetFunction(JSTaggedValue::Undefined()); 2534514f5e3Sopenharmony_ci callInfo->SetThis(map.GetTaggedValue()); 2544514f5e3Sopenharmony_ci callInfo->SetCallArg(0, JSTaggedValue(i)); 2554514f5e3Sopenharmony_ci 2564514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo); 2574514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedMap::Get(callInfo); 2584514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2594514f5e3Sopenharmony_ci EXPECT_EQ(result, JSTaggedValue(i)); 2604514f5e3Sopenharmony_ci } 2614514f5e3Sopenharmony_ci} 2624514f5e3Sopenharmony_ci 2634514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, ForEach) 2644514f5e3Sopenharmony_ci{ 2654514f5e3Sopenharmony_ci // generate a map has 5 entries{key1:0,key2:1,key3:2,key4:3,key5:4} 2664514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2674514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2684514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map(thread, CreateSBuiltinsMap(thread)); 2694514f5e3Sopenharmony_ci char keyArray[] = "key0"; 2704514f5e3Sopenharmony_ci for (uint32_t i = 0; i < 5; i++) { 2714514f5e3Sopenharmony_ci keyArray[3] = '1' + i; 2724514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromASCII(keyArray)); 2734514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{key.GetTaggedValue(), JSTaggedValue(static_cast<int32_t>(i))}; 2744514f5e3Sopenharmony_ci auto result1 = MapAlgorithm(thread, map.GetTaggedValue(), args, 8, AlgorithmType::SET); 2754514f5e3Sopenharmony_ci 2764514f5e3Sopenharmony_ci EXPECT_TRUE(result1.IsECMAObject()); 2774514f5e3Sopenharmony_ci JSHandle<JSSharedMap> jsSMap(thread, JSSharedMap::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()))); 2784514f5e3Sopenharmony_ci EXPECT_EQ(JSSharedMap::GetSize(thread, jsSMap), static_cast<int>(i) + 1); 2794514f5e3Sopenharmony_ci } 2804514f5e3Sopenharmony_ci JSHandle<JSArray> jsArray(JSArray::ArrayCreate(thread, JSTaggedNumber(0))); 2814514f5e3Sopenharmony_ci JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestFunc)); 2824514f5e3Sopenharmony_ci 2834514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{func.GetTaggedValue(), jsArray.GetTaggedValue()}; 2844514f5e3Sopenharmony_ci auto result2 = MapAlgorithm(thread, map.GetTaggedValue(), args, 8, AlgorithmType::FOR_EACH); 2854514f5e3Sopenharmony_ci 2864514f5e3Sopenharmony_ci EXPECT_EQ(result2.GetRawData(), JSTaggedValue::VALUE_UNDEFINED); 2874514f5e3Sopenharmony_ci EXPECT_EQ(jsArray->GetArrayLength(), 10U); 2884514f5e3Sopenharmony_ci} 2894514f5e3Sopenharmony_ci 2904514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, DeleteAndRemove) 2914514f5e3Sopenharmony_ci{ 2924514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2934514f5e3Sopenharmony_ci // create jsSMap 2944514f5e3Sopenharmony_ci JSHandle<JSSharedMap> map(thread, CreateSBuiltinsMap(thread)); 2954514f5e3Sopenharmony_ci 2964514f5e3Sopenharmony_ci // add 40 keys 2974514f5e3Sopenharmony_ci char keyArray[] = "key0"; 2984514f5e3Sopenharmony_ci for (uint32_t i = 0; i < 40; i++) { 2994514f5e3Sopenharmony_ci keyArray[3] = '1' + i; 3004514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(thread, factory->NewFromASCII(keyArray).GetTaggedValue()); 3014514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{key.GetTaggedValue(), JSTaggedValue(static_cast<int32_t>(i))}; 3024514f5e3Sopenharmony_ci auto result1 = MapAlgorithm(thread, map.GetTaggedValue(), args, 8, AlgorithmType::SET); 3034514f5e3Sopenharmony_ci 3044514f5e3Sopenharmony_ci EXPECT_TRUE(result1.IsECMAObject()); 3054514f5e3Sopenharmony_ci JSHandle<JSSharedMap> jsSMap(thread, JSSharedMap::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()))); 3064514f5e3Sopenharmony_ci EXPECT_EQ(JSSharedMap::GetSize(thread, jsSMap), static_cast<int>(i) + 1); 3074514f5e3Sopenharmony_ci } 3084514f5e3Sopenharmony_ci // whether jsSMap has delete key 3094514f5e3Sopenharmony_ci keyArray[3] = '1' + 8; 3104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> deleteKey(factory->NewFromASCII(keyArray)); 3114514f5e3Sopenharmony_ci 3124514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 3134514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 3144514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(map.GetTaggedValue()); 3154514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, deleteKey.GetTaggedValue()); 3164514f5e3Sopenharmony_ci 3174514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 3184514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsSharedMap::Has(ecmaRuntimeCallInfo1); 3194514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3204514f5e3Sopenharmony_ci 3214514f5e3Sopenharmony_ci EXPECT_EQ(result2.GetRawData(), JSTaggedValue::True().GetRawData()); 3224514f5e3Sopenharmony_ci 3234514f5e3Sopenharmony_ci // delete 3244514f5e3Sopenharmony_ci [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 3254514f5e3Sopenharmony_ci JSTaggedValue result3 = BuiltinsSharedMap::Delete(ecmaRuntimeCallInfo1); 3264514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev1); 3274514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData()); 3284514f5e3Sopenharmony_ci 3294514f5e3Sopenharmony_ci // check deleteKey is deleted 3304514f5e3Sopenharmony_ci [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 3314514f5e3Sopenharmony_ci JSTaggedValue result4 = BuiltinsSharedMap::Has(ecmaRuntimeCallInfo1); 3324514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev2); 3334514f5e3Sopenharmony_ci EXPECT_EQ(result4.GetRawData(), JSTaggedValue::False().GetRawData()); 3344514f5e3Sopenharmony_ci [[maybe_unused]] auto prev3 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 3354514f5e3Sopenharmony_ci JSTaggedValue result5 = BuiltinsSharedMap::GetSize(ecmaRuntimeCallInfo1); 3364514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev3); 3374514f5e3Sopenharmony_ci EXPECT_EQ(result5.GetRawData(), JSTaggedValue(39).GetRawData()); 3384514f5e3Sopenharmony_ci 3394514f5e3Sopenharmony_ci // clear 3404514f5e3Sopenharmony_ci [[maybe_unused]] auto prev4 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 3414514f5e3Sopenharmony_ci JSTaggedValue result6 = BuiltinsSharedMap::Clear(ecmaRuntimeCallInfo1); 3424514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev4); 3434514f5e3Sopenharmony_ci EXPECT_EQ(result6.GetRawData(), JSTaggedValue::VALUE_UNDEFINED); 3444514f5e3Sopenharmony_ci EXPECT_EQ(JSSharedMap::GetSize(thread, map), 0); 3454514f5e3Sopenharmony_ci} 3464514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, Species) 3474514f5e3Sopenharmony_ci{ 3484514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 3494514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 3504514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> map(thread, CreateSBuiltinsMap(thread)); 3514514f5e3Sopenharmony_ci 3524514f5e3Sopenharmony_ci // test species 3534514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 3544514f5e3Sopenharmony_ci EXPECT_TRUE(!speciesSymbol.GetTaggedValue().IsUndefined()); 3554514f5e3Sopenharmony_ci 3564514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetSBuiltininMapFunction()); 3574514f5e3Sopenharmony_ci 3584514f5e3Sopenharmony_ci JSTaggedValue value = 3594514f5e3Sopenharmony_ci JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(newTarget), speciesSymbol).GetValue().GetTaggedValue(); 3604514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> valueHandle(thread, value); 3614514f5e3Sopenharmony_ci EXPECT_EQ(value, newTarget.GetTaggedValue()); 3624514f5e3Sopenharmony_ci 3634514f5e3Sopenharmony_ci JSHandle<JSFunction> constructor = JSHandle<JSFunction>::Cast(JSTaggedValue::ToObject(thread, valueHandle)); 3644514f5e3Sopenharmony_ci EXPECT_EQ(JSTaggedValue::GetPrototype(thread, map), constructor->GetFunctionPrototype()); 3654514f5e3Sopenharmony_ci 3664514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key1(factory->NewFromASCII("set")); 3674514f5e3Sopenharmony_ci JSTaggedValue value1 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue(); 3684514f5e3Sopenharmony_ci EXPECT_FALSE(value1.IsUndefined()); 3694514f5e3Sopenharmony_ci 3704514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key2(factory->NewFromASCII("has")); 3714514f5e3Sopenharmony_ci JSTaggedValue value2 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue(); 3724514f5e3Sopenharmony_ci EXPECT_FALSE(value2.IsUndefined()); 3734514f5e3Sopenharmony_ci 3744514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key3(factory->NewFromASCII("clear")); 3754514f5e3Sopenharmony_ci JSTaggedValue value3 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue(); 3764514f5e3Sopenharmony_ci EXPECT_FALSE(value3.IsUndefined()); 3774514f5e3Sopenharmony_ci 3784514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key4(factory->NewFromASCII("size")); 3794514f5e3Sopenharmony_ci JSTaggedValue value4 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue(); 3804514f5e3Sopenharmony_ci EXPECT_FALSE(value4.IsUndefined()); 3814514f5e3Sopenharmony_ci 3824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key5(factory->NewFromASCII("delete")); 3834514f5e3Sopenharmony_ci JSTaggedValue value5 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue(); 3844514f5e3Sopenharmony_ci EXPECT_FALSE(value5.IsUndefined()); 3854514f5e3Sopenharmony_ci 3864514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key6(factory->NewFromASCII("forEach")); 3874514f5e3Sopenharmony_ci JSTaggedValue value6 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue(); 3884514f5e3Sopenharmony_ci EXPECT_FALSE(value6.IsUndefined()); 3894514f5e3Sopenharmony_ci 3904514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key7(factory->NewFromASCII("get")); 3914514f5e3Sopenharmony_ci JSTaggedValue value7 = JSObject::GetProperty(thread, map, key1).GetValue().GetTaggedValue(); 3924514f5e3Sopenharmony_ci EXPECT_FALSE(value7.IsUndefined()); 3934514f5e3Sopenharmony_ci} 3944514f5e3Sopenharmony_ci 3954514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, GetIterator) 3964514f5e3Sopenharmony_ci{ 3974514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> map(thread, CreateSBuiltinsMap(thread)); 3984514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 3994514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 4004514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(map.GetTaggedValue()); 4014514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 4024514f5e3Sopenharmony_ci 4034514f5e3Sopenharmony_ci // test Values() 4044514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSharedMap::Values(ecmaRuntimeCallInfo); 4054514f5e3Sopenharmony_ci JSHandle<JSSharedMapIterator> iter(thread, result); 4064514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsJSSharedMapIterator()); 4074514f5e3Sopenharmony_ci EXPECT_EQ(IterationKind::VALUE, iter->GetIterationKind()); 4084514f5e3Sopenharmony_ci 4094514f5e3Sopenharmony_ci // test Keys() 4104514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsSharedMap::Keys(ecmaRuntimeCallInfo); 4114514f5e3Sopenharmony_ci JSHandle<JSSharedMapIterator> iter1(thread, result1); 4124514f5e3Sopenharmony_ci EXPECT_TRUE(result1.IsJSSharedMapIterator()); 4134514f5e3Sopenharmony_ci EXPECT_EQ(IterationKind::KEY, iter1->GetIterationKind()); 4144514f5e3Sopenharmony_ci 4154514f5e3Sopenharmony_ci // test entries() 4164514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsSharedMap::Entries(ecmaRuntimeCallInfo); 4174514f5e3Sopenharmony_ci JSHandle<JSSharedMapIterator> iter2(thread, result2); 4184514f5e3Sopenharmony_ci EXPECT_TRUE(result2.IsJSSharedMapIterator()); 4194514f5e3Sopenharmony_ci EXPECT_EQ(IterationKind::KEY_AND_VALUE, iter2->GetIterationKind()); 4204514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 4214514f5e3Sopenharmony_ci} 4224514f5e3Sopenharmony_ci 4234514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedMapTest, NextInternalTest001) 4244514f5e3Sopenharmony_ci{ 4254514f5e3Sopenharmony_ci JSTaggedValue result2 = 4264514f5e3Sopenharmony_ci JSSharedMapIterator::NextInternal(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined())); 4274514f5e3Sopenharmony_ci EXPECT_EQ(result2.IsJSSharedSetIterator(), false); 4284514f5e3Sopenharmony_ci} 4294514f5e3Sopenharmony_ci} 430