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_function.h"
21 #include "ecmascript/js_handle.h"
22 #include "ecmascript/js_iterator.h"
23 #include "ecmascript/js_api/js_api_hashmap.h"
24 #include "ecmascript/js_api/js_api_hashmap_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/tests/test_helper.h"
29
30 using namespace panda;
31 using namespace panda::ecmascript;
32
33 namespace panda::test {
34 class JSAPIHashMapTest : public testing::Test {
35 public:
SetUpTestCase()36 static void SetUpTestCase()
37 {
38 GTEST_LOG_(INFO) << "SetUpTestCase";
39 }
40
TearDownTestCase()41 static void TearDownTestCase()
42 {
43 GTEST_LOG_(INFO) << "TearDownCase";
44 }
45
SetUp()46 void SetUp() override
47 {
48 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
49 }
50
TearDown()51 void TearDown() override
52 {
53 TestHelper::DestroyEcmaVMWithScope(instance, scope);
54 }
55
56 EcmaVM *instance {nullptr};
57 ecmascript::EcmaHandleScope *scope {nullptr};
58 JSThread *thread {nullptr};
59
60 protected:
CreateHashMap()61 JSAPIHashMap *CreateHashMap()
62 {
63 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
64 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
65
66 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
67 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
68 JSHandle<JSTaggedValue> value =
69 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
70
71 auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
72 objCallInfo->SetFunction(JSTaggedValue::Undefined());
73 objCallInfo->SetThis(value.GetTaggedValue());
74 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::HashMap)));
75
76 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
77 JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
78 TestHelper::TearDownFrame(thread, prev);
79
80 JSHandle<JSTaggedValue> constructor(thread, result);
81 JSHandle<JSAPIHashMap> map(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
82 JSTaggedValue hashMapArray = TaggedHashArray::Create(thread);
83 map->SetTable(thread, hashMapArray);
84 map->SetSize(0);
85 return *map;
86 }
87 };
88
HWTEST_F_L0(JSAPIHashMapTest,HashMapCreate)89 HWTEST_F_L0(JSAPIHashMapTest, HashMapCreate)
90 {
91 JSAPIHashMap *map = CreateHashMap();
92 EXPECT_TRUE(map != nullptr);
93 }
94
HWTEST_F_L0(JSAPIHashMapTest,HashMapSetAndGet)95 HWTEST_F_L0(JSAPIHashMapTest, HashMapSetAndGet)
96 {
97 constexpr uint32_t NODE_NUMBERS = 8;
98 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
99 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
100 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
101
102 // test JSAPIHashMap
103 JSHandle<JSAPIHashMap> hashMap(thread, CreateHashMap());
104
105 // test IsEmpty
106 EXPECT_EQ(hashMap->IsEmpty(), JSTaggedValue::True());
107
108 // test Set exception
109 key.Update(JSTaggedValue::Undefined());
110 JSAPIHashMap::Set(thread, hashMap, key, value);
111 EXPECT_EXCEPTION();
112
113 std::string myKey("mykey");
114 std::string myValue("myvalue");
115 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
116 std::string iKey = myKey + std::to_string(i);
117 std::string iValue = myValue + std::to_string(i);
118 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
119 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
120 JSAPIHashMap::Set(thread, hashMap, key, value);
121 }
122 EXPECT_EQ(hashMap->GetSize(), NODE_NUMBERS);
123
124 // test isEmpty
125 EXPECT_EQ(hashMap->IsEmpty(), JSTaggedValue::False());
126
127 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
128 std::string iKey = myKey + std::to_string(i);
129 std::string iValue = myValue + std::to_string(i);
130 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
131 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
132
133 // test get
134 JSTaggedValue gValue = hashMap->Get(thread, key.GetTaggedValue());
135 EXPECT_EQ(gValue, value.GetTaggedValue());
136 }
137 }
138
HWTEST_F_L0(JSAPIHashMapTest,HashMapRemoveAndHas)139 HWTEST_F_L0(JSAPIHashMapTest, HashMapRemoveAndHas)
140 {
141 constexpr uint32_t NODE_NUMBERS = 8;
142 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
143 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
144 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
145
146 // test JSAPIHashMap
147 JSHandle<JSAPIHashMap> hashMap(thread, CreateHashMap());
148
149 // test Remove Hole
150 JSTaggedValue undefined = JSAPIHashMap::Remove(thread, hashMap, JSTaggedValue::Hole());
151 EXPECT_EQ(undefined, JSTaggedValue::Undefined());
152
153 // test Remove empty hashmap
154 JSTaggedValue undefined1 = JSAPIHashMap::Remove(thread, hashMap, JSTaggedValue(0));
155 EXPECT_EQ(undefined1, JSTaggedValue::Undefined());
156
157 std::string myKey("mykey");
158 std::string myValue("myvalue");
159 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
160 std::string iKey = myKey + std::to_string(i);
161 std::string iValue = myValue + std::to_string(i);
162 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
163 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
164 JSAPIHashMap::Set(thread, hashMap, key, value);
165 }
166 EXPECT_EQ(hashMap->GetSize(), NODE_NUMBERS);
167
168 // test Remove non-existent
169 JSTaggedValue undefined2 = JSAPIHashMap::Remove(thread, hashMap, JSTaggedValue(0));
170 EXPECT_EQ(undefined2, JSTaggedValue::Undefined());
171
172 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) {
173 std::string iKey = myKey + std::to_string(i);
174 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
175 [[maybe_unused]] JSTaggedValue rValue = JSAPIHashMap::Remove(thread, hashMap, key.GetTaggedValue());
176 }
177 EXPECT_EQ(hashMap->GetSize(), NODE_NUMBERS / 2);
178
179 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) {
180 std::string iKey = myKey + std::to_string(i);
181 std::string iValue = myValue + std::to_string(i);
182 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
183 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
184
185 // test has
186 JSTaggedValue hasKey = hashMap->HasKey(thread, key.GetTaggedValue());
187 EXPECT_EQ(hasKey, JSTaggedValue::False());
188 JSTaggedValue hasValue = JSAPIHashMap::HasValue(thread, hashMap, value);
189 EXPECT_EQ(hasValue, JSTaggedValue::False());
190 }
191
192 for (uint32_t i = NODE_NUMBERS / 2; i < NODE_NUMBERS; i++) {
193 std::string iKey = myKey + std::to_string(i);
194 std::string iValue = myValue + std::to_string(i);
195 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
196 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
197
198 // test has
199 JSTaggedValue hasKey = hashMap->HasKey(thread, key.GetTaggedValue());
200 EXPECT_EQ(hasKey, JSTaggedValue::True());
201 JSTaggedValue hasValue = JSAPIHashMap::HasValue(thread, hashMap, value);
202 EXPECT_EQ(hasValue, JSTaggedValue::True());
203 }
204 }
205
HWTEST_F_L0(JSAPIHashMapTest,HashMapReplaceAndClear)206 HWTEST_F_L0(JSAPIHashMapTest, HashMapReplaceAndClear)
207 {
208 constexpr uint32_t NODE_NUMBERS = 8;
209 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
210 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
211 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
212 // test TaggedHashMap
213 JSHandle<JSAPIHashMap> hashMap(thread, CreateHashMap());
214 std::string myKey("mykey");
215 std::string myValue("myvalue");
216 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
217 std::string iKey = myKey + std::to_string(i);
218 std::string iValue = myValue + std::to_string(i);
219 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
220 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
221 JSAPIHashMap::Set(thread, hashMap, key, value);
222 }
223 EXPECT_EQ(hashMap->GetSize(), NODE_NUMBERS);
224 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) {
225 std::string iKey = myKey + std::to_string(i);
226 std::string iValue = myValue + std::to_string(i + 1);
227 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
228 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
229 // test replace
230 JSTaggedValue success = hashMap->Replace(thread, key.GetTaggedValue(), value.GetTaggedValue());
231 EXPECT_EQ(success, JSTaggedValue::True());
232 }
233 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) {
234 std::string iKey = myKey + std::to_string(i);
235 std::string iValue = myValue + std::to_string(i + 1);
236 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
237 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
238 // test get
239 JSTaggedValue gValue = hashMap->Get(thread, key.GetTaggedValue());
240 EXPECT_EQ(gValue, value.GetTaggedValue());
241 }
242 for (uint32_t i = NODE_NUMBERS / 2; i < NODE_NUMBERS; i++) {
243 std::string iKey = myKey + std::to_string(i);
244 std::string iValue = myValue + std::to_string(i);
245 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
246 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
247 // test get
248 JSTaggedValue gValue = hashMap->Get(thread, key.GetTaggedValue());
249 EXPECT_EQ(gValue, value.GetTaggedValue());
250 }
251 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) {
252 std::string iKey = myKey + std::to_string(i);
253 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
254 [[maybe_unused]] JSTaggedValue rValue = JSAPIHashMap::Remove(thread, hashMap, key.GetTaggedValue());
255 }
256 hashMap->Clear(thread);
257 EXPECT_EQ(hashMap->GetSize(), (uint32_t)0);
258 for (uint32_t 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 // test get
264 JSTaggedValue gValue = hashMap->Get(thread, key.GetTaggedValue());
265 EXPECT_EQ(gValue, JSTaggedValue::Undefined());
266 // test has
267 JSTaggedValue hasKey = hashMap->HasKey(thread, key.GetTaggedValue());
268 EXPECT_EQ(hasKey, JSTaggedValue::False());
269 JSTaggedValue hasValue = JSAPIHashMap::HasValue(thread, hashMap, value);
270 EXPECT_EQ(hasValue, JSTaggedValue::False());
271 }
272 }
273
HWTEST_F_L0(JSAPIHashMapTest,JSAPIHashMapIterator)274 HWTEST_F_L0(JSAPIHashMapTest, JSAPIHashMapIterator)
275 {
276 constexpr uint32_t NODE_NUMBERS = 8;
277 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
278 JSHandle<JSAPIHashMap> hashMap(thread, CreateHashMap());
279 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
280 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
281 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
282 key.Update(JSTaggedValue(i));
283 value.Update(JSTaggedValue(i));
284 JSAPIHashMap::Set(thread, hashMap, key, value);
285 }
286 // test key or value
287 JSHandle<JSTaggedValue> keyIter(factory->NewJSAPIHashMapIterator(hashMap, IterationKind::KEY));
288 JSHandle<JSTaggedValue> valueIter(factory->NewJSAPIHashMapIterator(hashMap, IterationKind::VALUE));
289 JSMutableHandle<JSTaggedValue> keyIterResult(thread, JSTaggedValue::Undefined());
290 JSMutableHandle<JSTaggedValue> valueIterResult(thread, JSTaggedValue::Undefined());
291 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) {
292 keyIterResult.Update(JSIterator::IteratorStep(thread, keyIter).GetTaggedValue());
293 valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue());
294 JSHandle<JSTaggedValue> tmpIterKey = JSIterator::IteratorValue(thread, keyIterResult);
295 JSTaggedValue iterKeyFlag = hashMap->HasKey(thread, tmpIterKey.GetTaggedValue());
296 EXPECT_EQ(JSTaggedValue::True(), iterKeyFlag);
297 JSHandle<JSTaggedValue> tmpIterValue = JSIterator::IteratorValue(thread, valueIterResult);
298 JSTaggedValue iterValueFlag = JSAPIHashMap::HasValue(thread, hashMap, tmpIterValue);
299 EXPECT_EQ(JSTaggedValue::True(), iterValueFlag);
300 }
301 // test key and value
302 JSHandle<JSTaggedValue> indexKey(thread, JSTaggedValue(0));
303 JSHandle<JSTaggedValue> elementKey(thread, JSTaggedValue(1));
304 JSHandle<JSTaggedValue> iter(factory->NewJSAPIHashMapIterator(hashMap, IterationKind::KEY_AND_VALUE));
305 JSMutableHandle<JSTaggedValue> iterResult(thread, JSTaggedValue::Undefined());
306 JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
307 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
308 iterResult.Update(JSIterator::IteratorStep(thread, iter).GetTaggedValue());
309 result.Update(JSIterator::IteratorValue(thread, iterResult).GetTaggedValue());
310 JSHandle<JSTaggedValue> tmpKey = JSObject::GetProperty(thread, result, indexKey).GetValue();
311 JSTaggedValue iterKeyFlag = hashMap->HasKey(thread, tmpKey.GetTaggedValue());
312 EXPECT_EQ(JSTaggedValue::True(), iterKeyFlag);
313 JSHandle<JSTaggedValue> tmpValue = JSObject::GetProperty(thread, result, elementKey).GetValue();
314 JSTaggedValue iterValueFlag = JSAPIHashMap::HasValue(thread, hashMap, tmpValue);
315 EXPECT_EQ(JSTaggedValue::True(), iterValueFlag);
316 }
317 // test delete
318 key.Update(JSTaggedValue(NODE_NUMBERS / 2));
319 JSTaggedValue rValue = JSAPIHashMap::Remove(thread, hashMap, key.GetTaggedValue());
320 EXPECT_EQ(rValue, JSTaggedValue(NODE_NUMBERS / 2));
321 for (uint32_t i = NODE_NUMBERS / 2 + 1; i < NODE_NUMBERS; i++) {
322 keyIterResult.Update(JSIterator::IteratorStep(thread, keyIter).GetTaggedValue());
323 valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue());
324 JSHandle<JSTaggedValue> tmpIterKey = JSIterator::IteratorValue(thread, keyIterResult);
325 JSTaggedValue iterKeyFlag = hashMap->HasKey(thread, tmpIterKey.GetTaggedValue());
326 EXPECT_EQ(JSTaggedValue::True(), iterKeyFlag);
327 JSHandle<JSTaggedValue> tmpIterValue = JSIterator::IteratorValue(thread, valueIterResult);
328 JSTaggedValue iterValueFlag = JSAPIHashMap::HasValue(thread, hashMap, tmpIterValue);
329 EXPECT_EQ(JSTaggedValue::True(), iterValueFlag);
330 }
331 // test set
332 key.Update(JSTaggedValue(NODE_NUMBERS));
333 JSAPIHashMap::Set(thread, hashMap, key, key);
334 keyIterResult.Update(JSIterator::IteratorStep(thread, keyIter).GetTaggedValue());
335 JSHandle<JSTaggedValue> tmpIterKey = JSIterator::IteratorValue(thread, keyIterResult);
336 JSTaggedValue iterKeyFlag = hashMap->HasKey(thread, tmpIterKey.GetTaggedValue());
337 EXPECT_EQ(JSTaggedValue::True(), iterKeyFlag);
338 EXPECT_EQ(hashMap->GetSize(), NODE_NUMBERS);
339 keyIterResult.Update(JSIterator::IteratorStep(thread, keyIter).GetTaggedValue());
340 EXPECT_EQ(JSTaggedValue::False(), keyIterResult.GetTaggedValue());
341 }
342
HWTEST_F_L0(JSAPIHashMapTest,JSAPIHashMapIteratorRBTreeTest)343 HWTEST_F_L0(JSAPIHashMapTest, JSAPIHashMapIteratorRBTreeTest)
344 {
345 constexpr uint32_t NODE_NUMBERS = 11;
346 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
347 JSHandle<JSAPIHashMap> hashMap(thread, CreateHashMap());
348 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
349 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
350 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
351 std::vector<int> hashCollisionVector = {1224, 1285, 1463, 4307, 5135, 5903, 6603, 6780, 8416, 9401, 9740};
352
353 for (size_t i = 0; i < hashCollisionVector.size(); i++) {
354 key.Update(JSTaggedValue(hashCollisionVector[i]));
355 value.Update(JSTaggedValue(hashCollisionVector[i]));
356 JSAPIHashMap::Set(thread, hashMap, key, value);
357 }
358
359 JSHandle<JSAPIHashMapIterator> hashmapIterator = factory->NewJSAPIHashMapIterator(hashMap, IterationKind::VALUE);
360 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
361 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
362 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
363 ecmaRuntimeCallInfo->SetThis(hashmapIterator.GetTaggedValue());
364
365 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
366 JSTaggedValue result = JSAPIHashMapIterator::Next(ecmaRuntimeCallInfo);
367 TestHelper::TearDownFrame(thread, prev);
368
369 JSHandle<JSObject> resultObj(thread, result);
370 if (i <= NODE_NUMBERS - 1U) {
371 EXPECT_TRUE(JSObject::GetProperty(thread, resultObj, valueStr).GetValue()->IsInt());
372 }
373 }
374 }
375
HWTEST_F_L0(JSAPIHashMapTest,JSAPIHashMapRBTreeHasValueReplaceGet)376 HWTEST_F_L0(JSAPIHashMapTest, JSAPIHashMapRBTreeHasValueReplaceGet)
377 {
378 std::vector<int> hashCollisionVector = {1224, 1285, 1463, 4307, 5135, 5903, 6780, 8416, 9401, 9740, 6603};
379 uint32_t NODE_NUMBERS = static_cast<uint32_t>(hashCollisionVector.size());
380 JSHandle<JSAPIHashMap> hashMap(thread, CreateHashMap());
381 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
382 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
383
384 for (uint32_t i = 0; i < NODE_NUMBERS - 1; i++) {
385 key.Update(JSTaggedValue(hashCollisionVector[i]));
386 value.Update(JSTaggedValue(hashCollisionVector[i]));
387 JSAPIHashMap::Set(thread, hashMap, key, value);
388 }
389
390 // test RBTree HasValue
391 for (uint32_t i = 0; i < NODE_NUMBERS - 1; i++) {
392 value.Update(JSTaggedValue(hashCollisionVector[i]));
393 JSTaggedValue hasValue = JSAPIHashMap::HasValue(thread, hashMap, value);
394 EXPECT_EQ(hasValue, JSTaggedValue::True());
395 }
396 value.Update(JSTaggedValue(hashCollisionVector[NODE_NUMBERS - 1]));
397 JSTaggedValue hasValue = JSAPIHashMap::HasValue(thread, hashMap, value);
398 EXPECT_EQ(hasValue, JSTaggedValue::False());
399
400 // test RBTree Replace and Get
401 for (uint32_t i = 0; i < NODE_NUMBERS - 1; i++) {
402 JSTaggedValue replaceResult = hashMap->Replace(
403 thread, JSTaggedValue(hashCollisionVector[i]), JSTaggedValue(hashCollisionVector[i] * 2));
404 EXPECT_EQ(replaceResult, JSTaggedValue::True());
405 }
406 for (uint32_t i = 0; i < NODE_NUMBERS - 1; i++) {
407 JSTaggedValue replaceResult = hashMap->Get(
408 thread, JSTaggedValue(hashCollisionVector[i]));
409 EXPECT_EQ(replaceResult, JSTaggedValue(hashCollisionVector[i] * 2));
410 }
411 }
412
HWTEST_F_L0(JSAPIHashMapTest,JSAPIHashMapRBTreeSetAllRemove)413 HWTEST_F_L0(JSAPIHashMapTest, JSAPIHashMapRBTreeSetAllRemove)
414 {
415 std::vector<int> hashCollisionVector = {1224, 1285, 1463, 4307, 5135, 5903, 6780, 8416, 9401, 9740, 6603};
416 uint32_t NODE_NUMBERS = static_cast<uint32_t>(hashCollisionVector.size());
417 uint32_t REMOVE_NUMBERS = 4;
418 JSHandle<JSAPIHashMap> dstHashMap(thread, CreateHashMap());
419 JSHandle<JSAPIHashMap> srcHashMap(thread, CreateHashMap());
420 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
421 JSMutableHandle<JSTaggedValue> dstValue(thread, JSTaggedValue::Undefined());
422 JSMutableHandle<JSTaggedValue> srcValue(thread, JSTaggedValue::Undefined());
423
424 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
425 key.Update(JSTaggedValue(hashCollisionVector[i]));
426 dstValue.Update(JSTaggedValue(hashCollisionVector[i]));
427 srcValue.Update(JSTaggedValue(hashCollisionVector[i] * 2));
428 JSAPIHashMap::Set(thread, dstHashMap, key, dstValue);
429 JSAPIHashMap::Set(thread, srcHashMap, key, srcValue);
430 }
431
432 // test SetAll and Get
433 JSAPIHashMap::SetAll(thread, dstHashMap, srcHashMap);
434 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
435 JSTaggedValue replaceResult = dstHashMap->Get(
436 thread, JSTaggedValue(hashCollisionVector[i]));
437 EXPECT_EQ(replaceResult, JSTaggedValue(hashCollisionVector[i] * 2));
438 }
439
440 // test Remove RBTree
441 for (uint32_t i = 0; i < REMOVE_NUMBERS; i++) {
442 key.Update(JSTaggedValue(hashCollisionVector[i]));
443 JSAPIHashMap::Remove(thread, dstHashMap, key.GetTaggedValue());
444 }
445 EXPECT_EQ(dstHashMap->GetSize(), NODE_NUMBERS - REMOVE_NUMBERS);
446
447 for (uint32_t i = 0; i < REMOVE_NUMBERS; i++) {
448 JSTaggedValue getResult = dstHashMap->Get(thread, JSTaggedValue(hashCollisionVector[i]));
449 EXPECT_EQ(getResult, JSTaggedValue::Undefined());
450 }
451
452 for (uint32_t i = REMOVE_NUMBERS; i < NODE_NUMBERS; i++) {
453 JSTaggedValue getResult = dstHashMap->Get(thread, JSTaggedValue(hashCollisionVector[i]));
454 EXPECT_EQ(getResult, JSTaggedValue(hashCollisionVector[i] * 2));
455 }
456 }
457 } // namespace panda::test
458