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