14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_set.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h" 194514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h" 204514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 214514f5e3Sopenharmony_ci#include "ecmascript/js_array.h" 224514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h" 234514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h" 244514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h" 254514f5e3Sopenharmony_ci#include "ecmascript/js_set.h" 264514f5e3Sopenharmony_ci#include "ecmascript/js_set_iterator.h" 274514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h" 284514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h" 294514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 304514f5e3Sopenharmony_ci#include "ecmascript/shared_objects/js_shared_set_iterator.h" 314514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 324514f5e3Sopenharmony_ci 334514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 344514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins; 354514f5e3Sopenharmony_ci 364514f5e3Sopenharmony_cinamespace panda::test { 374514f5e3Sopenharmony_ciusing BuiltinsSet = ecmascript::builtins::BuiltinsSet; 384514f5e3Sopenharmony_ciusing JSSet = ecmascript::JSSet; 394514f5e3Sopenharmony_ci 404514f5e3Sopenharmony_ciclass BuiltinsSetTest : public BaseTestWithScope<false> { 414514f5e3Sopenharmony_cipublic: 424514f5e3Sopenharmony_ci class TestClass : public base::BuiltinsBase { 434514f5e3Sopenharmony_ci public: 444514f5e3Sopenharmony_ci static JSTaggedValue TestFunc(EcmaRuntimeCallInfo *argv) 454514f5e3Sopenharmony_ci { 464514f5e3Sopenharmony_ci JSTaggedValue key = GetCallArg(argv, 0).GetTaggedValue(); 474514f5e3Sopenharmony_ci if (key.IsUndefined()) { 484514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 494514f5e3Sopenharmony_ci } 504514f5e3Sopenharmony_ci JSArray *jsArray = JSArray::Cast(GetThis(argv)->GetTaggedObject()); 514514f5e3Sopenharmony_ci uint32_t length = jsArray->GetArrayLength() + 1U; 524514f5e3Sopenharmony_ci jsArray->SetArrayLength(argv->GetThread(), length); 534514f5e3Sopenharmony_ci return JSTaggedValue::Undefined(); 544514f5e3Sopenharmony_ci } 554514f5e3Sopenharmony_ci }; 564514f5e3Sopenharmony_ci}; 574514f5e3Sopenharmony_ci 584514f5e3Sopenharmony_ciJSSet *CreateBuiltinsSet(JSThread *thread) 594514f5e3Sopenharmony_ci{ 604514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 614514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetBuiltinsSetFunction()); 624514f5e3Sopenharmony_ci // 4 : test case 634514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 4); 644514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue()); 654514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined()); 664514f5e3Sopenharmony_ci 674514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 684514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSet::SetConstructor(ecmaRuntimeCallInfo); 694514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_ci EXPECT_TRUE(result.IsECMAObject()); 724514f5e3Sopenharmony_ci return JSSet::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData())); 734514f5e3Sopenharmony_ci} 744514f5e3Sopenharmony_ci 754514f5e3Sopenharmony_cienum class AlgorithmType { 764514f5e3Sopenharmony_ci ADD, 774514f5e3Sopenharmony_ci HAS, 784514f5e3Sopenharmony_ci}; 794514f5e3Sopenharmony_ci 804514f5e3Sopenharmony_ciJSTaggedValue SetAlgorithm(JSThread *thread, JSTaggedValue jsSet, std::vector<JSTaggedValue>& args, 814514f5e3Sopenharmony_ci uint32_t argLen = 8, AlgorithmType type = AlgorithmType::ADD) 824514f5e3Sopenharmony_ci{ 834514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfos = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argLen); 844514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetFunction(JSTaggedValue::Undefined()); 854514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetThis(jsSet); 864514f5e3Sopenharmony_ci for (size_t i = 0; i < args.size(); i++) { 874514f5e3Sopenharmony_ci ecmaRuntimeCallInfos->SetCallArg(i, args[i]); 884514f5e3Sopenharmony_ci } 894514f5e3Sopenharmony_ci auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfos); 904514f5e3Sopenharmony_ci JSTaggedValue result; 914514f5e3Sopenharmony_ci switch (type) { 924514f5e3Sopenharmony_ci case AlgorithmType::ADD: 934514f5e3Sopenharmony_ci result = BuiltinsSet::Add(ecmaRuntimeCallInfos); 944514f5e3Sopenharmony_ci break; 954514f5e3Sopenharmony_ci case AlgorithmType::HAS: 964514f5e3Sopenharmony_ci result = BuiltinsSet::Has(ecmaRuntimeCallInfos); 974514f5e3Sopenharmony_ci break; 984514f5e3Sopenharmony_ci default: 994514f5e3Sopenharmony_ci break; 1004514f5e3Sopenharmony_ci } 1014514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1024514f5e3Sopenharmony_ci return result; 1034514f5e3Sopenharmony_ci} 1044514f5e3Sopenharmony_ci 1054514f5e3Sopenharmony_ci// new Set("abrupt").toString() 1064514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSetTest, CreateAndGetSize) 1074514f5e3Sopenharmony_ci{ 1084514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1094514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1104514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetBuiltinsSetFunction()); 1114514f5e3Sopenharmony_ci JSHandle<JSSet> set(thread, CreateBuiltinsSet(thread)); 1124514f5e3Sopenharmony_ci 1134514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1144514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1154514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(set.GetTaggedValue()); 1164514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue::Undefined()); 1174514f5e3Sopenharmony_ci { 1184514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1194514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSet::GetSize(ecmaRuntimeCallInfo); 1204514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1214514f5e3Sopenharmony_ci 1224514f5e3Sopenharmony_ci EXPECT_EQ(result.GetRawData(), JSTaggedValue(0).GetRawData()); 1234514f5e3Sopenharmony_ci } 1244514f5e3Sopenharmony_ci 1254514f5e3Sopenharmony_ci JSHandle<TaggedArray> array(factory->NewTaggedArray(5)); 1264514f5e3Sopenharmony_ci for (int i = 0; i < 5; i++) { 1274514f5e3Sopenharmony_ci array->Set(thread, i, JSTaggedValue(i)); 1284514f5e3Sopenharmony_ci } 1294514f5e3Sopenharmony_ci 1304514f5e3Sopenharmony_ci JSHandle<JSArray> values = JSArray::CreateArrayFromList(thread, array); 1314514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1324514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(newTarget.GetTaggedValue()); 1334514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(set.GetTaggedValue()); 1344514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, values.GetTaggedValue()); 1354514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetNewTarget(newTarget.GetTaggedValue()); 1364514f5e3Sopenharmony_ci { 1374514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 1384514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsSet::SetConstructor(ecmaRuntimeCallInfo1); 1394514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1404514f5e3Sopenharmony_ci 1414514f5e3Sopenharmony_ci EXPECT_EQ(JSSet::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()))->GetSize(), 5); 1424514f5e3Sopenharmony_ci } 1434514f5e3Sopenharmony_ci} 1444514f5e3Sopenharmony_ci 1454514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSetTest, AddAndHas) 1464514f5e3Sopenharmony_ci{ 1474514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1484514f5e3Sopenharmony_ci // create jsSet 1494514f5e3Sopenharmony_ci JSHandle<JSSet> set(thread, CreateBuiltinsSet(thread)); 1504514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(thread, factory->NewFromASCII("key").GetTaggedValue()); 1514514f5e3Sopenharmony_ci 1524514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1534514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 1544514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(set.GetTaggedValue()); 1554514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue()); 1564514f5e3Sopenharmony_ci 1574514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 1584514f5e3Sopenharmony_ci JSTaggedValue result1 = BuiltinsSet::Has(ecmaRuntimeCallInfo); 1594514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 1604514f5e3Sopenharmony_ci 1614514f5e3Sopenharmony_ci EXPECT_EQ(result1.GetRawData(), JSTaggedValue::False().GetRawData()); 1624514f5e3Sopenharmony_ci 1634514f5e3Sopenharmony_ci // test Add() 1644514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsSet::Add(ecmaRuntimeCallInfo); 1654514f5e3Sopenharmony_ci EXPECT_TRUE(result2.IsECMAObject()); 1664514f5e3Sopenharmony_ci JSSet *jsSet = JSSet::Cast(reinterpret_cast<TaggedObject *>(result2.GetRawData())); 1674514f5e3Sopenharmony_ci EXPECT_EQ(jsSet->GetSize(), 1); 1684514f5e3Sopenharmony_ci 1694514f5e3Sopenharmony_ci // test Has() 1704514f5e3Sopenharmony_ci JSTaggedValue jsSetTag(jsSet); 1714514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{key.GetTaggedValue()}; 1724514f5e3Sopenharmony_ci auto result3 = SetAlgorithm(thread, jsSetTag, args, 6, AlgorithmType::HAS); 1734514f5e3Sopenharmony_ci 1744514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData()); 1754514f5e3Sopenharmony_ci 1764514f5e3Sopenharmony_ci // test -0.0 1774514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> negativeZero(thread, JSTaggedValue(-0.0)); 1784514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> positiveZero(thread, JSTaggedValue(+0.0)); 1794514f5e3Sopenharmony_ci 1804514f5e3Sopenharmony_ci args[0] = negativeZero.GetTaggedValue(); 1814514f5e3Sopenharmony_ci SetAlgorithm(thread, jsSetTag, args, 6, AlgorithmType::ADD); 1824514f5e3Sopenharmony_ci 1834514f5e3Sopenharmony_ci args[0] = positiveZero.GetTaggedValue(); 1844514f5e3Sopenharmony_ci auto result4 = SetAlgorithm(thread, jsSetTag, args, 6, AlgorithmType::HAS); 1854514f5e3Sopenharmony_ci 1864514f5e3Sopenharmony_ci EXPECT_EQ(result4.GetRawData(), JSTaggedValue::True().GetRawData()); 1874514f5e3Sopenharmony_ci} 1884514f5e3Sopenharmony_ci 1894514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSetTest, ForEach) 1904514f5e3Sopenharmony_ci{ 1914514f5e3Sopenharmony_ci // generate a set has 5 entry{key1,key2,key3,key4,key5} 1924514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1934514f5e3Sopenharmony_ci JSHandle<JSSet> set(thread, CreateBuiltinsSet(thread)); 1944514f5e3Sopenharmony_ci char keyArray[] = "key0"; 1954514f5e3Sopenharmony_ci for (uint32_t i = 0U; i < 5U; i++) { 1964514f5e3Sopenharmony_ci keyArray[3] = '1' + i; 1974514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromASCII(keyArray)); 1984514f5e3Sopenharmony_ci 1994514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{key.GetTaggedValue()}; 2004514f5e3Sopenharmony_ci auto result1 = SetAlgorithm(thread, set.GetTaggedValue(), args, 6, AlgorithmType::ADD); 2014514f5e3Sopenharmony_ci 2024514f5e3Sopenharmony_ci EXPECT_TRUE(result1.IsECMAObject()); 2034514f5e3Sopenharmony_ci JSSet *jsSet = JSSet::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())); 2044514f5e3Sopenharmony_ci EXPECT_EQ(jsSet->GetSize(), static_cast<int>(i) + 1); 2054514f5e3Sopenharmony_ci } 2064514f5e3Sopenharmony_ci // test foreach 2074514f5e3Sopenharmony_ci JSHandle<JSArray> jsArray(JSArray::ArrayCreate(thread, JSTaggedNumber(0))); 2084514f5e3Sopenharmony_ci JSHandle<JSFunction> func = 2094514f5e3Sopenharmony_ci factory->NewJSFunction(thread->GetEcmaVM()->GetGlobalEnv(), reinterpret_cast<void *>(TestClass::TestFunc)); 2104514f5e3Sopenharmony_ci 2114514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); 2124514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 2134514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(set.GetTaggedValue()); 2144514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, func.GetTaggedValue()); 2154514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(1, jsArray.GetTaggedValue()); 2164514f5e3Sopenharmony_ci 2174514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 2184514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsSet::ForEach(ecmaRuntimeCallInfo1); 2194514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 2204514f5e3Sopenharmony_ci 2214514f5e3Sopenharmony_ci EXPECT_EQ(result2.GetRawData(), JSTaggedValue::VALUE_UNDEFINED); 2224514f5e3Sopenharmony_ci EXPECT_EQ(jsArray->GetArrayLength(), 5U); 2234514f5e3Sopenharmony_ci} 2244514f5e3Sopenharmony_ci 2254514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSetTest, DeleteAndRemove) 2264514f5e3Sopenharmony_ci{ 2274514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2284514f5e3Sopenharmony_ci // create jsSet 2294514f5e3Sopenharmony_ci JSHandle<JSSet> set(thread, CreateBuiltinsSet(thread)); 2304514f5e3Sopenharmony_ci 2314514f5e3Sopenharmony_ci // add 40 keys 2324514f5e3Sopenharmony_ci char keyArray[] = "key0"; 2334514f5e3Sopenharmony_ci for (uint32_t i = 0; i < 40; i++) { 2344514f5e3Sopenharmony_ci keyArray[3] = '1' + i; 2354514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromASCII(keyArray)); 2364514f5e3Sopenharmony_ci std::vector<JSTaggedValue> args{key.GetTaggedValue()}; 2374514f5e3Sopenharmony_ci auto result1 = SetAlgorithm(thread, set.GetTaggedValue(), args, 6, AlgorithmType::ADD); 2384514f5e3Sopenharmony_ci 2394514f5e3Sopenharmony_ci EXPECT_TRUE(result1.IsECMAObject()); 2404514f5e3Sopenharmony_ci JSSet *jsSet = JSSet::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())); 2414514f5e3Sopenharmony_ci EXPECT_EQ(jsSet->GetSize(), static_cast<int>(i) + 1); 2424514f5e3Sopenharmony_ci } 2434514f5e3Sopenharmony_ci // whether jsSet has delete key 2444514f5e3Sopenharmony_ci keyArray[3] = '1' + 8; 2454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> deleteKey(factory->NewFromASCII(keyArray)); 2464514f5e3Sopenharmony_ci 2474514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 2484514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined()); 2494514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetThis(set.GetTaggedValue()); 2504514f5e3Sopenharmony_ci ecmaRuntimeCallInfo1->SetCallArg(0, deleteKey.GetTaggedValue()); 2514514f5e3Sopenharmony_ci 2524514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1); 2534514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsSet::Has(ecmaRuntimeCallInfo1); 2544514f5e3Sopenharmony_ci 2554514f5e3Sopenharmony_ci EXPECT_EQ(result2.GetRawData(), JSTaggedValue::True().GetRawData()); 2564514f5e3Sopenharmony_ci 2574514f5e3Sopenharmony_ci // delete 2584514f5e3Sopenharmony_ci JSTaggedValue result3 = BuiltinsSet::Delete(ecmaRuntimeCallInfo1); 2594514f5e3Sopenharmony_ci 2604514f5e3Sopenharmony_ci EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData()); 2614514f5e3Sopenharmony_ci 2624514f5e3Sopenharmony_ci // check deleteKey is deleted 2634514f5e3Sopenharmony_ci JSTaggedValue result4 = BuiltinsSet::Has(ecmaRuntimeCallInfo1); 2644514f5e3Sopenharmony_ci 2654514f5e3Sopenharmony_ci EXPECT_EQ(result4.GetRawData(), JSTaggedValue::False().GetRawData()); 2664514f5e3Sopenharmony_ci 2674514f5e3Sopenharmony_ci JSTaggedValue result5 = BuiltinsSet::GetSize(ecmaRuntimeCallInfo1); 2684514f5e3Sopenharmony_ci 2694514f5e3Sopenharmony_ci EXPECT_EQ(result5.GetRawData(), JSTaggedValue(39).GetRawData()); 2704514f5e3Sopenharmony_ci 2714514f5e3Sopenharmony_ci // clear 2724514f5e3Sopenharmony_ci JSTaggedValue result6 = BuiltinsSet::Clear(ecmaRuntimeCallInfo1); 2734514f5e3Sopenharmony_ci EXPECT_EQ(result6.GetRawData(), JSTaggedValue::VALUE_UNDEFINED); 2744514f5e3Sopenharmony_ci EXPECT_EQ(set->GetSize(), 0); 2754514f5e3Sopenharmony_ci} 2764514f5e3Sopenharmony_ci 2774514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSetTest, Species) 2784514f5e3Sopenharmony_ci{ 2794514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 2804514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 2814514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> set(thread, CreateBuiltinsSet(thread)); 2824514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol(); 2834514f5e3Sopenharmony_ci EXPECT_TRUE(!speciesSymbol->IsUndefined()); 2844514f5e3Sopenharmony_ci 2854514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(env->GetBuiltinsSetFunction()); 2864514f5e3Sopenharmony_ci 2874514f5e3Sopenharmony_ci JSTaggedValue value = 2884514f5e3Sopenharmony_ci JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(newTarget), speciesSymbol).GetValue().GetTaggedValue(); 2894514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> valueHandle(thread, value); 2904514f5e3Sopenharmony_ci EXPECT_EQ(value, newTarget.GetTaggedValue()); 2914514f5e3Sopenharmony_ci 2924514f5e3Sopenharmony_ci // to string tag 2934514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> toStringTagSymbol = env->GetToStringTagSymbol(); 2944514f5e3Sopenharmony_ci JSHandle<EcmaString> stringTag(JSObject::GetProperty(thread, set, toStringTagSymbol).GetValue()); 2954514f5e3Sopenharmony_ci JSHandle<EcmaString> str = factory->NewFromASCII("Set"); 2964514f5e3Sopenharmony_ci EXPECT_TRUE(!stringTag.GetTaggedValue().IsUndefined()); 2974514f5e3Sopenharmony_ci EXPECT_TRUE(EcmaStringAccessor::StringsAreEqual(*str, *stringTag)); 2984514f5e3Sopenharmony_ci 2994514f5e3Sopenharmony_ci JSHandle<JSFunction> constructor = JSHandle<JSFunction>::Cast(JSTaggedValue::ToObject(thread, valueHandle)); 3004514f5e3Sopenharmony_ci EXPECT_EQ(JSTaggedValue::GetPrototype(thread, set), constructor->GetFunctionPrototype()); 3014514f5e3Sopenharmony_ci 3024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key1(factory->NewFromASCII("add")); 3034514f5e3Sopenharmony_ci JSTaggedValue value1 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue(); 3044514f5e3Sopenharmony_ci EXPECT_FALSE(value1.IsUndefined()); 3054514f5e3Sopenharmony_ci 3064514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key2(factory->NewFromASCII("has")); 3074514f5e3Sopenharmony_ci JSTaggedValue value2 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue(); 3084514f5e3Sopenharmony_ci EXPECT_FALSE(value2.IsUndefined()); 3094514f5e3Sopenharmony_ci 3104514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key3(factory->NewFromASCII("clear")); 3114514f5e3Sopenharmony_ci JSTaggedValue value3 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue(); 3124514f5e3Sopenharmony_ci EXPECT_FALSE(value3.IsUndefined()); 3134514f5e3Sopenharmony_ci 3144514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key4(factory->NewFromASCII("size")); 3154514f5e3Sopenharmony_ci JSTaggedValue value4 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue(); 3164514f5e3Sopenharmony_ci EXPECT_FALSE(value4.IsUndefined()); 3174514f5e3Sopenharmony_ci 3184514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key5(factory->NewFromASCII("delete")); 3194514f5e3Sopenharmony_ci JSTaggedValue value5 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue(); 3204514f5e3Sopenharmony_ci EXPECT_FALSE(value5.IsUndefined()); 3214514f5e3Sopenharmony_ci 3224514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key6(factory->NewFromASCII("forEach")); 3234514f5e3Sopenharmony_ci JSTaggedValue value6 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue(); 3244514f5e3Sopenharmony_ci EXPECT_FALSE(value6.IsUndefined()); 3254514f5e3Sopenharmony_ci} 3264514f5e3Sopenharmony_ci 3274514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSetTest, GetIterator) 3284514f5e3Sopenharmony_ci{ 3294514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> set(thread, CreateBuiltinsSet(thread)); 3304514f5e3Sopenharmony_ci 3314514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 3324514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3334514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(set.GetTaggedValue()); 3344514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3354514f5e3Sopenharmony_ci 3364514f5e3Sopenharmony_ci // test Values() 3374514f5e3Sopenharmony_ci JSTaggedValue result = BuiltinsSet::Values(ecmaRuntimeCallInfo); 3384514f5e3Sopenharmony_ci JSHandle<JSSetIterator> iter(thread, result); 3394514f5e3Sopenharmony_ci EXPECT_TRUE(iter->IsJSSetIterator()); 3404514f5e3Sopenharmony_ci EXPECT_EQ(IterationKind::VALUE, IterationKind(iter->GetIterationKind())); 3414514f5e3Sopenharmony_ci EXPECT_EQ(JSSet::Cast(set.GetTaggedValue().GetTaggedObject())->GetLinkedSet(), iter->GetIteratedSet()); 3424514f5e3Sopenharmony_ci 3434514f5e3Sopenharmony_ci // test entries() 3444514f5e3Sopenharmony_ci JSTaggedValue result2 = BuiltinsSet::Entries(ecmaRuntimeCallInfo); 3454514f5e3Sopenharmony_ci JSHandle<JSSetIterator> iter2(thread, result2); 3464514f5e3Sopenharmony_ci EXPECT_TRUE(iter2->IsJSSetIterator()); 3474514f5e3Sopenharmony_ci EXPECT_EQ(IterationKind::KEY_AND_VALUE, iter2->GetIterationKind()); 3484514f5e3Sopenharmony_ci} 3494514f5e3Sopenharmony_ci 3504514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSetTest, Exception) 3514514f5e3Sopenharmony_ci{ 3524514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> set(thread, CreateBuiltinsSet(thread)); 3534514f5e3Sopenharmony_ci auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 3544514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 3554514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetThis(set.GetTaggedValue()); 3564514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 3574514f5e3Sopenharmony_ci auto result = JSSharedSetIterator::CreateSetIterator(thread, set, IterationKind::KEY); 3584514f5e3Sopenharmony_ci EXPECT_TRUE(result->IsUndefined()); 3594514f5e3Sopenharmony_ci TestHelper::TearDownFrame(thread, prev); 3604514f5e3Sopenharmony_ci} 3614514f5e3Sopenharmony_ci} // namespace panda::test 362