| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --harmony-collections --expose-gc --allow-natives-syntax | 28 // Flags: --harmony-weak-collections --expose-gc --allow-natives-syntax |
| 29 | 29 |
| 30 | 30 |
| 31 // Test valid getter and setter calls on Sets. | 31 // Note: this test is superseded by harmony/collections.js. |
| 32 // IF YOU CHANGE THIS FILE, apply the same changes to harmony/collections.js! |
| 33 // TODO(rossberg): Remove once non-weak collections have caught up. |
| 34 |
| 35 // Test valid getter and setter calls on WeakSets. |
| 32 function TestValidSetCalls(m) { | 36 function TestValidSetCalls(m) { |
| 33 assertDoesNotThrow(function () { m.add(new Object) }); | 37 assertDoesNotThrow(function () { m.add(new Object) }); |
| 34 assertDoesNotThrow(function () { m.has(new Object) }); | 38 assertDoesNotThrow(function () { m.has(new Object) }); |
| 35 assertDoesNotThrow(function () { m.delete(new Object) }); | 39 assertDoesNotThrow(function () { m.delete(new Object) }); |
| 36 } | 40 } |
| 37 TestValidSetCalls(new Set); | |
| 38 TestValidSetCalls(new WeakSet); | 41 TestValidSetCalls(new WeakSet); |
| 39 | 42 |
| 40 | 43 |
| 41 // Test valid getter and setter calls on Maps and WeakMaps | 44 // Test valid getter and setter calls on WeakMaps |
| 42 function TestValidMapCalls(m) { | 45 function TestValidMapCalls(m) { |
| 43 assertDoesNotThrow(function () { m.get(new Object) }); | 46 assertDoesNotThrow(function () { m.get(new Object) }); |
| 44 assertDoesNotThrow(function () { m.set(new Object) }); | 47 assertDoesNotThrow(function () { m.set(new Object) }); |
| 45 assertDoesNotThrow(function () { m.has(new Object) }); | 48 assertDoesNotThrow(function () { m.has(new Object) }); |
| 46 assertDoesNotThrow(function () { m.delete(new Object) }); | 49 assertDoesNotThrow(function () { m.delete(new Object) }); |
| 47 } | 50 } |
| 48 TestValidMapCalls(new Map); | |
| 49 TestValidMapCalls(new WeakMap); | 51 TestValidMapCalls(new WeakMap); |
| 50 | 52 |
| 51 | 53 |
| 52 // Test invalid getter and setter calls for WeakMap only | 54 // Test invalid getter and setter calls for WeakMap |
| 53 function TestInvalidCalls(m) { | 55 function TestInvalidCalls(m) { |
| 54 assertThrows(function () { m.get(undefined) }, TypeError); | 56 assertThrows(function () { m.get(undefined) }, TypeError); |
| 55 assertThrows(function () { m.set(undefined, 0) }, TypeError); | 57 assertThrows(function () { m.set(undefined, 0) }, TypeError); |
| 56 assertThrows(function () { m.get(null) }, TypeError); | 58 assertThrows(function () { m.get(null) }, TypeError); |
| 57 assertThrows(function () { m.set(null, 0) }, TypeError); | 59 assertThrows(function () { m.set(null, 0) }, TypeError); |
| 58 assertThrows(function () { m.get(0) }, TypeError); | 60 assertThrows(function () { m.get(0) }, TypeError); |
| 59 assertThrows(function () { m.set(0, 0) }, TypeError); | 61 assertThrows(function () { m.set(0, 0) }, TypeError); |
| 60 assertThrows(function () { m.get('a-key') }, TypeError); | 62 assertThrows(function () { m.get('a-key') }, TypeError); |
| 61 assertThrows(function () { m.set('a-key', 0) }, TypeError); | 63 assertThrows(function () { m.set('a-key', 0) }, TypeError); |
| 62 } | 64 } |
| 63 TestInvalidCalls(new WeakMap); | 65 TestInvalidCalls(new WeakMap); |
| 64 | 66 |
| 65 | 67 |
| 66 // Test expected behavior for Sets | 68 // Test expected behavior for WeakSets |
| 67 function TestSet(set, key) { | 69 function TestSet(set, key) { |
| 68 assertFalse(set.has(key)); | 70 assertFalse(set.has(key)); |
| 69 assertSame(undefined, set.add(key)); | 71 assertSame(undefined, set.add(key)); |
| 70 assertTrue(set.has(key)); | 72 assertTrue(set.has(key)); |
| 71 assertTrue(set.delete(key)); | 73 assertTrue(set.delete(key)); |
| 72 assertFalse(set.has(key)); | 74 assertFalse(set.has(key)); |
| 73 assertFalse(set.delete(key)); | 75 assertFalse(set.delete(key)); |
| 74 assertFalse(set.has(key)); | 76 assertFalse(set.has(key)); |
| 75 } | 77 } |
| 76 function TestSetBehavior(set) { | 78 function TestSetBehavior(set) { |
| 77 for (var i = 0; i < 20; i++) { | 79 for (var i = 0; i < 20; i++) { |
| 78 TestSet(set, new Object); | 80 TestSet(set, new Object); |
| 79 TestSet(set, i); | 81 TestSet(set, i); |
| 80 TestSet(set, i / 100); | 82 TestSet(set, i / 100); |
| 81 TestSet(set, 'key-' + i); | 83 TestSet(set, 'key-' + i); |
| 82 } | 84 } |
| 83 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; | 85 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; |
| 84 for (var i = 0; i < keys.length; i++) { | 86 for (var i = 0; i < keys.length; i++) { |
| 85 TestSet(set, keys[i]); | 87 TestSet(set, keys[i]); |
| 86 } | 88 } |
| 87 } | 89 } |
| 88 TestSetBehavior(new Set); | |
| 89 TestSet(new WeakSet, new Object); | 90 TestSet(new WeakSet, new Object); |
| 90 | 91 |
| 91 | 92 |
| 92 // Test expected mapping behavior for Maps and WeakMaps | 93 // Test expected mapping behavior for WeakMaps |
| 93 function TestMapping(map, key, value) { | 94 function TestMapping(map, key, value) { |
| 94 assertSame(undefined, map.set(key, value)); | 95 assertSame(undefined, map.set(key, value)); |
| 95 assertSame(value, map.get(key)); | 96 assertSame(value, map.get(key)); |
| 96 } | 97 } |
| 97 function TestMapBehavior1(m) { | 98 function TestMapBehavior1(m) { |
| 98 TestMapping(m, new Object, 23); | 99 TestMapping(m, new Object, 23); |
| 99 TestMapping(m, new Object, 'the-value'); | 100 TestMapping(m, new Object, 'the-value'); |
| 100 TestMapping(m, new Object, new Object); | 101 TestMapping(m, new Object, new Object); |
| 101 } | 102 } |
| 102 TestMapBehavior1(new Map); | |
| 103 TestMapBehavior1(new WeakMap); | 103 TestMapBehavior1(new WeakMap); |
| 104 | 104 |
| 105 | 105 |
| 106 // Test expected mapping behavior for Maps only | 106 // Test expected querying behavior of WeakMaps |
| 107 function TestMapBehavior2(m) { | |
| 108 for (var i = 0; i < 20; i++) { | |
| 109 TestMapping(m, i, new Object); | |
| 110 TestMapping(m, i / 10, new Object); | |
| 111 TestMapping(m, 'key-' + i, new Object); | |
| 112 } | |
| 113 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; | |
| 114 for (var i = 0; i < keys.length; i++) { | |
| 115 TestMapping(m, keys[i], new Object); | |
| 116 } | |
| 117 } | |
| 118 TestMapBehavior2(new Map); | |
| 119 | |
| 120 | |
| 121 // Test expected querying behavior of Maps and WeakMaps | |
| 122 function TestQuery(m) { | 107 function TestQuery(m) { |
| 123 var key = new Object; | 108 var key = new Object; |
| 124 var values = [ 'x', 0, +Infinity, -Infinity, true, false, null, undefined ]; | 109 var values = [ 'x', 0, +Infinity, -Infinity, true, false, null, undefined ]; |
| 125 for (var i = 0; i < values.length; i++) { | 110 for (var i = 0; i < values.length; i++) { |
| 126 TestMapping(m, key, values[i]); | 111 TestMapping(m, key, values[i]); |
| 127 assertTrue(m.has(key)); | 112 assertTrue(m.has(key)); |
| 128 assertFalse(m.has(new Object)); | 113 assertFalse(m.has(new Object)); |
| 129 } | 114 } |
| 130 } | 115 } |
| 131 TestQuery(new Map); | |
| 132 TestQuery(new WeakMap); | 116 TestQuery(new WeakMap); |
| 133 | 117 |
| 134 | 118 |
| 135 // Test expected deletion behavior of Maps and WeakMaps | 119 // Test expected deletion behavior of WeakMaps |
| 136 function TestDelete(m) { | 120 function TestDelete(m) { |
| 137 var key = new Object; | 121 var key = new Object; |
| 138 TestMapping(m, key, 'to-be-deleted'); | 122 TestMapping(m, key, 'to-be-deleted'); |
| 139 assertTrue(m.delete(key)); | 123 assertTrue(m.delete(key)); |
| 140 assertFalse(m.delete(key)); | 124 assertFalse(m.delete(key)); |
| 141 assertFalse(m.delete(new Object)); | 125 assertFalse(m.delete(new Object)); |
| 142 assertSame(m.get(key), undefined); | 126 assertSame(m.get(key), undefined); |
| 143 } | 127 } |
| 144 TestDelete(new Map); | |
| 145 TestDelete(new WeakMap); | 128 TestDelete(new WeakMap); |
| 146 | 129 |
| 147 | 130 |
| 148 // Test GC of Maps and WeakMaps with entry | 131 // Test GC of WeakMaps with entry |
| 149 function TestGC1(m) { | 132 function TestGC1(m) { |
| 150 var key = new Object; | 133 var key = new Object; |
| 151 m.set(key, 'not-collected'); | 134 m.set(key, 'not-collected'); |
| 152 gc(); | 135 gc(); |
| 153 assertSame('not-collected', m.get(key)); | 136 assertSame('not-collected', m.get(key)); |
| 154 } | 137 } |
| 155 TestGC1(new Map); | |
| 156 TestGC1(new WeakMap); | 138 TestGC1(new WeakMap); |
| 157 | 139 |
| 158 | 140 |
| 159 // Test GC of Maps and WeakMaps with chained entries | 141 // Test GC of WeakMaps with chained entries |
| 160 function TestGC2(m) { | 142 function TestGC2(m) { |
| 161 var head = new Object; | 143 var head = new Object; |
| 162 for (key = head, i = 0; i < 10; i++, key = m.get(key)) { | 144 for (key = head, i = 0; i < 10; i++, key = m.get(key)) { |
| 163 m.set(key, new Object); | 145 m.set(key, new Object); |
| 164 } | 146 } |
| 165 gc(); | 147 gc(); |
| 166 var count = 0; | 148 var count = 0; |
| 167 for (key = head; key != undefined; key = m.get(key)) { | 149 for (key = head; key != undefined; key = m.get(key)) { |
| 168 count++; | 150 count++; |
| 169 } | 151 } |
| 170 assertEquals(11, count); | 152 assertEquals(11, count); |
| 171 } | 153 } |
| 172 TestGC2(new Map); | |
| 173 TestGC2(new WeakMap); | 154 TestGC2(new WeakMap); |
| 174 | 155 |
| 175 | 156 |
| 176 // Test property attribute [[Enumerable]] | 157 // Test property attribute [[Enumerable]] |
| 177 function TestEnumerable(func) { | 158 function TestEnumerable(func) { |
| 178 function props(x) { | 159 function props(x) { |
| 179 var array = []; | 160 var array = []; |
| 180 for (var p in x) array.push(p); | 161 for (var p in x) array.push(p); |
| 181 return array.sort(); | 162 return array.sort(); |
| 182 } | 163 } |
| 183 assertArrayEquals([], props(func)); | 164 assertArrayEquals([], props(func)); |
| 184 assertArrayEquals([], props(func.prototype)); | 165 assertArrayEquals([], props(func.prototype)); |
| 185 assertArrayEquals([], props(new func())); | 166 assertArrayEquals([], props(new func())); |
| 186 } | 167 } |
| 187 TestEnumerable(Set); | |
| 188 TestEnumerable(Map); | |
| 189 TestEnumerable(WeakMap); | 168 TestEnumerable(WeakMap); |
| 190 TestEnumerable(WeakSet); | 169 TestEnumerable(WeakSet); |
| 191 | 170 |
| 192 | 171 |
| 193 // Test arbitrary properties on Maps and WeakMaps | 172 // Test arbitrary properties on WeakMaps |
| 194 function TestArbitrary(m) { | 173 function TestArbitrary(m) { |
| 195 function TestProperty(map, property, value) { | 174 function TestProperty(map, property, value) { |
| 196 map[property] = value; | 175 map[property] = value; |
| 197 assertEquals(value, map[property]); | 176 assertEquals(value, map[property]); |
| 198 } | 177 } |
| 199 for (var i = 0; i < 20; i++) { | 178 for (var i = 0; i < 20; i++) { |
| 200 TestProperty(m, i, 'val' + i); | 179 TestProperty(m, i, 'val' + i); |
| 201 TestProperty(m, 'foo' + i, 'bar' + i); | 180 TestProperty(m, 'foo' + i, 'bar' + i); |
| 202 } | 181 } |
| 203 TestMapping(m, new Object, 'foobar'); | 182 TestMapping(m, new Object, 'foobar'); |
| 204 } | 183 } |
| 205 TestArbitrary(new Map); | |
| 206 TestArbitrary(new WeakMap); | 184 TestArbitrary(new WeakMap); |
| 207 | 185 |
| 208 | 186 |
| 209 // Test direct constructor call | 187 // Test direct constructor call |
| 210 assertThrows(function() { Set(); }, TypeError); | |
| 211 assertThrows(function() { Map(); }, TypeError); | |
| 212 assertThrows(function() { WeakMap(); }, TypeError); | 188 assertThrows(function() { WeakMap(); }, TypeError); |
| 213 assertThrows(function() { WeakSet(); }, TypeError); | 189 assertThrows(function() { WeakSet(); }, TypeError); |
| 214 | 190 |
| 215 | 191 |
| 216 // Test whether NaN values as keys are treated correctly. | |
| 217 var s = new Set; | |
| 218 assertFalse(s.has(NaN)); | |
| 219 assertFalse(s.has(NaN + 1)); | |
| 220 assertFalse(s.has(23)); | |
| 221 s.add(NaN); | |
| 222 assertTrue(s.has(NaN)); | |
| 223 assertTrue(s.has(NaN + 1)); | |
| 224 assertFalse(s.has(23)); | |
| 225 var m = new Map; | |
| 226 assertFalse(m.has(NaN)); | |
| 227 assertFalse(m.has(NaN + 1)); | |
| 228 assertFalse(m.has(23)); | |
| 229 m.set(NaN, 'a-value'); | |
| 230 assertTrue(m.has(NaN)); | |
| 231 assertTrue(m.has(NaN + 1)); | |
| 232 assertFalse(m.has(23)); | |
| 233 | |
| 234 | |
| 235 // Test some common JavaScript idioms for Sets | |
| 236 var s = new Set; | |
| 237 assertTrue(s instanceof Set); | |
| 238 assertTrue(Set.prototype.add instanceof Function) | |
| 239 assertTrue(Set.prototype.has instanceof Function) | |
| 240 assertTrue(Set.prototype.delete instanceof Function) | |
| 241 assertTrue(Set.prototype.clear instanceof Function) | |
| 242 | |
| 243 | |
| 244 // Test some common JavaScript idioms for Maps | |
| 245 var m = new Map; | |
| 246 assertTrue(m instanceof Map); | |
| 247 assertTrue(Map.prototype.set instanceof Function) | |
| 248 assertTrue(Map.prototype.get instanceof Function) | |
| 249 assertTrue(Map.prototype.has instanceof Function) | |
| 250 assertTrue(Map.prototype.delete instanceof Function) | |
| 251 assertTrue(Map.prototype.clear instanceof Function) | |
| 252 | |
| 253 | |
| 254 // Test some common JavaScript idioms for WeakMaps | 192 // Test some common JavaScript idioms for WeakMaps |
| 255 var m = new WeakMap; | 193 var m = new WeakMap; |
| 256 assertTrue(m instanceof WeakMap); | 194 assertTrue(m instanceof WeakMap); |
| 257 assertTrue(WeakMap.prototype.set instanceof Function) | 195 assertTrue(WeakMap.prototype.set instanceof Function) |
| 258 assertTrue(WeakMap.prototype.get instanceof Function) | 196 assertTrue(WeakMap.prototype.get instanceof Function) |
| 259 assertTrue(WeakMap.prototype.has instanceof Function) | 197 assertTrue(WeakMap.prototype.has instanceof Function) |
| 260 assertTrue(WeakMap.prototype.delete instanceof Function) | 198 assertTrue(WeakMap.prototype.delete instanceof Function) |
| 261 assertTrue(WeakMap.prototype.clear instanceof Function) | 199 assertTrue(WeakMap.prototype.clear instanceof Function) |
| 262 | 200 |
| 263 | 201 |
| 264 // Test some common JavaScript idioms for WeakSets | 202 // Test some common JavaScript idioms for WeakSets |
| 265 var s = new WeakSet; | 203 var s = new WeakSet; |
| 266 assertTrue(s instanceof WeakSet); | 204 assertTrue(s instanceof WeakSet); |
| 267 assertTrue(WeakSet.prototype.add instanceof Function) | 205 assertTrue(WeakSet.prototype.add instanceof Function) |
| 268 assertTrue(WeakSet.prototype.has instanceof Function) | 206 assertTrue(WeakSet.prototype.has instanceof Function) |
| 269 assertTrue(WeakSet.prototype.delete instanceof Function) | 207 assertTrue(WeakSet.prototype.delete instanceof Function) |
| 270 assertTrue(WeakSet.prototype.clear instanceof Function) | 208 assertTrue(WeakSet.prototype.clear instanceof Function) |
| 271 | 209 |
| 272 | 210 |
| 273 // Test class of instance and prototype. | 211 // Test class of instance and prototype. |
| 274 assertEquals("Set", %_ClassOf(new Set)) | |
| 275 assertEquals("Object", %_ClassOf(Set.prototype)) | |
| 276 assertEquals("Map", %_ClassOf(new Map)) | |
| 277 assertEquals("Object", %_ClassOf(Map.prototype)) | |
| 278 assertEquals("WeakMap", %_ClassOf(new WeakMap)) | 212 assertEquals("WeakMap", %_ClassOf(new WeakMap)) |
| 279 assertEquals("Object", %_ClassOf(WeakMap.prototype)) | 213 assertEquals("Object", %_ClassOf(WeakMap.prototype)) |
| 280 assertEquals("WeakSet", %_ClassOf(new WeakSet)) | 214 assertEquals("WeakSet", %_ClassOf(new WeakSet)) |
| 281 assertEquals("Object", %_ClassOf(WeakMap.prototype)) | 215 assertEquals("Object", %_ClassOf(WeakMap.prototype)) |
| 282 | 216 |
| 283 | 217 |
| 284 // Test name of constructor. | 218 // Test name of constructor. |
| 285 assertEquals("Set", Set.name); | |
| 286 assertEquals("Map", Map.name); | |
| 287 assertEquals("WeakMap", WeakMap.name); | 219 assertEquals("WeakMap", WeakMap.name); |
| 288 assertEquals("WeakSet", WeakSet.name); | 220 assertEquals("WeakSet", WeakSet.name); |
| 289 | 221 |
| 290 | 222 |
| 291 // Test prototype property of Set, Map, WeakMap and WeakSet. | 223 // Test prototype property of WeakMap and WeakSet. |
| 292 function TestPrototype(C) { | 224 function TestPrototype(C) { |
| 293 assertTrue(C.prototype instanceof Object); | 225 assertTrue(C.prototype instanceof Object); |
| 294 assertEquals({ | 226 assertEquals({ |
| 295 value: {}, | 227 value: {}, |
| 296 writable: true, // TODO(2793): This should be non-writable. | 228 writable: true, // TODO(2793): This should be non-writable. |
| 297 enumerable: false, | 229 enumerable: false, |
| 298 configurable: false | 230 configurable: false |
| 299 }, Object.getOwnPropertyDescriptor(C, "prototype")); | 231 }, Object.getOwnPropertyDescriptor(C, "prototype")); |
| 300 } | 232 } |
| 301 TestPrototype(Set); | |
| 302 TestPrototype(Map); | |
| 303 TestPrototype(WeakMap); | 233 TestPrototype(WeakMap); |
| 304 TestPrototype(WeakSet); | 234 TestPrototype(WeakSet); |
| 305 | 235 |
| 306 | 236 |
| 307 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype. | 237 // Test constructor property of the WeakMap and WeakSet prototype. |
| 308 function TestConstructor(C) { | 238 function TestConstructor(C) { |
| 309 assertFalse(C === Object.prototype.constructor); | 239 assertFalse(C === Object.prototype.constructor); |
| 310 assertSame(C, C.prototype.constructor); | 240 assertSame(C, C.prototype.constructor); |
| 311 assertSame(C, (new C).__proto__.constructor); | 241 assertSame(C, (new C).__proto__.constructor); |
| 312 } | 242 } |
| 313 TestConstructor(Set); | |
| 314 TestConstructor(Map); | |
| 315 TestConstructor(WeakMap); | 243 TestConstructor(WeakMap); |
| 316 TestConstructor(WeakSet); | 244 TestConstructor(WeakSet); |
| 317 | 245 |
| 318 | 246 |
| 319 // Test the Set, Map, WeakMap and WeakSet global properties themselves. | 247 // Test the WeakMap and WeakSet global properties themselves. |
| 320 function TestDescriptor(global, C) { | 248 function TestDescriptor(global, C) { |
| 321 assertEquals({ | 249 assertEquals({ |
| 322 value: C, | 250 value: C, |
| 323 writable: true, | 251 writable: true, |
| 324 enumerable: false, | 252 enumerable: false, |
| 325 configurable: true | 253 configurable: true |
| 326 }, Object.getOwnPropertyDescriptor(global, C.name)); | 254 }, Object.getOwnPropertyDescriptor(global, C.name)); |
| 327 } | 255 } |
| 328 TestDescriptor(this, Set); | |
| 329 TestDescriptor(this, Map); | |
| 330 TestDescriptor(this, WeakMap); | 256 TestDescriptor(this, WeakMap); |
| 331 TestDescriptor(this, WeakSet); | 257 TestDescriptor(this, WeakSet); |
| 332 | 258 |
| 333 | 259 |
| 334 // Regression test for WeakMap prototype. | 260 // Regression test for WeakMap prototype. |
| 335 assertTrue(WeakMap.prototype.constructor === WeakMap) | 261 assertTrue(WeakMap.prototype.constructor === WeakMap) |
| 336 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) | 262 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) |
| 337 | 263 |
| 338 | 264 |
| 339 // Regression test for issue 1617: The prototype of the WeakMap constructor | 265 // Regression test for issue 1617: The prototype of the WeakMap constructor |
| (...skipping 11 matching lines...) Expand all Loading... |
| 351 writable: true | 277 writable: true |
| 352 }}); | 278 }}); |
| 353 assertEquals(10, o.myValue); | 279 assertEquals(10, o.myValue); |
| 354 | 280 |
| 355 | 281 |
| 356 // Regression test for issue 1884: Invoking any of the methods for Harmony | 282 // Regression test for issue 1884: Invoking any of the methods for Harmony |
| 357 // maps, sets, or weak maps, with a wrong type of receiver should be throwing | 283 // maps, sets, or weak maps, with a wrong type of receiver should be throwing |
| 358 // a proper TypeError. | 284 // a proper TypeError. |
| 359 var alwaysBogus = [ undefined, null, true, "x", 23, {} ]; | 285 var alwaysBogus = [ undefined, null, true, "x", 23, {} ]; |
| 360 var bogusReceiversTestSet = [ | 286 var bogusReceiversTestSet = [ |
| 361 { proto: Set.prototype, | |
| 362 funcs: [ 'add', 'has', 'delete' ], | |
| 363 receivers: alwaysBogus.concat([ new Map, new WeakMap, new WeakSet ]), | |
| 364 }, | |
| 365 { proto: Map.prototype, | |
| 366 funcs: [ 'get', 'set', 'has', 'delete' ], | |
| 367 receivers: alwaysBogus.concat([ new Set, new WeakMap, new WeakSet ]), | |
| 368 }, | |
| 369 { proto: WeakMap.prototype, | 287 { proto: WeakMap.prototype, |
| 370 funcs: [ 'get', 'set', 'has', 'delete' ], | 288 funcs: [ 'get', 'set', 'has', 'delete' ], |
| 371 receivers: alwaysBogus.concat([ new Set, new Map, new WeakSet ]), | 289 receivers: alwaysBogus.concat([ new WeakSet ]), |
| 372 }, | 290 }, |
| 373 { proto: WeakSet.prototype, | 291 { proto: WeakSet.prototype, |
| 374 funcs: [ 'add', 'has', 'delete' ], | 292 funcs: [ 'add', 'has', 'delete' ], |
| 375 receivers: alwaysBogus.concat([ new Set, new Map, new WeakMap ]), | 293 receivers: alwaysBogus.concat([ new WeakMap ]), |
| 376 }, | 294 }, |
| 377 ]; | 295 ]; |
| 378 function TestBogusReceivers(testSet) { | 296 function TestBogusReceivers(testSet) { |
| 379 for (var i = 0; i < testSet.length; i++) { | 297 for (var i = 0; i < testSet.length; i++) { |
| 380 var proto = testSet[i].proto; | 298 var proto = testSet[i].proto; |
| 381 var funcs = testSet[i].funcs; | 299 var funcs = testSet[i].funcs; |
| 382 var receivers = testSet[i].receivers; | 300 var receivers = testSet[i].receivers; |
| 383 for (var j = 0; j < funcs.length; j++) { | 301 for (var j = 0; j < funcs.length; j++) { |
| 384 var func = proto[funcs[j]]; | 302 var func = proto[funcs[j]]; |
| 385 for (var k = 0; k < receivers.length; k++) { | 303 for (var k = 0; k < receivers.length; k++) { |
| 386 assertThrows(function () { func.call(receivers[k], {}) }, TypeError); | 304 assertThrows(function () { func.call(receivers[k], {}) }, TypeError); |
| 387 } | 305 } |
| 388 } | 306 } |
| 389 } | 307 } |
| 390 } | 308 } |
| 391 TestBogusReceivers(bogusReceiversTestSet); | 309 TestBogusReceivers(bogusReceiversTestSet); |
| 392 | 310 |
| 393 | 311 |
| 394 // Stress Test | |
| 395 // There is a proposed stress-test available at the es-discuss mailing list | |
| 396 // which cannot be reasonably automated. Check it out by hand if you like: | |
| 397 // https://mail.mozilla.org/pipermail/es-discuss/2011-May/014096.html | |
| 398 | |
| 399 | |
| 400 // Set and Map size getters | |
| 401 var setSizeDescriptor = Object.getOwnPropertyDescriptor(Set.prototype, 'size'); | |
| 402 assertEquals(undefined, setSizeDescriptor.value); | |
| 403 assertEquals(undefined, setSizeDescriptor.set); | |
| 404 assertTrue(setSizeDescriptor.get instanceof Function); | |
| 405 assertEquals(undefined, setSizeDescriptor.get.prototype); | |
| 406 assertFalse(setSizeDescriptor.enumerable); | |
| 407 assertTrue(setSizeDescriptor.configurable); | |
| 408 | |
| 409 var s = new Set(); | |
| 410 assertFalse(s.hasOwnProperty('size')); | |
| 411 for (var i = 0; i < 10; i++) { | |
| 412 assertEquals(i, s.size); | |
| 413 s.add(i); | |
| 414 } | |
| 415 for (var i = 9; i >= 0; i--) { | |
| 416 s.delete(i); | |
| 417 assertEquals(i, s.size); | |
| 418 } | |
| 419 | |
| 420 | |
| 421 var mapSizeDescriptor = Object.getOwnPropertyDescriptor(Map.prototype, 'size'); | |
| 422 assertEquals(undefined, mapSizeDescriptor.value); | |
| 423 assertEquals(undefined, mapSizeDescriptor.set); | |
| 424 assertTrue(mapSizeDescriptor.get instanceof Function); | |
| 425 assertEquals(undefined, mapSizeDescriptor.get.prototype); | |
| 426 assertFalse(mapSizeDescriptor.enumerable); | |
| 427 assertTrue(mapSizeDescriptor.configurable); | |
| 428 | |
| 429 var m = new Map(); | |
| 430 assertFalse(m.hasOwnProperty('size')); | |
| 431 for (var i = 0; i < 10; i++) { | |
| 432 assertEquals(i, m.size); | |
| 433 m.set(i, i); | |
| 434 } | |
| 435 for (var i = 9; i >= 0; i--) { | |
| 436 m.delete(i); | |
| 437 assertEquals(i, m.size); | |
| 438 } | |
| 439 | |
| 440 | |
| 441 // Test Set clear | |
| 442 (function() { | |
| 443 var s = new Set(); | |
| 444 s.add(42); | |
| 445 assertTrue(s.has(42)); | |
| 446 assertEquals(1, s.size); | |
| 447 s.clear(); | |
| 448 assertFalse(s.has(42)); | |
| 449 assertEquals(0, s.size); | |
| 450 })(); | |
| 451 | |
| 452 | |
| 453 // Test Map clear | |
| 454 (function() { | |
| 455 var m = new Map(); | |
| 456 m.set(42, true); | |
| 457 assertTrue(m.has(42)); | |
| 458 assertEquals(1, m.size); | |
| 459 m.clear(); | |
| 460 assertFalse(m.has(42)); | |
| 461 assertEquals(0, m.size); | |
| 462 })(); | |
| 463 | |
| 464 | |
| 465 // Test WeakMap clear | 312 // Test WeakMap clear |
| 466 (function() { | 313 (function() { |
| 467 var k = new Object(); | 314 var k = new Object(); |
| 468 var w = new WeakMap(); | 315 var w = new WeakMap(); |
| 469 w.set(k, 23); | 316 w.set(k, 23); |
| 470 assertTrue(w.has(k)); | 317 assertTrue(w.has(k)); |
| 471 assertEquals(23, w.get(k)); | 318 assertEquals(23, w.get(k)); |
| 472 w.clear(); | 319 w.clear(); |
| 473 assertFalse(w.has(k)); | 320 assertFalse(w.has(k)); |
| 474 assertEquals(undefined, w.get(k)); | 321 assertEquals(undefined, w.get(k)); |
| 475 })(); | 322 })(); |
| 476 | 323 |
| 477 | 324 |
| 478 // Test WeakSet clear | 325 // Test WeakSet clear |
| 479 (function() { | 326 (function() { |
| 480 var k = new Object(); | 327 var k = new Object(); |
| 481 var w = new WeakSet(); | 328 var w = new WeakSet(); |
| 482 w.add(k); | 329 w.add(k); |
| 483 assertTrue(w.has(k)); | 330 assertTrue(w.has(k)); |
| 484 w.clear(); | 331 w.clear(); |
| 485 assertFalse(w.has(k)); | 332 assertFalse(w.has(k)); |
| 486 })(); | 333 })(); |
| 487 | |
| 488 | |
| 489 (function TestMinusZeroSet() { | |
| 490 var m = new Set(); | |
| 491 m.add(0); | |
| 492 m.add(-0); | |
| 493 assertEquals(1, m.size); | |
| 494 assertTrue(m.has(0)); | |
| 495 assertTrue(m.has(-0)); | |
| 496 })(); | |
| 497 | |
| 498 | |
| 499 (function TestMinusZeroMap() { | |
| 500 var m = new Map(); | |
| 501 m.set(0, 'plus'); | |
| 502 m.set(-0, 'minus'); | |
| 503 assertEquals(1, m.size); | |
| 504 assertTrue(m.has(0)); | |
| 505 assertTrue(m.has(-0)); | |
| 506 assertEquals('minus', m.get(0)); | |
| 507 assertEquals('minus', m.get(-0)); | |
| 508 })(); | |
| OLD | NEW |