1/* 2 * Copyright (c) 2024 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:mapget 18 * @tc.desc: test Map.get 19 * @tc.type: FUNC 20 * @tc.require: issueI97M5M 21 */ 22let map = new Map(); 23 24map.set('key', 'value'); 25assert_equal(map.get('key'),"value"); 26 27for (let i = 0; i < 3; ++i) { 28 map.set(i, -i); 29} 30let objkeys1 = []; 31for (let i = 0; i < 4; ++i) { 32 let value = map.get(i); 33 objkeys1.push(value); 34} 35assert_equal(objkeys1,[-0, -1, -2, undefined]); 36 37map = new Map(); 38let key = Number.parseFloat("1392210229"); 39map.set(key, "success"); 40let value = map.get(key); 41assert_equal(value,"success"); 42 43 44function check(key) { 45 let irHash = ArkTools.hashCode(key); 46 let rtHash = ArkTools.hashCode(key, true); 47 if (irHash != rtHash) { 48 throw new Error("Mismatch hash for " + key + ": expected " + rtHash + ", but got " + irHash); 49 } 50} 51 52function checkIntAsDouble(intKey) { 53 intKey /= 2; 54 intKey += 0.5; 55 intKey *= 2; 56 intKey -= 1; 57 check(intKey); 58} 59 60check(0); 61check(1); 62check(1 << 30); 63check((1 << 30) - 1); 64check(-1); 65check(1.5); 66check(-1.5); 67check(Number.EPSILON); 68check(Number.NaN); 69check(Number.MIN_VALUE); 70check(Number.MAX_VALUE); 71check(Number.MIN_SAFE_INTEGER); 72check(Number.MIN_SAFE_INTEGER - 1); 73check(Number.MAX_SAFE_INTEGER); 74check(Number.MAX_SAFE_INTEGER + 1); 75check(Number.NaN); 76check(Number.POSITIVE_INFINITY); 77check(Number.NEGATIVE_INFINITY); 78check(Number.parseFloat("+0.0")); 79check(Number.parseFloat("-0.0")); 80check(true); 81check(false); 82check(undefined); 83check(null); 84check(""); 85check("ab"); 86check({}); 87check(12n); 88checkIntAsDouble(0); 89checkIntAsDouble(1); 90checkIntAsDouble(-1); 91checkIntAsDouble(-1234); 92checkIntAsDouble(1234); 93checkIntAsDouble(1 << 29); 94checkIntAsDouble(-(1 << 29)); 95checkIntAsDouble(1 << 30); 96checkIntAsDouble(-(1 << 30)); 97check(Symbol.iterator); 98 99// regression test 100check(Number.parseFloat("1392210229")); 101 102/* 103 * @tc.name:MapConstructor,species 104 * @tc.desc: test MapConstructor,species 105 * @tc.type: FUNC 106 */ 107const maps = [ 108 new Map([["ark_stringKey", "ark_stringValue"]]), 109 new Map([[1, "ark_numberValue"]]), 110 new Map([[true, "ark_booleanValue"]]), 111 new Map([[{}, "ark_objectValue"]]), 112 new Map([[null, "ark_nullValue"]]), 113 new Map([[undefined, "ark_undefinedValue"]]), 114 new Map([[NaN, "ark_NaNValue"]]), 115 new Map([[Infinity, "ark_infinityValue"]]), 116 new Map([[-Infinity, "ark_negativeInfinityValue"]]), 117 new Map([[RegExp("ark_regexKey"), "ark_regexValue"]]), 118 new Map([[new Map(), "ark_mapValue"]]), 119 new Map([[new Set(), "ark_setValue"]]), 120 new Map([[Array.from([1, 2, 3]), "ark_arrayValue"]]), 121 new Map([["ark_unicodeKey ", "ark_unicodeValue "]]) 122]; 123 124let objkeys2 = []; 125maps.forEach((map, index) => { 126 objkeys2.push("Map " + (index + 1) + ":"); 127 map.forEach((value, key) => { 128 objkeys2.push("Key: " + key + ", Value: " + value); 129 }); 130}); 131assert_equal(objkeys2,["Map 1:", "Key: ark_stringKey, Value: ark_stringValue", "Map 2:", "Key: 1, Value: ark_numberValue", "Map 3:", "Key: true, Value: ark_booleanValue", "Map 4:", "Key: [object Object], Value: ark_objectValue", "Map 5:", "Key: null, Value: ark_nullValue", "Map 6:", "Key: undefined, Value: ark_undefinedValue", "Map 7:", "Key: NaN, Value: ark_NaNValue", "Map 8:", "Key: Infinity, Value: ark_infinityValue", "Map 9:", "Key: -Infinity, Value: ark_negativeInfinityValue", "Map 10:", "Key: /ark_regexKey/, Value: ark_regexValue", "Map 11:", "Key: [object Map], Value: ark_mapValue", "Map 12:", "Key: [object Set], Value: ark_setValue", "Map 13:", "Key: 1,2,3, Value: ark_arrayValue", "Map 14:", "Key: ark_unicodeKey , Value: ark_unicodeValue "]); 132 133let result = Map[Symbol.species]; 134assert_equal(result.toString(),'function Map() { [native code] }'); 135 136/* 137 * @tc.name:forEach,get,has,keys,set,clear 138 * @tc.name:delete,values,size,entries 139 * @tc.type: FUNC 140 */ 141const combinedMap = new Map([ 142 ["ark_stringKey", "ark_stringValue"], 143 [1, "ark_numberValue"], 144 [true, "ark_booleanValue"], 145 [{}, "ark_objectValue"], 146 [null, "ark_nullValue"], 147 [undefined, "ark_undefinedValue"], 148 [NaN, "ark_NaNValue"], 149 [Infinity, "ark_infinityValue"], 150 [-Infinity, "ark_negativeInfinityValue"], 151 [RegExp("ark_regexKey"), "ark_regexValue"], 152 [new Map(), "ark_mapValue"], 153 [new Set(), "ark_setValue"], 154 [Array.from([1, 2, 3]), "ark_arrayValue"], 155 ["ark_unicodeKey ", "ark_unicodeValue "] 156]); 157 158const newMap = new Map(); 159let objkeys3= []; 160const keysArray = Array.from(combinedMap.keys()); 161keysArray.forEach(key => { 162 objkeys3.push("Keys: " + key); 163}); 164assert_equal(objkeys3,["Keys: ark_stringKey", "Keys: 1", "Keys: true", "Keys: [object Object]", "Keys: null", "Keys: undefined", "Keys: NaN", "Keys: Infinity", "Keys: -Infinity", "Keys: /ark_regexKey/", "Keys: [object Map]", "Keys: [object Set]", "Keys: 1,2,3", "Keys: ark_unicodeKey "]); 165 166const valuesArray = Array.from(combinedMap.values()); 167let objkeys4 = []; 168valuesArray.forEach(value => { 169 objkeys4.push("Value: " + value); 170}); 171assert_equal(objkeys4,["Value: ark_stringValue", "Value: ark_numberValue", "Value: ark_booleanValue", "Value: ark_objectValue", "Value: ark_nullValue", "Value: ark_undefinedValue", "Value: ark_NaNValue", "Value: ark_infinityValue", "Value: ark_negativeInfinityValue", "Value: ark_regexValue", "Value: ark_mapValue", "Value: ark_setValue", "Value: ark_arrayValue", "Value: ark_unicodeValue "]); 172 173const entriesArray = Array.from(combinedMap.entries()); 174let objkeys5 = []; 175entriesArray.forEach(entry => { 176 const [key, value] = entry; 177 objkeys5.push("Key: " + key + ", Value: " + value); 178}); 179assert_equal(objkeys5,["Key: ark_stringKey, Value: ark_stringValue", "Key: 1, Value: ark_numberValue", "Key: true, Value: ark_booleanValue", "Key: [object Object], Value: ark_objectValue", "Key: null, Value: ark_nullValue", "Key: undefined, Value: ark_undefinedValue", "Key: NaN, Value: ark_NaNValue", "Key: Infinity, Value: ark_infinityValue", "Key: -Infinity, Value: ark_negativeInfinityValue", "Key: /ark_regexKey/, Value: ark_regexValue", "Key: [object Map], Value: ark_mapValue", "Key: [object Set], Value: ark_setValue", "Key: 1,2,3, Value: ark_arrayValue", "Key: ark_unicodeKey , Value: ark_unicodeValue "]); 180 181let objkeys6= []; 182combinedMap.forEach((value, key) => { 183 const retrievedValue = combinedMap.get(key); 184 const hasKey = combinedMap.has(key); 185 newMap.set(key, value); 186 objkeys6.push("Key: " + key + ", Retrieved Value: " + retrievedValue); 187 objkeys6.push("Key: " + key + ", Exists: " + hasKey); 188 combinedMap.delete(key); 189}); 190assert_equal(objkeys6,["Key: ark_stringKey, Retrieved Value: ark_stringValue", "Key: ark_stringKey, Exists: true", "Key: 1, Retrieved Value: ark_numberValue", "Key: 1, Exists: true", "Key: true, Retrieved Value: ark_booleanValue", "Key: true, Exists: true", "Key: [object Object], Retrieved Value: ark_objectValue", "Key: [object Object], Exists: true", "Key: null, Retrieved Value: ark_nullValue", "Key: null, Exists: true", "Key: undefined, Retrieved Value: ark_undefinedValue", "Key: undefined, Exists: true", "Key: NaN, Retrieved Value: ark_NaNValue", "Key: NaN, Exists: true", "Key: Infinity, Retrieved Value: ark_infinityValue", "Key: Infinity, Exists: true", "Key: -Infinity, Retrieved Value: ark_negativeInfinityValue", "Key: -Infinity, Exists: true", "Key: /ark_regexKey/, Retrieved Value: ark_regexValue", "Key: /ark_regexKey/, Exists: true", "Key: [object Map], Retrieved Value: ark_mapValue", "Key: [object Map], Exists: true", "Key: [object Set], Retrieved Value: ark_setValue", "Key: [object Set], Exists: true", "Key: 1,2,3, Retrieved Value: ark_arrayValue", "Key: 1,2,3, Exists: true", "Key: ark_unicodeKey , Retrieved Value: ark_unicodeValue ", "Key: ark_unicodeKey , Exists: true"]); 191 192assert_equal(combinedMap.size,0); 193newMap.clear(); 194assert_equal(newMap.size,0); 195 196const testMap = new Map([ 197 ["key1", "value1"], 198 ["key2", "value2"], 199]); 200const emptyMap = new Map(); 201 202try { 203 const abnormalMap = new Map(5); 204 assert_unreachable(); 205} catch (error) { 206 assert_equal("Caught an error: "+ error, "Caught an error: TypeError: iterable is not object"); 207} 208 209try { 210 const value = testMap.get(NaN); 211 const result1 = testMap.has(NaN); 212 const result2 = testMap.has(); 213 testMap.set(NaN, "value"); 214 testMap.set("key", "value1"); 215 testMap.set("key", "value2"); 216 emptyMap.clear(); 217 emptyMap.delete(NaN); 218} catch (error) { 219 assert_unreachable(); 220} 221 222try { 223 testMap.forEach(5); 224 assert_unreachable(); 225} catch (error) { 226 assert_equal("Caught an error: "+ error, "Caught an error: TypeError: obj is not Callable"); 227} 228 229try { 230 testMap.forEach((value, key) => { 231 if (key === "key2") { 232 throw new Error("Encountered key2"); 233 } 234 }); 235} catch (error) { 236 assert_equal("Caught an error: "+ error, "Caught an error: Error: Encountered key2"); 237} 238 239// Map.clear tests 240map = new Map(); 241map.set(1, null); 242map.set(2, null); 243map.set(3, null); 244let beginNotTought = map.entries(); 245let midNotTought = map.entries(); 246midNotTought.next(); 247let begin = map.entries(); // points to (1, null) 248let mid = map.entries(); // points to (2, null) 249mid.next(); 250let last = map.entries(); // points to (3, null) 251last.next(); 252last.next(); 253let end = map.entries(); // points to the end 254while (end.next().done) { 255} 256map.clear(); 257if (map.size != 0) { 258 throw new Error("Map size must be 0"); 259} 260if (!begin.next().done) { 261 throw new Error("Invalid 'begin' iterator"); 262} 263if (!mid.next().done) { 264 throw new Error("Invalid 'mid' iterator"); 265} 266if (!last.next().done) { 267 throw new Error("Invalid 'last' iterator"); 268} 269if (!end.next().done) { 270 throw new Error("Invalid 'end' iterator"); 271} 272map.set(-1, null); 273map.set(-2, null); 274map.set(-3, null); 275let v = beginNotTought.next(); 276if (v.done) { 277 throw new Error("Invalid 'beginNotTought' iterator"); 278} 279if (v.value[0] != -1) { 280 throw new Error("Invalid 'beginNotTought' iterator's value"); 281} 282v = midNotTought.next(); 283if (v.done) { 284 throw new Error("Invalid 'midNotTought' iterator"); 285} 286if (v.value[0] != -1) { 287 throw new Error("Invalid 'midNotTought' iterator's value"); 288} 289if (!begin.next().done) { 290 throw new Error("Invalid 'begin' iterator"); 291} 292if (!mid.next().done) { 293 throw new Error("Invalid 'mid' iterator"); 294} 295if (!last.next().done) { 296 throw new Error("Invalid 'last' iterator"); 297} 298if (!end.next().done) { 299 throw new Error("Invalid 'end' iterator"); 300} 301 302test_end();