• 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 fastmap = undefined;
23if (globalThis["ArkPrivate"] != undefined) {
24    fastmap = ArkPrivate.Load(ArkPrivate.HashMap);
25
26    let res = new Map();
27    let map = new fastmap();
28
29    // test isEmpty true
30    res.set("test isEmpty ture:", map.isEmpty() == true)
31
32    map.set("a", "aa");
33    map.set("b", "bb");
34
35    // test isEmpty false
36    res.set("test isEmpty false:", map.isEmpty() == false)
37    // test get: true
38    res.set("test get:", map.length == 2 && map.get("a") == "aa" && map.get("b") == "bb");
39    // test hasKey and hasValue: true
40    res.set("test hasKey and hasValue:", map.hasKey("a") && map.hasKey("b") && map.hasValue("aa") &&
41            map.hasValue("bb") && !map.hasKey("c") && !map.hasValue("cc"));
42
43    map.set("c", "cc");
44    // test keys: true
45    let iteratorKey = map.keys();
46    res.set("test keys:", iteratorKey.next().value == "a" && iteratorKey.next().value == "b" &&
47            iteratorKey.next().value == "c" && iteratorKey.next().value == undefined);
48    // test values: true
49    let iteratorValues = map.values();
50    res.set("test values:", iteratorValues.next().value == "aa" && iteratorValues.next().value == "bb" &&
51            iteratorValues.next().value == "cc" && iteratorValues.next().value == undefined);
52    // test entries: [c,cc], undefined
53    let iteratorEntries = map.entries();
54    iteratorEntries.next().value;
55    iteratorEntries.next().value;
56    res.set("test entries1:", iteratorEntries.next().value != undefined);
57    res.set("itest entries2:", iteratorEntries.next().value == undefined);
58
59    // test forof: [a, aa], [b, bb], [c, cc]
60    let arr = ["aa", "bb", "cc"];
61    let i = 0;
62    for (const item of map) {
63        res.set(arr[i], item[1] == arr[i]);
64        i++;
65    }
66    // test forin:
67    for (const item in map) {
68        res.set("test forin", false);
69    }
70    // test forEach:
71    let flag = false;
72    function TestForEach(value, key, map) {
73        flag = map.get(key) === value;
74        res.set("test forEach" + key, flag)
75    }
76    map.forEach(TestForEach);
77
78    let dmap = new fastmap();
79    // test setAll: 3
80    dmap.setAll(map);
81    res.set("test setAll:", dmap.length == 3);
82    // test remove: true
83    res.set("test remove:", dmap.remove("a") == "aa" && dmap.length == 2);
84    // test replace: true
85    res.set("test replace:", dmap.replace("b", "dd") && dmap.get("b") == "dd");
86    // test clear: 0
87    dmap.clear();
88    res.set("test clear:", dmap.length == 0);
89
90    flag = false;
91    try {
92        map["aa"] = 3;
93    } catch (e) {
94        flag = true;
95    }
96    res.set("test map throw error", flag);
97
98    let map1 = new fastmap();
99    let proxy = new Proxy(map1, {});
100
101    // test isEmpty true
102    res.set("test proxy isEmpty ture:", proxy.isEmpty() == true)
103
104    proxy.set("a", "aa");
105    proxy.set("b", "bb");
106
107    // test isEmpty false
108    res.set("test proxy isEmpty false:", proxy.isEmpty() == false)
109
110    // test get: true
111    res.set("test get:", proxy.length == 2 && proxy.get("a") == "aa" && proxy.get("b") == "bb");
112    // test hasKey and hasValue: true
113    res.set("test hasKey and hasValue:", proxy.hasKey("a") && proxy.hasKey("b") && proxy.hasValue("aa") &&
114            proxy.hasValue("bb") && !proxy.hasKey("c") && !proxy.hasValue("cc"));
115
116    proxy.set("c", "cc");
117    // test keys: true
118    let iteratorKey1 = proxy.keys();
119    res.set("test keys:", iteratorKey1.next().value == "a" && iteratorKey1.next().value == "b" &&
120            iteratorKey1.next().value == "c" && iteratorKey1.next().value == undefined);
121    // test values: true
122    let iteratorValues1 = proxy.values();
123    res.set("test values:", iteratorValues1.next().value == "aa" && iteratorValues1.next().value == "bb" &&
124            iteratorValues1.next().value == "cc" && iteratorValues1.next().value == undefined);
125    // test entries: [c,cc], undefined
126    let iteratorEntries1 = proxy.entries();
127    iteratorEntries1.next().value;
128    iteratorEntries1.next().value;
129    res.set("test entries1:", iteratorEntries1.next().value != undefined);
130    res.set("itest entries2:", iteratorEntries1.next().value == undefined);
131
132    // test forof: [a, aa], [b, bb], [c, cc]
133    let arr1 = ["aa", "bb", "cc"];
134    let j = 0;
135    for (const item of proxy) {
136        res.set(arr1[j], item[1] == arr1[j]);
137        j++;
138    }
139    // test forin:
140    for (const item in proxy) {
141        res.set("test forin", false);
142    }
143    // test forEach:
144    flag = false;
145    function TestForEach1(value, key, proxy) {
146        flag = proxy.get(key) === value;
147        res.set("test forEach" + key, flag)
148    }
149    proxy.forEach(TestForEach1);
150
151    let dmap1 = new fastmap();
152    let dProxy = new Proxy(dmap1, {})
153    // test setAll: 3
154    dProxy.setAll(proxy);
155    res.set("test setAll:", dProxy.length == 3);
156    // test remove: true
157    res.set("test remove:", dProxy.remove("a") == "aa" && dProxy.length == 2);
158    // test replace: true
159    res.set("test replace:", dProxy.replace("b", "dd") && dProxy.get("b") == "dd");
160    // test clear: 0
161    dProxy.clear();
162    res.set("test clear:", dProxy.length == 0);
163
164    flag = false;
165    try {
166        proxy["aa"] = 3;
167    } catch (e) {
168        flag = true;
169    }
170    res.set("test map throw error", flag);
171    flag = undefined;
172    function elements(value, key, map) {
173        if (!value) {
174            if (!flag) {
175                flag = [];
176            }
177            flag.push(key);
178        }
179    }
180    res.forEach(elements);
181
182    // test RBTree
183    let collisionMap = new fastmap();
184    let count = 0;
185    // same hash when mod 1024
186    collisionMap.set(1224, 1);
187    collisionMap.set(1285, 2);
188    collisionMap.set(1463, 3);
189    collisionMap.set(4307, 4);
190    collisionMap.set(5135, 5);
191    collisionMap.set(5903, 6);
192    collisionMap.set(6603, 7);
193    collisionMap.set(6780, 8);
194    collisionMap.set(8416, 9);
195    collisionMap.set(9401, 10);
196    collisionMap.set(9740, 11);
197    collisionMap.forEach((value, key, hashMap) => {
198        if (hashMap.get(key) == value) {
199            count += value;
200        }
201    });
202    if (count != 66) {  // 66: 1 + 2 + 3 + ... + 11
203        print("test RBTree forEach fail. count=" + count);
204    }
205
206    let de = new fastmap();
207    try {
208        de.forEach(123);
209    } catch(err) {
210        if (err.name != "BusinessError") {
211            print("HashMap forEach throw error fail");
212        }
213    }
214    if (!flag) {
215        print("Test HashMap success!!!");
216    } else {
217        print("Test HashMap fail: " + flag);
218    }
219}
220