1 /*
2 * Copyright (c) 2021 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/global_env.h"
17 #include "ecmascript/js_array.h"
18 #include "ecmascript/js_array_iterator.h"
19 #include "ecmascript/object_factory.h"
20 #include "ecmascript/tests/test_helper.h"
21
22 using namespace panda::ecmascript;
23
24 namespace panda::test {
25 class JSArrayIteratorTest : public BaseTestWithScope<false> {
26 };
27
28 /*
29 * Feature: JSArrayIterator
30 * Function: SetIteratedArray
31 * SubFunction: GetIteratedArray
32 * FunctionPoints: Set Iterated Array
33 * CaseDescription: Call the "SetIteratedArray" function, check whether the result returned through "GetIteratedArray"
34 * function from the JSArrayIterator is within expectations.
35 */
HWTEST_F_L0(JSArrayIteratorTest,SetIteratedArray)36 HWTEST_F_L0(JSArrayIteratorTest, SetIteratedArray)
37 {
38 EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
39 ObjectFactory *factory = ecmaVMPtr->GetFactory();
40
41 int32_t arrayFrom1[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100};
42 int32_t arrayFrom2[10] = {1111, 3211, 737, 0, 1267, 174, 2763, 832, 11, 93};
43 int numArrayFrom1 = sizeof(arrayFrom1)/sizeof(arrayFrom1[0]);
44 int numArrayFrom2 = sizeof(arrayFrom2)/sizeof(arrayFrom2[0]);
45 JSHandle<TaggedArray> handleTaggedArrayFrom1(factory->NewTaggedArray(numArrayFrom1));
46 JSHandle<TaggedArray> handleTaggedArrayFrom2(factory->NewTaggedArray(numArrayFrom2));
47 for (int i = 0; i < numArrayFrom1; i++) {
48 handleTaggedArrayFrom1->Set(thread, i, JSTaggedValue(arrayFrom1[i]));
49 }
50 for (int i = 0; i < numArrayFrom2; i++) {
51 handleTaggedArrayFrom2->Set(thread, i, JSTaggedValue(arrayFrom2[i]));
52 }
53 JSHandle<JSObject> handleJSObjectTaggedArrayFrom1(JSArray::CreateArrayFromList(thread, handleTaggedArrayFrom1));
54 JSHandle<JSObject> handleJSObjectTaggedArrayFrom2(JSArray::CreateArrayFromList(thread, handleTaggedArrayFrom2));
55
56 // Call "SetIteratedArray" function through "NewJSArrayIterator" function of "object_factory.cpp".
57 JSHandle<JSArrayIterator> handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArrayFrom1,
58 IterationKind::KEY);
59
60 JSHandle<JSArray> handleJSArrayTo1(thread,
61 JSArray::Cast(handleJSArrayIter->GetIteratedArray(thread).GetTaggedObject()));
62 EXPECT_EQ(handleJSArrayTo1->GetArrayLength(), static_cast<uint32_t>(numArrayFrom1));
63 for (int i = 0; i < numArrayFrom1; i++) {
64 EXPECT_EQ(JSArray::FastGetPropertyByValue(thread, JSHandle<JSTaggedValue>(handleJSArrayTo1), i)->GetNumber(),
65 arrayFrom1[i]);
66 }
67
68 // Call "SetIteratedArray" function in this HWTEST_F_L0.
69 handleJSArrayIter->SetIteratedArray(thread, handleJSObjectTaggedArrayFrom2);
70
71 JSHandle<JSArray> handleJSArrayTo2(thread,
72 JSArray::Cast(handleJSArrayIter->GetIteratedArray(thread).GetTaggedObject()));
73 EXPECT_EQ(handleJSArrayTo2->GetArrayLength(), static_cast<uint32_t>(numArrayFrom2));
74 for (int i = 0; i < numArrayFrom2; i++) {
75 EXPECT_EQ(JSArray::FastGetPropertyByValue(thread, JSHandle<JSTaggedValue>(handleJSArrayTo2), i)->GetNumber(),
76 arrayFrom2[i]);
77 }
78 }
79
SetCommon(JSThread * thread)80 static JSHandle<JSObject> SetCommon(JSThread *thread)
81 {
82 EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
83 ObjectFactory *factory = ecmaVMPtr->GetFactory();
84
85 int32_t array[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100};
86 int numArray = sizeof(array)/sizeof(array[0]);
87 JSHandle<TaggedArray> handleTaggedArray(factory->NewTaggedArray(numArray));
88 for (int i = 0; i < numArray; i++) {
89 handleTaggedArray->Set(thread, i, JSTaggedValue(array[i]));
90 }
91 JSHandle<JSObject> handleJSObjectTaggedArray(JSArray::CreateArrayFromList(thread, handleTaggedArray));
92 return handleJSObjectTaggedArray;
93 }
94 /*
95 * Feature: JSArrayIterator
96 * Function: SetNextIndex
97 * SubFunction: GetNextIndex
98 * FunctionPoints: Set Next Index
99 * CaseDescription: Call the "SetNextIndex" function, check whether the result returned through "GetNextIndex" function
100 * from the JSArrayIterator is within expectations.
101 */
HWTEST_F_L0(JSArrayIteratorTest,SetNextIndex)102 HWTEST_F_L0(JSArrayIteratorTest, SetNextIndex)
103 {
104 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
105
106 auto handleJSObjectTaggedArray = SetCommon(thread);
107
108 // Call "SetNextIndex" function through "NewJSArrayIterator" function of "object_factory.cpp".
109 JSHandle<JSArrayIterator> handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray,
110 IterationKind::KEY);
111 EXPECT_EQ(handleJSArrayIter->GetNextIndex(), 0U);
112
113 uint32_t testQuantity = 100;
114 for (uint32_t i = 1; i <= testQuantity; i++) {
115 // Call "SetNextIndex" function in this HWTEST_F_L0.
116 handleJSArrayIter->SetNextIndex(i);
117 EXPECT_EQ(handleJSArrayIter->GetNextIndex(), i);
118 }
119 }
120
121 /*
122 * Feature: JSArrayIterator
123 * Function: SetIterationKind
124 * SubFunction: GetIterationKind
125 * FunctionPoints: Set Iteration Kind
126 * CaseDescription: Call the "SetIterationKind" function, check whether the result returned through "GetIterationKind"
127 * function from the JSArrayIterator is within expectations.
128 */
HWTEST_F_L0(JSArrayIteratorTest,SetIterationKind)129 HWTEST_F_L0(JSArrayIteratorTest, SetIterationKind)
130 {
131 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
132
133 auto handleJSObjectTaggedArray = SetCommon(thread);
134 // Call "SetIterationKind" function through "NewJSArrayIterator" function of "object_factory.cpp".
135 JSHandle<JSArrayIterator> handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray,
136 IterationKind::KEY);
137 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY);
138 handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, IterationKind::VALUE);
139 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::VALUE);
140 handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, IterationKind::KEY_AND_VALUE);
141 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY_AND_VALUE);
142
143 // Call "SetIterationKind" function in this HWTEST_F_L0.
144 handleJSArrayIter->SetIterationKind(IterationKind::KEY);
145 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY);
146 handleJSArrayIter->SetIterationKind(IterationKind::VALUE);
147 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::VALUE);
148 handleJSArrayIter->SetIterationKind(IterationKind::KEY_AND_VALUE);
149 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY_AND_VALUE);
150 }
151 } // namespace panda::test
152