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