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_vector_iterator.h"
17 #include "ecmascript/containers/containers_private.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_api/js_api_vector.h"
20 #include "ecmascript/js_iterator.h"
21 #include "ecmascript/tagged_tree.h"
22 #include "ecmascript/tests/test_helper.h"
23
24 using namespace panda;
25 using namespace panda::ecmascript;
26
27 namespace panda::test {
28 class JSAPIVectorIteratorTest : public BaseTestWithScope<false> {
29 protected:
CreateVector()30 JSHandle<JSAPIVector> CreateVector()
31 {
32 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
33 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
34
35 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
36 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
37 JSHandle<JSTaggedValue> value =
38 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
39
40 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
41 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
42 ecmaRuntimeCallInfo->SetThis(value.GetTaggedValue());
43 ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::Vector)));
44
45 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
46 JSTaggedValue result = containers::ContainersPrivate::Load(ecmaRuntimeCallInfo);
47 TestHelper::TearDownFrame(thread, prev);
48
49 JSHandle<JSTaggedValue> constructor(thread, result);
50 JSHandle<JSAPIVector> jsVector(
51 factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
52 jsVector->SetLength(0);
53 return jsVector;
54 }
55 };
56
57 /**
58 * @tc.name: SetIteratedSet
59 * @tc.desc: Call the "SetIteratedSet" function, check whether the result returned through "GetIteratedSet"
60 * function from the JSAPIVectorIterator is within expectations.
61 * @tc.type: FUNC
62 * @tc.require:
63 */
HWTEST_F_L0(JSAPIVectorIteratorTest,SetIteratedVector)64 HWTEST_F_L0(JSAPIVectorIteratorTest, SetIteratedVector)
65 {
66 constexpr uint32_t DEFAULT_LENGTH = 10;
67 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
68 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
69 JSHandle<JSAPIVector> jsVector = CreateVector();
70 EXPECT_TRUE(*jsVector != nullptr);
71 JSHandle<JSAPIVectorIterator> vectorIterator = factory->NewJSAPIVectorIterator(jsVector);
72
73 std::string vectorValue("vectorvalue");
74 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
75 std::string ivalue = vectorValue + std::to_string(i);
76 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
77 JSAPIVector::Add(thread, jsVector, value);
78 }
79 vectorIterator->SetIteratedVector(thread, jsVector.GetTaggedValue());
80 JSHandle<JSAPIVector> VectorTo(thread,
81 JSAPIVector::Cast(vectorIterator->GetIteratedVector(thread).GetTaggedObject()));
82 EXPECT_EQ(VectorTo->GetSize(), static_cast<int>(DEFAULT_LENGTH));
83 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
84 std::string ivalue = vectorValue + std::to_string(i);
85 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
86 EXPECT_EQ(JSAPIVector::Get(thread, jsVector, i), value.GetTaggedValue());
87 }
88 }
89
90 /**
91 * @tc.name: SetNextIndex
92 * @tc.desc: Call the "SetNextIndex" function, check whether the result returned through "GetNextIndex"
93 * function from the JSAPIVectorIterator is within expectations.
94 * @tc.type: FUNC
95 * @tc.require:
96 */
HWTEST_F_L0(JSAPIVectorIteratorTest,SetNextIndex)97 HWTEST_F_L0(JSAPIVectorIteratorTest, SetNextIndex)
98 {
99 constexpr uint32_t DEFAULT_LENGTH = 10;
100 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
101 JSHandle<JSAPIVector> jsVector = CreateVector();
102 EXPECT_TRUE(*jsVector != nullptr);
103 JSHandle<JSAPIVectorIterator> vectorIterator = factory->NewJSAPIVectorIterator(jsVector);
104 EXPECT_EQ(vectorIterator->GetNextIndex(), 0U);
105
106 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
107 vectorIterator->SetNextIndex(i);
108 EXPECT_EQ(vectorIterator->GetNextIndex(), i);
109 }
110 }
111
112 /**
113 * @tc.name: Next
114 * @tc.desc: Create an iterator of JSAPIVector,and then loop through the elements of the iterator to check
115 * whether the elements through "Next" function are consistent.
116 * @tc.type: FUNC
117 * @tc.require:
118 */
HWTEST_F_L0(JSAPIVectorIteratorTest,Next)119 HWTEST_F_L0(JSAPIVectorIteratorTest, Next)
120 {
121 constexpr uint32_t DEFAULT_LENGTH = 10;
122 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
123 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
124 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
125 JSHandle<JSAPIVector> jsVector = CreateVector();
126 std::string vectorValue("vectorvalue");
127 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
128 std::string ivalue = vectorValue + std::to_string(i);
129 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
130 JSAPIVector::Add(thread, jsVector, value);
131 }
132 JSHandle<JSAPIVectorIterator> vectorIterator = factory->NewJSAPIVectorIterator(jsVector);
133 // traversal iterator
134 for (uint32_t i = 0; i <= DEFAULT_LENGTH; i++) {
135 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
136 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
137 ecmaRuntimeCallInfo->SetThis(vectorIterator.GetTaggedValue());
138
139 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
140 JSTaggedValue result = JSAPIVectorIterator::Next(ecmaRuntimeCallInfo);
141 TestHelper::TearDownFrame(thread, prev);
142
143 JSHandle<JSObject> resultObj(thread, result);
144 std::string resultValue = vectorValue + std::to_string(i);
145 if (i <= DEFAULT_LENGTH - 1U) {
146 value.Update(factory->NewFromStdString(resultValue).GetTaggedValue());
147 EXPECT_EQ(JSTaggedValue::SameValue(thread,
148 JSObject::GetProperty(thread, resultObj, valueStr).GetValue(), value), true);
149 }
150 else {
151 EXPECT_TRUE(vectorIterator->GetIteratedVector(thread).IsUndefined());
152 EXPECT_TRUE(JSObject::GetProperty(thread, resultObj, valueStr).GetValue()->IsUndefined());
153 }
154 }
155 }
156
157 /**
158 * @tc.name: Next
159 * @tc.desc: test special return of Next, including throw exception and return undefined
160 * @tc.type: FUNC
161 * @tc.require:
162 */
HWTEST_F_L0(JSAPIVectorIteratorTest,SpecialReturnOfNext)163 HWTEST_F_L0(JSAPIVectorIteratorTest, SpecialReturnOfNext)
164 {
165 JSHandle<JSAPIVector> jsVector = CreateVector();
166 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
167 JSHandle<JSAPIVectorIterator> vectorIterator = factory->NewJSAPIVectorIterator(jsVector);
168 vectorIterator->SetIteratedVector(thread, JSTaggedValue::Undefined());
169
170 // test Next exception
171 {
172 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
173 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
174 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
175
176 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
177 JSTaggedValue result = JSAPIVectorIterator::Next(ecmaRuntimeCallInfo);
178 TestHelper::TearDownFrame(thread, prev);
179 EXPECT_EQ(result, JSTaggedValue::Exception());
180 EXPECT_EXCEPTION();
181 }
182
183 // test Next return undefined
184 {
185 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
186 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
187 ecmaRuntimeCallInfo->SetThis(vectorIterator.GetTaggedValue());
188
189 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
190 JSTaggedValue result = JSAPIVectorIterator::Next(ecmaRuntimeCallInfo);
191 TestHelper::TearDownFrame(thread, prev);
192 EXPECT_EQ(result, thread->GetEcmaVM()->GetGlobalEnv()->GetUndefinedIteratorResult().GetTaggedValue());
193 }
194 }
195 } // namespace panda::ecmascript
196