1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 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 16var m = new Map(); 17assert(m.size == 0); 18assert(m.set(1, 1) === m); 19assert(m.has(1)); 20assert(m.size == 1); 21 22assert(m.set(undefined, 123) === m); 23assert(m.size == 2); 24assert(m.has(undefined)); 25assert(m.get(undefined) === 123); 26 27assert(m.set(null, 456) === m); 28assert(m.size == 3); 29assert(m.has(null)); 30assert(m.get(null) === 456); 31 32assert(m.set("strItem", { x:789 }) === m); 33assert(m.size == 4); 34assert(m.has("str" + "Item")); 35assert(m.get("st" + "rItem").x === 789); 36 37assert(m.set(12.25, 12.25) === m); 38assert(m.size == 5); 39assert(m.has(12 + (function() { return 0.25 })())); 40assert(m.get(13 - (function() { return 0.75 })()) === 12.25); 41 42assert(m.delete(1)) 43assert(m.size == 4); 44assert(!m.has(1)); 45assert(m.get(1) === undefined); 46 47assert(!m.delete(2)); 48 49assert(m.delete(12 + (function() { return 0.25 })())); 50assert(m.size == 3); 51assert(!m.has(12.25)); 52assert(m.get(12.25) === undefined); 53 54assert(m.delete("strI" + "tem")) 55assert(m.delete(undefined)) 56assert(m.size == 1); 57 58assert(m.delete(null)) 59assert(m.size == 0); 60 61m.set(1,1) 62m.set(2,2) 63m.set(3,3) 64m.set(1,7) 65m.set(1,8) 66m.set(2,7) 67m.set(2,8) 68m.set(3,7) 69m.set(3,8) 70 71assert(m.size == 3); 72assert(m.get(1) === 8); 73assert(m.get(2) === 8); 74assert(m.get(3) === 8); 75 76m.set(NaN, "not a number"); 77assert(m.size === 4); 78assert(m.get(NaN) === "not a number"); 79assert(m.get(Number("foo")) === "not a number") 80 81m.set(0, "PosZero"); 82assert(m.size === 5); 83m.set(-0, "NegZero"); 84assert(m.size === 5); 85assert(m.get (0) === "NegZero"); 86assert(m.get (-0) === "NegZero"); 87 88var symbolFoo = Symbol ("foo"); 89m.set (symbolFoo, "SymbolFoo"); 90assert(m.size === 6); 91assert(m.get(symbolFoo) === "SymbolFoo"); 92 93var object = {}; 94m.set (object, "object"); 95assert(m.size === 7); 96assert(m.get(object) === "object"); 97assert(m.get({}) === undefined); 98 99var myObj = { o : 4 }; 100m.set("foo", myObj); 101assert(m.size === 8); 102assert(m.get ("foo") === myObj); 103assert(m.get ("foo").o === 4); 104 105m.clear(); 106assert(m.size === 0); 107 108m.set("foo", myObj); 109assert(m.size === 1); 110assert(m.get ("foo") === myObj); 111assert(m.get ("foo").o === 4); 112 113var mapNameDesc = Object.getOwnPropertyDescriptor (Map, 'name'); 114assert(mapNameDesc.value === "Map"); 115assert(mapNameDesc.writable === false); 116assert(mapNameDesc.enumerable === false); 117assert(mapNameDesc.configurable === true); 118 119assert(Map.prototype.name === undefined); 120 121m = new Map([{0: "foo", 1: 3}, {0 : "bar", 1 : 2}]); 122 123assert (m.size === 2); 124assert (m.get("foo") === 3); 125assert (m.get("bar") === 2); 126 127function createIterable(arr, methods = {}) { 128 let iterable = function *() { 129 let idx = 0; 130 while (idx < arr.length) { 131 yield arr[idx]; 132 idx++; 133 } 134 }(); 135 iterable['return'] = methods['return']; 136 iterable['throw'] = methods['throw']; 137 138 return iterable; 139}; 140 141var closed = false; 142var iter = createIterable([1, 2, 3], { 143 'return': function(){ closed = true; return {}; } 144}); 145try { 146 new Map(iter); 147} catch(e){} 148 149assert(closed === true); 150 151var map = new Map(); 152map.set(-0, "foo"); 153var k; 154map.forEach(function (value, key) { 155 k = 1 / key; 156}); 157 158assert(k === Infinity); 159assert(map.get(+0) === "foo"); 160