• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(thread, FuncHClass, handleStrKey.GetTaggedValue(), i);
73         EXPECT_EQ(handleProCache->Get(thread, FuncHClass, handleStrKey.GetTaggedValue()), i);
74     }
75     EXPECT_EQ(handleProCache->Get(thread, FuncHClass,
76         handleKey10.GetTaggedValue()), -1); // PropertiesCache::NOT_FOUND
77     // key is symbol
78     for (int i = 0; i < 10; i++) {
79         handleSymbol->SetHashField(static_cast<uint32_t>(i));
80         handleProCache->Set(thread, FuncHClass, handleSymbol.GetTaggedValue(), i);
81         EXPECT_EQ(handleProCache->Get(thread, FuncHClass, handleSymbol.GetTaggedValue()), i);
82     }
83     handleSymbol->SetHashField(static_cast<uint32_t>(10));
84     EXPECT_EQ(handleProCache->Get(thread, FuncHClass,
85         handleSymbol.GetTaggedValue()), -1); // PropertiesCache::NOT_FOUND
86     handleProCache->Clear();
87 }
88 
89 /**
90  * @tc.name: Clear
91  * @tc.desc: Creating PropertiesCache through "GetPropertiesCache" function,Set two values through "Set" function,
92  *           then call "Clear" function,check the returned value through "get" function is within expectations.
93  * @tc.type: FUNC
94  * @tc.requre:
95  */
HWTEST_F_L0(PropertiesCacheTest,Clear)96 HWTEST_F_L0(PropertiesCacheTest, Clear)
97 {
98     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
99     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
100 
101     JSHandle<JSTaggedValue> handleKey(factory->NewFromASCII("10"));
102     JSHandle<JSTaggedValue> handleFunction(factory->NewJSFunction(env));
103     JSHClass *FuncHClass = JSObject::Cast(handleFunction->GetTaggedObject())->GetJSHClass();
104     PropertiesCache *handleProCache = thread->GetPropertiesCache();
105 
106     handleProCache->Set(thread, FuncHClass, handleKey.GetTaggedValue(), 10);
107     EXPECT_EQ(handleProCache->Get(thread, FuncHClass, handleKey.GetTaggedValue()), 10);
108     handleProCache->Clear();
109     EXPECT_EQ(handleProCache->Get(thread, FuncHClass, handleKey.GetTaggedValue()), -1); // PropertiesCache::NOT_FOUND
110 }
111 } // namespace panda::test
112