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_linked_list.h"
21 #include "ecmascript/js_api/js_api_linked_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 JSAPILinkedListIteratorTest : 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:
CreateLinkedList()65 JSAPILinkedList *CreateLinkedList()
66 {
67 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
68 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
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::LinkedList)));
79
80 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
81 JSHandle<JSTaggedValue> contianer =
82 JSHandle<JSTaggedValue>(thread, ContainersPrivate::Load(objCallInfo));
83 JSHandle<JSAPILinkedList> linkedList =
84 JSHandle<JSAPILinkedList>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(contianer),
85 contianer));
86 JSTaggedValue doubleList = TaggedDoubleList::Create(thread);
87 linkedList->SetDoubleList(thread, doubleList);
88 return *linkedList;
89 }
90 };
91
92 /**
93 * @tc.name: Next and CreateLinkedListIterator
94 * @tc.desc:
95 * @tc.type: FUNC
96 * @tc.require:
97 */
HWTEST_F_L0(JSAPILinkedListIteratorTest,NextCreateLinkedListIterator)98 HWTEST_F_L0(JSAPILinkedListIteratorTest, NextCreateLinkedListIterator)
99 {
100 JSHandle<JSAPILinkedList> linkedList(thread, CreateLinkedList());
101 uint32_t elementsNum = 256;
102 for (uint32_t i = 0; i < elementsNum; i++) {
103 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
104 JSAPILinkedList::Add(thread, linkedList, value);
105 }
106 JSHandle<JSTaggedValue> taggedValueHandle(thread, linkedList.GetTaggedValue());
107
108 JSHandle<JSAPILinkedListIterator> linkedListIterator(
109 thread, JSAPILinkedListIterator::CreateLinkedListIterator(thread, taggedValueHandle).GetTaggedValue());
110 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
111 uint32_t capacity = static_cast<uint32_t>(linkedList->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(linkedListIterator.GetTaggedValue());
116
117 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
118 JSTaggedValue result = JSAPILinkedListIterator::Next(ecmaRuntimeCallInfo);
119 TestHelper::TearDownFrame(thread, prev);
120
121 JSHandle<JSObject> resultObj(thread, result);
122 if (i < elementsNum) {
123 EXPECT_EQ(linkedListIterator->GetNextIndex(), i + 1U);
124 EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
125 JSTaggedValue(i));
126 } else {
127 EXPECT_EQ(linkedListIterator->GetIteratedLinkedList(), 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 = JSAPILinkedListIterator::Next(ecmaRuntimeCallInfo);
140 TestHelper::TearDownFrame(thread, prev);
141 EXPECT_EQ(result, JSTaggedValue::Exception());
142 EXPECT_EXCEPTION();
143
144 // test CreateLinkedListIterator exception
145 JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
146 JSHandle<JSTaggedValue> result1 = JSAPILinkedListIterator::CreateLinkedListIterator(thread, undefined);
147 EXPECT_EQ(result1.GetTaggedValue(), JSTaggedValue::Exception());
148 EXPECT_EXCEPTION();
149 }
150 } // namespace panda::test
151