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/builtins/builtins_shared_set.h"
164514f5e3Sopenharmony_ci#include "ecmascript/shared_objects/js_shared_set.h"
174514f5e3Sopenharmony_ci#include "ecmascript/shared_objects/js_shared_set_iterator.h"
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/linked_hash_table.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_set.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_set_iterator.h"
254514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
264514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h"
274514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h"
284514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
294514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
304514f5e3Sopenharmony_ci#include "ecmascript/object_factory.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 BuiltinsSharedSet = ecmascript::builtins::BuiltinsSharedSet;
384514f5e3Sopenharmony_ciusing JSSharedSet = ecmascript::JSSharedSet;
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_ciclass BuiltinsSharedSetTest : 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_ciJSSharedSet *CreateBuiltinsSharedSet(JSThread *thread)
594514f5e3Sopenharmony_ci{
604514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
614514f5e3Sopenharmony_ci    JSHandle<JSFunction> newTarget(env->GetSBuiltininSetFunction());
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 = BuiltinsSharedSet::Constructor(ecmaRuntimeCallInfo);
694514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsECMAObject());
724514f5e3Sopenharmony_ci    JSSharedSet *jsSSet = JSSharedSet::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData()));
734514f5e3Sopenharmony_ci    return jsSSet;
744514f5e3Sopenharmony_ci}
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ciJSSet *CreateJSSet(JSThread *thread)
774514f5e3Sopenharmony_ci{
784514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
794514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> constructor = thread->GetEcmaVM()->GetGlobalEnv()->GetBuiltinsSetFunction();
804514f5e3Sopenharmony_ci    JSHandle<JSSet> set =
814514f5e3Sopenharmony_ci        JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
824514f5e3Sopenharmony_ci    JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
834514f5e3Sopenharmony_ci    set->SetLinkedSet(thread, hashSet);
844514f5e3Sopenharmony_ci    return JSSet::Cast(set.GetTaggedValue().GetTaggedObject());
854514f5e3Sopenharmony_ci}
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_cienum class AlgorithmType {
884514f5e3Sopenharmony_ci    ADD,
894514f5e3Sopenharmony_ci    HAS,
904514f5e3Sopenharmony_ci};
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_ciJSTaggedValue SharedSetAlgorithm(JSThread *thread, JSTaggedValue jsSet, std::vector<JSTaggedValue>& args,
934514f5e3Sopenharmony_ci    uint32_t argLen = 8, AlgorithmType type = AlgorithmType::ADD)
944514f5e3Sopenharmony_ci{
954514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfos = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argLen);
964514f5e3Sopenharmony_ci    ecmaRuntimeCallInfos->SetFunction(JSTaggedValue::Undefined());
974514f5e3Sopenharmony_ci    ecmaRuntimeCallInfos->SetThis(jsSet);
984514f5e3Sopenharmony_ci    for (size_t i = 0; i < args.size(); i++) {
994514f5e3Sopenharmony_ci        ecmaRuntimeCallInfos->SetCallArg(i, args[i]);
1004514f5e3Sopenharmony_ci    }
1014514f5e3Sopenharmony_ci    auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfos);
1024514f5e3Sopenharmony_ci    JSTaggedValue result;
1034514f5e3Sopenharmony_ci    switch (type) {
1044514f5e3Sopenharmony_ci        case AlgorithmType::ADD:
1054514f5e3Sopenharmony_ci            result = BuiltinsSharedSet::Add(ecmaRuntimeCallInfos);
1064514f5e3Sopenharmony_ci            break;
1074514f5e3Sopenharmony_ci        case AlgorithmType::HAS:
1084514f5e3Sopenharmony_ci            result = BuiltinsSharedSet::Has(ecmaRuntimeCallInfos);
1094514f5e3Sopenharmony_ci            break;
1104514f5e3Sopenharmony_ci        default:
1114514f5e3Sopenharmony_ci            break;
1124514f5e3Sopenharmony_ci    }
1134514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1144514f5e3Sopenharmony_ci    return result;
1154514f5e3Sopenharmony_ci}
1164514f5e3Sopenharmony_ci
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, CreateSetIteratorTest001)
1194514f5e3Sopenharmony_ci{
1204514f5e3Sopenharmony_ci    JSHandle<JSSet> jsSet(thread, CreateJSSet(thread));
1214514f5e3Sopenharmony_ci    EXPECT_TRUE(*jsSet != nullptr);
1224514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> setIteratorValue1 =
1234514f5e3Sopenharmony_ci        JSSharedSetIterator::CreateSetIterator(thread, JSHandle<JSTaggedValue>(jsSet), IterationKind::KEY);
1244514f5e3Sopenharmony_ci    EXPECT_EQ(setIteratorValue1->IsJSSetIterator(), false);
1254514f5e3Sopenharmony_ci}
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, NextInternalTest001)
1284514f5e3Sopenharmony_ci{
1294514f5e3Sopenharmony_ci    JSTaggedValue setIteratorValue1 =
1304514f5e3Sopenharmony_ci        JSSharedSetIterator::NextInternal(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue::Undefined()));
1314514f5e3Sopenharmony_ci    EXPECT_EQ(setIteratorValue1.IsJSSharedSetIterator(), false);
1324514f5e3Sopenharmony_ci}
1334514f5e3Sopenharmony_ci
1344514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, DeleteTest001)
1354514f5e3Sopenharmony_ci{
1364514f5e3Sopenharmony_ci    JSHandle<JSSharedSet> set(thread, CreateBuiltinsSharedSet(thread));
1374514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(0));
1384514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(1));
1394514f5e3Sopenharmony_ci    JSSharedSet::Add(thread, set, value1);
1404514f5e3Sopenharmony_ci    JSSharedSet::Add(thread, set, value2);
1414514f5e3Sopenharmony_ci    JSSharedSet::Delete(thread, set, JSHandle<JSTaggedValue>(thread, JSTaggedValue(20)));
1424514f5e3Sopenharmony_ci}
1434514f5e3Sopenharmony_ci
1444514f5e3Sopenharmony_ci// new Set("abrupt").toString()
1454514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, CreateAndGetSize)
1464514f5e3Sopenharmony_ci{
1474514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1484514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1494514f5e3Sopenharmony_ci    JSHandle<JSFunction> newTarget(env->GetSBuiltininSetFunction());
1504514f5e3Sopenharmony_ci    JSHandle<JSSharedSet> set(thread, CreateBuiltinsSharedSet(thread));
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, JSTaggedValue::Undefined());
1564514f5e3Sopenharmony_ci    {
1574514f5e3Sopenharmony_ci        [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1584514f5e3Sopenharmony_ci        JSTaggedValue result = BuiltinsSharedSet::GetSize(ecmaRuntimeCallInfo);
1594514f5e3Sopenharmony_ci        TestHelper::TearDownFrame(thread, prev);
1604514f5e3Sopenharmony_ci
1614514f5e3Sopenharmony_ci        EXPECT_EQ(result.GetRawData(), JSTaggedValue(0).GetRawData());
1624514f5e3Sopenharmony_ci    }
1634514f5e3Sopenharmony_ci
1644514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array(factory->NewTaggedArray(5));
1654514f5e3Sopenharmony_ci    for (int i = 0; i < 5; i++) {
1664514f5e3Sopenharmony_ci        array->Set(thread, i, JSTaggedValue(i));
1674514f5e3Sopenharmony_ci    }
1684514f5e3Sopenharmony_ci
1694514f5e3Sopenharmony_ci    JSHandle<JSArray> values = JSArray::CreateArrayFromList(thread, array);
1704514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
1714514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetFunction(newTarget.GetTaggedValue());
1724514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetThis(set.GetTaggedValue());
1734514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetCallArg(0, values.GetTaggedValue());
1744514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetNewTarget(newTarget.GetTaggedValue());
1754514f5e3Sopenharmony_ci    {
1764514f5e3Sopenharmony_ci        [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
1774514f5e3Sopenharmony_ci        JSTaggedValue result1 = BuiltinsSharedSet::Constructor(ecmaRuntimeCallInfo1);
1784514f5e3Sopenharmony_ci        TestHelper::TearDownFrame(thread, prev);
1794514f5e3Sopenharmony_ci        JSHandle<JSSharedSet> set1(thread, JSSharedSet::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())));
1804514f5e3Sopenharmony_ci        EXPECT_EQ(JSSharedSet::GetSize(thread, set1), 5);
1814514f5e3Sopenharmony_ci    }
1824514f5e3Sopenharmony_ci}
1834514f5e3Sopenharmony_ci
1844514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, AddAndHas)
1854514f5e3Sopenharmony_ci{
1864514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1874514f5e3Sopenharmony_ci    // create jsSet
1884514f5e3Sopenharmony_ci    JSHandle<JSSharedSet> set(thread, CreateBuiltinsSharedSet(thread));
1894514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key(thread, factory->NewFromASCII("key").GetTaggedValue());
1904514f5e3Sopenharmony_ci
1914514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
1924514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1934514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(set.GetTaggedValue());
1944514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue());
1954514f5e3Sopenharmony_ci
1964514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1974514f5e3Sopenharmony_ci    JSTaggedValue result1 = BuiltinsSharedSet::Has(ecmaRuntimeCallInfo);
1984514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1994514f5e3Sopenharmony_ci
2004514f5e3Sopenharmony_ci    EXPECT_EQ(result1.GetRawData(), JSTaggedValue::False().GetRawData());
2014514f5e3Sopenharmony_ci
2024514f5e3Sopenharmony_ci    // test Add()
2034514f5e3Sopenharmony_ci    JSTaggedValue result2 = BuiltinsSharedSet::Add(ecmaRuntimeCallInfo);
2044514f5e3Sopenharmony_ci    EXPECT_TRUE(result2.IsECMAObject());
2054514f5e3Sopenharmony_ci    JSSharedSet *jsSSet = JSSharedSet::Cast(reinterpret_cast<TaggedObject *>(result2.GetRawData()));
2064514f5e3Sopenharmony_ci    JSHandle<JSSharedSet> set2(thread, jsSSet);
2074514f5e3Sopenharmony_ci    EXPECT_EQ(JSSharedSet::GetSize(thread, set2), 1);
2084514f5e3Sopenharmony_ci
2094514f5e3Sopenharmony_ci    // test Has()
2104514f5e3Sopenharmony_ci    JSTaggedValue jsSetTag(jsSSet);
2114514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{key.GetTaggedValue()};
2124514f5e3Sopenharmony_ci    auto result3 = SharedSetAlgorithm(thread, jsSetTag, args, 6, AlgorithmType::HAS);
2134514f5e3Sopenharmony_ci
2144514f5e3Sopenharmony_ci    EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData());
2154514f5e3Sopenharmony_ci
2164514f5e3Sopenharmony_ci    // test -0.0
2174514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> negativeZero(thread, JSTaggedValue(-0.0));
2184514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> positiveZero(thread, JSTaggedValue(+0.0));
2194514f5e3Sopenharmony_ci
2204514f5e3Sopenharmony_ci    args[0] = negativeZero.GetTaggedValue();
2214514f5e3Sopenharmony_ci    SharedSetAlgorithm(thread, jsSetTag, args, 6, AlgorithmType::ADD);
2224514f5e3Sopenharmony_ci
2234514f5e3Sopenharmony_ci    args[0] = positiveZero.GetTaggedValue();
2244514f5e3Sopenharmony_ci    auto result4 = SharedSetAlgorithm(thread, jsSetTag, args, 6, AlgorithmType::HAS);
2254514f5e3Sopenharmony_ci
2264514f5e3Sopenharmony_ci    EXPECT_EQ(result4.GetRawData(), JSTaggedValue::True().GetRawData());
2274514f5e3Sopenharmony_ci}
2284514f5e3Sopenharmony_ci
2294514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, ForEach)
2304514f5e3Sopenharmony_ci{
2314514f5e3Sopenharmony_ci    // generate a set has 5 entry{key1,key2,key3,key4,key5}
2324514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2334514f5e3Sopenharmony_ci    JSHandle<JSSharedSet> set(thread, CreateBuiltinsSharedSet(thread));
2344514f5e3Sopenharmony_ci    char keyArray[] = "key0";
2354514f5e3Sopenharmony_ci    for (uint32_t i = 0U; i < 5U; i++) {
2364514f5e3Sopenharmony_ci        keyArray[3] = '1' + i;
2374514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key(factory->NewFromASCII(keyArray));
2384514f5e3Sopenharmony_ci
2394514f5e3Sopenharmony_ci        std::vector<JSTaggedValue> args{key.GetTaggedValue()};
2404514f5e3Sopenharmony_ci        auto result1 = SharedSetAlgorithm(thread, set.GetTaggedValue(), args, 6, AlgorithmType::ADD);
2414514f5e3Sopenharmony_ci
2424514f5e3Sopenharmony_ci        EXPECT_TRUE(result1.IsECMAObject());
2434514f5e3Sopenharmony_ci        JSHandle<JSSharedSet> set1(thread, JSSharedSet::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())));
2444514f5e3Sopenharmony_ci        EXPECT_EQ(JSSharedSet::GetSize(thread, set1), static_cast<int>(i) + 1);
2454514f5e3Sopenharmony_ci    }
2464514f5e3Sopenharmony_ci    // test foreach
2474514f5e3Sopenharmony_ci    JSHandle<JSArray> jsArray(JSArray::ArrayCreate(thread, JSTaggedNumber(0)));
2484514f5e3Sopenharmony_ci    JSHandle<JSFunction> func =
2494514f5e3Sopenharmony_ci        factory->NewJSFunction(thread->GetEcmaVM()->GetGlobalEnv(), reinterpret_cast<void *>(TestClass::TestFunc));
2504514f5e3Sopenharmony_ci
2514514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
2524514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
2534514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetThis(set.GetTaggedValue());
2544514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetCallArg(0, func.GetTaggedValue());
2554514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetCallArg(1, jsArray.GetTaggedValue());
2564514f5e3Sopenharmony_ci
2574514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
2584514f5e3Sopenharmony_ci    JSTaggedValue result2 = BuiltinsSharedSet::ForEach(ecmaRuntimeCallInfo1);
2594514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
2604514f5e3Sopenharmony_ci
2614514f5e3Sopenharmony_ci    EXPECT_EQ(result2.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
2624514f5e3Sopenharmony_ci    EXPECT_EQ(jsArray->GetArrayLength(), 5U);
2634514f5e3Sopenharmony_ci}
2644514f5e3Sopenharmony_ci
2654514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, DeleteAndRemove)
2664514f5e3Sopenharmony_ci{
2674514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2684514f5e3Sopenharmony_ci    // create jsSet
2694514f5e3Sopenharmony_ci    JSHandle<JSSharedSet> set(thread, CreateBuiltinsSharedSet(thread));
2704514f5e3Sopenharmony_ci
2714514f5e3Sopenharmony_ci    // add 40 keys
2724514f5e3Sopenharmony_ci    char keyArray[] = "key0";
2734514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < 40; i++) {
2744514f5e3Sopenharmony_ci        keyArray[3] = '1' + i;
2754514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key(factory->NewFromASCII(keyArray));
2764514f5e3Sopenharmony_ci        std::vector<JSTaggedValue> args{key.GetTaggedValue()};
2774514f5e3Sopenharmony_ci        auto result1 = SharedSetAlgorithm(thread, set.GetTaggedValue(), args, 6, AlgorithmType::ADD);
2784514f5e3Sopenharmony_ci
2794514f5e3Sopenharmony_ci        EXPECT_TRUE(result1.IsECMAObject());
2804514f5e3Sopenharmony_ci        JSHandle<JSSharedSet> set1(thread, JSSharedSet::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())));
2814514f5e3Sopenharmony_ci        EXPECT_EQ(JSSharedSet::GetSize(thread, set1), static_cast<int>(i) + 1);
2824514f5e3Sopenharmony_ci    }
2834514f5e3Sopenharmony_ci    // whether jsSet has delete key
2844514f5e3Sopenharmony_ci    keyArray[3] = '1' + 8;
2854514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> deleteKey(factory->NewFromASCII(keyArray));
2864514f5e3Sopenharmony_ci
2874514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
2884514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
2894514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetThis(set.GetTaggedValue());
2904514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetCallArg(0, deleteKey.GetTaggedValue());
2914514f5e3Sopenharmony_ci
2924514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
2934514f5e3Sopenharmony_ci    JSTaggedValue result2 = BuiltinsSharedSet::Has(ecmaRuntimeCallInfo1);
2944514f5e3Sopenharmony_ci
2954514f5e3Sopenharmony_ci    EXPECT_EQ(result2.GetRawData(), JSTaggedValue::True().GetRawData());
2964514f5e3Sopenharmony_ci
2974514f5e3Sopenharmony_ci    // delete
2984514f5e3Sopenharmony_ci    JSTaggedValue result3 = BuiltinsSharedSet::Delete(ecmaRuntimeCallInfo1);
2994514f5e3Sopenharmony_ci
3004514f5e3Sopenharmony_ci    EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData());
3014514f5e3Sopenharmony_ci
3024514f5e3Sopenharmony_ci    // check deleteKey is deleted
3034514f5e3Sopenharmony_ci    JSTaggedValue result4 = BuiltinsSharedSet::Has(ecmaRuntimeCallInfo1);
3044514f5e3Sopenharmony_ci
3054514f5e3Sopenharmony_ci    EXPECT_EQ(result4.GetRawData(), JSTaggedValue::False().GetRawData());
3064514f5e3Sopenharmony_ci
3074514f5e3Sopenharmony_ci    JSTaggedValue result5 = BuiltinsSharedSet::GetSize(ecmaRuntimeCallInfo1);
3084514f5e3Sopenharmony_ci
3094514f5e3Sopenharmony_ci    EXPECT_EQ(result5.GetRawData(), JSTaggedValue(39).GetRawData());
3104514f5e3Sopenharmony_ci
3114514f5e3Sopenharmony_ci    // clear
3124514f5e3Sopenharmony_ci    JSTaggedValue result6 = BuiltinsSharedSet::Clear(ecmaRuntimeCallInfo1);
3134514f5e3Sopenharmony_ci    EXPECT_EQ(result6.GetRawData(), JSTaggedValue::VALUE_UNDEFINED);
3144514f5e3Sopenharmony_ci    EXPECT_EQ(set->JSSharedSet::GetSize(thread, set), 0);
3154514f5e3Sopenharmony_ci}
3164514f5e3Sopenharmony_ci
3174514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, Species)
3184514f5e3Sopenharmony_ci{
3194514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3204514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
3214514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> set(thread, CreateBuiltinsSharedSet(thread));
3224514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> speciesSymbol = env->GetSpeciesSymbol();
3234514f5e3Sopenharmony_ci    EXPECT_TRUE(!speciesSymbol->IsUndefined());
3244514f5e3Sopenharmony_ci
3254514f5e3Sopenharmony_ci    JSHandle<JSFunction> newTarget(env->GetSBuiltininSetFunction());
3264514f5e3Sopenharmony_ci
3274514f5e3Sopenharmony_ci    JSTaggedValue value =
3284514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(newTarget), speciesSymbol).GetValue().GetTaggedValue();
3294514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> valueHandle(thread, value);
3304514f5e3Sopenharmony_ci    EXPECT_EQ(value, newTarget.GetTaggedValue());
3314514f5e3Sopenharmony_ci
3324514f5e3Sopenharmony_ci    // to string tag
3334514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> toStringTagSymbol = env->GetToStringTagSymbol();
3344514f5e3Sopenharmony_ci    JSHandle<EcmaString> stringTag(JSObject::GetProperty(thread, set, toStringTagSymbol).GetValue());
3354514f5e3Sopenharmony_ci    EXPECT_TRUE(!stringTag.GetTaggedValue().IsUndefined());
3364514f5e3Sopenharmony_ci
3374514f5e3Sopenharmony_ci    JSHandle<JSFunction> constructor = JSHandle<JSFunction>::Cast(JSTaggedValue::ToObject(thread, valueHandle));
3384514f5e3Sopenharmony_ci    EXPECT_EQ(JSTaggedValue::GetPrototype(thread, set), constructor->GetFunctionPrototype());
3394514f5e3Sopenharmony_ci
3404514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key1(factory->NewFromASCII("add"));
3414514f5e3Sopenharmony_ci    JSTaggedValue value1 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue();
3424514f5e3Sopenharmony_ci    EXPECT_FALSE(value1.IsUndefined());
3434514f5e3Sopenharmony_ci
3444514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key2(factory->NewFromASCII("has"));
3454514f5e3Sopenharmony_ci    JSTaggedValue value2 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue();
3464514f5e3Sopenharmony_ci    EXPECT_FALSE(value2.IsUndefined());
3474514f5e3Sopenharmony_ci
3484514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key3(factory->NewFromASCII("clear"));
3494514f5e3Sopenharmony_ci    JSTaggedValue value3 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue();
3504514f5e3Sopenharmony_ci    EXPECT_FALSE(value3.IsUndefined());
3514514f5e3Sopenharmony_ci
3524514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key4(factory->NewFromASCII("size"));
3534514f5e3Sopenharmony_ci    JSTaggedValue value4 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue();
3544514f5e3Sopenharmony_ci    EXPECT_FALSE(value4.IsUndefined());
3554514f5e3Sopenharmony_ci
3564514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key5(factory->NewFromASCII("delete"));
3574514f5e3Sopenharmony_ci    JSTaggedValue value5 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue();
3584514f5e3Sopenharmony_ci    EXPECT_FALSE(value5.IsUndefined());
3594514f5e3Sopenharmony_ci
3604514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key6(factory->NewFromASCII("forEach"));
3614514f5e3Sopenharmony_ci    JSTaggedValue value6 = JSObject::GetProperty(thread, set, key1).GetValue().GetTaggedValue();
3624514f5e3Sopenharmony_ci    EXPECT_FALSE(value6.IsUndefined());
3634514f5e3Sopenharmony_ci}
3644514f5e3Sopenharmony_ci
3654514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, GetIterator)
3664514f5e3Sopenharmony_ci{
3674514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> set(thread, CreateBuiltinsSharedSet(thread));
3684514f5e3Sopenharmony_ci
3694514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
3704514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
3714514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(set.GetTaggedValue());
3724514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
3734514f5e3Sopenharmony_ci
3744514f5e3Sopenharmony_ci    // test Values()
3754514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsSharedSet::Values(ecmaRuntimeCallInfo);
3764514f5e3Sopenharmony_ci    JSHandle<JSSharedSetIterator> iter(thread, result);
3774514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsJSSharedSetIterator());
3784514f5e3Sopenharmony_ci    EXPECT_EQ(IterationKind::VALUE, IterationKind(iter->GetIterationKind()));
3794514f5e3Sopenharmony_ci
3804514f5e3Sopenharmony_ci    // test entries()
3814514f5e3Sopenharmony_ci    JSTaggedValue result2 = BuiltinsSharedSet::Entries(ecmaRuntimeCallInfo);
3824514f5e3Sopenharmony_ci    JSHandle<JSSharedSetIterator> iter2(thread, result2);
3834514f5e3Sopenharmony_ci    EXPECT_TRUE(result2.IsJSSharedSetIterator());
3844514f5e3Sopenharmony_ci    EXPECT_EQ(IterationKind::KEY_AND_VALUE, iter2->GetIterationKind());
3854514f5e3Sopenharmony_ci}
3864514f5e3Sopenharmony_ci
3874514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, GetValue)
3884514f5e3Sopenharmony_ci{
3894514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> set(thread, CreateBuiltinsSharedSet(thread));
3904514f5e3Sopenharmony_ci
3914514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
3924514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
3934514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(set.GetTaggedValue());
3944514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
3954514f5e3Sopenharmony_ci
3964514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsSharedSet::Values(ecmaRuntimeCallInfo);
3974514f5e3Sopenharmony_ci    JSHandle<JSSharedSetIterator> iter(thread, result);
3984514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsJSSharedSetIterator());
3994514f5e3Sopenharmony_ci    EXPECT_EQ(IterationKind::VALUE, IterationKind(iter->GetIterationKind()));
4004514f5e3Sopenharmony_ci
4014514f5e3Sopenharmony_ci    JSTaggedValue result2 = BuiltinsSharedSet::Entries(ecmaRuntimeCallInfo);
4024514f5e3Sopenharmony_ci    JSHandle<JSSharedSetIterator> iter2(thread, result2);
4034514f5e3Sopenharmony_ci    EXPECT_TRUE(result2.IsJSSharedSetIterator());
4044514f5e3Sopenharmony_ci    EXPECT_EQ(IterationKind::KEY_AND_VALUE, iter2->GetIterationKind());
4054514f5e3Sopenharmony_ci}
4064514f5e3Sopenharmony_ci
4074514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSharedSetTest, KEY_AND_VALUE_Next)
4084514f5e3Sopenharmony_ci{
4094514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> index0(thread, JSTaggedValue(0));
4104514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> index1(thread, JSTaggedValue(1));
4114514f5e3Sopenharmony_ci    JSHandle<JSSharedSetIterator> setIterator;
4124514f5e3Sopenharmony_ci    JSHandle<JSSharedSet> jsSet(thread, CreateBuiltinsSharedSet(thread));
4134514f5e3Sopenharmony_ci    EXPECT_TRUE(*jsSet != nullptr);
4144514f5e3Sopenharmony_ci
4154514f5e3Sopenharmony_ci    for (int i = 0; i < 3; i++) {  // 3 : 3 default numberOfElements
4164514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key(thread, JSTaggedValue(i));
4174514f5e3Sopenharmony_ci        JSSharedSet::Add(thread, jsSet, key);
4184514f5e3Sopenharmony_ci    }
4194514f5e3Sopenharmony_ci    // set IterationKind(key or value)
4204514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> setIteratorValue =
4214514f5e3Sopenharmony_ci        JSSharedSetIterator::CreateSetIterator(thread, JSHandle<JSTaggedValue>(jsSet), IterationKind::KEY_AND_VALUE);
4224514f5e3Sopenharmony_ci    setIterator = JSHandle<JSSharedSetIterator>(setIteratorValue);
4234514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue::Undefined()};
4244514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
4254514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6, setIteratorValue.GetTaggedValue());
4264514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
4274514f5e3Sopenharmony_ci
4284514f5e3Sopenharmony_ci    for (int i = 0; i <= 3; i++) { // 3 : 3 default numberOfElements
4294514f5e3Sopenharmony_ci        JSTaggedValue result = JSSharedSetIterator::Next(ecmaRuntimeCallInfo);
4304514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> resultObj(thread, result);
4314514f5e3Sopenharmony_ci        if (i < 3) {
4324514f5e3Sopenharmony_ci            JSHandle<JSArray> arrayList(thread, JSIterator::IteratorValue(thread, resultObj).GetTaggedValue());
4334514f5e3Sopenharmony_ci            EXPECT_EQ(setIterator->GetNextIndex(), static_cast<uint32_t>(i+1));
4344514f5e3Sopenharmony_ci            EXPECT_EQ(JSArray::GetProperty(thread, JSHandle<JSTaggedValue>(arrayList), index0).GetValue()->GetInt(), i);
4354514f5e3Sopenharmony_ci            EXPECT_EQ(JSArray::GetProperty(thread, JSHandle<JSTaggedValue>(arrayList), index1).GetValue()->GetInt(), i);
4364514f5e3Sopenharmony_ci        }
4374514f5e3Sopenharmony_ci        else {
4384514f5e3Sopenharmony_ci            EXPECT_EQ(JSIterator::IteratorValue(thread, resultObj).GetTaggedValue(), JSTaggedValue::Undefined());
4394514f5e3Sopenharmony_ci        }
4404514f5e3Sopenharmony_ci    }
4414514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
4424514f5e3Sopenharmony_ci}
4434514f5e3Sopenharmony_ci
4444514f5e3Sopenharmony_ci}  // namespace panda::test
445