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 "ecmascript/containers/containers_private.h"
17 #include "ecmascript/ecma_string.h"
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_api/js_api_tree_map.h"
21 #include "ecmascript/js_api/js_api_tree_map_iterator.h"
22 #include "ecmascript/js_function.h"
23 #include "ecmascript/js_handle.h"
24 #include "ecmascript/js_iterator.h"
25 #include "ecmascript/js_object-inl.h"
26 #include "ecmascript/js_tagged_value.h"
27 #include "ecmascript/object_factory.h"
28 #include "ecmascript/tagged_tree.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 JSAPITreeMapTest : 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:
CreateTreeMap()63 JSAPITreeMap *CreateTreeMap()
64 {
65 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
66 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
67
68 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
69 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
70 JSHandle<JSTaggedValue> value =
71 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
72
73 auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
74 objCallInfo->SetFunction(JSTaggedValue::Undefined());
75 objCallInfo->SetThis(value.GetTaggedValue());
76 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::TreeMap)));
77
78 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
79 JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
80 TestHelper::TearDownFrame(thread, prev);
81
82 JSHandle<JSTaggedValue> constructor(thread, result);
83 JSHandle<JSAPITreeMap> map(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
84 JSTaggedValue internal = TaggedTreeMap::Create(thread);
85 map->SetTreeMap(thread, internal);
86 return *map;
87 }
88 };
89
HWTEST_F_L0(JSAPITreeMapTest,TreeMapCreate)90 HWTEST_F_L0(JSAPITreeMapTest, TreeMapCreate)
91 {
92 JSAPITreeMap *map = CreateTreeMap();
93 EXPECT_TRUE(map != nullptr);
94 }
95
HWTEST_F_L0(JSAPITreeMapTest,TreeMapSetAndGet)96 HWTEST_F_L0(JSAPITreeMapTest, TreeMapSetAndGet)
97 {
98 constexpr int NODE_NUMBERS = 8;
99 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
100 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
101 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
102
103 // test JSAPITreeMap
104 JSHandle<JSAPITreeMap> tmap(thread, CreateTreeMap());
105 std::string myKey("mykey");
106 std::string myValue("myvalue");
107 for (int i = 0; i < NODE_NUMBERS; i++) {
108 std::string ikey = myKey + std::to_string(i);
109 std::string ivalue = myValue + std::to_string(i);
110 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
111 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
112 JSAPITreeMap::Set(thread, tmap, key, value);
113 }
114 EXPECT_EQ(tmap->GetSize(), NODE_NUMBERS);
115
116 // test Set exception
117 key.Update(JSTaggedValue::Hole());
118 JSAPITreeMap::Set(thread, tmap, key, value);
119 EXPECT_EXCEPTION();
120
121 for (int i = 0; i < NODE_NUMBERS; i++) {
122 std::string ikey = myKey + std::to_string(i);
123 std::string ivalue = myValue + std::to_string(i);
124 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
125 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
126
127 // test get
128 JSTaggedValue gvalue = JSAPITreeMap::Get(thread, tmap, key);
129 EXPECT_EQ(gvalue, value.GetTaggedValue());
130 }
131 }
132
HWTEST_F_L0(JSAPITreeMapTest,TreeMapDeleteAndHas)133 HWTEST_F_L0(JSAPITreeMapTest, TreeMapDeleteAndHas)
134 {
135 constexpr int NODE_NUMBERS = 64;
136 constexpr int REMOVE_SIZE = 48;
137 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
138 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
139 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
140
141 // test JSAPITreeMap
142 JSHandle<JSAPITreeMap> tmap(thread, CreateTreeMap());
143 std::string myKey("mykey");
144 std::string myValue("myvalue");
145 for (int i = 0; i < NODE_NUMBERS; i++) {
146 std::string ikey = myKey + std::to_string(i);
147 std::string ivalue = myValue + std::to_string(i);
148 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
149 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
150 JSAPITreeMap::Set(thread, tmap, key, value);
151 }
152 EXPECT_EQ(tmap->GetSize(), NODE_NUMBERS);
153
154 for (int i = 0; i < REMOVE_SIZE; i++) {
155 std::string ikey = myKey + std::to_string(i);
156 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
157 [[maybe_unused]] JSTaggedValue dvalue = JSAPITreeMap::Delete(thread, tmap, key);
158 }
159 EXPECT_EQ(tmap->GetSize(), NODE_NUMBERS - REMOVE_SIZE);
160
161 for (int i = 0; i < REMOVE_SIZE; i++) {
162 std::string ikey = myKey + std::to_string(i);
163 std::string ivalue = myValue + std::to_string(i);
164 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
165 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
166
167 // test has
168 bool hasKey = JSAPITreeMap::HasKey(thread, tmap, key);
169 EXPECT_EQ(hasKey, false);
170 bool hasValue = tmap->HasValue(thread, value);
171 EXPECT_EQ(hasValue, false);
172 }
173
174 for (int i = REMOVE_SIZE; i < NODE_NUMBERS; i++) {
175 std::string ikey = myKey + std::to_string(i);
176 std::string ivalue = myValue + std::to_string(i);
177 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
178 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
179
180 // test has
181 bool hasKey = JSAPITreeMap::HasKey(thread, tmap, key);
182 EXPECT_EQ(hasKey, true);
183 bool hasValue = tmap->HasValue(thread, value);
184 EXPECT_EQ(hasValue, true);
185 }
186 }
187
HWTEST_F_L0(JSAPITreeMapTest,TreeMapReplaceAndClear)188 HWTEST_F_L0(JSAPITreeMapTest, TreeMapReplaceAndClear)
189 {
190 constexpr int NODE_NUMBERS = 8;
191 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
192 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
193 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
194
195 // test TaggedTreeMap
196 JSHandle<JSAPITreeMap> tmap(thread, CreateTreeMap());
197 std::string myKey("mykey");
198 std::string myValue("myvalue");
199 for (int i = 0; i < NODE_NUMBERS; i++) {
200 std::string ikey = myKey + std::to_string(i);
201 std::string ivalue = myValue + std::to_string(i);
202 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
203 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
204 JSAPITreeMap::Set(thread, tmap, key, value);
205 }
206 EXPECT_EQ(tmap->GetSize(), NODE_NUMBERS);
207
208 for (int i = 0; i < NODE_NUMBERS / 2; i++) {
209 std::string ikey = myKey + std::to_string(i);
210 std::string ivalue = myValue + std::to_string(i + 1);
211 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
212 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
213
214 // test replace
215 bool success = JSAPITreeMap::Replace(thread, tmap, key, value);
216 EXPECT_EQ(success, true);
217 }
218
219 {
220 std::string ikey = myKey + std::to_string(NODE_NUMBERS);
221 std::string ivalue = myValue + std::to_string(NODE_NUMBERS + 1);
222 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
223 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
224 bool success = JSAPITreeMap::Replace(thread, tmap, key, value);
225 EXPECT_FALSE(success);
226 }
227
228 for (int i = 0; i < NODE_NUMBERS / 2; i++) {
229 std::string ikey = myKey + std::to_string(i);
230 std::string ivalue = myValue + std::to_string(i + 1);
231 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
232 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
233
234 // test get
235 JSTaggedValue gvalue = JSAPITreeMap::Get(thread, tmap, key);
236 EXPECT_EQ(gvalue, value.GetTaggedValue());
237 }
238
239 for (int i = NODE_NUMBERS / 2; i < NODE_NUMBERS; i++) {
240 std::string ikey = myKey + std::to_string(i);
241 std::string ivalue = myValue + std::to_string(i);
242 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
243 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
244
245 // test get
246 JSTaggedValue gvalue = JSAPITreeMap::Get(thread, tmap, key);
247 EXPECT_EQ(gvalue, value.GetTaggedValue());
248 }
249
250 for (int i = 0; i < NODE_NUMBERS / 2; i++) {
251 std::string ikey = myKey + std::to_string(i);
252 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
253 [[maybe_unused]] JSTaggedValue dvalue = JSAPITreeMap::Delete(thread, tmap, key);
254 }
255
256 JSAPITreeMap::Clear(thread, tmap);
257 EXPECT_EQ(tmap->GetSize(), 0);
258 for (int i = 0; i < NODE_NUMBERS; i++) {
259 std::string ikey = myKey + std::to_string(i);
260 std::string ivalue = myValue + std::to_string(i);
261 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
262 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
263
264 // test get
265 JSTaggedValue gvalue = JSAPITreeMap::Get(thread, tmap, key);
266 EXPECT_EQ(gvalue, JSTaggedValue::Undefined());
267
268 // test has
269 bool hasKey = JSAPITreeMap::HasKey(thread, tmap, key);
270 EXPECT_EQ(hasKey, false);
271 bool hasValue = tmap->HasValue(thread, value);
272 EXPECT_EQ(hasValue, false);
273 }
274 }
275
HWTEST_F_L0(JSAPITreeMapTest,JSAPITreeMapIterator)276 HWTEST_F_L0(JSAPITreeMapTest, JSAPITreeMapIterator)
277 {
278 constexpr int NODE_NUMBERS = 8;
279 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
280 JSHandle<JSAPITreeMap> tmap(thread, CreateTreeMap());
281
282 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
283 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
284 for (int i = 0; i < NODE_NUMBERS; i++) {
285 key.Update(JSTaggedValue(i));
286 value.Update(JSTaggedValue(i + 1));
287 JSAPITreeMap::Set(thread, tmap, key, value);
288 }
289
290 // test key or value
291 JSHandle<JSTaggedValue> keyIter(factory->NewJSAPITreeMapIterator(tmap, IterationKind::KEY));
292 JSHandle<JSTaggedValue> valueIter(factory->NewJSAPITreeMapIterator(tmap, IterationKind::VALUE));
293 JSMutableHandle<JSTaggedValue> keyIterResult(thread, JSTaggedValue::Undefined());
294 JSMutableHandle<JSTaggedValue> valueIterResult(thread, JSTaggedValue::Undefined());
295 for (int i = 0; i < NODE_NUMBERS / 2; i++) {
296 keyIterResult.Update(JSIterator::IteratorStep(thread, keyIter).GetTaggedValue());
297 valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue());
298 EXPECT_EQ(i, JSIterator::IteratorValue(thread, keyIterResult)->GetInt());
299 EXPECT_EQ(i + 1, JSIterator::IteratorValue(thread, valueIterResult)->GetInt());
300 }
301
302 // test key and value
303 JSHandle<JSTaggedValue> indexKey(thread, JSTaggedValue(0));
304 JSHandle<JSTaggedValue> elementKey(thread, JSTaggedValue(1));
305 JSHandle<JSTaggedValue> iter(factory->NewJSAPITreeMapIterator(tmap, IterationKind::KEY_AND_VALUE));
306 JSMutableHandle<JSTaggedValue> iterResult(thread, JSTaggedValue::Undefined());
307 JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
308 for (int i = 0; i < NODE_NUMBERS; i++) {
309 iterResult.Update(JSIterator::IteratorStep(thread, iter).GetTaggedValue());
310 result.Update(JSIterator::IteratorValue(thread, iterResult).GetTaggedValue());
311 EXPECT_EQ(i, JSObject::GetProperty(thread, result, indexKey).GetValue()->GetInt());
312 EXPECT_EQ(i + 1, JSObject::GetProperty(thread, result, elementKey).GetValue()->GetInt());
313 }
314
315 // test delete
316 key.Update(JSTaggedValue(NODE_NUMBERS / 2));
317 JSTaggedValue dvalue = JSAPITreeMap::Delete(thread, tmap, key);
318 EXPECT_EQ(dvalue, JSTaggedValue(NODE_NUMBERS / 2 + 1));
319 for (int i = NODE_NUMBERS / 2 + 1; i < NODE_NUMBERS; i++) {
320 keyIterResult.Update(JSIterator::IteratorStep(thread, keyIter).GetTaggedValue());
321 valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue());
322 EXPECT_EQ(i, JSIterator::IteratorValue(thread, keyIterResult)->GetInt());
323 EXPECT_EQ(i + 1, JSIterator::IteratorValue(thread, valueIterResult)->GetInt());
324 }
325
326 // test set
327 key.Update(JSTaggedValue(NODE_NUMBERS));
328 JSAPITreeMap::Set(thread, tmap, key, key);
329 keyIterResult.Update(JSIterator::IteratorStep(thread, keyIter).GetTaggedValue());
330 EXPECT_EQ(NODE_NUMBERS, JSIterator::IteratorValue(thread, keyIterResult)->GetInt());
331
332 // test end
333 keyIterResult.Update(JSIterator::IteratorStep(thread, keyIter).GetTaggedValue());
334 EXPECT_EQ(JSTaggedValue::False(), keyIterResult.GetTaggedValue());
335 }
336
HWTEST_F_L0(JSAPITreeMapTest,TreeMapGetKeyAndGetValue)337 HWTEST_F_L0(JSAPITreeMapTest, TreeMapGetKeyAndGetValue)
338 {
339 constexpr int NODE_NUMBERS = 8;
340 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
341 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
342
343 // init treemap
344 JSHandle<JSAPITreeMap> tmap(thread, CreateTreeMap());
345 for (int i = 0; i < NODE_NUMBERS; i++) {
346 key.Update(JSTaggedValue(i));
347 if (i == NODE_NUMBERS / 2) {
348 value.Update(JSTaggedValue::Hole());
349 } else {
350 value.Update(JSTaggedValue(i));
351 }
352 JSAPITreeMap::Set(thread, tmap, key, value);
353 }
354 EXPECT_EQ(tmap->GetSize(), NODE_NUMBERS);
355
356 // test GetKey and GetValue
357 for (int i = 0; i < NODE_NUMBERS; i++) {
358 EXPECT_EQ(tmap->GetKey(i), JSTaggedValue(i));
359 if (i == NODE_NUMBERS / 2) {
360 EXPECT_EQ(tmap->GetValue(i), JSTaggedValue::Undefined());
361 } else {
362 EXPECT_EQ(tmap->GetValue(i), JSTaggedValue(i));
363 }
364 }
365 EXPECT_EQ(tmap->GetKey(-1), JSTaggedValue::Undefined());
366 }
367 } // namespace panda::test
368