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/containers/containers_hashset.h"
18 #include "ecmascript/ecma_runtime_call_info.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_handle.h"
21 #include "ecmascript/js_tagged_value-inl.h"
22 #include "ecmascript/js_thread.h"
23 #include "ecmascript/js_api/js_api_hashset.h"
24 #include "ecmascript/js_api/js_api_hashset_iterator.h"
25 #include "ecmascript/object_factory.h"
26 #include "ecmascript/tests/test_helper.h"
27 #include "ecmascript/containers/tests/containers_test_helper.h"
28
29 using namespace panda::ecmascript;
30 using namespace panda::ecmascript::containers;
31
32 namespace panda::test {
33 class ContainersHashSetTest : public testing::Test {
34 public:
SetUpTestCase()35 static void SetUpTestCase()
36 {
37 GTEST_LOG_(INFO) << "SetUpTestCase";
38 }
39
TearDownTestCase()40 static void TearDownTestCase()
41 {
42 GTEST_LOG_(INFO) << "TearDownCase";
43 }
44
SetUp()45 void SetUp() override
46 {
47 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
48 }
49
TearDown()50 void TearDown() override
51 {
52 TestHelper::DestroyEcmaVMWithScope(instance, scope);
53 }
54
55 EcmaVM *instance {nullptr};
56 EcmaHandleScope *scope {nullptr};
57 JSThread *thread {nullptr};
58 protected:
InitializeHashSetConstructor()59 JSTaggedValue InitializeHashSetConstructor()
60 {
61 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
62 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
63
64 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
65 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
66 JSHandle<JSTaggedValue> value =
67 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
68
69 auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
70 objCallInfo->SetFunction(JSTaggedValue::Undefined());
71 objCallInfo->SetThis(value.GetTaggedValue());
72 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::HashSet)));
73 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
74 JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
75 TestHelper::TearDownFrame(thread, prev);
76 return result;
77 }
78
CreateJSAPIHashSet()79 JSHandle<JSAPIHashSet> CreateJSAPIHashSet()
80 {
81 JSHandle<JSFunction> newTarget(thread, InitializeHashSetConstructor());
82 auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
83 objCallInfo->SetFunction(newTarget.GetTaggedValue());
84 objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
85 objCallInfo->SetThis(JSTaggedValue::Undefined());
86
87 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
88 JSTaggedValue result = ContainersHashSet::HashSetConstructor(objCallInfo);
89 TestHelper::TearDownFrame(thread, prev);
90 JSHandle<JSAPIHashSet> map(thread, result);
91 return map;
92 }
93 };
94
95 // new HashSet()
HWTEST_F_L0(ContainersHashSetTest,HashSetConstructor)96 HWTEST_F_L0(ContainersHashSetTest, HashSetConstructor)
97 {
98 InitializeHashSetConstructor();
99 JSHandle<JSFunction> newTarget(thread, InitializeHashSetConstructor());
100
101 auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
102 objCallInfo->SetFunction(newTarget.GetTaggedValue());
103 objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
104 objCallInfo->SetThis(JSTaggedValue::Undefined());
105
106 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
107 JSTaggedValue result = ContainersHashSet::HashSetConstructor(objCallInfo);
108 TestHelper::TearDownFrame(thread, prev);
109
110 ASSERT_TRUE(result.IsJSAPIHashSet());
111 JSHandle<JSAPIHashSet> setHandle(thread, result);
112 JSTaggedValue resultProto = JSTaggedValue::GetPrototype(thread, JSHandle<JSTaggedValue>(setHandle));
113 JSTaggedValue funcProto = newTarget->GetFunctionPrototype();
114 ASSERT_EQ(resultProto, funcProto);
115 int size = setHandle->GetSize();
116 ASSERT_EQ(size, 0);
117
118 // test HashSetConstructor exception
119 objCallInfo->SetNewTarget(JSTaggedValue::Undefined());
120 CONTAINERS_API_EXCEPTION_TEST(ContainersHashSet, HashSetConstructor, objCallInfo);
121 }
122
123 // hashset.add(key), hashset.has(key)
HWTEST_F_L0(ContainersHashSetTest,AddAndHas)124 HWTEST_F_L0(ContainersHashSetTest, AddAndHas)
125 {
126 constexpr uint32_t NODE_NUMBERS = 8;
127 JSHandle<JSAPIHashSet> tSet = CreateJSAPIHashSet();
128 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
129 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
130 callInfo->SetFunction(JSTaggedValue::Undefined());
131 callInfo->SetThis(tSet.GetTaggedValue());
132 callInfo->SetCallArg(0, JSTaggedValue(i));
133
134 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
135 JSTaggedValue result = ContainersHashSet::Add(callInfo);
136 TestHelper::TearDownFrame(thread, prev);
137 EXPECT_TRUE(result.IsJSAPIHashSet());
138 EXPECT_EQ(JSAPIHashSet::Cast(result.GetTaggedObject())->GetSize(), i + 1);
139 }
140
141 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
142 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
143 callInfo->SetFunction(JSTaggedValue::Undefined());
144 callInfo->SetThis(tSet.GetTaggedValue());
145 callInfo->SetCallArg(0, JSTaggedValue(i));
146
147 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
148 JSTaggedValue result = ContainersHashSet::Has(callInfo);
149 TestHelper::TearDownFrame(thread, prev);
150 EXPECT_EQ(result, JSTaggedValue::True());
151 }
152 }
153
154 // hashset.remove(key)
HWTEST_F_L0(ContainersHashSetTest,Remove)155 HWTEST_F_L0(ContainersHashSetTest, Remove)
156 {
157 constexpr uint32_t NODE_NUMBERS = 8;
158 JSHandle<JSAPIHashSet> tSet = CreateJSAPIHashSet();
159 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
160 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
161 callInfo->SetFunction(JSTaggedValue::Undefined());
162 callInfo->SetThis(tSet.GetTaggedValue());
163 callInfo->SetCallArg(0, JSTaggedValue(i));
164 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
165 JSTaggedValue result = ContainersHashSet::Add(callInfo);
166 TestHelper::TearDownFrame(thread, prev);
167 EXPECT_TRUE(result.IsJSAPIHashSet());
168 EXPECT_EQ(JSAPIHashSet::Cast(result.GetTaggedObject())->GetSize(), i + 1);
169 }
170 {
171 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
172 callInfo->SetFunction(JSTaggedValue::Undefined());
173 callInfo->SetThis(tSet.GetTaggedValue());
174 callInfo->SetCallArg(0, JSTaggedValue(NODE_NUMBERS / 2));
175 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
176 JSTaggedValue rValue = ContainersHashSet::Remove(callInfo);
177 TestHelper::TearDownFrame(thread, prev);
178 EXPECT_EQ(rValue, JSTaggedValue::True());
179 EXPECT_EQ(tSet->GetSize(), NODE_NUMBERS - 1);
180 }
181 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
182 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
183 callInfo->SetFunction(JSTaggedValue::Undefined());
184 callInfo->SetThis(tSet.GetTaggedValue());
185 callInfo->SetCallArg(0, JSTaggedValue(i));
186 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
187 JSTaggedValue result = ContainersHashSet::Has(callInfo);
188 TestHelper::TearDownFrame(thread, prev);
189 if (i == (NODE_NUMBERS / 2)) {
190 EXPECT_EQ(result, JSTaggedValue::False());
191 } else {
192 EXPECT_EQ(result, JSTaggedValue::True());
193 }
194 }
195 // test add string
196 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
197 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
198 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
199 std::string myKey("mykey");
200 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
201 std::string iKey = myKey + std::to_string(i);
202 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
203
204 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
205 callInfo->SetFunction(JSTaggedValue::Undefined());
206 callInfo->SetThis(tSet.GetTaggedValue());
207 callInfo->SetCallArg(0, key.GetTaggedValue());
208 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
209 JSTaggedValue result = ContainersHashSet::Add(callInfo);
210 TestHelper::TearDownFrame(thread, prev);
211 EXPECT_TRUE(result.IsJSAPIHashSet());
212 EXPECT_EQ(JSAPIHashSet::Cast(result.GetTaggedObject())->GetSize(), NODE_NUMBERS + i);
213 }
214 {
215 std::string iKey = myKey + std::to_string(NODE_NUMBERS / 2);
216 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
217 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
218 callInfo->SetFunction(JSTaggedValue::Undefined());
219 callInfo->SetThis(tSet.GetTaggedValue());
220 callInfo->SetCallArg(0, key.GetTaggedValue());
221 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
222 JSTaggedValue rValue = ContainersHashSet::Remove(callInfo);
223 TestHelper::TearDownFrame(thread, prev);
224 EXPECT_TRUE(JSTaggedValue::SameValue(rValue, JSTaggedValue::True()));
225 EXPECT_EQ(tSet->GetSize(), NODE_NUMBERS * 2 - 2);
226 }
227 }
228
229 // hashset.clear()
HWTEST_F_L0(ContainersHashSetTest,Clear)230 HWTEST_F_L0(ContainersHashSetTest, Clear)
231 {
232 constexpr uint32_t NODE_NUMBERS = 8;
233 JSHandle<JSAPIHashSet> tSet = CreateJSAPIHashSet();
234 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
235 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
236 callInfo->SetFunction(JSTaggedValue::Undefined());
237 callInfo->SetThis(tSet.GetTaggedValue());
238 callInfo->SetCallArg(0, JSTaggedValue(i));
239
240 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
241 JSTaggedValue result = ContainersHashSet::Add(callInfo);
242 TestHelper::TearDownFrame(thread, prev);
243 EXPECT_TRUE(result.IsJSAPIHashSet());
244 EXPECT_EQ(JSAPIHashSet::Cast(result.GetTaggedObject())->GetSize(), i + 1);
245 }
246
247 // test clear
248 {
249 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
250 callInfo->SetFunction(JSTaggedValue::Undefined());
251 callInfo->SetThis(tSet.GetTaggedValue());
252
253 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
254 ContainersHashSet::Clear(callInfo);
255 TestHelper::TearDownFrame(thread, prev);
256 EXPECT_EQ(tSet->GetSize(), (uint32_t)0);
257 }
258 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
259 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
260 callInfo->SetFunction(JSTaggedValue::Undefined());
261 callInfo->SetThis(tSet.GetTaggedValue());
262 callInfo->SetCallArg(0, JSTaggedValue(i));
263
264 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
265 JSTaggedValue result = ContainersHashSet::Has(callInfo);
266 TestHelper::TearDownFrame(thread, prev);
267 EXPECT_EQ(result, JSTaggedValue::False());
268 }
269
270 // test add string
271 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
272 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
273 std::string myKey("mykey");
274 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
275 std::string iKey = myKey + std::to_string(i);
276 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
277
278 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
279 callInfo->SetFunction(JSTaggedValue::Undefined());
280 callInfo->SetThis(tSet.GetTaggedValue());
281 callInfo->SetCallArg(0, key.GetTaggedValue());
282 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
283 JSTaggedValue result = ContainersHashSet::Add(callInfo);
284 TestHelper::TearDownFrame(thread, prev);
285 EXPECT_TRUE(result.IsJSAPIHashSet());
286 EXPECT_EQ(JSAPIHashSet::Cast(result.GetTaggedObject())->GetSize(), i + 1);
287 }
288
289 // test clear
290 {
291 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
292 callInfo->SetFunction(JSTaggedValue::Undefined());
293 callInfo->SetThis(tSet.GetTaggedValue());
294
295 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
296 ContainersHashSet::Clear(callInfo);
297 TestHelper::TearDownFrame(thread, prev);
298 EXPECT_EQ(tSet->GetSize(), (uint32_t)0);
299 }
300 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
301 std::string iKey = myKey + std::to_string(i);
302 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
303 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
304 callInfo->SetFunction(JSTaggedValue::Undefined());
305 callInfo->SetThis(tSet.GetTaggedValue());
306 callInfo->SetCallArg(0, key.GetTaggedValue());
307
308 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
309 JSTaggedValue result = ContainersHashSet::Has(callInfo);
310 TestHelper::TearDownFrame(thread, prev);
311 EXPECT_EQ(result, JSTaggedValue::False());
312 }
313 }
314
315 // hashset.values(), hashset.entries()
HWTEST_F_L0(ContainersHashSetTest,KeysAndValuesAndEntries)316 HWTEST_F_L0(ContainersHashSetTest, KeysAndValuesAndEntries)
317 {
318 constexpr uint32_t NODE_NUMBERS = 8;
319 JSHandle<JSAPIHashSet> tSet = CreateJSAPIHashSet();
320 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
321 auto callInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
322 callInfo1->SetFunction(JSTaggedValue::Undefined());
323 callInfo1->SetThis(tSet.GetTaggedValue());
324 callInfo1->SetCallArg(0, JSTaggedValue(i));
325
326 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo1);
327 JSTaggedValue result = ContainersHashSet::Add(callInfo1);
328 TestHelper::TearDownFrame(thread, prev);
329 EXPECT_TRUE(result.IsJSAPIHashSet());
330 EXPECT_EQ(JSAPIHashSet::Cast(result.GetTaggedObject())->GetSize(), i + 1);
331 }
332
333 // test values
334 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
335 callInfo->SetFunction(JSTaggedValue::Undefined());
336 callInfo->SetThis(tSet.GetTaggedValue());
337 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
338 JSHandle<JSTaggedValue> iterValues(thread, ContainersHashSet::Values(callInfo));
339 TestHelper::TearDownFrame(thread, prev);
340 EXPECT_TRUE(iterValues->IsJSAPIHashSetIterator());
341 JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
342 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
343 auto callInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
344 callInfo2->SetFunction(JSTaggedValue::Undefined());
345 callInfo2->SetThis(iterValues.GetTaggedValue());
346
347 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, callInfo2);
348 result.Update(JSAPIHashSetIterator::Next(callInfo2));
349 TestHelper::TearDownFrame(thread, prev1);
350 JSHandle<JSTaggedValue> iterValue = JSIterator::IteratorValue(thread, result);
351 JSTaggedValue valueFlag = tSet->Has(thread, iterValue.GetTaggedValue());
352 EXPECT_EQ(JSTaggedValue::True(), valueFlag);
353 }
354
355 // test add string
356 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
357 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
358 std::string myKey("mykey");
359 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
360 std::string iKey = myKey + std::to_string(i);
361 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
362
363 auto callInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
364 callInfo3->SetFunction(JSTaggedValue::Undefined());
365 callInfo3->SetThis(tSet.GetTaggedValue());
366 callInfo3->SetCallArg(0, key.GetTaggedValue());
367
368 [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, callInfo3);
369 JSTaggedValue result1 = ContainersHashSet::Add(callInfo3);
370 TestHelper::TearDownFrame(thread, prev2);
371 EXPECT_TRUE(result1.IsJSAPIHashSet());
372 EXPECT_EQ(tSet->GetSize(), NODE_NUMBERS + i + 1);
373 }
374 EXPECT_EQ(tSet->GetSize(), NODE_NUMBERS * 2);
375 {
376 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
377 std::string iKey = myKey + std::to_string(i);
378 key.Update(factory->NewFromStdString(iKey).GetTaggedValue());
379 JSTaggedValue keyFlag = tSet->Has(thread, key.GetTaggedValue());
380 EXPECT_EQ(JSTaggedValue::True(), keyFlag);
381 }
382 }
383 // test entries
384 {
385 auto callInfo4 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
386 callInfo4->SetFunction(JSTaggedValue::Undefined());
387 callInfo4->SetThis(tSet.GetTaggedValue());
388 [[maybe_unused]] auto prev6 = TestHelper::SetupFrame(thread, callInfo4);
389 JSHandle<JSTaggedValue> iter(thread, ContainersHashSet::Entries(callInfo4));
390 TestHelper::TearDownFrame(thread, prev6);
391 EXPECT_TRUE(iter->IsJSAPIHashSetIterator());
392
393 JSHandle<JSTaggedValue> first(thread, JSTaggedValue(0));
394 JSHandle<JSTaggedValue> second(thread, JSTaggedValue(1));
395 JSMutableHandle<JSTaggedValue> result2(thread, JSTaggedValue::Undefined());
396 JSMutableHandle<JSTaggedValue> entries(thread, JSTaggedValue::Undefined());
397 for (uint32_t i = 0; i < NODE_NUMBERS * 2; i++) {
398 auto callInfo6 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
399 callInfo6->SetFunction(JSTaggedValue::Undefined());
400 callInfo6->SetThis(iter.GetTaggedValue());
401
402 [[maybe_unused]] auto prev4 = TestHelper::SetupFrame(thread, callInfo6);
403 result2.Update(JSAPIHashSetIterator::Next(callInfo6));
404 TestHelper::TearDownFrame(thread, prev4);
405 entries.Update(JSIterator::IteratorValue(thread, result2).GetTaggedValue());
406 EXPECT_EQ(JSTaggedValue(i), JSObject::GetProperty(thread, entries, first).GetValue().GetTaggedValue());
407 JSHandle<JSTaggedValue> iterValue = JSObject::GetProperty(thread, entries, second).GetValue();
408 JSTaggedValue valueFlag = tSet->Has(thread, iterValue.GetTaggedValue());
409 EXPECT_EQ(JSTaggedValue::True(), valueFlag);
410 }
411 }
412 }
413
HWTEST_F_L0(ContainersHashSetTest,ProxyOfGetLength)414 HWTEST_F_L0(ContainersHashSetTest, ProxyOfGetLength)
415 {
416 constexpr uint32_t NODE_NUMBERS = 8;
417 JSHandle<JSAPIHashSet> hashSet = CreateJSAPIHashSet();
418 auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
419 callInfo->SetFunction(JSTaggedValue::Undefined());
420 JSHandle<JSProxy> proxy = CreateJSProxyHandle(thread);
421 proxy->SetTarget(thread, hashSet.GetTaggedValue());
422 callInfo->SetThis(proxy.GetTaggedValue());
423
424 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
425 callInfo->SetCallArg(0, JSTaggedValue(i));
426 callInfo->SetCallArg(1, JSTaggedValue(i + 1));
427 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
428 ContainersHashSet::Add(callInfo);
429 TestHelper::TearDownFrame(thread, prev);
430
431 [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, callInfo);
432 JSTaggedValue retult = ContainersHashSet::GetLength(callInfo);
433 TestHelper::TearDownFrame(thread, prev1);
434 EXPECT_EQ(retult, JSTaggedValue(i + 1));
435 }
436 }
437
HWTEST_F_L0(ContainersHashSetTest,ExceptionReturn)438 HWTEST_F_L0(ContainersHashSetTest, ExceptionReturn)
439 {
440 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, Values);
441 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, Entries);
442 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, Add);
443 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, Remove);
444 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, Has);
445 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, Clear);
446 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, GetLength);
447 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, IsEmpty);
448 CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersHashSet, ForEach);
449 }
450 }
451