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 */
22
23import {testCommon, testdProxySet, testdProxyIterator}  from "./utility";
24
25var fastmap = undefined;
26if (globalThis["ArkPrivate"] != undefined) {
27    fastmap = ArkPrivate.Load(ArkPrivate.HashMap);
28
29    let res = new Map();
30    let map = new fastmap();
31
32    // test isEmpty true
33    res.set("test isEmpty ture:", map.isEmpty() == true)
34
35    map.set("a", "aa");
36    map.set("b", "bb");
37
38    // test isEmpty false
39    res.set("test isEmpty false:", map.isEmpty() == false)
40    // test get: true
41    res.set("test get:", map.length == 2 && map.get("a") == "aa" && map.get("b") == "bb");
42    // test hasKey and hasValue: true
43    res.set("test hasKey and hasValue:", map.hasKey("a") && map.hasKey("b") && map.hasValue("aa") &&
44            map.hasValue("bb") && !map.hasKey("c") && !map.hasValue("cc"));
45
46    map.set("c", "cc");
47    testdProxyIterator(map, res);
48
49    let flag = false;
50    let doublemap = new fastmap();
51    for (let i = 0; i < 10; i++) {
52        doublemap.set(i, i);
53    }
54    let ss = Math.floor(Math.random()*10);
55    flag = doublemap.hasKey(ss);
56    if (flag != true) {
57        print("HashMap hasKey find key error");
58    }
59
60    // check key is Double, but search for Int
61    let myTest = new fastmap();
62    myTest.set(Math.floor(1.4), 2);
63    res.set("test key is Double 1, searching for Int 1", myTest.hasKey(1));
64
65    let dmap = new fastmap();
66    // test setAll: 3
67    dmap.setAll(map);
68    res.set("test setAll:", dmap.length == 3);
69    // test remove: true
70    res.set("test remove:", dmap.remove("a") == "aa" && dmap.length == 2);
71    // test replace: true
72    res.set("test replace:", dmap.replace("b", "dd") && dmap.get("b") == "dd");
73    // test clear: 0
74    dmap.clear();
75    res.set("test clear:", dmap.length == 0);
76
77    flag = false;
78    try {
79        map["aa"] = 3;
80    } catch (e) {
81        flag = true;
82    }
83    res.set("test map throw error", flag);
84
85    let map1 = new fastmap();
86    let proxy = new Proxy(map1, {});
87
88    // test isEmpty true
89    res.set("test proxy isEmpty ture:", proxy.isEmpty() == true)
90
91    proxy.set("a", "aa");
92    proxy.set("b", "bb");
93
94    // test isEmpty false
95    res.set("test proxy isEmpty false:", proxy.isEmpty() == false)
96
97    // test get: true
98    res.set("test get:", proxy.length == 2 && proxy.get("a") == "aa" && proxy.get("b") == "bb");
99    // test hasKey and hasValue: true
100    res.set("test hasKey and hasValue:", proxy.hasKey("a") && proxy.hasKey("b") && proxy.hasValue("aa") &&
101            proxy.hasValue("bb") && !proxy.hasKey("c") && !proxy.hasValue("cc"));
102
103    proxy.set("c", "cc");
104
105    testCommon(proxy, res);
106    // test forEach:
107    flag = false;
108    function TestForEachHashmap(valueHashMap, keyHashMap, proxy) {
109        flag = proxy.get(keyHashMap) === valueHashMap;
110        res.set("test forEach" + keyHashMap, flag)
111    }
112    proxy.forEach(TestForEachHashmap);
113
114    let dmap1 = new fastmap();
115    let dProxy = new Proxy(dmap1, {})
116    testdProxySet(proxy, res, dProxy);
117
118    flag = false;
119    try {
120        proxy["aa"] = 3;
121    } catch (e) {
122        flag = true;
123    }
124    res.set("test map throw error", flag);
125    flag = undefined;
126    function elements(value, key, map) {
127        if (!value) {
128            if (!flag) {
129                flag = [];
130            }
131            flag.push(key);
132        }
133    }
134    res.forEach(elements);
135
136    // test RBTree
137    let collisionMap = new fastmap();
138    let count = 0;
139    // same hash when mod 1024
140    collisionMap.set(1224, 1);
141    collisionMap.set(1285, 2);
142    collisionMap.set(1463, 3);
143    collisionMap.set(4307, 4);
144    collisionMap.set(5135, 5);
145    collisionMap.set(5903, 6);
146    collisionMap.set(6603, 7);
147    collisionMap.set(6780, 8);
148    collisionMap.set(8416, 9);
149    collisionMap.set(9401, 10);
150    collisionMap.set(9740, 11);
151    collisionMap.forEach((value, key, hashMap) => {
152        if (hashMap.get(key) == value) {
153            count += value;
154        }
155    });
156    if (count != 66) {  // 66: 1 + 2 + 3 + ... + 11
157        print("test RBTree forEach fail. count=" + count);
158    }
159
160    let de = new fastmap();
161    try {
162        de.forEach(123);
163    } catch(err) {
164        if (err.name != "BusinessError") {
165            print("HashMap forEach throw error fail");
166        }
167    }
168    if (!flag) {
169        print("Test HashMap success!!!");
170    } else {
171        print("Test HashMap fail: " + flag);
172    }
173}
174export let hashmapRes = "Test hashmap done";