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/ecma_string.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_function.h"
20 #include "ecmascript/js_handle.h"
21 #include "ecmascript/js_iterator.h"
22 #include "ecmascript/js_map.h"
23 #include "ecmascript/js_map_iterator.h"
24 #include "ecmascript/js_object-inl.h"
25 #include "ecmascript/js_tagged_value.h"
26 #include "ecmascript/linked_hash_table.h"
27 #include "ecmascript/object_factory.h"
28 #include "ecmascript/tagged_hash_table.h"
29 #include "ecmascript/tests/test_helper.h"
30
31 using namespace panda;
32
33 using namespace panda::ecmascript;
34
35 namespace panda::test {
36 class JSMapTest : public testing::Test {
37 public:
SetUpTestCase()38 static void SetUpTestCase()
39 {
40 GTEST_LOG_(INFO) << "SetUpTestCase";
41 }
42
TearDownTestCase()43 static void TearDownTestCase()
44 {
45 GTEST_LOG_(INFO) << "TearDownCase";
46 }
47
SetUp()48 void SetUp() override
49 {
50 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
51 }
52
TearDown()53 void TearDown() override
54 {
55 TestHelper::DestroyEcmaVMWithScope(instance, scope);
56 }
57
58 EcmaVM *instance {nullptr};
59 ecmascript::EcmaHandleScope *scope {nullptr};
60 JSThread *thread {nullptr};
61
62 protected:
CreateMap()63 JSMap *CreateMap()
64 {
65 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
66 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
67 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsMapFunction();
68 JSHandle<JSMap> map =
69 JSHandle<JSMap>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
70 JSHandle<LinkedHashMap> hashMap = LinkedHashMap::Create(thread);
71 map->SetLinkedMap(thread, hashMap);
72 return *map;
73 }
74 };
75
HWTEST_F_L0(JSMapTest,MapCreate)76 HWTEST_F_L0(JSMapTest, MapCreate)
77 {
78 JSMap *map = CreateMap();
79 EXPECT_TRUE(map != nullptr);
80 }
81
HWTEST_F_L0(JSMapTest,AddAndHas)82 HWTEST_F_L0(JSMapTest, AddAndHas)
83 {
84 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
85 // create jsMap
86 JSHandle<JSMap> map(thread, CreateMap());
87
88 JSHandle<JSTaggedValue> key(factory->NewFromASCII("key"));
89 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
90 JSMap::Set(thread, map, key, value);
91 EXPECT_TRUE(map->Has(key.GetTaggedValue()));
92 }
93
HWTEST_F_L0(JSMapTest,DeleteAndGet)94 HWTEST_F_L0(JSMapTest, DeleteAndGet)
95 {
96 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
97 // create jsMap
98 JSHandle<JSMap> map(thread, CreateMap());
99
100 // add 40 keys
101 char keyArray[] = "key0";
102 for (uint32_t i = 0; i < 40; i++) {
103 keyArray[3] = '1' + i;
104 JSHandle<JSTaggedValue> key(factory->NewFromASCII(keyArray));
105 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
106 JSMap::Set(thread, map, key, value);
107 EXPECT_TRUE(map->Has(key.GetTaggedValue()));
108 }
109 EXPECT_EQ(map->GetSize(), 40);
110 // whether jsMap has delete key
111 keyArray[3] = '1' + 8;
112 JSHandle<JSTaggedValue> deleteKey(factory->NewFromASCII(keyArray));
113 EXPECT_EQ(map->GetValue(8), JSTaggedValue(8));
114 JSMap::Delete(thread, map, deleteKey);
115 EXPECT_FALSE(map->Has(deleteKey.GetTaggedValue()));
116 EXPECT_EQ(map->GetSize(), 39);
117 }
118
HWTEST_F_L0(JSMapTest,Iterator)119 HWTEST_F_L0(JSMapTest, Iterator)
120 {
121 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
122
123 JSHandle<JSMap> map(thread, CreateMap());
124 for (int i = 0; i < 5; i++) {
125 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(i));
126 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i + 10));
127 JSMap::Set(thread, map, key, value);
128 }
129
130 JSHandle<JSTaggedValue> keyIter(factory->NewJSMapIterator(map, IterationKind::KEY));
131 JSHandle<JSTaggedValue> valueIter(factory->NewJSMapIterator(map, IterationKind::VALUE));
132 JSHandle<JSTaggedValue> iter(factory->NewJSMapIterator(map, IterationKind::KEY_AND_VALUE));
133
134 JSHandle<JSTaggedValue> indexKey(thread, JSTaggedValue(0));
135 JSHandle<JSTaggedValue> elementKey(thread, JSTaggedValue(1));
136
137 JSHandle<JSTaggedValue> keyResult0 = JSIterator::IteratorStep(thread, keyIter);
138 JSHandle<JSTaggedValue> valueResult0 = JSIterator::IteratorStep(thread, valueIter);
139 JSHandle<JSTaggedValue> result0 = JSIterator::IteratorStep(thread, iter);
140
141 EXPECT_EQ(0, JSIterator::IteratorValue(thread, keyResult0)->GetInt());
142 EXPECT_EQ(10, JSIterator::IteratorValue(thread, valueResult0)->GetInt());
143 JSHandle<JSTaggedValue> result0Handle = JSIterator::IteratorValue(thread, result0);
144 EXPECT_EQ(0, JSObject::GetProperty(thread, result0Handle, indexKey).GetValue()->GetInt());
145 EXPECT_EQ(10, JSObject::GetProperty(thread, result0Handle, elementKey).GetValue()->GetInt());
146
147 JSHandle<JSTaggedValue> keyResult1 = JSIterator::IteratorStep(thread, keyIter);
148 EXPECT_EQ(1, JSIterator::IteratorValue(thread, keyResult1)->GetInt());
149 for (int i = 0; i < 3; i++) {
150 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(i));
151 JSMap::Delete(thread, map, key);
152 }
153 JSHandle<JSTaggedValue> keyResult2 = JSIterator::IteratorStep(thread, keyIter);
154 EXPECT_EQ(3, JSIterator::IteratorValue(thread, keyResult2)->GetInt());
155 JSHandle<JSTaggedValue> keyResult3 = JSIterator::IteratorStep(thread, keyIter);
156 EXPECT_EQ(4, JSIterator::IteratorValue(thread, keyResult3)->GetInt());
157 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(5));
158 JSMap::Set(thread, map, key, key);
159 JSHandle<JSTaggedValue> keyResult4 = JSIterator::IteratorStep(thread, keyIter);
160
161 EXPECT_EQ(5, JSIterator::IteratorValue(thread, keyResult4)->GetInt());
162 JSHandle<JSTaggedValue> keyResult5 = JSIterator::IteratorStep(thread, keyIter);
163 EXPECT_EQ(JSTaggedValue::False(), keyResult5.GetTaggedValue());
164 }
165 } // namespace panda::test
166