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 JSAPIListTest : public BaseTestWithScope<false> {
39 protected:
CreateList()40 JSAPIList *CreateList()
41 {
42 return EcmaContainerCommon::CreateList(thread);
43 }
44 };
45
HWTEST_F_L0(JSAPIListTest,listCreate)46 HWTEST_F_L0(JSAPIListTest, listCreate)
47 {
48 JSAPIList *list = CreateList();
49 EXPECT_TRUE(list != nullptr);
50 }
51
HWTEST_F_L0(JSAPIListTest,AddHasAndIsEmpty)52 HWTEST_F_L0(JSAPIListTest, AddHasAndIsEmpty)
53 {
54 constexpr int NODE_NUMBERS = 9;
55 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
56 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
57
58 JSHandle<JSAPIList> toor(thread, CreateList());
59
60 EXPECT_TRUE(toor->IsEmpty(thread));
61
62 std::string myValue("myvalue");
63 for (int i = 0; i < NODE_NUMBERS; i++) {
64 std::string ivalue = myValue + std::to_string(i);
65 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
66 JSAPIList::Add(thread, toor, value);
67 }
68 EXPECT_EQ(toor->Length(thread), NODE_NUMBERS);
69 EXPECT_FALSE(toor->IsEmpty(thread));
70
71 EcmaTestCommon::ListAddHasCommon(thread, toor, value, myValue, NODE_NUMBERS);
72 toor->Dump(thread);
73 }
74
HWTEST_F_L0(JSAPIListTest,InsertAndGetLastAndGetFirst)75 HWTEST_F_L0(JSAPIListTest, InsertAndGetLastAndGetFirst)
76 {
77 JSHandle<JSAPIList> toor(thread, CreateList());
78 EcmaTestCommon::InsertAndGetLastCommon<JSAPIList>(thread, toor);
79 }
80
HWTEST_F_L0(JSAPIListTest,GetIndexOfAndGetLastIndexOf)81 HWTEST_F_L0(JSAPIListTest, GetIndexOfAndGetLastIndexOf)
82 {
83 JSHandle<JSAPIList> toor(thread, CreateList());
84 EcmaTestCommon::GetIndexOfAndGetLastIndexOfCommon<JSAPIList>(thread, toor);
85 }
86
HWTEST_F_L0(JSAPIListTest,Remove)87 HWTEST_F_L0(JSAPIListTest, Remove)
88 {
89 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
90 JSHandle<JSAPIList> toor(thread, CreateList());
91 EcmaTestCommon::ListRemoveCommon<JSAPIList>(thread, toor, value);
92
93 value.Update(JSTaggedValue(4));
94 EXPECT_EQ(JSAPIList::RemoveByIndex(thread, toor, 4), value.GetTaggedValue());
95 EXPECT_EQ(toor->Has(thread, value.GetTaggedValue()), false);
96 EXPECT_EQ(toor->Length(thread), 19);
97
98 value.Update(JSTaggedValue(8));
99 EXPECT_EQ(toor->Remove(thread, value.GetTaggedValue()), JSTaggedValue::True());
100 EXPECT_EQ(toor->Has(thread, value.GetTaggedValue()), false);
101 EXPECT_EQ(toor->Length(thread), 18);
102
103 toor->Dump(thread);
104 }
105
HWTEST_F_L0(JSAPIListTest,Clear)106 HWTEST_F_L0(JSAPIListTest, Clear)
107 {
108 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
109 JSHandle<JSAPIList> list(thread, CreateList());
110 JSAPIList::Add(thread, list, value);
111
112 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(2));
113 JSAPIList::Insert(thread, list, value1, 0);
114
115 list->Clear(thread);
116
117 EXPECT_EQ(list->Length(thread), 0);
118 EXPECT_TRUE(list->GetFirst(thread).IsUndefined());
119 }
120
HWTEST_F_L0(JSAPIListTest,Set)121 HWTEST_F_L0(JSAPIListTest, Set)
122 {
123 constexpr uint32_t NODE_NUMBERS = 20;
124 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
125 JSHandle<JSAPIList> toor(thread, CreateList());
126 EcmaTestCommon::ListRemoveCommon<JSAPIList>(thread, toor, value);
127
128 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
129 value.Update(JSTaggedValue(i + 1));
130 JSAPIList::Set(thread, toor, i, value);
131 }
132
133 for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
134 value.Update(JSTaggedValue(i + 1));
135 JSTaggedValue gValue = toor->Get(thread, i);
136 EXPECT_EQ(gValue, value.GetTaggedValue());
137 }
138 }
139
HWTEST_F_L0(JSAPIListTest,GetOwnProperty)140 HWTEST_F_L0(JSAPIListTest, GetOwnProperty)
141 {
142 constexpr uint32_t DEFAULT_LENGTH = 8;
143 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
144 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined());
145 JSHandle<JSAPIList> toor(thread, CreateList());
146
147 std::string listvalue("listvalue");
148 for (uint32_t i = 0; i < DEFAULT_LENGTH; i++) {
149 std::string ivalue = listvalue + std::to_string(i);
150 value.Update(factory->NewFromStdString(ivalue).GetTaggedValue());
151 JSAPIList::Add(thread, toor, value);
152 }
153 // test GetOwnProperty
154 int testInt = 1;
155 JSHandle<JSTaggedValue> listKey1(thread, JSTaggedValue(testInt));
156 EXPECT_TRUE(JSAPIList::GetOwnProperty(thread, toor, listKey1));
157 testInt = 20;
158 JSHandle<JSTaggedValue> listKey2(thread, JSTaggedValue(testInt));
159 EXPECT_FALSE(JSAPIList::GetOwnProperty(thread, toor, listKey2));
160 EXPECT_EXCEPTION();
161
162 // test GetOwnProperty exception
163 JSHandle<JSTaggedValue> undefined(thread, JSTaggedValue::Undefined());
164 EXPECT_FALSE(JSAPIList::GetOwnProperty(thread, toor, undefined));
165 EXPECT_EXCEPTION();
166 }
167
168 /**
169 * @tc.name: GetProperty
170 * @tc.desc:
171 * @tc.type: FUNC
172 * @tc.require:
173 */
HWTEST_F_L0(JSAPIListTest,GetProperty)174 HWTEST_F_L0(JSAPIListTest, GetProperty)
175 {
176 JSHandle<JSAPIList> toor(thread, CreateList());
177 uint32_t elementsNums = 8;
178 for (uint32_t i = 0; i < elementsNums; i++) {
179 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
180 JSAPIList::Add(thread, toor, value);
181 }
182 for (uint32_t i = 0; i < elementsNums; i++) {
183 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(i));
184 OperationResult getPropertyRes = JSAPIList::GetProperty(thread, toor, key);
185 EXPECT_EQ(getPropertyRes.GetValue().GetTaggedValue(), JSTaggedValue(i));
186 }
187 }
188
189 /**
190 * @tc.name: SetProperty
191 * @tc.desc:
192 * @tc.type: FUNC
193 * @tc.require:
194 */
HWTEST_F_L0(JSAPIListTest,SetProperty)195 HWTEST_F_L0(JSAPIListTest, SetProperty)
196 {
197 JSHandle<JSAPIList> toor(thread, CreateList());
198 uint32_t elementsNums = 8;
199 for (uint32_t i = 0; i < elementsNums; i++) {
200 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
201 JSAPIList::Add(thread, toor, value);
202 }
203 for (uint32_t i = 0; i < elementsNums; i++) {
204 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(i));
205 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i * 2)); // 2 : It means double
206 bool setPropertyRes = JSAPIList::SetProperty(thread, toor, key, value);
207 EXPECT_EQ(setPropertyRes, true);
208 }
209 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(-1));
210 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(-1));
211 EXPECT_FALSE(JSAPIList::SetProperty(thread, toor, key, value));
212 JSHandle<JSTaggedValue> key1(thread, JSTaggedValue(elementsNums));
213 EXPECT_FALSE(JSAPIList::SetProperty(thread, toor, key1, value));
214 }
215
216 /**
217 * @tc.name: GetSubList
218 * @tc.desc:
219 * @tc.type: FUNC
220 * @tc.require:
221 */
HWTEST_F_L0(JSAPIListTest,GetSubList)222 HWTEST_F_L0(JSAPIListTest, GetSubList)
223 {
224 JSHandle<JSAPIList> List(thread, CreateList());
225 uint32_t addElementNums = 256;
226 for (uint32_t i = 0; i < addElementNums; i++) {
227 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
228 JSAPIList::Add(thread, List, value);
229 }
230
231 // throw error test
232 uint32_t smallIndex = -1;
233 uint32_t bigIndex = List->Length(thread) + 10;
234 uint32_t zeroIndex = 0;
235
236 // fromIndex < 0
237 JSAPIList::GetSubList(thread, List, smallIndex, zeroIndex);
238 EXPECT_EXCEPTION();
239
240 // fromIndex >= size
241 JSAPIList::GetSubList(thread, List, bigIndex, zeroIndex);
242 EXPECT_EXCEPTION();
243
244 // toIndex <= fromIndex
245 JSAPIList::GetSubList(thread, List, zeroIndex, zeroIndex);
246 EXPECT_EXCEPTION();
247
248 // toIndex < 0
249 JSAPIList::GetSubList(thread, List, zeroIndex, smallIndex);
250 EXPECT_EXCEPTION();
251
252 // toIndex > length
253 JSAPIList::GetSubList(thread, List, zeroIndex, bigIndex);
254 EXPECT_EXCEPTION();
255 }
256
HWTEST_F_L0(JSAPIListTest,OwnKeys)257 HWTEST_F_L0(JSAPIListTest, OwnKeys)
258 {
259 uint32_t elementsNums = 8;
260 JSHandle<JSAPIList> list(thread, CreateList());
261 for (uint32_t i = 0; i < elementsNums; i++) {
262 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(i));
263 JSAPIList::Add(thread, list, value);
264 }
265 JSHandle<TaggedArray> keyArray = JSAPIList::OwnKeys(thread, list);
266 EXPECT_TRUE(keyArray->GetClass()->IsTaggedArray());
267 EXPECT_TRUE(keyArray->GetLength() == elementsNums);
268 for (uint32_t i = 0; i < elementsNums; i++) {
269 ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(thread,
270 *(base::NumberHelper::NumberToString(thread, JSTaggedValue(i))),
271 EcmaString::Cast(keyArray->Get(thread, i).GetTaggedObject())));
272 }
273 }
274 } // namespace panda::test
275