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
25using namespace panda;
26using namespace panda::ecmascript;
27namespace panda::test {
28class LinkedNodeTest : public BaseTestWithScope<false> {
29public:
30    JSHandle<GlobalEnv> GetGlobalEnv()
31    {
32        EcmaVM *ecma = thread->GetEcmaVM();
33        return ecma->GetGlobalEnv();
34    }
35    uint32_t NODE_NUMBERS = 8;
36
37protected:
38    JSHandle<LinkedNode> CreateLinkedList()
39    {
40        ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
41        JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
42        JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
43        std::string myKey("mykey");
44        std::string myValue("myvalue");
45        JSHandle<LinkedNode> head(thread, JSTaggedValue::Hole());
46        for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
47            std::string iKey = myKey + std::to_string(i);
48            std::string iValue = myValue + std::to_string(i);
49            key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
50            value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
51            int hash = TaggedNode::Hash(thread, key.GetTaggedValue());
52            head = factory->NewLinkedNode(hash, key, value, head);
53        }
54        return head;
55    }
56};
57
58HWTEST_F_L0(LinkedNodeTest, LinkedNodeCreate)
59{
60    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
61    std::string k("testKey");
62    std::string v("testValue");
63    JSHandle<JSTaggedValue> key(thread, factory->NewFromStdString(k).GetTaggedValue());
64    JSHandle<JSTaggedValue> value(thread, factory->NewFromStdString(v).GetTaggedValue());
65    int hash = TaggedNode::Hash(thread, factory->NewFromStdString(k).GetTaggedValue());
66    JSHandle<LinkedNode> hole(thread, JSTaggedValue::Hole());
67    JSHandle<LinkedNode> newNode = factory->NewLinkedNode(hash, key, value, hole);
68    EXPECT_TRUE(!newNode.GetTaggedValue().IsHole());
69    EXPECT_TRUE(newNode.GetTaggedValue().IsLinkedNode());
70}
71
72HWTEST_F_L0(LinkedNodeTest, Treeify)
73{
74    JSHandle<LinkedNode> head = CreateLinkedList();
75    JSHandle<RBTreeNode> root = LinkedNode::Treeing(thread, head);
76    EXPECT_EQ(root->GetCount(), NODE_NUMBERS);
77}
78}