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} from "./utility"; 23 24var fastmap = undefined; 25if (globalThis["ArkPrivate"] != undefined) { 26 fastmap = ArkPrivate.Load(ArkPrivate.LightWeightMap); 27 28 let res = new Map(); 29 let map = new fastmap(); 30 let proxy = new Proxy(map, {}); 31 // test isEmpty: true 32 res.set("test isEmpty:", proxy.isEmpty()); 33 proxy.set("a", "aa"); 34 proxy.set("b", "bb"); 35 36 // test get: true 37 res.set("test get:", proxy.length == 2 && proxy.get("a") == "aa" && proxy.get("b") == "bb"); 38 // test hasKey and hasValue: true 39 res.set("test hasKey and hasValue:", proxy.hasKey("a") && proxy.hasKey("b") && proxy.hasValue("aa") && 40 proxy.hasValue("bb") && !proxy.hasKey("c") && !proxy.hasValue("cc")); 41 42 proxy.set("c", "cc"); 43 // test getIndexOfKey and getIndexOfValue: true 44 res.set("test getIndexOfKey and getIndexOfValue:", proxy.getIndexOfKey("a") === 0 && proxy.getIndexOfValue("bb") === 1); 45 // test getKeyAt: true 46 res.set("test getKeyAt-1:", proxy.getKeyAt(1) == "b"); 47 res.set("test getKeyAt-2:", proxy.getKeyAt(Math.floor(1.3)) == "b"); 48 testCommon(proxy, res); 49 50 // test forEach: 51 let flag = false; 52 function TestForEach(value, key, proxy) { 53 flag = proxy.get(key) === value; 54 res.set("test forEach" + key, flag) 55 } 56 proxy.forEach(TestForEach); 57 58 let dmap = new fastmap(); 59 let dProxy = new Proxy(dmap, {}); 60 dProxy.set("a", "aa"); 61 dProxy.set("b", "bb"); 62 dProxy.set("c", "cc"); 63 dProxy.set("d", "dd"); 64 dProxy.set("e", "ee"); 65 // test setAll: 66 dProxy.setAll(proxy); 67 res.set("test setAll:", dProxy.length === 5); 68 res.set("test hasAll:", dProxy.hasAll(proxy)); 69 // test remove: true 70 res.set("test remove:", dProxy.remove("a") == "aa" && dProxy.length == 4); 71 // test removeAt: true 72 res.set("test removeAt-1:", dProxy.removeAt(dProxy.getIndexOfKey("b")) && dProxy.length == 3); 73 // test setValueAt: true 74 res.set("test setValueAt-1:", dProxy.setValueAt(dProxy.getIndexOfKey("d"), "ee")); 75 // test getValueAt: true 76 res.set("test getValueAt:", dProxy.getValueAt(dProxy.getIndexOfKey("d")) === "ee"); 77 res.set("test getValueAt:", dProxy.getValueAt(Math.floor(0)) === "cc"); 78 res.set("test getValueAt:", dProxy.getValueAt(Math.floor(1)) === "ee"); 79 res.set("test setValueAt-2:", dProxy.setValueAt(Math.floor(1.3), "ff")); 80 res.set("test getValueAt-2:", dProxy.getValueAt(Math.floor(1.3)) === "ff"); 81 // test toString: true 82 res.set("test toString:", dProxy.toString() === "c:cc,d:ff,e:ee"); 83 res.set("test removeAt-2:", dProxy.removeAt(Math.floor(1.3)) && dProxy.length == 2); 84 // test increaseCapacityTo: true 85 dProxy.increaseCapacityTo(20) 86 dProxy.increaseCapacityTo(Math.floor(1.3)); 87 res.set("test increaseCapacityTo:", true); 88 // test clear: 0 89 let ret = dProxy.clear(); 90 res.set("test clear:", dProxy.length == 0); 91 res.set("test 'clear' ret:", ret === undefined); 92 93 let empty_fm = new fastmap(); 94 try { 95 empty_fm.getKeyAt(0); 96 } catch(err) { 97 res.set("test GetKeyAt exception when arraylist is empty:", err == "BusinessError: Container is empty") 98 } 99 try { 100 empty_fm.getValueAt(0); 101 } catch(err) { 102 res.set("test GetValueAt exception when arraylist is empty:", err == "BusinessError: Container is empty") 103 } 104 try { 105 empty_fm.setValueAt(0); 106 } catch(err) { 107 res.set("test SetValueAt exception when arraylist is empty:", err == "BusinessError: Container is empty") 108 } 109 110 flag = false; 111 try { 112 proxy["aa"] = 3; 113 } catch (e) { 114 flag = true; 115 } 116 res.set("test map throw error", flag); 117 flag = undefined; 118 function elements(value, key, res) { 119 if (!value) { 120 if (!flag) { 121 flag = []; 122 } 123 flag.push(key); 124 } 125 } 126 res.forEach(elements); 127 128 let de = new fastmap(); 129 try { 130 de.forEach(123); 131 } catch(err) { 132 if (err.name != "BusinessError") { 133 print("LightWeightMap forEach throw error fail"); 134 } 135 } 136 137 // Math.floor as index input should not throw exception. 138 let myLm = new fastmap(); 139 myLm.set("a", "aa"); 140 myLm.set("b", "bb"); 141 myLm.getKeyAt(Math.floor(1.5)); 142 myLm.getValueAt(Math.floor(1.5)); 143 myLm.setValueAt(Math.floor(1.5), "cc"); 144 myLm.removeAt(Math.floor(1.5)); 145 146 if (!flag) { 147 print("Test LightWeightMap success!!!"); 148 } else { 149 print("Test LightWeightMap fail: " + flag); 150 } 151} 152export let lightweightmapRes = "Test LightWeightMap done"; 153