• 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_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, JSAPIVector::Cast(vectorIterator->GetIteratedVector().GetTaggedObject()));
81     EXPECT_EQ(VectorTo->GetSize(), static_cast<int>(DEFAULT_LENGTH));
82     for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
83         std::string ivalue = vectorValue + std::to_string(i);
84         value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
85         EXPECT_EQ(JSAPIVector::Get(thread, jsVector, i), value.GetTaggedValue());
86     }
87 }
88 
89 /**
90  * @tc.name: SetNextIndex
91  * @tc.desc: Call the "SetNextIndex" function, check whether the result returned through "GetNextIndex"
92  *           function from the JSAPIVectorIterator is within expectations.
93  * @tc.type: FUNC
94  * @tc.require:
95  */
HWTEST_F_L0(JSAPIVectorIteratorTest,SetNextIndex)96 HWTEST_F_L0(JSAPIVectorIteratorTest, SetNextIndex)
97 {
98     constexpr uint32_t DEFAULT_LENGTH = 10;
99     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
100     JSHandle<JSAPIVector> jsVector = CreateVector();
101     EXPECT_TRUE(*jsVector != nullptr);
102     JSHandle<JSAPIVectorIterator> vectorIterator = factory->NewJSAPIVectorIterator(jsVector);
103     EXPECT_EQ(vectorIterator->GetNextIndex(), 0U);
104 
105     for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
106         vectorIterator->SetNextIndex(i);
107         EXPECT_EQ(vectorIterator->GetNextIndex(), i);
108     }
109 }
110 
111 /**
112  * @tc.name: Next
113  * @tc.desc: Create an iterator of JSAPIVector,and then loop through the elements of the iterator to check
114  *           whether the elements through "Next" function are consistent.
115  * @tc.type: FUNC
116  * @tc.require:
117  */
HWTEST_F_L0(JSAPIVectorIteratorTest,Next)118 HWTEST_F_L0(JSAPIVectorIteratorTest, Next)
119 {
120     constexpr uint32_t DEFAULT_LENGTH = 10;
121     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
122     JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
123     JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
124     JSHandle<JSAPIVector> jsVector = CreateVector();
125     std::string vectorValue("vectorvalue");
126     for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
127         std::string ivalue = vectorValue + std::to_string(i);
128         value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
129         JSAPIVector::Add(thread, jsVector, value);
130     }
131     JSHandle<JSAPIVectorIterator> vectorIterator = factory->NewJSAPIVectorIterator(jsVector);
132     // traversal iterator
133     for (uint32_t i = 0; i <= DEFAULT_LENGTH; i++) {
134         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
135         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
136         ecmaRuntimeCallInfo->SetThis(vectorIterator.GetTaggedValue());
137 
138         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
139         JSTaggedValue result = JSAPIVectorIterator::Next(ecmaRuntimeCallInfo);
140         TestHelper::TearDownFrame(thread, prev);
141 
142         JSHandle<JSObject> resultObj(thread, result);
143         std::string resultValue = vectorValue + std::to_string(i);
144         if (i <= DEFAULT_LENGTH - 1U) {
145             value.Update(factory->NewFromStdString(resultValue).GetTaggedValue());
146             EXPECT_EQ(JSTaggedValue::SameValue(
147             JSObject::GetProperty(thread, resultObj, valueStr).GetValue(), value), true);
148         }
149         else {
150             EXPECT_TRUE(vectorIterator->GetIteratedVector().IsUndefined());
151             EXPECT_TRUE(JSObject::GetProperty(thread, resultObj, valueStr).GetValue()->IsUndefined());
152         }
153     }
154 }
155 
156 /**
157  * @tc.name: Next
158  * @tc.desc: test special return of Next, including throw exception and return undefined
159  * @tc.type: FUNC
160  * @tc.require:
161  */
HWTEST_F_L0(JSAPIVectorIteratorTest,SpecialReturnOfNext)162 HWTEST_F_L0(JSAPIVectorIteratorTest, SpecialReturnOfNext)
163 {
164     JSHandle<JSAPIVector> jsVector = CreateVector();
165     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
166     JSHandle<JSAPIVectorIterator> vectorIterator = factory->NewJSAPIVectorIterator(jsVector);
167     vectorIterator->SetIteratedVector(thread, JSTaggedValue::Undefined());
168 
169     // test Next exception
170     {
171         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
172         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
173         ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
174 
175         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
176         JSTaggedValue result = JSAPIVectorIterator::Next(ecmaRuntimeCallInfo);
177         TestHelper::TearDownFrame(thread, prev);
178         EXPECT_EQ(result, JSTaggedValue::Exception());
179         EXPECT_EXCEPTION();
180     }
181 
182     // test Next return undefined
183     {
184         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
185         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
186         ecmaRuntimeCallInfo->SetThis(vectorIterator.GetTaggedValue());
187 
188         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
189         JSTaggedValue result = JSAPIVectorIterator::Next(ecmaRuntimeCallInfo);
190         TestHelper::TearDownFrame(thread, prev);
191         EXPECT_EQ(result, thread->GlobalConstants()->GetUndefinedIterResult());
192     }
193 }
194 }  // namespace panda::ecmascript
195