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