• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/*
17 * @tc.name:container
18 * @tc.desc:test container
19 * @tc.type: FUNC
20 * @tc.require: issueI5NO8G
21 */
22var List = undefined;
23if (globalThis["ArkPrivate"] != undefined) {
24    List = ArkPrivate.Load(ArkPrivate.List);
25    let list = new List();
26    const testArray = []
27    let map = new Map();
28    map.set("test list isEmpty1:", list.isEmpty() === true)
29    for(let i = 0; i < 10; i++) {
30        list.add(i)
31        testArray.push(i)
32    }
33
34    map.set("test list isEmpty2:", list.isEmpty() === false)
35    map.set("test list get 1:", list.get(1) === 1)
36    map.set("test list has:",  list.has(8))
37    map.set("test list not has:", list.has(123) === false)
38
39    let list1 = new List();
40    const testArray2 = []
41    for(let i = 0; i < 10; i++) {
42        list1.add(i)
43        testArray2.push(i)
44    }
45
46    map.set("test list equal:", list.equal(list1))
47    list.add(10)
48    testArray.push(10)
49    map.set("test list equal:", list.equal(list1) === false)
50    map.set("test list getLastIndexOf:", list.getLastIndexOf(1) === 1)
51    map.set("test list getIndexOf:", list.getIndexOf(5) === 5)
52
53    list.removeByIndex(10)
54    testArray.splice(10, 1)
55    let res = true
56    for(let i = 0; i < testArray.length; i++) {
57        if (list[i] !== testArray[i]) {
58            res = false
59        }
60    }
61    map.set("test list removeByIndex:", res)
62
63    list.remove(9)
64    testArray.splice(9, 1)
65    res = true
66    for(let i = 0; i < testArray.length; i++) {
67        if (list[i] !== testArray[i]) {
68            res = false
69        }
70        testArray[i] = testArray[i] * 2
71    }
72    map.set("test list remove:", res)
73
74    list.replaceAllElements((item, index) => {
75        return item * 2
76    })
77    res = true
78    for(let i = 0; i < testArray.length; i++) {
79        if (list[i] !== testArray[i] * 2) {
80            res = false
81        }
82    }
83    map.set("test list replaceAllElements:", res)
84    map.set("test list getFirst:", list.getFirst() === 0)
85    map.set("test list getLast:", list.getLast() === 16)
86    list.insert(999, 3)
87    testArray.splice(3, 0, 999)
88    res = true
89    for(let i = 0; i < testArray.length; i++) {
90        if (list[i] !== testArray[i]) {
91            res = false
92        }
93    }
94    map.set("test list insert:", res)
95
96    list.set(5, 888)
97    testArray[5] = 888
98    res = true
99    for(let i = 0; i < testArray.length; i++) {
100        if (list[i] !== testArray[i]) {
101            res = false
102        }
103    }
104    map.set("test list set:", res)
105
106    let list2 = new List();
107    list2.add(4);
108    list2.add(3);
109    list2.add(1);
110    list2.add(2);
111    list2.add(0);
112    list2.sort((a,b) => a-b);
113    res = true
114    for (let i = 0; i < 5; i++) {
115        if (list2[i] !== i) {
116            res = false
117        }
118    }
119    map.set("test list sort:", res)
120
121    res = true
122    let subList = list.getSubList(1, 3)
123    const newtestArray = testArray.slice(1, 3)
124    for(let i = 0; i < subList.length; i++) {
125        if (newtestArray[i] !== subList[i]) {
126            res =  false
127        }
128    }
129    map.set("test list getSubList:", res)
130
131    res = true
132    const arr = list.convertToArray()
133    for (let i = 0; i < arr.length; i++) {
134        if (arr[i] !== testArray[i]) {
135            res = false
136        }
137    }
138    map.set("test list convertToArray:", res)
139
140    res = true
141    let i = 0
142    for (const data of list) {
143        if (data !== testArray[i]) {
144            res = false
145        }
146        i++;
147    }
148    map.set("test list itertor:", res)
149
150    res = true
151    list1.forEach((i, d) => {
152        if (d !== testArray2[i]) {
153            res = false
154        }
155    })
156    map.set("test list forEach:", res)
157    list2.clear()
158    map.set("test list clear:", list2.length === 0)
159    res = true
160    const testArrayClear = []
161    for(let i = 0; i < 10; i++) {
162        list2.add(i)
163        testArrayClear.push(i)
164    }
165    for(let i = 0; i < testArrayClear.length; i++) {
166        if (list2[i] !== testArrayClear[i]) {
167            res = false
168        }
169    }
170    map.set("test list clear and add:", res)
171    map.set("test list get:", list1.get(200) === undefined)
172    map.set("test list getLastIndexOf:", list1.getLastIndexOf('abc') === -1)
173    let flag = false;
174    try {
175        list1.removeByIndex(99)
176    } catch (error) {
177        flag = true;
178    }
179    map.set("test list removeByIndex:", flag)
180    res = list1.remove(888)
181    map.set("test list remove:", !res)
182
183    let list3 = new List();
184    let proxy = new Proxy(list3, {});
185    map.set("test list proxy isEmpty1:", proxy.isEmpty() === true);
186    const testArray3 = []
187    for(let i = 0; i < 10; i++) {
188        proxy.add(i)
189        testArray3.push(i)
190    }
191
192    map.set("test list proxy isEmpty2:", proxy.isEmpty() === false);
193    map.set("test list get 1:", proxy.get(1) === 1)
194    map.set("test list has:",  proxy.has(8))
195    map.set("test list not has:", proxy.has(123) === false)
196
197    let list4 = new List();
198    let proxy1 = new Proxy(list4, {});
199    const testArray4 = []
200    for(let i = 0; i < 10; i++) {
201        proxy1.add(i)
202        testArray4.push(i)
203    }
204
205    proxy.add(10)
206    testArray3.push(10)
207    map.set("test list equal:", proxy.equal(proxy1) === false)
208    map.set("test list getLastIndexOf:", proxy.getLastIndexOf(1) === 1)
209    map.set("test list getIndexOf:", proxy.getIndexOf(5) === 5)
210
211    proxy.removeByIndex(10)
212    testArray3.splice(10, 1)
213    res = true
214    for(let i = 0; i < testArray3.length; i++) {
215        if (proxy[i] !== testArray3[i]) {
216            res = false
217        }
218    }
219    map.set("test list removeByIndex:", res)
220
221    proxy.remove(9)
222    testArray3.splice(9, 1)
223    res = true
224    for(let i = 0; i < testArray3.length; i++) {
225        if (proxy[i] !== testArray3[i]) {
226            res = false
227        }
228        testArray3[i] = testArray3[i] * 2
229    }
230    map.set("test list remove:", res)
231
232    proxy.replaceAllElements((item, index) => {
233        return item * 2
234    })
235    res = true
236    for(let i = 0; i < testArray3.length; i++) {
237        if (proxy[i] !== testArray3[i]) {
238            res = false
239        }
240    }
241    map.set("test list replaceAllElements:", res)
242    map.set("test list getFirst:", proxy.getFirst() === 0)
243    map.set("test list getLast:", proxy.getLast() === 16)
244    proxy.insert(999, 3)
245    testArray3.splice(3, 0, 999)
246    res = true
247    for(let i = 0; i < testArray3.length; i++) {
248        if (proxy[i] !== testArray3[i]) {
249            res = false
250        }
251    }
252    map.set("test list insert:", res)
253
254    proxy.set(5, 888)
255    testArray3[5] = 888
256    res = true
257    for(let i = 0; i < testArray3.length; i++) {
258        if (proxy[i] !== testArray3[i]) {
259            res = false
260        }
261    }
262    map.set("test list set:", res)
263
264    let list5 = new List();
265    let proxy2 = new Proxy(list5, {});
266    proxy2.add(4);
267    proxy2.add(3);
268    proxy2.add(1);
269    proxy2.add(2);
270    proxy2.add(0);
271    proxy2.sort((a,b) => a-b);
272    res = true
273    for (let i = 0; i < 5; i++) {
274        if (proxy2[i] !== i) {
275            res = false
276        }
277    }
278    map.set("test list sort:", res)
279
280    res = true
281    let subList1 = proxy.getSubList(1, 3)
282    const newtestArray1 = testArray3.slice(1, 3)
283    for(let i = 0; i < subList1.length; i++) {
284        if (newtestArray1[i] !== subList1[i]) {
285            res =  false
286        }
287    }
288    map.set("test list getSubList:", res)
289
290    res = true
291    const arr1 = proxy.convertToArray()
292    for (let i = 0; i < arr1.length; i++) {
293        if (arr1[i] !== testArray3[i]) {
294            res = false
295        }
296    }
297    map.set("test list convertToArray:", res)
298
299    res = true
300    let k = 0
301    for (const data of proxy1) {
302        if (data !== testArray4[k]) {
303            res = false
304        }
305        k++;
306    }
307    map.set("test list iterator:", res)
308
309    res = true
310    proxy1.forEach((i, d) => {
311        if (d !== testArray4[i]) {
312            res = false
313        }
314    })
315    map.set("test list forEach:", res)
316    proxy2.clear()
317    map.set("test list clear:", proxy2.length === 0)
318    map.set("test list get:", proxy1.get(200) === undefined)
319    map.set("test list getLastIndexOf:", proxy1.getLastIndexOf('abc') === -1)
320    flag = false;
321    try {
322        proxy1.removeByIndex(99)
323    } catch (error) {
324        flag = true;
325    }
326    map.set("test list removeByIndex:", flag)
327    res = proxy1.remove(888)
328    map.set("test list remove:", !res)
329    flag = undefined;
330    function elements(value, key, map) {
331        if (!value) {
332            if (!flag) {
333                flag = [];
334            }
335            flag.push(key);
336        }
337    }
338    map.forEach(elements);
339
340    let de = new List();
341    try {
342        de.forEach(123);
343    } catch(err) {
344        if (err.name != "BusinessError") {
345            print("List forEach throw error fail");
346        }
347    }
348    if (!flag) {
349        print("Test List success!!!");
350    } else {
351        print("Test List fail: " + flag);
352    }
353}
354