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/ecma_test_common.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 BaseTestWithScope<false> {
39 protected:
CreateList()40 JSAPIList *CreateList()
41 {
42 return EcmaContainerCommon::CreateList(thread);
43 }
44 };
45
46 /**
47 * @tc.name: Next and CreateListIterator
48 * @tc.desc:
49 * @tc.type: FUNC
50 * @tc.require:
51 */
HWTEST_F_L0(JSAPIListIteratorTest,NextCreateListIterator)52 HWTEST_F_L0(JSAPIListIteratorTest, NextCreateListIterator)
53 {
54 JSHandle<JSAPIList> list(thread, CreateList());
55 uint32_t elementsNum = 256;
56 for (uint32_t i = 0; i < elementsNum; i++) {
57 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
58 JSAPIList::Add(thread, list, value);
59 }
60 JSHandle<JSTaggedValue> taggedValueHandle(thread, list.GetTaggedValue());
61
62 JSHandle<JSAPIListIterator> listIterator(
63 thread, JSAPIListIterator::CreateListIterator(thread, taggedValueHandle).GetTaggedValue());
64 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
65 uint32_t capacity = static_cast<uint32_t>(list->Length());
66 for (uint32_t i = 0; i < capacity; i++) {
67 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
68 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
69 ecmaRuntimeCallInfo->SetThis(listIterator.GetTaggedValue());
70
71 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
72 JSTaggedValue result = JSAPIListIterator::Next(ecmaRuntimeCallInfo);
73 TestHelper::TearDownFrame(thread, prev);
74
75 JSHandle<JSObject> resultObj(thread, result);
76 if (i < elementsNum) {
77 EXPECT_EQ(listIterator->GetNextIndex(), i + 1U);
78 EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
79 JSTaggedValue(i));
80 } else {
81 EXPECT_EQ(listIterator->GetIteratedList(), JSTaggedValue::Undefined());
82 EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
83 JSTaggedValue::Undefined());
84 }
85 }
86
87 // test Next exception
88 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
89 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
90 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
91
92 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
93 JSTaggedValue result = JSAPIListIterator::Next(ecmaRuntimeCallInfo);
94 TestHelper::TearDownFrame(thread, prev);
95 EXPECT_EQ(result, JSTaggedValue::Exception());
96 EXPECT_EXCEPTION();
97
98 // test CreateListIterator exception
99 JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
100 JSHandle<JSTaggedValue> result1 = JSAPIListIterator::CreateListIterator(thread, undefined);
101 EXPECT_EQ(result1.GetTaggedValue(), JSTaggedValue::Exception());
102 EXPECT_EXCEPTION();
103 }
104 } // namespace panda::test
105