1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/js_object.h"
17 #include "ecmascript/js_symbol.h"
18 #include "ecmascript/tests/test_helper.h"
19
20 using namespace panda::ecmascript;
21
22 namespace panda::test {
23 class PropertiesCacheTest : public testing::Test {
24 public:
SetUpTestCase()25 static void SetUpTestCase()
26 {
27 GTEST_LOG_(INFO) << "SetUpTestCase";
28 }
29
TearDownTestCase()30 static void TearDownTestCase()
31 {
32 GTEST_LOG_(INFO) << "TearDownCase";
33 }
34
SetUp()35 void SetUp() override
36 {
37 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
38 }
39
TearDown()40 void TearDown() override
41 {
42 TestHelper::DestroyEcmaVMWithScope(instance, scope);
43 }
44
45 EcmaVM *instance {nullptr};
46 EcmaHandleScope *scope {nullptr};
47 JSThread *thread {nullptr};
48 };
49
50 /**
51 * @tc.name: SetAndGet
52 * @tc.desc: Creating PropertiesCache through "GetPropertiesCache" function,Set two values through "Set" function,
53 * one is string and the other is symbol, and then check whether it is correct through the "Get" function.
54 * @tc.type: FUNC
55 * @tc.requre:
56 */
HWTEST_F_L0(PropertiesCacheTest,SetAndGet)57 HWTEST_F_L0(PropertiesCacheTest, SetAndGet)
58 {
59 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
60 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
61
62 JSHandle<JSTaggedValue> handleFunction(factory->NewJSFunction(env));
63 JSHandle<JSSymbol> handleSymbol(factory->NewJSSymbol());
64 JSHandle<JSTaggedValue> handleKey10(factory->NewFromASCII("10"));
65 JSHClass *FuncHClass = JSObject::Cast(handleFunction->GetTaggedObject())->GetJSHClass();
66
67 PropertiesCache *handleProCache = thread->GetPropertiesCache();
68 // key is string
69 for (int i = 0; i < 10; i++) {
70 JSHandle<JSTaggedValue> handleNumKey(thread, JSTaggedValue(1));
71 JSHandle<JSTaggedValue> handleStrKey(JSTaggedValue::ToString(thread, handleNumKey));
72 handleProCache->Set(FuncHClass, handleStrKey.GetTaggedValue(), i);
73 EXPECT_EQ(handleProCache->Get(FuncHClass, handleStrKey.GetTaggedValue()), i);
74 }
75 EXPECT_EQ(handleProCache->Get(FuncHClass, handleKey10.GetTaggedValue()), -1); // PropertiesCache::NOT_FOUND
76 // key is symbol
77 for (int i = 0; i < 10; i++) {
78 handleSymbol->SetHashField(static_cast<uint32_t>(i));
79 handleProCache->Set(FuncHClass, handleSymbol.GetTaggedValue(), i);
80 EXPECT_EQ(handleProCache->Get(FuncHClass, handleSymbol.GetTaggedValue()), i);
81 }
82 handleSymbol->SetHashField(static_cast<uint32_t>(10));
83 EXPECT_EQ(handleProCache->Get(FuncHClass, handleSymbol.GetTaggedValue()), -1); // PropertiesCache::NOT_FOUND
84 handleProCache->Clear();
85 }
86
87 /**
88 * @tc.name: Clear
89 * @tc.desc: Creating PropertiesCache through "GetPropertiesCache" function,Set two values through "Set" function,
90 * then call "Clear" function,check the returned value through "get" function is within expectations.
91 * @tc.type: FUNC
92 * @tc.requre:
93 */
HWTEST_F_L0(PropertiesCacheTest,Clear)94 HWTEST_F_L0(PropertiesCacheTest, Clear)
95 {
96 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
97 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
98
99 JSHandle<JSTaggedValue> handleKey(factory->NewFromASCII("10"));
100 JSHandle<JSTaggedValue> handleFunction(factory->NewJSFunction(env));
101 JSHClass *FuncHClass = JSObject::Cast(handleFunction->GetTaggedObject())->GetJSHClass();
102 PropertiesCache *handleProCache = thread->GetPropertiesCache();
103
104 handleProCache->Set(FuncHClass, handleKey.GetTaggedValue(), 10);
105 EXPECT_EQ(handleProCache->Get(FuncHClass, handleKey.GetTaggedValue()), 10);
106 handleProCache->Clear();
107 EXPECT_EQ(handleProCache->Get(FuncHClass, handleKey.GetTaggedValue()), -1); // PropertiesCache::NOT_FOUND
108 }
109 } // namespace panda::test
110