• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <string>
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/js_handle.h"
19 #include "ecmascript/js_object-inl.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/object_factory.h"
22 #include "ecmascript/tagged_node.h"
23 #include "ecmascript/tests/test_helper.h"
24 
25 using namespace panda;
26 using namespace panda::ecmascript;
27 namespace panda::test {
28 class LinkedNodeTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase()
31     {
32         GTEST_LOG_(INFO) << "SetUpTestCase";
33     }
34 
TearDownTestCase()35     static void TearDownTestCase()
36     {
37         GTEST_LOG_(INFO) << "TearDownCase";
38     }
39 
SetUp()40     void SetUp() override
41     {
42         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
43     }
44 
TearDown()45     void TearDown() override
46     {
47         TestHelper::DestroyEcmaVMWithScope(instance, scope);
48     }
49 
50     EcmaVM *instance {nullptr};
51     EcmaHandleScope *scope {nullptr};
52     JSThread *thread {nullptr};
53 
GetGlobalEnv()54     JSHandle<GlobalEnv> GetGlobalEnv()
55     {
56         EcmaVM *ecma = thread->GetEcmaVM();
57         return ecma->GetGlobalEnv();
58     }
59     uint32_t NODE_NUMBERS = 8;
60 
61 protected:
CreateLinkedList()62     JSHandle<LinkedNode> CreateLinkedList()
63     {
64         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65         JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
66         JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
67         std::string myKey("mykey");
68         std::string myValue("myvalue");
69         JSHandle<LinkedNode> head(thread, JSTaggedValue::Hole());
70         for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
71             std::string iKey = myKey + std::to_string(i);
72             std::string iValue = myValue + std::to_string(i);
73             key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
74             value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
75             int hash = TaggedNode::Hash(key.GetTaggedValue());
76             head = factory->NewLinkedNode(hash, key, value, head);
77         }
78         return head;
79     }
80 };
81 
HWTEST_F_L0(LinkedNodeTest,LinkedNodeCreate)82 HWTEST_F_L0(LinkedNodeTest, LinkedNodeCreate)
83 {
84     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
85     std::string k("testKey");
86     std::string v("testValue");
87     JSHandle<JSTaggedValue> key(thread, factory->NewFromStdString(k).GetTaggedValue());
88     JSHandle<JSTaggedValue> value(thread, factory->NewFromStdString(v).GetTaggedValue());
89     int hash = TaggedNode::Hash(factory->NewFromStdString(k).GetTaggedValue());
90     JSHandle<LinkedNode> hole(thread, JSTaggedValue::Hole());
91     JSHandle<LinkedNode> newNode = factory->NewLinkedNode(hash, key, value, hole);
92     EXPECT_TRUE(!newNode.GetTaggedValue().IsHole());
93     EXPECT_TRUE(newNode.GetTaggedValue().IsLinkedNode());
94 }
95 
HWTEST_F_L0(LinkedNodeTest,Treeify)96 HWTEST_F_L0(LinkedNodeTest, Treeify)
97 {
98     JSHandle<LinkedNode> head = CreateLinkedList();
99     JSHandle<RBTreeNode> root = LinkedNode::Treeing(thread, head);
100     EXPECT_EQ(root->GetCount(), NODE_NUMBERS);
101 }
102 }