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