• 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:
21 */
22var PlainArray = undefined;
23if (globalThis["ArkPrivate"] != undefined) {
24    PlainArray = ArkPrivate.Load(ArkPrivate.PlainArray);
25
26    let map = new Map();
27    let plainArray = new PlainArray();
28    let proxy = new Proxy(plainArray, {});
29    let testArray = ["0", "1", "2", "3", "4", "5"]
30    let res = true
31    proxy.add(0, "0")
32    proxy.add(1, "1")
33    proxy.add(2, "2")
34    proxy.add(3, "3")
35    proxy.add(4, "4")
36    proxy.add(5, "5")
37
38    for(let i = 0; i < testArray.length; i++) {
39        if (proxy[i] !== testArray[i]) {
40            res = false
41        }
42    }
43    map.set("test plainarray add:", res)
44    map.set("test plainarray length:", proxy.length === 6)
45    map.set("test plainarray has:", proxy.has(2))
46    map.set("test plainarray getIndexOfValue:", proxy.getIndexOfValue("1") === 1)
47    map.set("test plainarray getIndexOfKey:", proxy.getIndexOfKey(5) === 5)
48    map.set("test plainarray getKeyAt:", proxy.getKeyAt(1) === 1)
49    map.set("test plainarray getValueAt:", proxy.getValueAt(2) === "2")
50
51    let newPlainArray = proxy.clone()
52    res = true
53    for(let i = 0; i < testArray.length; i++) {
54        if (newPlainArray[i] !== testArray[i]) {
55            res = false
56        }
57    }
58    map.set("test plainarray clone:", res)
59
60    const removeRes = proxy.remove(3)
61    testArray.splice(3, 1)
62    map.set("test plainarray removeRes:", removeRes)
63
64    proxy.removeAt(2)
65    testArray.splice(2, 1)
66    res = true
67    for(let i = 0; i < testArray.length; i++) {
68        if (proxy.get(i) !== testArray[i]) {
69            res = false
70        }
71    }
72
73    let removeFrom = proxy.removeRangeFrom(1, 2)
74    testArray.splice(1, 2)
75
76    map.set("test plainarray removeRangeFrom:", removeFrom)
77
78    res = true
79    proxy.forEach((i, d) => {
80    })
81
82    map.set("test plainarray forEach:", res)
83
84    res = true
85    let testArray3 = [0, 5]
86    let j = 0
87    for (const data of proxy) {
88    }
89    map.set("test plainarray for of:", res)
90
91    res = true
92    let itr = proxy[Symbol.iterator]();
93    let tmp = undefined;
94    let testArray1 = []
95    do {
96      tmp = itr.next().value;
97      testArray1.push(tmp);
98    } while (tmp != undefined);
99    map.set("test plainarray Symbol.iterator:", res)
100
101    let arr2 = new PlainArray();
102    let proxy1 = new Proxy(arr2, {});
103    proxy1.add(0, "0")
104    proxy1.add(1, "1")
105    proxy1.add(2, "2")
106    proxy1.add(3, "3")
107    proxy1.add(4, "4")
108    proxy1.add(5, "5")
109    proxy1.setValueAt(2, "123")
110    map.set("test plainarray setValueAt and get:", proxy1.get(2) === "123")
111    proxy1.clear()
112    map.set("test plainarray clear:", proxy1.length === 0)
113    map.set("test plainarray isEmpty:", proxy1.isEmpty())
114    proxy1.add(0, "0")
115    proxy1.add(1, "1")
116    proxy1.add(2, "2")
117    proxy1.add(3, "3")
118    proxy1.add(4, "4")
119    proxy1.add(5, "5")
120    map.set("test plainarray toString:", proxy1.toString() == "0:0,1:1,2:2,3:3,4:4,5:5");
121
122    res = undefined;
123    function elements(value, key, map) {
124        if (!value) {
125            if (!res) {
126                res = [];
127            }
128            res.push(key);
129        }
130    }
131    map.forEach(elements);
132
133    let de = new PlainArray();
134    try {
135        de.forEach(123);
136    } catch(err) {
137        if (err.name != "BusinessError") {
138            print("PlainArray forEach throw error fail");
139        }
140    }
141    if (!res) {
142        print("Test PlainArray success!!!");
143    } else {
144        print("Test PlainArray fail: " + res);
145    }
146}
147