• 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 fastset = undefined;
23if (globalThis["ArkPrivate"] != undefined) {
24    fastset = ArkPrivate.Load(ArkPrivate.LightWeightSet);
25
26    let res = new Map();
27    let set = new fastset();
28    let proxy = new Proxy(set, {});
29    // test isEmpty: true
30    res.set("test isEmpty:", proxy.isEmpty());
31    proxy.add(1);
32    proxy.add(2);
33    proxy.add(3);
34    // test has: true
35    res.set("test has 1:", proxy.has(1));
36    res.set("test has 2:", proxy.has(2));
37    res.set("test has 3:", proxy.has(3));
38    // test has: false
39    res.set("test has 4:", proxy.has(4) == false);
40    // test values: true
41    let iteratorValues1 = proxy.values();
42    res.set("test values:", iteratorValues1.next().value == 1 && iteratorValues1.next().value == 2 &&
43            iteratorValues1.next().value == 3 && iteratorValues1.next().value == undefined);
44    // test entries: [c,cc], undefined
45    let iteratorEntries1 = proxy.entries();
46    iteratorEntries1.next().value;
47    iteratorEntries1.next().value;
48    res.set("test entries1:", iteratorEntries1.next().value != undefined);
49    res.set("itest entries2:", iteratorEntries1.next().value == undefined);
50
51    // test forof
52    let arr1 = [1, 2, 3];
53    let j = 0;
54    for (const item of proxy) {
55    }
56    // test forin:
57    for (const item in proxy) {
58        res.set("test forin", true);
59    }
60    // test forEach:
61    let flag = false;
62    function TestForEach1(value, key, proxy) {
63        flag = proxy.has(key) && proxy.has(value);
64        res.set("test forEach" + key, flag);
65    }
66    proxy.forEach(TestForEach1);
67
68    let dset = new fastset();
69    let dProxy = new Proxy(dset, {});
70    dProxy.add(4);
71    dProxy.add(5);
72    dProxy.add(6);
73    dProxy.add(7);
74    dProxy.add(8);
75    dProxy.add(9);
76    res.set("test addAll:", dProxy.addAll(proxy));
77    res.set("test hasAll:", dProxy.hasAll(proxy));
78    let obj = ["a", "b"]
79    res.set("test equal:", !dProxy.equal(obj));
80    // test remove: true
81    res.set("test remove:", dProxy.remove(1) === 1 && dProxy.length === 8);
82    // test removeAt: true
83    res.set("test removeAt:", dProxy.removeAt(3) && dProxy.length === 7);
84    // test setValueAt: true
85    res.set("test getValueAt:", dProxy.getValueAt(3) === 6);
86    // test setValueAt: true
87    res.set("test getIndexOf:", dProxy.getIndexOf(2) === 0);
88    // test toString: true
89    res.set("test toString:", dProxy.toString() === "2,3,4,6,7,8,9");
90    let arr = dProxy.toArray()
91    res.set("test toArray:", true);
92    // test increaseCapacityTo: true
93    dProxy.increaseCapacityTo(20)
94    res.set("test increaseCapacityTo:", true);
95    // test clear: 0
96    dProxy.clear();
97    res.set("test clear:", dProxy.length == 0);
98
99    flag = false;
100    try {
101        proxy["aa"] = 3;
102    } catch (e) {
103        flag = true;
104    }
105    res.set("test map throw error", flag);
106
107    flag = undefined;
108    function elements(value, key, res) {
109        if (!value) {
110            if (!flag) {
111                flag = [];
112            }
113            flag.push(key);
114        }
115    }
116    res.forEach(elements);
117
118    let de = new fastset();
119    try {
120        de.forEach(123);
121    } catch(err) {
122        if (err.name != "BusinessError") {
123            print("LightWeightSet forEach throw error fail");
124        }
125    }
126    if (!flag) {
127        print("Test LightWeightSet success!!!");
128    } else {
129        print("Test LightWeightSet fail: " + flag);
130    }
131}
132