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