1/* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 */ 15export function testCommon(proxy, res) { 16 // test keys: true 17 let iteratorKey1 = proxy.keys(); 18 res.set("test keys:", iteratorKey1.next().value == "a" && iteratorKey1.next().value == "b" && 19 iteratorKey1.next().value == "c" && iteratorKey1.next().value == undefined); 20 // test values: true 21 let iteratorValues1 = proxy.values(); 22 res.set("test values:", iteratorValues1.next().value == "aa" && iteratorValues1.next().value == "bb" && 23 iteratorValues1.next().value == "cc" && iteratorValues1.next().value == undefined); 24 // test entries: [c,cc], undefined 25 let iteratorEntries1 = proxy.entries(); 26 iteratorEntries1.next().value; 27 iteratorEntries1.next().value; 28 res.set("test entries1:", iteratorEntries1.next().value != undefined); 29 res.set("itest entries2:", iteratorEntries1.next().value == undefined); 30 31 // test forof: [a, aa], [b, bb], [c, cc] 32 let arr1 = ["aa", "bb", "cc"]; 33 let j = 0; 34 for (const item of proxy) { 35 res.set(arr1[j], item[1] == arr1[j]); 36 j++; 37 } 38 // test forin: 39 for (const item in proxy) { 40 res.set("test forin", false); 41 } 42} 43 44export function testdProxySet(proxy, res, dProxy) { 45 // test setAll: 3 46 dProxy.setAll(proxy); 47 res.set("test setAll:", dProxy.length == 3); 48 // test remove: true 49 res.set("test remove:", dProxy.remove("a") == "aa" && dProxy.length == 2); 50 // test replace: true 51 res.set("test replace:", dProxy.replace("b", "dd") && dProxy.get("b") == "dd"); 52 // test clear: 0 53 dProxy.clear(); 54 res.set("test clear:", dProxy.length == 0); 55} 56 57export function testdProxyIterator(map, res) { 58 // test keys: true 59 let iteratorKey = map.keys(); 60 res.set("test keys:", iteratorKey.next().value == "a" && iteratorKey.next().value == "b" && 61 iteratorKey.next().value == "c" && iteratorKey.next().value == undefined); 62 // test values: true 63 let iteratorValues = map.values(); 64 res.set("test values:", iteratorValues.next().value == "aa" && iteratorValues.next().value == "bb" && 65 iteratorValues.next().value == "cc" && iteratorValues.next().value == undefined); 66 // test entries: [c,cc], undefined 67 let iteratorEntries = map.entries(); 68 iteratorEntries.next().value; 69 iteratorEntries.next().value; 70 res.set("test entries1:", iteratorEntries.next().value != undefined); 71 res.set("itest entries2:", iteratorEntries.next().value == undefined); 72 73 // test forof: [a, aa], [b, bb], [c, cc] 74 let arr = ["aa", "bb", "cc"]; 75 let i = 0; 76 for (const item of map) { 77 res.set(arr[i], item[1] == arr[i]); 78 i++; 79 } 80 // test forin: 81 for (const item in map) { 82 res.set("test forin", false); 83 } 84 // test forEach: 85 let flag = false; 86 function TestForEach(value, key, map) { 87 flag = map.get(key) === value; 88 res.set("test forEach" + key, flag); 89 } 90 map.forEach(TestForEach); 91} 92 93export function testdProxyArray1(proxy, res, testArray) { 94 let itr = proxy[Symbol.iterator](); 95 let tmp = undefined; 96 let testArray1 = []; 97 do { 98 tmp = itr.next().value; 99 testArray1.push(tmp); 100 } while (tmp != undefined); 101 102 for (let k = 0; k < proxy.length; k++) { 103 if (testArray1[k] !== testArray[k]) { 104 res = false; 105 } 106 } 107}