• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/js_api/js_api_lightweightset.h"
17 #include "ecmascript/containers/containers_private.h"
18 #include "ecmascript/ecma_string.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_api/js_api_lightweightset_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/tests/test_helper.h"
29 
30 using namespace panda;
31 using namespace panda::ecmascript;
32 
33 namespace panda::test {
34 class JSAPILightWeightSetTest : 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:
CreateLightWeightSet()61     JSAPILightWeightSet *CreateLightWeightSet()
62     {
63         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
64         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
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 =
72             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 3 means the value
73         objCallInfo->SetFunction(JSTaggedValue::Undefined());
74         objCallInfo->SetThis(value.GetTaggedValue());
75         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightSet)));
76 
77         auto prev = TestHelper::SetupFrame(thread, objCallInfo);
78         JSHandle<JSTaggedValue> constructor =
79             JSHandle<JSTaggedValue>(thread, containers::ContainersPrivate::Load(objCallInfo));
80         TestHelper::TearDownFrame(thread, prev);
81         JSHandle<JSAPILightWeightSet> lightweightSet =
82             JSHandle<JSAPILightWeightSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor),
83                                                                                   constructor));
84         JSHandle<JSTaggedValue> hashArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(8)); // 8 means the value
85         JSHandle<JSTaggedValue> valueArray = JSHandle<JSTaggedValue>(factory->NewTaggedArray(8)); // 8 means the value
86         lightweightSet->SetHashes(thread, hashArray);
87         lightweightSet->SetValues(thread, valueArray);
88         lightweightSet->SetLength(0); // 0 means the value
89         return *lightweightSet;
90     }
91 };
92 
HWTEST_F_L0(JSAPILightWeightSetTest,LightWeightSetCreate)93 HWTEST_F_L0(JSAPILightWeightSetTest, LightWeightSetCreate)
94 {
95     JSAPILightWeightSet *lightweightSet = CreateLightWeightSet();
96     EXPECT_TRUE(lightweightSet != nullptr);
97 }
98 
HWTEST_F_L0(JSAPILightWeightSetTest,AddIncreaseCapacityAddAll)99 HWTEST_F_L0(JSAPILightWeightSetTest, AddIncreaseCapacityAddAll)
100 {
101     constexpr uint32_t NODE_NUMBERS = 8; // 8 means the value
102     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
103     JSHandle<JSAPILightWeightSet> srcLws(thread, CreateLightWeightSet());
104     JSHandle<JSAPILightWeightSet> destLws(thread, CreateLightWeightSet());
105     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
106 
107     // test IncreaseCapacityTo
108     std::string myValue("myvalue");
109     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
110         bool result = JSAPILightWeightSet::Add(thread, lws, JSHandle<JSTaggedValue>(thread, JSTaggedValue(i)));
111         EXPECT_TRUE(result);
112     }
113     EXPECT_EQ(lws->GetSize(), NODE_NUMBERS);
114 
115     uint32_t tmp = NODE_NUMBERS * 2; // 2 means the value
116     JSAPILightWeightSet::IncreaseCapacityTo(thread, lws, static_cast<int32_t>(tmp));
117     uint32_t capacity = TaggedArray::Cast(lws->GetValues().GetTaggedObject())->GetLength();
118     EXPECT_EQ(JSTaggedValue(capacity), JSTaggedValue(tmp));
119 
120     // test IncreaseCapacityTo exception
121     JSAPILightWeightSet::IncreaseCapacityTo(thread, lws, 0);
122     EXPECT_EXCEPTION();
123     JSAPILightWeightSet::IncreaseCapacityTo(thread, lws, NODE_NUMBERS);
124     EXPECT_EXCEPTION();
125 
126     // test AddAll
127     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
128         bool result = JSAPILightWeightSet::Add(thread, destLws, JSHandle<JSTaggedValue>(thread, JSTaggedValue(i)));
129         EXPECT_TRUE(result);
130     }
131 
132     for (uint32_t i = 0; i < NODE_NUMBERS + 2; i++) {
133         JSAPILightWeightSet::Add(thread, srcLws, JSHandle<JSTaggedValue>(thread, JSTaggedValue(i)));
134     }
135     bool result = JSAPILightWeightSet::AddAll(thread, destLws, JSHandle<JSTaggedValue>::Cast(srcLws));
136     EXPECT_TRUE(result);
137     tmp = NODE_NUMBERS + 2; // 2 means the value
138     EXPECT_EQ(destLws->GetSize(), tmp);
139 }
140 
HWTEST_F_L0(JSAPILightWeightSetTest,EqualClearNotEqual)141 HWTEST_F_L0(JSAPILightWeightSetTest, EqualClearNotEqual)
142 {
143     constexpr uint32_t NODE_NUMBERS = 8; // 8 means the value
144     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
145     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
146     JSHandle<JSAPILightWeightSet> equalLws(thread, CreateLightWeightSet());
147     JSHandle<JSTaggedValue> jsArray = JSArray::ArrayCreate(thread, JSTaggedNumber(0));
148     JSMutableHandle<JSTaggedValue> value1(thread, JSTaggedValue::Undefined());
149     JSMutableHandle<JSTaggedValue> value2(thread, JSTaggedValue::Undefined());
150     bool result = false;
151 
152     // test Equal of two empty lightweightset
153     result = JSAPILightWeightSet::Equal(thread, lws, JSHandle<JSTaggedValue>::Cast(equalLws));
154     EXPECT_FALSE(result);
155     result = JSAPILightWeightSet::Equal(thread, lws, jsArray);
156     EXPECT_FALSE(result);
157 
158     // test equal
159     std::string myValue1("myvalue");
160     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
161         std::string iValue = myValue1 + std::to_string(i);
162         value1.Update(factory->NewFromStdString(iValue).GetTaggedValue());
163         result = JSAPILightWeightSet::Add(thread, lws, value1);
164         EXPECT_TRUE(result);
165     }
166     EXPECT_EQ(lws->GetSize(), NODE_NUMBERS);
167 
168     std::string myValue2("myvalue");
169     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
170         std::string iValue = myValue2 + std::to_string(i);
171         value2.Update(factory->NewFromStdString(iValue).GetTaggedValue());
172         result = JSAPILightWeightSet::Add(thread, equalLws, value2);
173         EXPECT_TRUE(result);
174     }
175     EXPECT_EQ(equalLws->GetSize(), NODE_NUMBERS);
176     result = JSAPILightWeightSet::Equal(thread, lws, JSHandle<JSTaggedValue>::Cast(equalLws));
177     EXPECT_FALSE(result);
178 
179     equalLws->Clear(thread);
180     EXPECT_EQ(equalLws->GetSize(), static_cast<uint32_t>(0)); // 0 means the value
181 
182     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
183         std::string iValue = myValue2 + std::to_string(i);
184         if (i == 2) {
185             LOG_ECMA(ERROR) << " {} " << iValue;
186         } else {
187             value2.Update(factory->NewFromStdString(iValue).GetTaggedValue());
188             result = JSAPILightWeightSet::Add(thread, equalLws, value2);
189             EXPECT_TRUE(result);
190         }
191     }
192     EXPECT_EQ(equalLws->GetSize(), NODE_NUMBERS - 1);
193     result = JSAPILightWeightSet::Equal(thread, lws, JSHandle<JSTaggedValue>::Cast(equalLws));
194     EXPECT_FALSE(result);
195 }
196 
HWTEST_F_L0(JSAPILightWeightSetTest,IsEmptyHasHasAll)197 HWTEST_F_L0(JSAPILightWeightSetTest, IsEmptyHasHasAll)
198 {
199     constexpr uint32_t NODE_NUMBERS = 8;
200     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
201     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
202     JSHandle<JSAPILightWeightSet> hasAllLws(thread, CreateLightWeightSet());
203     JSMutableHandle<JSTaggedValue> value1(thread, JSTaggedValue::Undefined());
204     JSMutableHandle<JSTaggedValue> value2(thread, JSTaggedValue::Undefined());
205     JSMutableHandle<JSTaggedValue> value3(thread, JSTaggedValue::Undefined());
206     bool result = false;
207     std::string tValue;
208     // test IsEmpty
209     result = lws->IsEmpty();
210     EXPECT_TRUE(result);
211     // test Has
212     std::string myValue1("myvalue");
213     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
214         std::string iValue = myValue1 + std::to_string(i);
215         value1.Update(factory->NewFromStdString(iValue).GetTaggedValue());
216         result = JSAPILightWeightSet::Add(thread, lws, value1);
217         EXPECT_TRUE(result);
218     }
219     EXPECT_EQ(lws->GetSize(), NODE_NUMBERS);
220 
221     tValue = myValue1 + std::to_string(5);
222     value1.Update(factory->NewFromStdString(tValue).GetTaggedValue());
223     result = lws->Has(value1);
224     EXPECT_TRUE(result);
225     tValue = myValue1 + std::to_string(NODE_NUMBERS);
226     value1.Update(factory->NewFromStdString(tValue).GetTaggedValue());
227     result = lws->Has(value1);
228     EXPECT_FALSE(result);
229 
230     std::string myValue2("myvalue");
231     for (uint32_t i = 0; i < NODE_NUMBERS - 5; i++) {
232         if (i == 1) {
233             std::string myValue3("destValue");
234             std::string iValue3 = myValue3 + std::to_string(i);
235             value3.Update(factory->NewFromStdString(iValue3).GetTaggedValue());
236             result = JSAPILightWeightSet::Add(thread, hasAllLws, value3);
237         } else {
238             std::string iValue = myValue2 + std::to_string(i);
239             value2.Update(factory->NewFromStdString(iValue).GetTaggedValue());
240             result = JSAPILightWeightSet::Add(thread, hasAllLws, value2);
241             EXPECT_TRUE(result);
242         }
243     }
244     EXPECT_EQ(hasAllLws->GetSize(), NODE_NUMBERS - 5); // 5 means the value
245     result = lws->HasAll(JSHandle<JSTaggedValue>::Cast(hasAllLws));
246     EXPECT_FALSE(result);
247     result = hasAllLws->HasAll(JSHandle<JSTaggedValue>::Cast(lws));
248     EXPECT_FALSE(result);
249 }
250 
HWTEST_F_L0(JSAPILightWeightSetTest,GetIndexOfRemoveRemoveAtGetValueAt)251 HWTEST_F_L0(JSAPILightWeightSetTest, GetIndexOfRemoveRemoveAtGetValueAt)
252 {
253     constexpr uint32_t NODE_NUMBERS = 8; // 8 means the value
254     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
255     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
256     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
257     bool result = false;
258 
259     // test GetSize
260     std::string myValue("myvalue");
261     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
262         std::string iValue = myValue + std::to_string(i);
263         value.Update(factory->NewFromStdString(iValue).GetTaggedValue());
264         result = JSAPILightWeightSet::Add(thread, lws, value);
265     }
266     EXPECT_EQ(lws->GetSize(), NODE_NUMBERS);
267 
268     // test GetIndexOf
269     std::string tValue("myvalue5");
270     value.Update(factory->NewFromStdString(tValue).GetTaggedValue());
271     int32_t index = lws->GetIndexOf(value);
272     EXPECT_EQ(index, 5); // 5 means the value
273 
274     // test GetValueAt
275     JSTaggedValue jsValue = lws->GetValueAt(5); // 5 means the value
276     EXPECT_EQ(value.GetTaggedValue(), jsValue);
277 
278     // test Remove
279     jsValue = lws->Remove(thread, value);
280     EXPECT_EQ(value.GetTaggedValue(), jsValue);
281     jsValue = lws->Remove(thread, value);
282     EXPECT_EQ(jsValue, JSTaggedValue::Undefined());
283 
284     // test RemoveAt
285     result = lws->RemoveAt(thread, 4); // 4 means the value
286     EXPECT_EQ(lws->GetSize(), NODE_NUMBERS - 2); // 2 means the value
287     EXPECT_TRUE(result);
288     result = lws->RemoveAt(thread, -1);
289     EXPECT_FALSE(result);
290     result = lws->RemoveAt(thread, static_cast<int32_t>(NODE_NUMBERS));
291     EXPECT_FALSE(result);
292 }
293 
HWTEST_F_L0(JSAPILightWeightSetTest,Iterator)294 HWTEST_F_L0(JSAPILightWeightSetTest, Iterator)
295 {
296     constexpr uint32_t NODE_NUMBERS = 8; // 8 means the value
297     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
298     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
299 
300     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
301     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
302         value.Update(JSTaggedValue(i));
303         JSAPILightWeightSet::Add(thread, lws, value);
304     }
305 
306     JSHandle<JSTaggedValue> valueIter(factory->NewJSAPILightWeightSetIterator(lws, IterationKind::VALUE));
307     JSMutableHandle<JSTaggedValue> valueIterResult(thread, JSTaggedValue::Undefined());
308     for (int i = 0; i < static_cast<int>(NODE_NUMBERS); i++) {
309         valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue());
310         int v = JSIterator::IteratorValue(thread, valueIterResult)->GetInt();
311         EXPECT_TRUE(v == i);
312     }
313 }
314 
HWTEST_F_L0(JSAPILightWeightSetTest,RBTreeGetHashIndex)315 HWTEST_F_L0(JSAPILightWeightSetTest, RBTreeGetHashIndex)
316 {
317     std::vector<int> hashCollisionVector = {4307, 5135, 5903, 6603, 6780, 8416, 1224, 1285, 1463, 9401, 9740};
318     uint32_t NODE_NUMBERS = static_cast<uint32_t>(hashCollisionVector.size());
319     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
320 
321     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
322     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
323         value.Update(JSTaggedValue(hashCollisionVector[i]));
324         JSAPILightWeightSet::Add(thread, lws, value);
325     }
326     int32_t size = static_cast<uint32_t>(lws->GetLength());
327     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
328         value.Update(JSTaggedValue(hashCollisionVector[i]));
329         int32_t index = lws->GetHashIndex(value, size);
330         EXPECT_TRUE(0 <= index && index < size);
331     }
332 }
333 
HWTEST_F_L0(JSAPILightWeightSetTest,SpecialReturnTestEnsureCapacityGetValueAtGetHashAt)334 HWTEST_F_L0(JSAPILightWeightSetTest, SpecialReturnTestEnsureCapacityGetValueAtGetHashAt)
335 {
336     constexpr uint32_t NODE_NUMBERS = 8;
337     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
338 
339     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
340     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
341         value.Update(JSTaggedValue(i));
342         JSAPILightWeightSet::Add(thread, lws, value);
343     }
344 
345     // test special return of EnsureCapacity
346     JSHandle<TaggedArray> array(thread, lws->GetValues());
347     JSAPILightWeightSet::EnsureCapacity(thread, lws, 0);
348     JSHandle<TaggedArray> newArray(thread, lws->GetValues());
349     EXPECT_TRUE(array->GetLength() == newArray->GetLength());
350 
351     // test special return of GetValueAt
352     JSTaggedValue result1 = lws->GetValueAt(-1);
353     EXPECT_EQ(result1, JSTaggedValue::Undefined());
354     JSTaggedValue result2 = lws->GetValueAt(static_cast<int32_t>(NODE_NUMBERS * 2));
355     EXPECT_EQ(result2, JSTaggedValue::Undefined());
356 
357     // test special return of GetHashAt
358     JSTaggedValue result3 = lws->GetHashAt(-1);
359     EXPECT_EQ(result3, JSTaggedValue::Undefined());
360     JSTaggedValue result4 = lws->GetHashAt(static_cast<int32_t>(NODE_NUMBERS * 2));
361     EXPECT_EQ(result4, JSTaggedValue::Undefined());
362 }
363 
HWTEST_F_L0(JSAPILightWeightSetTest,GetHashAtHasHash)364 HWTEST_F_L0(JSAPILightWeightSetTest, GetHashAtHasHash)
365 {
366     constexpr uint32_t NODE_NUMBERS = 8;
367     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
368 
369     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
370     JSMutableHandle<JSTaggedValue> hash(thread, JSTaggedValue::Undefined());
371     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
372         value.Update(JSTaggedValue(i));
373         JSAPILightWeightSet::Add(thread, lws, value);
374     }
375 
376     // test GetHashAt
377     int32_t size = static_cast<int32_t>(lws->GetLength());
378     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
379         hash.Update(JSTaggedValue(lws->Hash(JSTaggedValue(i))));
380         int32_t index = lws->GetHashIndex(hash, size);
381         JSTaggedValue getHash= lws->GetHashAt(index);
382         EXPECT_EQ(getHash, hash.GetTaggedValue());
383     }
384 
385     // test HasHash
386     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
387         hash.Update(JSTaggedValue(lws->Hash(JSTaggedValue(i))));
388         EXPECT_TRUE(lws->HasHash(hash));
389     }
390     hash.Update(JSTaggedValue(lws->Hash(JSTaggedValue(NODE_NUMBERS))));
391     EXPECT_FALSE(lws->HasHash(hash));
392 }
393 
HWTEST_F_L0(JSAPILightWeightSetTest,ToString)394 HWTEST_F_L0(JSAPILightWeightSetTest, ToString)
395 {
396     constexpr uint32_t NODE_NUMBERS = 3;
397     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
398 
399     JSHandle<JSAPILightWeightSet> lws(thread, CreateLightWeightSet());
400     JSTaggedValue result1 = JSAPILightWeightSet::ToString(thread, lws);
401     JSHandle<EcmaString> resultHandle1(thread, result1);
402     JSHandle<EcmaString> det = thread->GetEcmaVM()->GetFactory()->NewFromASCII("");
403     ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle1, det), 0);
404     for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
405         value.Update(JSTaggedValue(i));
406         JSAPILightWeightSet::Add(thread, lws, value);
407     }
408     JSHandle<EcmaString> str = thread->GetEcmaVM()->GetFactory()->NewFromASCII("0,1,2");
409     JSTaggedValue result = JSAPILightWeightSet::ToString(thread, lws);
410     JSHandle<EcmaString> resultHandle(thread, result);
411     ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, str), 0);
412 }
413 }  // namespace panda::test
414