1/** 2 * Copyright (c) 2025 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 16const etsVm = globalThis.gtest.etsVm; 17 18const myMap = etsVm.getClass('Lmap/test/ETSGLOBAL;').myMap; 19const GetMapElement = etsVm.getFunction('Lmap/test/ETSGLOBAL;', 'GetMapElement'); 20const HasMapElement = etsVm.getFunction('Lmap/test/ETSGLOBAL;', 'HasMapElement'); 21const GetMapSize = etsVm.getFunction('Lmap/test/ETSGLOBAL;', 'GetMapSize'); 22 23function testMapSet(): boolean { 24 let res: boolean = true; 25 myMap.set('banana', 2); 26 myMap.set('cherry', 3); 27 res = myMap.get('apple') === 1 && myMap.get('banana') === 2 && myMap.get('cherry') === 3 && 28 GetMapElement('banana') === 2 && GetMapElement('cherry') === 3; 29 return res; 30} 31 32function testMapGet(): boolean { 33 let res: boolean = true; 34 res = myMap.get('apple') === 1 && myMap.get('banana') === 2 && 35 myMap.get('cherry') === 3 && myMap.get('unknown') === undefined; 36 return res; 37} 38 39function testMapHas(): boolean { 40 let res: boolean = true; 41 res = myMap.has('apple') && myMap.has('banana') && 42 myMap.has('cherry') && !myMap.has('unknown'); 43 return res; 44} 45 46function testMapDelete(): boolean { 47 let res: boolean = true; 48 myMap.delete('banana') 49 res = !myMap.has('banana') && !HasMapElement('banana'); 50 return res; 51} 52 53function testMapSize(): boolean { 54 let res: boolean = true; 55 myMap.set('banana', 2); 56 res = myMap.size === 3 && GetMapSize() === 3; 57 return res; 58} 59 60function testMapKeys(): boolean { 61 let res: boolean = true; 62 let keys: string[] = []; 63 myMap.set('banana', 2); 64 for (let key of myMap.keys()) { 65 keys.push(key); 66 } 67 res = keys[0] === 'apple' && keys[1] === 'cherry' && 68 keys[2] === 'banana' && keys.length === 3; 69 return res; 70} 71 72function testMapValues(): boolean { 73 let res: boolean = true; 74 let values: number[] = []; 75 for (let value of myMap.values()) { 76 values.push(value); 77 } 78 res = values[0] === 1 && values[1] === 3 && 79 values[2] === 2 && values.length === 3; 80 return res; 81} 82 83function testMapEntries(): boolean { 84 let res: boolean = true; 85 myMap.entries(); 86 return res; 87} 88 89function testMapForEach(): boolean { 90 let res: boolean = true; 91 let keys: string[] = []; 92 let values: number[] = []; 93 myMap.forEach((val, key) => { 94 keys.push(key); 95 values.push(val); 96 }); 97 res = keys[0] === 'apple' && keys[1] === 'cherry' && keys[2] === 'banana' && keys.length === 3 && 98 values[0] === 1 && values[1] === 3 && values[2] === 2 && values.length === 3; 99 return res; 100} 101 102function testMapClear(): boolean { 103 let res: boolean = true; 104 myMap.clear(); 105 res = myMap.size === 0 && GetMapSize() === 0; 106 return res; 107} 108 109 110ASSERT_TRUE(testMapSet()); 111ASSERT_TRUE(testMapGet()); 112ASSERT_TRUE(testMapHas()); 113ASSERT_TRUE(testMapDelete()); 114ASSERT_TRUE(testMapSize()); 115ASSERT_TRUE(testMapKeys()); 116ASSERT_TRUE(testMapValues()); 117ASSERT_TRUE(testMapEntries()); 118ASSERT_TRUE(testMapForEach()); 119ASSERT_TRUE(testMapClear());