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/ecma_test_common.h"
25
26 using namespace panda;
27 using namespace panda::ecmascript;
28
29 namespace panda::test {
30 class JSAPIArrayListIteratorTest : public BaseTestWithScope<false> {
31 protected:
CreateArrayList()32 JSAPIArrayList *CreateArrayList()
33 {
34 return EcmaContainerCommon::CreateArrayList(thread);
35 }
36 };
37
38 /**
39 * @tc.name: Next
40 * @tc.desc:
41 * @tc.type: FUNC
42 * @tc.require:
43 */
HWTEST_F_L0(JSAPIArrayListIteratorTest,Next)44 HWTEST_F_L0(JSAPIArrayListIteratorTest, Next)
45 {
46 JSHandle<JSAPIArrayList> arrayList(thread, CreateArrayList());
47 uint32_t elementsNum = 256;
48 for (uint32_t i = 0; i < elementsNum; i++) {
49 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
50 JSAPIArrayList::Add(thread, arrayList, value);
51 }
52 JSHandle<JSAPIArrayListIterator> arrayListIterator(thread, JSAPIArrayList::GetIteratorObj(thread, arrayList));
53 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
54 uint32_t capacity = JSAPIArrayList::GetCapacity(thread, arrayList);
55 for (uint32_t i = 0; i < capacity; i++) {
56 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
57 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
58 ecmaRuntimeCallInfo->SetThis(arrayListIterator.GetTaggedValue());
59
60 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
61 JSTaggedValue result = JSAPIArrayListIterator::Next(ecmaRuntimeCallInfo);
62 TestHelper::TearDownFrame(thread, prev);
63
64 JSHandle<JSObject> resultObj(thread, result);
65 if (i < elementsNum) {
66 EXPECT_EQ(arrayListIterator->GetNextIndex(), i + 1U);
67 EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
68 JSTaggedValue(i));
69 } else {
70 EXPECT_EQ(arrayListIterator->GetIteratedArrayList(), JSTaggedValue::Undefined());
71 EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
72 JSTaggedValue::Undefined());
73 }
74 }
75
76 // test Next exception
77 {
78 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
79 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
80 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
81
82 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
83 JSTaggedValue result = JSAPIArrayListIterator::Next(ecmaRuntimeCallInfo);
84 TestHelper::TearDownFrame(thread, prev);
85 EXPECT_EQ(result, JSTaggedValue::Exception());
86 EXPECT_EXCEPTION();
87 }
88 }
89 } // namespace panda::test
90