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_hashset.h"
24 #include "ecmascript/js_api/js_api_hashset_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 JSAPIHashSetTest : 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:
CreateHashSet()61 JSAPIHashSet *CreateHashSet()
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::HashSet)));
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<JSAPIHashSet> set(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
82 JSTaggedValue hashSetArray = TaggedHashArray::Create(thread);
83 set->SetTable(thread, hashSetArray);
84 set->SetSize(0);
85 return *set;
86 }
87 };
88
HWTEST_F_L0(JSAPIHashSetTest,HashSetCreate)89 HWTEST_F_L0(JSAPIHashSetTest, HashSetCreate)
90 {
91 JSAPIHashSet *set = CreateHashSet();
92 EXPECT_TRUE(set != nullptr);
93 }
94
HWTEST_F_L0(JSAPIHashSetTest,HashSetAddAndHas)95 HWTEST_F_L0(JSAPIHashSetTest, HashSetAddAndHas)
96 {
97 constexpr uint32_t NODE_NUMBERS = 8;
98 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
99 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
100
101 // test JSAPIHashSet
102 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet());
103 std::string myValue("myvalue");
104 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
105 std::string iValue = myValue + std::to_string(i);
106 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
107 JSAPIHashSet::Add(thread, hashSet, value);
108 }
109 EXPECT_EQ(hashSet->GetSize(), NODE_NUMBERS);
110
111 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
112 std::string iValue = myValue + std::to_string(i);
113 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
114
115 // test has
116 JSTaggedValue bHas = hashSet->Has(thread, value.GetTaggedValue());
117 EXPECT_EQ(bHas, JSTaggedValue::True());
118 }
119
120 // test add exception
121 JSHandle<JSTaggedValue> hole(thread, JSTaggedValue::Hole());
122 JSAPIHashSet::Add(thread, hashSet, hole);
123 EXPECT_EXCEPTION();
124
125 // test Has exception
126 JSTaggedValue exceptionHas = hashSet->Has(thread, JSTaggedValue::Hole());
127 EXPECT_EQ(exceptionHas, JSTaggedValue::Exception());
128 EXPECT_EXCEPTION();
129 }
130
HWTEST_F_L0(JSAPIHashSetTest,HashSetRemoveAndHas)131 HWTEST_F_L0(JSAPIHashSetTest, HashSetRemoveAndHas)
132 {
133 constexpr uint32_t NODE_NUMBERS = 8;
134 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
135 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
136
137 // test JSAPIHashSet
138 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet());
139 std::string myValue("myvalue");
140 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
141 std::string iValue = myValue + std::to_string(i);
142 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
143 JSAPIHashSet::Add(thread, hashSet, value);
144 }
145 EXPECT_EQ(hashSet->GetSize(), NODE_NUMBERS);
146
147 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) {
148 std::string iValue = myValue + std::to_string(i);
149 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
150 [[maybe_unused]] JSTaggedValue rvalue = JSAPIHashSet::Remove(thread, hashSet, value.GetTaggedValue());
151 }
152 EXPECT_EQ(hashSet->GetSize(), NODE_NUMBERS / 2);
153
154 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) {
155 std::string iValue = myValue + std::to_string(i);
156 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
157
158 // test has
159 JSTaggedValue has = hashSet->Has(thread, value.GetTaggedValue());
160 EXPECT_EQ(has, JSTaggedValue::False());
161 }
162
163 for (uint32_t i = NODE_NUMBERS / 2; i < NODE_NUMBERS; i++) {
164 std::string iValue = myValue + std::to_string(i);
165 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
166
167 // test has
168 JSTaggedValue has = hashSet->Has(thread, value.GetTaggedValue());
169 EXPECT_EQ(has, JSTaggedValue::True());
170 }
171
172 // test Remove exception
173 JSTaggedValue exceptionValue = JSAPIHashSet::Remove(thread, hashSet, JSTaggedValue::Hole());
174 EXPECT_EQ(exceptionValue, JSTaggedValue::Exception());
175 EXPECT_EXCEPTION();
176 }
177
HWTEST_F_L0(JSAPIHashSetTest,JSAPIHashSetRemoveRBTreeTest)178 HWTEST_F_L0(JSAPIHashSetTest, JSAPIHashSetRemoveRBTreeTest)
179 {
180 std::vector<int> hashCollisionVector = {1224, 1285, 1463, 4307, 5135, 5903, 6603, 6780, 8416, 9401, 9740};
181 uint32_t NODE_NUMBERS = static_cast<uint32_t>(hashCollisionVector.size());
182 constexpr uint32_t REMOVE_NUMBERS = 4;
183 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet());
184
185 // test Remove empty
186 JSTaggedValue emptyValue = JSAPIHashSet::Remove(thread, hashSet, JSTaggedValue(0));
187 EXPECT_EQ(emptyValue, JSTaggedValue::False());
188
189 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
190
191 for (size_t i = 0; i < hashCollisionVector.size(); i++) {
192 value.Update(JSTaggedValue(hashCollisionVector[i]));
193 JSAPIHashSet::Add(thread, hashSet, value);
194 }
195
196 // test Remove non-existent value
197 JSTaggedValue nonExistentValue = JSAPIHashSet::Remove(thread, hashSet, JSTaggedValue(0));
198 EXPECT_EQ(nonExistentValue, JSTaggedValue::False());
199
200 // test Remove RBTree
201 for (uint32_t i = 0; i < REMOVE_NUMBERS; i++) {
202 value.Update(JSTaggedValue(hashCollisionVector[i]));
203 JSAPIHashSet::Remove(thread, hashSet, value.GetTaggedValue());
204 }
205 EXPECT_EQ(hashSet->GetSize(), NODE_NUMBERS - REMOVE_NUMBERS);
206
207 for (uint32_t i = 0; i < REMOVE_NUMBERS; i++) {
208 value.Update(JSTaggedValue(hashCollisionVector[i]));
209 JSTaggedValue has = hashSet->Has(thread, value.GetTaggedValue());
210 EXPECT_EQ(has, JSTaggedValue::False());
211 }
212
213 for (uint32_t i = REMOVE_NUMBERS; i < NODE_NUMBERS; i++) {
214 value.Update(JSTaggedValue(hashCollisionVector[i]));
215 JSTaggedValue has = hashSet->Has(thread, value.GetTaggedValue());
216 EXPECT_EQ(has, JSTaggedValue::True());
217 }
218 }
219
HWTEST_F_L0(JSAPIHashSetTest,HashSetClearAddHas)220 HWTEST_F_L0(JSAPIHashSetTest, HashSetClearAddHas)
221 {
222 constexpr uint32_t NODE_NUMBERS = 8;
223 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
224 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
225
226 // test JSAPIHashSet
227 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet());
228 std::string myValue("myvalue");
229 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
230 std::string iValue = myValue + std::to_string(i);
231 value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
232 JSAPIHashSet::Add(thread, hashSet, value);
233 }
234 EXPECT_EQ(hashSet->GetSize(), NODE_NUMBERS);
235
236 hashSet->Clear(thread);
237 JSTaggedValue isEmpty = hashSet->IsEmpty();
238 EXPECT_EQ(isEmpty, JSTaggedValue::True());
239 }
240
HWTEST_F_L0(JSAPIHashSetTest,JSAPIHashSetIterator)241 HWTEST_F_L0(JSAPIHashSetTest, JSAPIHashSetIterator)
242 {
243 constexpr uint32_t NODE_NUMBERS = 8;
244 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
245 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet());
246
247 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
248 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
249 value.Update(JSTaggedValue(i));
250 JSAPIHashSet::Add(thread, hashSet, value);
251 }
252
253 // test value
254 JSHandle<JSTaggedValue> valueIter(factory->NewJSAPIHashSetIterator(hashSet, IterationKind::VALUE));
255 JSMutableHandle<JSTaggedValue> valueIterResult(thread, JSTaggedValue::Undefined());
256 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
257 valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue());
258 JSHandle<JSTaggedValue> tmpIterValue = JSIterator::IteratorValue(thread, valueIterResult);
259 JSTaggedValue iterValueFlag = hashSet->Has(thread, tmpIterValue.GetTaggedValue());
260 EXPECT_EQ(JSTaggedValue::True(), iterValueFlag);
261 }
262 // test end
263 valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue());
264 EXPECT_EQ(JSTaggedValue::False(), valueIterResult.GetTaggedValue());
265
266 // test key and value
267 JSHandle<JSTaggedValue> indexKey(thread, JSTaggedValue(0));
268 JSHandle<JSTaggedValue> elementKey(thread, JSTaggedValue(1));
269 JSHandle<JSTaggedValue> iter(factory->NewJSAPIHashSetIterator(hashSet, IterationKind::KEY_AND_VALUE));
270 JSMutableHandle<JSTaggedValue> iterResult(thread, JSTaggedValue::Undefined());
271 JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
272 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
273 iterResult.Update(JSIterator::IteratorStep(thread, iter).GetTaggedValue());
274 result.Update(JSIterator::IteratorValue(thread, iterResult).GetTaggedValue());
275 EXPECT_EQ(JSTaggedValue(i), JSObject::GetProperty(thread, result, indexKey).GetValue().GetTaggedValue());
276 JSHandle<JSTaggedValue> tmpValue = JSObject::GetProperty(thread, result, elementKey).GetValue();
277 JSTaggedValue iterValueFlag = hashSet->Has(thread, tmpValue.GetTaggedValue());
278 EXPECT_EQ(JSTaggedValue::True(), iterValueFlag);
279 }
280 }
281
HWTEST_F_L0(JSAPIHashSetTest,JSAPIHashSetIteratorRBTreeTest)282 HWTEST_F_L0(JSAPIHashSetTest, JSAPIHashSetIteratorRBTreeTest)
283 {
284 constexpr uint32_t NODE_NUMBERS = 11;
285 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
286 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet());
287 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
288 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
289 std::vector<int> hashCollisionVector = {1224, 1285, 1463, 4307, 5135, 5903, 6603, 6780, 8416, 9401, 9740};
290
291 for (size_t i = 0; i < hashCollisionVector.size(); i++) {
292 value.Update(JSTaggedValue(hashCollisionVector[i]));
293 JSAPIHashSet::Add(thread, hashSet, value);
294 }
295
296 JSHandle<JSAPIHashSetIterator> hashsetIterator = factory->NewJSAPIHashSetIterator(hashSet, IterationKind::VALUE);
297 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
298 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
299 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
300 ecmaRuntimeCallInfo->SetThis(hashsetIterator.GetTaggedValue());
301
302 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
303 JSTaggedValue result = JSAPIHashSetIterator::Next(ecmaRuntimeCallInfo);
304 TestHelper::TearDownFrame(thread, prev);
305
306 JSHandle<JSObject> resultObj(thread, result);
307 if (i <= NODE_NUMBERS - 1U) {
308 EXPECT_TRUE(JSObject::GetProperty(thread, resultObj, valueStr).GetValue()->IsInt());
309 }
310 }
311 }
312 } // namespace panda::test
313