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_weak_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_iterator.h"
264514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
274514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
284514f5e3Sopenharmony_ci#include "ecmascript/js_weak_container.h"
294514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
304514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
334514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins;
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_cinamespace panda::test {
364514f5e3Sopenharmony_ciusing BuiltinsWeakSet = ecmascript::builtins::BuiltinsWeakSet;
374514f5e3Sopenharmony_ciusing JSWeakSet = ecmascript::JSWeakSet;
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_ciclass BuiltinsWeakSetTest : public BaseTestWithScope<false> {
404514f5e3Sopenharmony_ci};
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_cistatic JSObject *JSObjectTestCreate(JSThread *thread)
434514f5e3Sopenharmony_ci{
444514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope scope(thread);
454514f5e3Sopenharmony_ci    EcmaVM *ecmaVM = thread->GetEcmaVM();
464514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
474514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> jsFunc = globalEnv->GetObjectFunction();
484514f5e3Sopenharmony_ci    JSHandle<JSObject> newObj =
494514f5e3Sopenharmony_ci        thread->GetEcmaVM()->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc), jsFunc);
504514f5e3Sopenharmony_ci    return *newObj;
514514f5e3Sopenharmony_ci}
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ciJSWeakSet *CreateBuiltinsWeakSet(JSThread *thread)
544514f5e3Sopenharmony_ci{
554514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
564514f5e3Sopenharmony_ci    JSHandle<JSFunction> newTarget(env->GetBuiltinsWeakSetFunction());
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    // 4 : test case
594514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 4);
604514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
614514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
624514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
634514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsWeakSet::WeakSetConstructor(ecmaRuntimeCallInfo);
644514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsECMAObject());
674514f5e3Sopenharmony_ci    JSWeakSet *jsWeakSet = JSWeakSet::Cast(reinterpret_cast<TaggedObject *>(result.GetRawData()));
684514f5e3Sopenharmony_ci    return jsWeakSet;
694514f5e3Sopenharmony_ci}
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsWeakSetTest, CreateAndGetSize)
724514f5e3Sopenharmony_ci{
734514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
744514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
754514f5e3Sopenharmony_ci    JSHandle<JSFunction> newTarget(env->GetBuiltinsWeakSetFunction());
764514f5e3Sopenharmony_ci    JSHandle<JSWeakSet> weakSet(thread, CreateBuiltinsWeakSet(thread));
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    JSHandle<TaggedArray> array(factory->NewTaggedArray(5));
794514f5e3Sopenharmony_ci    for (int i = 0; i < 5; i++) {
804514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key(thread, JSObjectTestCreate(thread));
814514f5e3Sopenharmony_ci        array->Set(thread, i, key.GetTaggedValue());
824514f5e3Sopenharmony_ci    }
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    JSHandle<JSArray> values = JSArray::CreateArrayFromList(thread, array);
854514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
864514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
874514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(weakSet.GetTaggedValue());
884514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, values.GetTaggedValue());
894514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetNewTarget(newTarget.GetTaggedValue());
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci    JSTaggedValue result1 = BuiltinsWeakSet::WeakSetConstructor(ecmaRuntimeCallInfo);
944514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
954514f5e3Sopenharmony_ci    JSHandle<JSWeakSet> weakSetResult(thread,
964514f5e3Sopenharmony_ci                                        JSWeakSet::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())));
974514f5e3Sopenharmony_ci    EXPECT_EQ(weakSetResult->GetSize(), 5);
984514f5e3Sopenharmony_ci}
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsWeakSetTest, AddAndHas)
1014514f5e3Sopenharmony_ci{
1024514f5e3Sopenharmony_ci    // create jsWeakSet
1034514f5e3Sopenharmony_ci    JSHandle<JSWeakSet> weakSet(thread, CreateBuiltinsWeakSet(thread));
1044514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key(thread, JSObjectTestCreate(thread));
1054514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
1064514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1074514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(weakSet.GetTaggedValue());
1084514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue());
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    JSWeakSet *jsWeakSet;
1114514f5e3Sopenharmony_ci    {
1124514f5e3Sopenharmony_ci        [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1134514f5e3Sopenharmony_ci        JSTaggedValue result1 = BuiltinsWeakSet::Has(ecmaRuntimeCallInfo);
1144514f5e3Sopenharmony_ci        TestHelper::TearDownFrame(thread, prev);
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ci        EXPECT_EQ(result1.GetRawData(), JSTaggedValue::False().GetRawData());
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci        // test Add()
1194514f5e3Sopenharmony_ci        JSTaggedValue result2 = BuiltinsWeakSet::Add(ecmaRuntimeCallInfo);
1204514f5e3Sopenharmony_ci        EXPECT_TRUE(result2.IsECMAObject());
1214514f5e3Sopenharmony_ci        jsWeakSet = JSWeakSet::Cast(reinterpret_cast<TaggedObject *>(result2.GetRawData()));
1224514f5e3Sopenharmony_ci        EXPECT_EQ(jsWeakSet->GetSize(), 1);
1234514f5e3Sopenharmony_ci    }
1244514f5e3Sopenharmony_ci
1254514f5e3Sopenharmony_ci    // test Has()
1264514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
1274514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
1284514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetThis(JSTaggedValue(jsWeakSet));
1294514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetCallArg(0, key.GetTaggedValue());
1304514f5e3Sopenharmony_ci    {
1314514f5e3Sopenharmony_ci        [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
1324514f5e3Sopenharmony_ci        JSTaggedValue result3 = BuiltinsWeakSet::Has(ecmaRuntimeCallInfo1);
1334514f5e3Sopenharmony_ci        TestHelper::TearDownFrame(thread, prev);
1344514f5e3Sopenharmony_ci
1354514f5e3Sopenharmony_ci        EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData());
1364514f5e3Sopenharmony_ci    }
1374514f5e3Sopenharmony_ci}
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_civoid AddCommon(JSThread* thread, JSHandle<JSWeakSet>& weakSet, JSHandle<JSTaggedValue>& key, int i)
1404514f5e3Sopenharmony_ci{
1414514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
1424514f5e3Sopenharmony_ci            TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means 1 call arg
1434514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1444514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(weakSet.GetTaggedValue());
1454514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, key.GetTaggedValue());
1464514f5e3Sopenharmony_ci
1474514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1484514f5e3Sopenharmony_ci    // add
1494514f5e3Sopenharmony_ci    JSTaggedValue result1 = BuiltinsWeakSet::Add(ecmaRuntimeCallInfo);
1504514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1514514f5e3Sopenharmony_ci
1524514f5e3Sopenharmony_ci    EXPECT_TRUE(result1.IsECMAObject());
1534514f5e3Sopenharmony_ci    JSWeakSet *jsWeakSet = JSWeakSet::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData()));
1544514f5e3Sopenharmony_ci    EXPECT_EQ(jsWeakSet->GetSize(), i + 1);
1554514f5e3Sopenharmony_ci}
1564514f5e3Sopenharmony_ci
1574514f5e3Sopenharmony_civoid HasAndDeleteCommon(JSThread* thread, JSHandle<JSWeakSet>& weakSet, JSTaggedValue& lastKey)
1584514f5e3Sopenharmony_ci{
1594514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
1604514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
1614514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetThis(weakSet.GetTaggedValue());
1624514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetCallArg(0, lastKey);
1634514f5e3Sopenharmony_ci
1644514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
1654514f5e3Sopenharmony_ci    JSTaggedValue result2 = BuiltinsWeakSet::Has(ecmaRuntimeCallInfo1);
1664514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1674514f5e3Sopenharmony_ci
1684514f5e3Sopenharmony_ci    EXPECT_EQ(result2.GetRawData(), JSTaggedValue::True().GetRawData());
1694514f5e3Sopenharmony_ci
1704514f5e3Sopenharmony_ci    // delete
1714514f5e3Sopenharmony_ci    JSTaggedValue result3 = BuiltinsWeakSet::Delete(ecmaRuntimeCallInfo1);
1724514f5e3Sopenharmony_ci
1734514f5e3Sopenharmony_ci    EXPECT_EQ(result3.GetRawData(), JSTaggedValue::True().GetRawData());
1744514f5e3Sopenharmony_ci
1754514f5e3Sopenharmony_ci    // check deleteKey is deleted
1764514f5e3Sopenharmony_ci    JSTaggedValue result4 = BuiltinsWeakSet::Has(ecmaRuntimeCallInfo1);
1774514f5e3Sopenharmony_ci
1784514f5e3Sopenharmony_ci    EXPECT_EQ(result4.GetRawData(), JSTaggedValue::False().GetRawData());
1794514f5e3Sopenharmony_ci}
1804514f5e3Sopenharmony_ci
1814514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsWeakSetTest, DeleteAndRemove)
1824514f5e3Sopenharmony_ci{
1834514f5e3Sopenharmony_ci    // create jsSet
1844514f5e3Sopenharmony_ci    JSHandle<JSWeakSet> weakSet(thread, CreateBuiltinsWeakSet(thread));
1854514f5e3Sopenharmony_ci
1864514f5e3Sopenharmony_ci    // add 40 keys
1874514f5e3Sopenharmony_ci    JSTaggedValue lastKey(JSTaggedValue::Undefined());
1884514f5e3Sopenharmony_ci    for (int i = 0; i < 40; i++) {
1894514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key(thread, JSObjectTestCreate(thread));
1904514f5e3Sopenharmony_ci
1914514f5e3Sopenharmony_ci        AddCommon(thread, weakSet, key, i);
1924514f5e3Sopenharmony_ci        lastKey = key.GetTaggedValue();
1934514f5e3Sopenharmony_ci    }
1944514f5e3Sopenharmony_ci    // whether jsWeakSet has delete lastKey
1954514f5e3Sopenharmony_ci
1964514f5e3Sopenharmony_ci    HasAndDeleteCommon(thread, weakSet, lastKey);
1974514f5e3Sopenharmony_ci}
1984514f5e3Sopenharmony_ci
1994514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsWeakSetTest, SymbolKey)
2004514f5e3Sopenharmony_ci{
2014514f5e3Sopenharmony_ci    // create jsSet
2024514f5e3Sopenharmony_ci    JSHandle<JSWeakSet> weakSet(thread, CreateBuiltinsWeakSet(thread));
2034514f5e3Sopenharmony_ci
2044514f5e3Sopenharmony_ci    // add 2 keys
2054514f5e3Sopenharmony_ci    JSTaggedValue lastKey(JSTaggedValue::Undefined());
2064514f5e3Sopenharmony_ci    for (int i = 0; i < 2; i++) {
2074514f5e3Sopenharmony_ci        JSHandle<JSSymbol> symbolKey = thread->GetEcmaVM()->GetFactory()->NewJSSymbol();
2084514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key(symbolKey);
2094514f5e3Sopenharmony_ci
2104514f5e3Sopenharmony_ci        AddCommon(thread, weakSet, key, i);
2114514f5e3Sopenharmony_ci        lastKey = key.GetTaggedValue();
2124514f5e3Sopenharmony_ci    }
2134514f5e3Sopenharmony_ci    // whether jsWeakSet has delete lastKey
2144514f5e3Sopenharmony_ci
2154514f5e3Sopenharmony_ci    HasAndDeleteCommon(thread, weakSet, lastKey);
2164514f5e3Sopenharmony_ci}
2174514f5e3Sopenharmony_ci}  // namespace panda::test
218