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, JSArray::Cast(handleJSArrayIter->GetIteratedArray().GetTaggedObject()));
61 EXPECT_EQ(handleJSArrayTo1->GetArrayLength(), static_cast<uint32_t>(numArrayFrom1));
62 for (int i = 0; i < numArrayFrom1; i++) {
63 EXPECT_EQ(JSArray::FastGetPropertyByValue(thread, JSHandle<JSTaggedValue>(handleJSArrayTo1), i)->GetNumber(),
64 arrayFrom1[i]);
65 }
66
67 // Call "SetIteratedArray" function in this HWTEST_F_L0.
68 handleJSArrayIter->SetIteratedArray(thread, handleJSObjectTaggedArrayFrom2);
69
70 JSHandle<JSArray> handleJSArrayTo2(thread, JSArray::Cast(handleJSArrayIter->GetIteratedArray().GetTaggedObject()));
71 EXPECT_EQ(handleJSArrayTo2->GetArrayLength(), static_cast<uint32_t>(numArrayFrom2));
72 for (int i = 0; i < numArrayFrom2; i++) {
73 EXPECT_EQ(JSArray::FastGetPropertyByValue(thread, JSHandle<JSTaggedValue>(handleJSArrayTo2), i)->GetNumber(),
74 arrayFrom2[i]);
75 }
76 }
77
SetCommon(JSThread * thread)78 static JSHandle<JSObject> SetCommon(JSThread *thread)
79 {
80 EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
81 ObjectFactory *factory = ecmaVMPtr->GetFactory();
82
83 int32_t array[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100};
84 int numArray = sizeof(array)/sizeof(array[0]);
85 JSHandle<TaggedArray> handleTaggedArray(factory->NewTaggedArray(numArray));
86 for (int i = 0; i < numArray; i++) {
87 handleTaggedArray->Set(thread, i, JSTaggedValue(array[i]));
88 }
89 JSHandle<JSObject> handleJSObjectTaggedArray(JSArray::CreateArrayFromList(thread, handleTaggedArray));
90 return handleJSObjectTaggedArray;
91 }
92 /*
93 * Feature: JSArrayIterator
94 * Function: SetNextIndex
95 * SubFunction: GetNextIndex
96 * FunctionPoints: Set Next Index
97 * CaseDescription: Call the "SetNextIndex" function, check whether the result returned through "GetNextIndex" function
98 * from the JSArrayIterator is within expectations.
99 */
HWTEST_F_L0(JSArrayIteratorTest,SetNextIndex)100 HWTEST_F_L0(JSArrayIteratorTest, SetNextIndex)
101 {
102 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
103
104 auto handleJSObjectTaggedArray = SetCommon(thread);
105
106 // Call "SetNextIndex" function through "NewJSArrayIterator" function of "object_factory.cpp".
107 JSHandle<JSArrayIterator> handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray,
108 IterationKind::KEY);
109 EXPECT_EQ(handleJSArrayIter->GetNextIndex(), 0U);
110
111 uint32_t testQuantity = 100;
112 for (uint32_t i = 1; i <= testQuantity; i++) {
113 // Call "SetNextIndex" function in this HWTEST_F_L0.
114 handleJSArrayIter->SetNextIndex(i);
115 EXPECT_EQ(handleJSArrayIter->GetNextIndex(), i);
116 }
117 }
118
119 /*
120 * Feature: JSArrayIterator
121 * Function: SetIterationKind
122 * SubFunction: GetIterationKind
123 * FunctionPoints: Set Iteration Kind
124 * CaseDescription: Call the "SetIterationKind" function, check whether the result returned through "GetIterationKind"
125 * function from the JSArrayIterator is within expectations.
126 */
HWTEST_F_L0(JSArrayIteratorTest,SetIterationKind)127 HWTEST_F_L0(JSArrayIteratorTest, SetIterationKind)
128 {
129 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
130
131 auto handleJSObjectTaggedArray = SetCommon(thread);
132 // Call "SetIterationKind" function through "NewJSArrayIterator" function of "object_factory.cpp".
133 JSHandle<JSArrayIterator> handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray,
134 IterationKind::KEY);
135 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY);
136 handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, IterationKind::VALUE);
137 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::VALUE);
138 handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, IterationKind::KEY_AND_VALUE);
139 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY_AND_VALUE);
140
141 // Call "SetIterationKind" function in this HWTEST_F_L0.
142 handleJSArrayIter->SetIterationKind(IterationKind::KEY);
143 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY);
144 handleJSArrayIter->SetIterationKind(IterationKind::VALUE);
145 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::VALUE);
146 handleJSArrayIter->SetIterationKind(IterationKind::KEY_AND_VALUE);
147 EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY_AND_VALUE);
148 }
149 } // namespace panda::test
150