• 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/containers/containers_private.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/ecma_runtime_call_info.h"
19 #include "ecmascript/js_tagged_value.h"
20 #include "ecmascript/js_api/js_api_arraylist.h"
21 #include "ecmascript/js_api/js_api_arraylist_iterator.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/object_factory.h"
24 #include "ecmascript/tests/test_helper.h"
25 
26 using namespace panda;
27 using namespace panda::ecmascript;
28 
29 namespace panda::test {
30 class JSAPIArrayListIteratorTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase()
33     {
34         GTEST_LOG_(INFO) << "SetUpTestCase";
35     }
36 
TearDownTestCase()37     static void TearDownTestCase()
38     {
39         GTEST_LOG_(INFO) << "TearDownCase";
40     }
41 
SetUp()42     void SetUp() override
43     {
44         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
45     }
46 
TearDown()47     void TearDown() override
48     {
49         TestHelper::DestroyEcmaVMWithScope(instance, scope);
50     }
51 
52     EcmaVM *instance {nullptr};
53     EcmaHandleScope *scope {nullptr};
54     JSThread *thread {nullptr};
55 protected:
CreateArrayList()56     JSAPIArrayList *CreateArrayList()
57     {
58         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
59         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
60 
61         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
62         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
63         JSHandle<JSTaggedValue> value =
64             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
65 
66         auto objCallInfo =
67             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); // 6 means the value
68         objCallInfo->SetFunction(JSTaggedValue::Undefined());
69         objCallInfo->SetThis(value.GetTaggedValue());
70         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::ArrayList)));
71 
72         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
73         JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
74         TestHelper::TearDownFrame(thread, prev);
75 
76         JSHandle<JSTaggedValue> constructor(thread, result);
77         JSHandle<JSAPIArrayList> arrayList(
78             factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
79         JSHandle<TaggedArray> taggedArray = factory->NewTaggedArray(JSAPIArrayList::DEFAULT_CAPACITY_LENGTH);
80         arrayList->SetElements(thread, taggedArray);
81         return *arrayList;
82     }
83 };
84 
85 /**
86  * @tc.name: Next
87  * @tc.desc:
88  * @tc.type: FUNC
89  * @tc.require:
90  */
HWTEST_F_L0(JSAPIArrayListIteratorTest,Next)91 HWTEST_F_L0(JSAPIArrayListIteratorTest, Next)
92 {
93     JSHandle<JSAPIArrayList> arrayList(thread, CreateArrayList());
94     uint32_t elementsNum = 256;
95     for (uint32_t i = 0; i < elementsNum; i++) {
96         JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
97         JSAPIArrayList::Add(thread, arrayList, value);
98     }
99     JSHandle<JSAPIArrayListIterator> arrayListIterator(thread, JSAPIArrayList::GetIteratorObj(thread, arrayList));
100     JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
101     uint32_t capacity = JSAPIArrayList::GetCapacity(thread, arrayList);
102     for (uint32_t i = 0; i < capacity; i++) {
103         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
104         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
105         ecmaRuntimeCallInfo->SetThis(arrayListIterator.GetTaggedValue());
106 
107         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
108         JSTaggedValue result = JSAPIArrayListIterator::Next(ecmaRuntimeCallInfo);
109         TestHelper::TearDownFrame(thread, prev);
110 
111         JSHandle<JSObject> resultObj(thread, result);
112         if (i < elementsNum) {
113             EXPECT_EQ(arrayListIterator->GetNextIndex(), i + 1U);
114             EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
115                 JSTaggedValue(i));
116         } else {
117             EXPECT_EQ(arrayListIterator->GetIteratedArrayList(), JSTaggedValue::Undefined());
118             EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
119                 JSTaggedValue::Undefined());
120         }
121     }
122 
123     // test Next exception
124     {
125         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
126         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
127         ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
128 
129         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
130         JSTaggedValue result = JSAPIArrayListIterator::Next(ecmaRuntimeCallInfo);
131         TestHelper::TearDownFrame(thread, prev);
132         EXPECT_EQ(result, JSTaggedValue::Exception());
133         EXPECT_EXCEPTION();
134     }
135 }
136 } // namespace panda::test
137