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_tree_set_iterator.h"
17 #include "ecmascript/containers/containers_private.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_api/js_api_tree_set.h"
20 #include "ecmascript/js_iterator.h"
21 #include "ecmascript/tagged_tree.h"
22 #include "ecmascript/tests/ecma_test_common.h"
23
24 using namespace panda;
25 using namespace panda::ecmascript;
26
27 namespace panda::test {
28 class JSAPITreeSetIteratorTest : public BaseTestWithScope<false> {
29 protected:
CreateTreeSet()30 JSHandle<JSAPITreeSet> CreateTreeSet()
31 {
32 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
33 auto result = TestCommon::CreateContainerTaggedValue(thread, containers::ContainerTag::TreeSet);
34 JSHandle<JSTaggedValue> constructor(thread, result);
35 JSHandle<JSAPITreeSet> set(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
36 JSTaggedValue internal = TaggedTreeSet::Create(thread);
37 set->SetTreeSet(thread, internal);
38 return set;
39 }
40 };
41
42 /**
43 * @tc.name: SetIterationKind
44 * @tc.desc: Call the "SetIterationKind" function, check whether the result returned through "GetIterationKind"
45 * function from the JSAPITreeSetIterator is within expectations.
46 * @tc.type: FUNC
47 * @tc.require:
48 */
HWTEST_F_L0(JSAPITreeSetIteratorTest,SetIterationKind)49 HWTEST_F_L0(JSAPITreeSetIteratorTest, SetIterationKind)
50 {
51 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
52 JSHandle<JSAPITreeSet> jsTreeSet = CreateTreeSet();
53 EXPECT_TRUE(*jsTreeSet != nullptr);
54 JSHandle<JSAPITreeSetIterator> treeSetIterator =
55 factory->NewJSAPITreeSetIterator(jsTreeSet, IterationKind::KEY);
56 EXPECT_EQ(treeSetIterator->GetIterationKind(), IterationKind::KEY);
57 treeSetIterator->SetIterationKind(IterationKind::VALUE);
58 EXPECT_EQ(treeSetIterator->GetIterationKind(), IterationKind::VALUE);
59 treeSetIterator->SetIterationKind(IterationKind::KEY_AND_VALUE);
60 EXPECT_EQ(treeSetIterator->GetIterationKind(), IterationKind::KEY_AND_VALUE);
61 }
62
63 /**
64 * @tc.name: SetIteratedSet
65 * @tc.desc: Call the "SetIteratedSet" function, check whether the result returned through "GetIteratedSet"
66 * function from the JSAPITreeSetIterator is within expectations.
67 * @tc.type: FUNC
68 * @tc.require:
69 */
HWTEST_F_L0(JSAPITreeSetIteratorTest,SetIteratedSet)70 HWTEST_F_L0(JSAPITreeSetIteratorTest, SetIteratedSet)
71 {
72 constexpr uint32_t DEFAULT_LENGTH = 8;
73 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
74 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
75 JSHandle<JSAPITreeSet> jsTreeSet = CreateTreeSet();
76 EXPECT_TRUE(*jsTreeSet != nullptr);
77 JSHandle<JSAPITreeSetIterator> treeSetIterator =
78 factory->NewJSAPITreeSetIterator(jsTreeSet, IterationKind::VALUE);
79
80 std::string setKey("setkey");
81 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
82 std::string ikey = setKey + std::to_string(i);
83 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
84 JSAPITreeSet::Add(thread, jsTreeSet, key);
85 }
86 treeSetIterator->SetIteratedSet(thread, jsTreeSet.GetTaggedValue());
87 JSHandle<JSAPITreeSet> treeSetTo(thread, JSAPITreeSet::Cast(treeSetIterator->GetIteratedSet().GetTaggedObject()));
88 EXPECT_EQ(treeSetTo->GetSize(), static_cast<int>(DEFAULT_LENGTH));
89 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
90 std::string ikey = setKey + std::to_string(i);
91 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
92 EXPECT_TRUE(JSAPITreeSet::Has(thread, jsTreeSet, key));
93 }
94 }
95
96 /**
97 * @tc.name: SetNextIndex
98 * @tc.desc: Call the "SetNextIndex" function, check whether the result returned through "GetNextIndex"
99 * function from the JSAPITreeSetIterator is within expectations.
100 * @tc.type: FUNC
101 * @tc.require:
102 */
HWTEST_F_L0(JSAPITreeSetIteratorTest,SetNextIndex)103 HWTEST_F_L0(JSAPITreeSetIteratorTest, SetNextIndex)
104 {
105 constexpr uint32_t DEFAULT_LENGTH = 8;
106 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
107 JSHandle<JSAPITreeSet> jsTreeSet = CreateTreeSet();
108 EXPECT_TRUE(*jsTreeSet != nullptr);
109 JSHandle<JSAPITreeSetIterator> treeSetIterator =
110 factory->NewJSAPITreeSetIterator(jsTreeSet, IterationKind::KEY_AND_VALUE);
111 EXPECT_EQ(treeSetIterator->GetNextIndex(), 0U);
112
113 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
114 treeSetIterator->SetNextIndex(i);
115 EXPECT_EQ(treeSetIterator->GetNextIndex(), i);
116 }
117 }
118
119 /**
120 * @tc.name: CreateTreeSetIterator
121 * @tc.desc: Create TreeSet iterator, check whether the result returned through "IsJSAPITreeSetIterator"
122 * function from the JSAPITreeSetIterator is within expectations.
123 * @tc.type: FUNC
124 * @tc.require:
125 */
HWTEST_F_L0(JSAPITreeSetIteratorTest,CreateTreeSetIterator)126 HWTEST_F_L0(JSAPITreeSetIteratorTest, CreateTreeSetIterator)
127 {
128 JSHandle<JSAPITreeSet> jsTreeSet = CreateTreeSet();
129 EXPECT_TRUE(*jsTreeSet != nullptr);
130 JSHandle<JSTaggedValue> treeSetVal(jsTreeSet);
131 // Create Iterator with KEY
132 JSHandle<JSTaggedValue> treeSetIterator =
133 JSAPITreeSetIterator::CreateTreeSetIterator(thread, treeSetVal, IterationKind::KEY);
134 EXPECT_TRUE(*treeSetIterator != nullptr);
135 EXPECT_TRUE(treeSetIterator->IsJSAPITreeSetIterator());
136 // Create Iterator with VALUE
137 treeSetIterator = JSAPITreeSetIterator::CreateTreeSetIterator(thread, treeSetVal, IterationKind::VALUE);
138 EXPECT_TRUE(*treeSetIterator != nullptr);
139 EXPECT_TRUE(treeSetIterator->IsJSAPITreeSetIterator());
140 // Create Iterator with KEY_AND_VALUE
141 treeSetIterator = JSAPITreeSetIterator::CreateTreeSetIterator(thread, treeSetVal, IterationKind::KEY_AND_VALUE);
142 EXPECT_TRUE(*treeSetIterator != nullptr);
143 EXPECT_TRUE(treeSetIterator->IsJSAPITreeSetIterator());
144 }
145
146 /**
147 * @tc.name: Next
148 * @tc.desc: Create an iterator of JSAPITreeSet,and then loop through the elements(key and keyAndvalue) of the
149 * iterator to check whether the elements through "Next" function are consistent.
150 * @tc.type: FUNC
151 * @tc.require:
152 */
HWTEST_F_L0(JSAPITreeSetIteratorTest,KEY_Next)153 HWTEST_F_L0(JSAPITreeSetIteratorTest, KEY_Next)
154 {
155 constexpr uint32_t DEFAULT_LENGTH = 8;
156 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
157 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
158 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
159 JSHandle<JSAPITreeSet> jsTreeSet = CreateTreeSet();
160 std::string setKey("setkey");
161 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
162 std::string ikey = setKey + std::to_string(i);
163 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
164 JSAPITreeSet::Add(thread, jsTreeSet, key);
165 }
166 // Create Iterator with KEY or VALUE
167 JSHandle<JSAPITreeSetIterator> treeSetIterator =
168 factory->NewJSAPITreeSetIterator(jsTreeSet, IterationKind::KEY);
169 // traversal iterator
170 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
171 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
172 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
173 ecmaRuntimeCallInfo->SetThis(treeSetIterator.GetTaggedValue());
174
175 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
176 JSTaggedValue result = JSAPITreeSetIterator::Next(ecmaRuntimeCallInfo);
177 TestHelper::TearDownFrame(thread, prev);
178
179 JSHandle<JSObject> resultObj(thread, result);
180 std::string resultKey = setKey + std::to_string(i);
181 key.Update(factory->NewFromStdString(resultKey).GetTaggedValue());
182 EXPECT_EQ(JSTaggedValue::SameValue(
183 JSObject::GetProperty(thread, resultObj, valueStr).GetValue(), key), true);
184 EXPECT_EQ(treeSetIterator->GetNextIndex(), (i + 1U));
185 }
186 }
187
188 /**
189 * @tc.name: Next
190 * @tc.desc: test special return of Next, including throw exception and return undefined
191 * @tc.type: FUNC
192 * @tc.require:
193 */
HWTEST_F_L0(JSAPITreeSetIteratorTest,SpecialReturnOfNext)194 HWTEST_F_L0(JSAPITreeSetIteratorTest, SpecialReturnOfNext)
195 {
196 JSHandle<JSAPITreeSet> jsTreeSet = CreateTreeSet();
197 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
198 JSHandle<JSAPITreeSetIterator> treeSetIterator = factory->NewJSAPITreeSetIterator(jsTreeSet, IterationKind::KEY);
199 treeSetIterator->SetIteratedSet(thread, JSTaggedValue::Undefined());
200
201 // test Next exception
202 {
203 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
204 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
205 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
206
207 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
208 JSTaggedValue result = JSAPITreeSetIterator::Next(ecmaRuntimeCallInfo);
209 TestHelper::TearDownFrame(thread, prev);
210 EXPECT_EQ(result, JSTaggedValue::Exception());
211 EXPECT_EXCEPTION();
212 }
213
214 // test Next return undefined
215 {
216 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
217 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
218 ecmaRuntimeCallInfo->SetThis(treeSetIterator.GetTaggedValue());
219
220 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
221 JSTaggedValue result = JSAPITreeSetIterator::Next(ecmaRuntimeCallInfo);
222 TestHelper::TearDownFrame(thread, prev);
223 EXPECT_EQ(result, thread->GlobalConstants()->GetUndefinedIterResult());
224 }
225 }
226
HWTEST_F_L0(JSAPITreeSetIteratorTest,KEY_AND_VALUE_Next)227 HWTEST_F_L0(JSAPITreeSetIteratorTest, KEY_AND_VALUE_Next)
228 {
229 constexpr uint32_t DEFAULT_LENGTH = 8;
230 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
231 JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
232 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
233 JSHandle<JSAPITreeSet> jsTreeSet = CreateTreeSet();
234 std::string setKey("setkey");
235 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
236 std::string ikey = setKey + std::to_string(i + 1U);
237 key.Update(factory->NewFromStdString(ikey).GetTaggedValue());
238 JSAPITreeSet::Add(thread, jsTreeSet, key);
239 }
240 // Create Iterator with KEY_AND_VALUE
241 JSHandle<JSAPITreeSetIterator> treeSetIterator =
242 factory->NewJSAPITreeSetIterator(jsTreeSet, IterationKind::KEY_AND_VALUE);
243 // traversal iterator
244 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
245 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
246 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
247 ecmaRuntimeCallInfo->SetThis(treeSetIterator.GetTaggedValue());
248
249 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
250 JSTaggedValue result = JSAPITreeSetIterator::Next(ecmaRuntimeCallInfo);
251 TestHelper::TearDownFrame(thread, prev);
252
253 JSHandle<JSObject> resultObj(thread, result);
254 std::string resultKeyAndValue = setKey + std::to_string(i + 1U);
255 key.Update(factory->NewFromStdString(resultKeyAndValue).GetTaggedValue());
256
257 JSHandle<JSTaggedValue> keyValueArr(JSObject::GetProperty(thread, resultObj, valueStr).GetValue());
258 for (int index = 0; index < 2; index++) {
259 JSHandle<JSTaggedValue> indexValue(thread, JSTaggedValue(index));
260 EXPECT_EQ(JSTaggedValue::SameValue(
261 JSObject::GetProperty(thread, keyValueArr, indexValue).GetValue(), key), true);
262 }
263 EXPECT_EQ(treeSetIterator->GetNextIndex(), (i + 1U));
264 }
265 }
266 } // namespace panda::ecmascript
267