• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase()
28     {
29         GTEST_LOG_(INFO) << "SetUpTestCase";
30     }
31 
TearDownTestCase()32     static void TearDownTestCase()
33     {
34         GTEST_LOG_(INFO) << "TearDownCase";
35     }
36 
SetUp()37     void SetUp() override
38     {
39         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40     }
41 
TearDown()42     void TearDown() override
43     {
44         TestHelper::DestroyEcmaVMWithScope(instance, scope);
45     }
46     EcmaVM *instance {nullptr};
47     ecmascript::EcmaHandleScope *scope {nullptr};
48     JSThread *thread {nullptr};
49 };
50 
51 /*
52  * Feature: JSArrayIterator
53  * Function: SetIteratedArray
54  * SubFunction: GetIteratedArray
55  * FunctionPoints: Set Iterated Array
56  * CaseDescription: Call the "SetIteratedArray" function, check whether the result returned through "GetIteratedArray"
57  *                  function from the JSArrayIterator is within expectations.
58  */
HWTEST_F_L0(JSArrayIteratorTest,SetIteratedArray)59 HWTEST_F_L0(JSArrayIteratorTest, SetIteratedArray)
60 {
61     EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
62     ObjectFactory *factory = ecmaVMPtr->GetFactory();
63 
64     int32_t arrayFrom1[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100};
65     int32_t arrayFrom2[10] = {1111, 3211, 737, 0, 1267, 174, 2763, 832, 11, 93};
66     int numArrayFrom1 = sizeof(arrayFrom1)/sizeof(arrayFrom1[0]);
67     int numArrayFrom2 = sizeof(arrayFrom2)/sizeof(arrayFrom2[0]);
68     JSHandle<TaggedArray> handleTaggedArrayFrom1(factory->NewTaggedArray(numArrayFrom1));
69     JSHandle<TaggedArray> handleTaggedArrayFrom2(factory->NewTaggedArray(numArrayFrom2));
70     for (int i = 0; i < numArrayFrom1; i++) {
71         handleTaggedArrayFrom1->Set(thread, i, JSTaggedValue(arrayFrom1[i]));
72     }
73     for (int i = 0; i < numArrayFrom2; i++) {
74         handleTaggedArrayFrom2->Set(thread, i, JSTaggedValue(arrayFrom2[i]));
75     }
76     JSHandle<JSObject> handleJSObjectTaggedArrayFrom1(JSArray::CreateArrayFromList(thread, handleTaggedArrayFrom1));
77     JSHandle<JSObject> handleJSObjectTaggedArrayFrom2(JSArray::CreateArrayFromList(thread, handleTaggedArrayFrom2));
78 
79     // Call "SetIteratedArray" function through "NewJSArrayIterator" function of "object_factory.cpp".
80     JSHandle<JSArrayIterator> handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArrayFrom1,
81         IterationKind::KEY);
82 
83     JSHandle<JSArray> handleJSArrayTo1(thread, JSArray::Cast(handleJSArrayIter->GetIteratedArray().GetTaggedObject()));
84     EXPECT_EQ(handleJSArrayTo1->GetArrayLength(), static_cast<uint32_t>(numArrayFrom1));
85     for (int i = 0; i < numArrayFrom1; i++) {
86         EXPECT_EQ(JSArray::FastGetPropertyByValue(thread, JSHandle<JSTaggedValue>(handleJSArrayTo1), i)->GetNumber(),
87                   arrayFrom1[i]);
88     }
89 
90     // Call "SetIteratedArray" function in this HWTEST_F_L0.
91     handleJSArrayIter->SetIteratedArray(thread, handleJSObjectTaggedArrayFrom2);
92 
93     JSHandle<JSArray> handleJSArrayTo2(thread, JSArray::Cast(handleJSArrayIter->GetIteratedArray().GetTaggedObject()));
94     EXPECT_EQ(handleJSArrayTo2->GetArrayLength(), static_cast<uint32_t>(numArrayFrom2));
95     for (int i = 0; i < numArrayFrom2; i++) {
96         EXPECT_EQ(JSArray::FastGetPropertyByValue(thread, JSHandle<JSTaggedValue>(handleJSArrayTo2), i)->GetNumber(),
97                   arrayFrom2[i]);
98     }
99 }
100 
101 /*
102  * Feature: JSArrayIterator
103  * Function: SetNextIndex
104  * SubFunction: GetNextIndex
105  * FunctionPoints: Set Next Index
106  * CaseDescription: Call the "SetNextIndex" function, check whether the result returned through "GetNextIndex" function
107  *                  from the JSArrayIterator is within expectations.
108  */
HWTEST_F_L0(JSArrayIteratorTest,SetNextIndex)109 HWTEST_F_L0(JSArrayIteratorTest, SetNextIndex)
110 {
111     EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
112     ObjectFactory *factory = ecmaVMPtr->GetFactory();
113 
114     int32_t array[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100};
115     int numArray = sizeof(array)/sizeof(array[0]);
116     JSHandle<TaggedArray> handleTaggedArray(factory->NewTaggedArray(numArray));
117     for (int i = 0; i < numArray; i++) {
118         handleTaggedArray->Set(thread, i, JSTaggedValue(array[i]));
119     }
120     JSHandle<JSObject> handleJSObjectTaggedArray(JSArray::CreateArrayFromList(thread, handleTaggedArray));
121 
122     // Call "SetNextIndex" function through "NewJSArrayIterator" function of "object_factory.cpp".
123     JSHandle<JSArrayIterator> handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray,
124         IterationKind::KEY);
125     EXPECT_EQ(handleJSArrayIter->GetNextIndex(), 0U);
126 
127     uint32_t testQuantity = 100;
128     for (uint32_t i = 1; i <= testQuantity; i++) {
129         // Call "SetNextIndex" function in this HWTEST_F_L0.
130         handleJSArrayIter->SetNextIndex(i);
131         EXPECT_EQ(handleJSArrayIter->GetNextIndex(), i);
132     }
133 }
134 
135 /*
136  * Feature: JSArrayIterator
137  * Function: SetIterationKind
138  * SubFunction: GetIterationKind
139  * FunctionPoints: Set Iteration Kind
140  * CaseDescription: Call the "SetIterationKind" function, check whether the result returned through "GetIterationKind"
141  *                  function from the JSArrayIterator is within expectations.
142  */
HWTEST_F_L0(JSArrayIteratorTest,SetIterationKind)143 HWTEST_F_L0(JSArrayIteratorTest, SetIterationKind)
144 {
145     EcmaVM *ecmaVMPtr = thread->GetEcmaVM();
146     ObjectFactory *factory = ecmaVMPtr->GetFactory();
147 
148     int32_t array[10] = {0, 6, 8, 99, 200, 1, -1, -199, 33, 100};
149     int numArray = sizeof(array)/sizeof(array[0]);
150     JSHandle<TaggedArray> handleTaggedArray(factory->NewTaggedArray(numArray));
151     for (int i = 0; i < numArray; i++) {
152         handleTaggedArray->Set(thread, i, JSTaggedValue(array[i]));
153     }
154     JSHandle<JSObject> handleJSObjectTaggedArray(JSArray::CreateArrayFromList(thread, handleTaggedArray));
155 
156     // Call "SetIterationKind" function through "NewJSArrayIterator" function of "object_factory.cpp".
157     JSHandle<JSArrayIterator> handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray,
158         IterationKind::KEY);
159     EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY);
160     handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, IterationKind::VALUE);
161     EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::VALUE);
162     handleJSArrayIter = factory->NewJSArrayIterator(handleJSObjectTaggedArray, IterationKind::KEY_AND_VALUE);
163     EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY_AND_VALUE);
164 
165     // Call "SetIterationKind" function in this HWTEST_F_L0.
166     handleJSArrayIter->SetIterationKind(IterationKind::KEY);
167     EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY);
168     handleJSArrayIter->SetIterationKind(IterationKind::VALUE);
169     EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::VALUE);
170     handleJSArrayIter->SetIterationKind(IterationKind::KEY_AND_VALUE);
171     EXPECT_EQ(handleJSArrayIter->GetIterationKind(), IterationKind::KEY_AND_VALUE);
172 }
173 }  // namespace panda::test
174