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 */ 22import {testCommon, testdProxySet, testdProxyIterator} from "./utility"; 23export let treemapRes = "Test TreeMap done"; 24 25var fastmap = undefined; 26if (globalThis["ArkPrivate"] != undefined) { 27 fastmap = ArkPrivate.Load(ArkPrivate.TreeMap); 28 29 let res = new Map(); 30 let map = new fastmap(); 31 map.set("a", "aa"); 32 map.set("b", "bb"); 33 34 // test get: true 35 res.set("test get:", map.length == 2 && map.get("a") == "aa" && map.get("b") == "bb"); 36 // test hasKey and hasValue: true 37 res.set("test hasKey and hasValue:", map.hasKey("a") && map.hasKey("b") && map.hasValue("aa") && 38 map.hasValue("bb") && !map.hasKey("c") && !map.hasValue("cc")); 39 40 map.set("c", "cc"); 41 // test getFirstKey and getLastKey: true 42 res.set("test getFirstKey and getLastKey:", map.getFirstKey() == "a" && map.getLastKey() == "c"); 43 // test getLowerKey and getHigherKey: true 44 res.set("test getLowerKey and getHigherKey:", map.getLowerKey("b") == "a" && map.getLowerKey("a") == undefined && 45 map.getHigherKey("b") == "c" && map.getHigherKey("c") == undefined); 46 47 testdProxyIterator(map, res); 48 49 let dmap = new fastmap(); 50 // test setAll: 3 51 dmap.setAll(map); 52 res.set("test setAll:", dmap.length == 3); 53 // test remove: true 54 res.set("test remove:", dmap.remove("a") == "aa" && dmap.length == 2); 55 // test replace: true 56 res.set("test replace:", dmap.replace("b", "dd") && dmap.get("b") == "dd"); 57 // test clear: 0 58 dmap.clear(); 59 res.set("test clear:", dmap.length == 0); 60 61 let flag = false; 62 try { 63 map["aa"] = 3; 64 } catch (e) { 65 flag = true; 66 } 67 res.set("test map throw error", flag); 68 69 let map1 = new fastmap(); 70 let proxy = new Proxy(map1, {}); 71 72 // test proxy isEmpty: true 73 res.set("test proxy isEmpty true:", proxy.isEmpty() == true) 74 75 proxy.set("a", "aa"); 76 proxy.set("b", "bb"); 77 78 // test proxy isEmpty: false 79 res.set("test proxy isEmpty false:", proxy.isEmpty() == false) 80 81 // test get: true 82 res.set("test get:", proxy.length == 2 && proxy.get("a") == "aa" && proxy.get("b") == "bb"); 83 // test hasKey and hasValue: true 84 res.set("test hasKey and hasValue:", proxy.hasKey("a") && proxy.hasKey("b") && proxy.hasValue("aa") && 85 proxy.hasValue("bb") && !proxy.hasKey("c") && !proxy.hasValue("cc")); 86 87 proxy.set("c", "cc"); 88 // test getFirstKey and getLastKey: true 89 res.set("test getFirstKey and getLastKey:", proxy.getFirstKey() == "a" && proxy.getLastKey() == "c"); 90 // test getLowerKey and getHigherKey: true 91 res.set("test getLowerKey and getHigherKey:", proxy.getLowerKey("b") == "a" && proxy.getLowerKey("a") == undefined && 92 proxy.getHigherKey("b") == "c" && proxy.getHigherKey("c") == undefined); 93 94 testCommon(proxy, res); 95 // test forEach: 96 flag = false; 97 function TestForEachTreeMap(valueTreeMap, keyTreeMap, proxy) { 98 flag = proxy.get(keyTreeMap) === valueTreeMap; 99 res.set("test forEach" + keyTreeMap, flag) 100 } 101 proxy.forEach(TestForEachTreeMap); 102 103 let dmap1 = new fastmap(); 104 let dProxy = new Proxy(dmap1, {}); 105 testdProxySet(proxy, res, dProxy); 106 107 let cmmp = new fastmap((firstValue, secondValue) => {return firstValue > secondValue}); 108 cmmp.set("aa", 1); 109 cmmp.set("bb", 1); 110 res.set("test hasKey undefined", cmmp.hasKey(undefined) == false); 111 res.set("test hasKey null", cmmp.hasKey(null) == false); 112 cmmp.set(null, 1); 113 cmmp.set(undefined, 1); 114 res.set("test hasKey undefined true", cmmp.hasKey(undefined) == true); 115 res.set("test hasKey null true", cmmp.hasKey(null) == true); 116 117 cmmp.clear(); 118 let commap1 = new fastmap(); 119 commap1.setAll(cmmp); 120 res.set("test setAll null map", commap1.length == 0); 121 122 let commap = new fastmap((firstValue, secondValue) => {return firstValue > secondValue}); 123 commap.set("c","1"); 124 commap.set("a","8"); 125 commap.set("b","2"); 126 commap.set("d","4"); 127 if (commap.length == 4) { 128 commap.remove("a"); 129 commap.remove("b"); 130 commap.remove("c"); 131 commap.remove("d"); 132 } 133 res.set("test commpare", commap.length == 0); 134 class Person { 135 id = 0; 136 name = ''; 137 constructor(id, name) { 138 this.id = id; 139 this.name = name; 140 } 141 } 142 commap = new fastmap((firstValue, secondValue) => {return firstValue.id > secondValue.id}); 143 let personone = new Person(1,'张三'); 144 let persontwo = new Person(3,'李四'); 145 let personsec = new Person(2,'王五'); 146 commap.set(personone,"1") 147 commap.set(persontwo,"1") 148 commap.set(personsec,"1") 149 res.set("test clear and set", commap.getFirstKey().id === 3); 150 commap.clear(); 151 commap = new fastmap((firstValue, secondValue) => {return firstValue < secondValue}); 152 commap.set("c","1"); 153 commap.set("a","8"); 154 commap.set("b","2"); 155 commap.set("d","4"); 156 commap.clear(); 157 commap.set("c","1"); 158 commap.set("a","8"); 159 commap.set("b","2"); 160 commap.set("d","4"); 161 res.set("test clear and set", commap.getFirstKey() === "a"); 162 163 flag = false; 164 try { 165 proxy["aa"] = 3; 166 } catch (e) { 167 flag = true; 168 } 169 res.set("test map throw error", flag); 170 flag = undefined; 171 function elements(value, key, map) { 172 if (!value) { 173 if (!flag) { 174 flag = []; 175 } 176 flag.push(key); 177 } 178 } 179 res.forEach(elements); 180 181 let de = new fastmap(); 182 try { 183 de.forEach(123); 184 } catch(err) { 185 if (err.name != "BusinessError") { 186 print("TreeMap forEach throw error fail"); 187 } 188 } 189 if (!flag) { 190 print("Test TreeMap success!!!"); 191 } else { 192 print("Test TreeMap fail: " + flag); 193 } 194 map = new fastmap(); 195 map.set("a", "av"); 196 map.clear(); 197 map.set("b", "bv"); 198 map.set("c", "cv"); 199 print("Test TreeMap set After Clear Success") 200 201 let growMap = new fastmap(); 202 const keyStr = 'KEY_TEST_MAP'; 203 growMap.set(keyStr,21) 204 growMap.set(keyStr,68) 205 growMap.remove(keyStr) 206 growMap.set(keyStr,23) 207 growMap.remove(keyStr) 208 growMap.set(keyStr,86) 209 growMap.remove(keyStr) 210 growMap.set(keyStr,37) 211 growMap.remove(keyStr) 212 growMap.set(keyStr,33) 213 growMap.remove(keyStr) 214 growMap.set(keyStr,34) 215 growMap.remove(keyStr) 216 growMap.set(keyStr,64) 217 growMap.set(keyStr,100) 218 print(growMap.get(keyStr)) 219} 220