• 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    flag = false;
79    let doublemap = new fastmap();
80    for (let i = 0; i < 10; i++) {
81        doublemap.set(i, i);
82    }
83    let ss = Math.floor(Math.random()*10);
84    flag = doublemap.hasKey(ss);
85    if (flag != true) {
86        print("HashMap hasKey find key error");
87    }
88
89    // check key is Double, but search for Int
90    let myTest = new fastmap();
91    myTest.set(Math.floor(1.4), 2);
92    res.set("test key is Double 1, searching for Int 1", myTest.hasKey(1));
93
94    let dmap = new fastmap();
95    // test setAll: 3
96    dmap.setAll(map);
97    res.set("test setAll:", dmap.length == 3);
98    // test remove: true
99    res.set("test remove:", dmap.remove("a") == "aa" && dmap.length == 2);
100    // test replace: true
101    res.set("test replace:", dmap.replace("b", "dd") && dmap.get("b") == "dd");
102    // test clear: 0
103    dmap.clear();
104    res.set("test clear:", dmap.length == 0);
105
106    flag = false;
107    try {
108        map["aa"] = 3;
109    } catch (e) {
110        flag = true;
111    }
112    res.set("test map throw error", flag);
113
114    let map1 = new fastmap();
115    let proxy = new Proxy(map1, {});
116
117    // test isEmpty true
118    res.set("test proxy isEmpty ture:", proxy.isEmpty() == true)
119
120    proxy.set("a", "aa");
121    proxy.set("b", "bb");
122
123    // test isEmpty false
124    res.set("test proxy isEmpty false:", proxy.isEmpty() == false)
125
126    // test get: true
127    res.set("test get:", proxy.length == 2 && proxy.get("a") == "aa" && proxy.get("b") == "bb");
128    // test hasKey and hasValue: true
129    res.set("test hasKey and hasValue:", proxy.hasKey("a") && proxy.hasKey("b") && proxy.hasValue("aa") &&
130            proxy.hasValue("bb") && !proxy.hasKey("c") && !proxy.hasValue("cc"));
131
132    proxy.set("c", "cc");
133    // test keys: true
134    let iteratorKey1 = proxy.keys();
135    res.set("test keys:", iteratorKey1.next().value == "a" && iteratorKey1.next().value == "b" &&
136            iteratorKey1.next().value == "c" && iteratorKey1.next().value == undefined);
137    // test values: true
138    let iteratorValues1 = proxy.values();
139    res.set("test values:", iteratorValues1.next().value == "aa" && iteratorValues1.next().value == "bb" &&
140            iteratorValues1.next().value == "cc" && iteratorValues1.next().value == undefined);
141    // test entries: [c,cc], undefined
142    let iteratorEntries1 = proxy.entries();
143    iteratorEntries1.next().value;
144    iteratorEntries1.next().value;
145    res.set("test entries1:", iteratorEntries1.next().value != undefined);
146    res.set("itest entries2:", iteratorEntries1.next().value == undefined);
147
148    // test forof: [a, aa], [b, bb], [c, cc]
149    let arr1 = ["aa", "bb", "cc"];
150    let j = 0;
151    for (const item of proxy) {
152        res.set(arr1[j], item[1] == arr1[j]);
153        j++;
154    }
155    // test forin:
156    for (const item in proxy) {
157        res.set("test forin", false);
158    }
159    // test forEach:
160    flag = false;
161    function TestForEach1(value, key, proxy) {
162        flag = proxy.get(key) === value;
163        res.set("test forEach" + key, flag)
164    }
165    proxy.forEach(TestForEach1);
166
167    let dmap1 = new fastmap();
168    let dProxy = new Proxy(dmap1, {})
169    // test setAll: 3
170    dProxy.setAll(proxy);
171    res.set("test setAll:", dProxy.length == 3);
172    // test remove: true
173    res.set("test remove:", dProxy.remove("a") == "aa" && dProxy.length == 2);
174    // test replace: true
175    res.set("test replace:", dProxy.replace("b", "dd") && dProxy.get("b") == "dd");
176    // test clear: 0
177    dProxy.clear();
178    res.set("test clear:", dProxy.length == 0);
179
180    flag = false;
181    try {
182        proxy["aa"] = 3;
183    } catch (e) {
184        flag = true;
185    }
186    res.set("test map throw error", flag);
187    flag = undefined;
188    function elements(value, key, map) {
189        if (!value) {
190            if (!flag) {
191                flag = [];
192            }
193            flag.push(key);
194        }
195    }
196    res.forEach(elements);
197
198    // test RBTree
199    let collisionMap = new fastmap();
200    let count = 0;
201    // same hash when mod 1024
202    collisionMap.set(1224, 1);
203    collisionMap.set(1285, 2);
204    collisionMap.set(1463, 3);
205    collisionMap.set(4307, 4);
206    collisionMap.set(5135, 5);
207    collisionMap.set(5903, 6);
208    collisionMap.set(6603, 7);
209    collisionMap.set(6780, 8);
210    collisionMap.set(8416, 9);
211    collisionMap.set(9401, 10);
212    collisionMap.set(9740, 11);
213    collisionMap.forEach((value, key, hashMap) => {
214        if (hashMap.get(key) == value) {
215            count += value;
216        }
217    });
218    if (count != 66) {  // 66: 1 + 2 + 3 + ... + 11
219        print("test RBTree forEach fail. count=" + count);
220    }
221
222    let de = new fastmap();
223    try {
224        de.forEach(123);
225    } catch(err) {
226        if (err.name != "BusinessError") {
227            print("HashMap forEach throw error fail");
228        }
229    }
230    if (!flag) {
231        print("Test HashMap success!!!");
232    } else {
233        print("Test HashMap fail: " + flag);
234    }
235}
236