Chromium Code Reviews| 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); | 41 print(WeakSet); |
|
Michael Starzinger
2014/03/18 09:54:26
nit: Let's drop this print statement.
rossberg
2014/03/18 09:57:24
Done.
| |
| 38 TestValidSetCalls(new WeakSet); | 42 TestValidSetCalls(new WeakSet); |
| 39 | 43 |
| 40 | 44 |
| 41 // Test valid getter and setter calls on Maps and WeakMaps | 45 // Test valid getter and setter calls on WeakMaps |
| 42 function TestValidMapCalls(m) { | 46 function TestValidMapCalls(m) { |
| 43 assertDoesNotThrow(function () { m.get(new Object) }); | 47 assertDoesNotThrow(function () { m.get(new Object) }); |
| 44 assertDoesNotThrow(function () { m.set(new Object) }); | 48 assertDoesNotThrow(function () { m.set(new Object) }); |
| 45 assertDoesNotThrow(function () { m.has(new Object) }); | 49 assertDoesNotThrow(function () { m.has(new Object) }); |
| 46 assertDoesNotThrow(function () { m.delete(new Object) }); | 50 assertDoesNotThrow(function () { m.delete(new Object) }); |
| 47 } | 51 } |
| 48 TestValidMapCalls(new Map); | |
| 49 TestValidMapCalls(new WeakMap); | 52 TestValidMapCalls(new WeakMap); |
| 50 | 53 |
| 51 | 54 |
| 52 // Test invalid getter and setter calls for WeakMap only | 55 // Test invalid getter and setter calls for WeakMap |
| 53 function TestInvalidCalls(m) { | 56 function TestInvalidCalls(m) { |
| 54 assertThrows(function () { m.get(undefined) }, TypeError); | 57 assertThrows(function () { m.get(undefined) }, TypeError); |
| 55 assertThrows(function () { m.set(undefined, 0) }, TypeError); | 58 assertThrows(function () { m.set(undefined, 0) }, TypeError); |
| 56 assertThrows(function () { m.get(null) }, TypeError); | 59 assertThrows(function () { m.get(null) }, TypeError); |
| 57 assertThrows(function () { m.set(null, 0) }, TypeError); | 60 assertThrows(function () { m.set(null, 0) }, TypeError); |
| 58 assertThrows(function () { m.get(0) }, TypeError); | 61 assertThrows(function () { m.get(0) }, TypeError); |
| 59 assertThrows(function () { m.set(0, 0) }, TypeError); | 62 assertThrows(function () { m.set(0, 0) }, TypeError); |
| 60 assertThrows(function () { m.get('a-key') }, TypeError); | 63 assertThrows(function () { m.get('a-key') }, TypeError); |
| 61 assertThrows(function () { m.set('a-key', 0) }, TypeError); | 64 assertThrows(function () { m.set('a-key', 0) }, TypeError); |
| 62 } | 65 } |
| 63 TestInvalidCalls(new WeakMap); | 66 TestInvalidCalls(new WeakMap); |
| 64 | 67 |
| 65 | 68 |
| 66 // Test expected behavior for Sets | 69 // Test expected behavior for WeakSets |
| 67 function TestSet(set, key) { | 70 function TestSet(set, key) { |
| 68 assertFalse(set.has(key)); | 71 assertFalse(set.has(key)); |
| 69 assertSame(undefined, set.add(key)); | 72 assertSame(undefined, set.add(key)); |
| 70 assertTrue(set.has(key)); | 73 assertTrue(set.has(key)); |
| 71 assertTrue(set.delete(key)); | 74 assertTrue(set.delete(key)); |
| 72 assertFalse(set.has(key)); | 75 assertFalse(set.has(key)); |
| 73 assertFalse(set.delete(key)); | 76 assertFalse(set.delete(key)); |
| 74 assertFalse(set.has(key)); | 77 assertFalse(set.has(key)); |
| 75 } | 78 } |
| 76 function TestSetBehavior(set) { | 79 function TestSetBehavior(set) { |
| 77 for (var i = 0; i < 20; i++) { | 80 for (var i = 0; i < 20; i++) { |
| 78 TestSet(set, new Object); | 81 TestSet(set, new Object); |
| 79 TestSet(set, i); | 82 TestSet(set, i); |
| 80 TestSet(set, i / 100); | 83 TestSet(set, i / 100); |
| 81 TestSet(set, 'key-' + i); | 84 TestSet(set, 'key-' + i); |
| 82 } | 85 } |
| 83 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; | 86 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; |
| 84 for (var i = 0; i < keys.length; i++) { | 87 for (var i = 0; i < keys.length; i++) { |
| 85 TestSet(set, keys[i]); | 88 TestSet(set, keys[i]); |
| 86 } | 89 } |
| 87 } | 90 } |
| 88 TestSetBehavior(new Set); | |
| 89 TestSet(new WeakSet, new Object); | 91 TestSet(new WeakSet, new Object); |
| 90 | 92 |
| 91 | 93 |
| 92 // Test expected mapping behavior for Maps and WeakMaps | 94 // Test expected mapping behavior for WeakMaps |
| 93 function TestMapping(map, key, value) { | 95 function TestMapping(map, key, value) { |
| 94 assertSame(undefined, map.set(key, value)); | 96 assertSame(undefined, map.set(key, value)); |
| 95 assertSame(value, map.get(key)); | 97 assertSame(value, map.get(key)); |
| 96 } | 98 } |
| 97 function TestMapBehavior1(m) { | 99 function TestMapBehavior1(m) { |
| 98 TestMapping(m, new Object, 23); | 100 TestMapping(m, new Object, 23); |
| 99 TestMapping(m, new Object, 'the-value'); | 101 TestMapping(m, new Object, 'the-value'); |
| 100 TestMapping(m, new Object, new Object); | 102 TestMapping(m, new Object, new Object); |
| 101 } | 103 } |
| 102 TestMapBehavior1(new Map); | |
| 103 TestMapBehavior1(new WeakMap); | 104 TestMapBehavior1(new WeakMap); |
| 104 | 105 |
| 105 | 106 |
| 106 // Test expected mapping behavior for Maps only | 107 // 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) { | 108 function TestQuery(m) { |
| 123 var key = new Object; | 109 var key = new Object; |
| 124 var values = [ 'x', 0, +Infinity, -Infinity, true, false, null, undefined ]; | 110 var values = [ 'x', 0, +Infinity, -Infinity, true, false, null, undefined ]; |
| 125 for (var i = 0; i < values.length; i++) { | 111 for (var i = 0; i < values.length; i++) { |
| 126 TestMapping(m, key, values[i]); | 112 TestMapping(m, key, values[i]); |
| 127 assertTrue(m.has(key)); | 113 assertTrue(m.has(key)); |
| 128 assertFalse(m.has(new Object)); | 114 assertFalse(m.has(new Object)); |
| 129 } | 115 } |
| 130 } | 116 } |
| 131 TestQuery(new Map); | |
| 132 TestQuery(new WeakMap); | 117 TestQuery(new WeakMap); |
| 133 | 118 |
| 134 | 119 |
| 135 // Test expected deletion behavior of Maps and WeakMaps | 120 // Test expected deletion behavior of WeakMaps |
| 136 function TestDelete(m) { | 121 function TestDelete(m) { |
| 137 var key = new Object; | 122 var key = new Object; |
| 138 TestMapping(m, key, 'to-be-deleted'); | 123 TestMapping(m, key, 'to-be-deleted'); |
| 139 assertTrue(m.delete(key)); | 124 assertTrue(m.delete(key)); |
| 140 assertFalse(m.delete(key)); | 125 assertFalse(m.delete(key)); |
| 141 assertFalse(m.delete(new Object)); | 126 assertFalse(m.delete(new Object)); |
| 142 assertSame(m.get(key), undefined); | 127 assertSame(m.get(key), undefined); |
| 143 } | 128 } |
| 144 TestDelete(new Map); | |
| 145 TestDelete(new WeakMap); | 129 TestDelete(new WeakMap); |
| 146 | 130 |
| 147 | 131 |
| 148 // Test GC of Maps and WeakMaps with entry | 132 // Test GC of WeakMaps with entry |
| 149 function TestGC1(m) { | 133 function TestGC1(m) { |
| 150 var key = new Object; | 134 var key = new Object; |
| 151 m.set(key, 'not-collected'); | 135 m.set(key, 'not-collected'); |
| 152 gc(); | 136 gc(); |
| 153 assertSame('not-collected', m.get(key)); | 137 assertSame('not-collected', m.get(key)); |
| 154 } | 138 } |
| 155 TestGC1(new Map); | |
| 156 TestGC1(new WeakMap); | 139 TestGC1(new WeakMap); |
| 157 | 140 |
| 158 | 141 |
| 159 // Test GC of Maps and WeakMaps with chained entries | 142 // Test GC of WeakMaps with chained entries |
| 160 function TestGC2(m) { | 143 function TestGC2(m) { |
| 161 var head = new Object; | 144 var head = new Object; |
| 162 for (key = head, i = 0; i < 10; i++, key = m.get(key)) { | 145 for (key = head, i = 0; i < 10; i++, key = m.get(key)) { |
| 163 m.set(key, new Object); | 146 m.set(key, new Object); |
| 164 } | 147 } |
| 165 gc(); | 148 gc(); |
| 166 var count = 0; | 149 var count = 0; |
| 167 for (key = head; key != undefined; key = m.get(key)) { | 150 for (key = head; key != undefined; key = m.get(key)) { |
| 168 count++; | 151 count++; |
| 169 } | 152 } |
| 170 assertEquals(11, count); | 153 assertEquals(11, count); |
| 171 } | 154 } |
| 172 TestGC2(new Map); | |
| 173 TestGC2(new WeakMap); | 155 TestGC2(new WeakMap); |
| 174 | 156 |
| 175 | 157 |
| 176 // Test property attribute [[Enumerable]] | 158 // Test property attribute [[Enumerable]] |
| 177 function TestEnumerable(func) { | 159 function TestEnumerable(func) { |
| 178 function props(x) { | 160 function props(x) { |
| 179 var array = []; | 161 var array = []; |
| 180 for (var p in x) array.push(p); | 162 for (var p in x) array.push(p); |
| 181 return array.sort(); | 163 return array.sort(); |
| 182 } | 164 } |
| 183 assertArrayEquals([], props(func)); | 165 assertArrayEquals([], props(func)); |
| 184 assertArrayEquals([], props(func.prototype)); | 166 assertArrayEquals([], props(func.prototype)); |
| 185 assertArrayEquals([], props(new func())); | 167 assertArrayEquals([], props(new func())); |
| 186 } | 168 } |
| 187 TestEnumerable(Set); | |
| 188 TestEnumerable(Map); | |
| 189 TestEnumerable(WeakMap); | 169 TestEnumerable(WeakMap); |
| 190 TestEnumerable(WeakSet); | 170 TestEnumerable(WeakSet); |
| 191 | 171 |
| 192 | 172 |
| 193 // Test arbitrary properties on Maps and WeakMaps | 173 // Test arbitrary properties on WeakMaps |
| 194 function TestArbitrary(m) { | 174 function TestArbitrary(m) { |
| 195 function TestProperty(map, property, value) { | 175 function TestProperty(map, property, value) { |
| 196 map[property] = value; | 176 map[property] = value; |
| 197 assertEquals(value, map[property]); | 177 assertEquals(value, map[property]); |
| 198 } | 178 } |
| 199 for (var i = 0; i < 20; i++) { | 179 for (var i = 0; i < 20; i++) { |
| 200 TestProperty(m, i, 'val' + i); | 180 TestProperty(m, i, 'val' + i); |
| 201 TestProperty(m, 'foo' + i, 'bar' + i); | 181 TestProperty(m, 'foo' + i, 'bar' + i); |
| 202 } | 182 } |
| 203 TestMapping(m, new Object, 'foobar'); | 183 TestMapping(m, new Object, 'foobar'); |
| 204 } | 184 } |
| 205 TestArbitrary(new Map); | |
| 206 TestArbitrary(new WeakMap); | 185 TestArbitrary(new WeakMap); |
| 207 | 186 |
| 208 | 187 |
| 209 // Test direct constructor call | 188 // Test direct constructor call |
| 210 assertThrows(function() { Set(); }, TypeError); | |
| 211 assertThrows(function() { Map(); }, TypeError); | |
| 212 assertThrows(function() { WeakMap(); }, TypeError); | 189 assertThrows(function() { WeakMap(); }, TypeError); |
| 213 assertThrows(function() { WeakSet(); }, TypeError); | 190 assertThrows(function() { WeakSet(); }, TypeError); |
| 214 | 191 |
| 215 | 192 |
| 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 | 193 // Test some common JavaScript idioms for WeakMaps |
| 255 var m = new WeakMap; | 194 var m = new WeakMap; |
| 256 assertTrue(m instanceof WeakMap); | 195 assertTrue(m instanceof WeakMap); |
| 257 assertTrue(WeakMap.prototype.set instanceof Function) | 196 assertTrue(WeakMap.prototype.set instanceof Function) |
| 258 assertTrue(WeakMap.prototype.get instanceof Function) | 197 assertTrue(WeakMap.prototype.get instanceof Function) |
| 259 assertTrue(WeakMap.prototype.has instanceof Function) | 198 assertTrue(WeakMap.prototype.has instanceof Function) |
| 260 assertTrue(WeakMap.prototype.delete instanceof Function) | 199 assertTrue(WeakMap.prototype.delete instanceof Function) |
| 261 assertTrue(WeakMap.prototype.clear instanceof Function) | 200 assertTrue(WeakMap.prototype.clear instanceof Function) |
| 262 | 201 |
| 263 | 202 |
| 264 // Test some common JavaScript idioms for WeakSets | 203 // Test some common JavaScript idioms for WeakSets |
| 265 var s = new WeakSet; | 204 var s = new WeakSet; |
| 266 assertTrue(s instanceof WeakSet); | 205 assertTrue(s instanceof WeakSet); |
| 267 assertTrue(WeakSet.prototype.add instanceof Function) | 206 assertTrue(WeakSet.prototype.add instanceof Function) |
| 268 assertTrue(WeakSet.prototype.has instanceof Function) | 207 assertTrue(WeakSet.prototype.has instanceof Function) |
| 269 assertTrue(WeakSet.prototype.delete instanceof Function) | 208 assertTrue(WeakSet.prototype.delete instanceof Function) |
| 270 assertTrue(WeakSet.prototype.clear instanceof Function) | 209 assertTrue(WeakSet.prototype.clear instanceof Function) |
| 271 | 210 |
| 272 | 211 |
| 273 // Test class of instance and prototype. | 212 // 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)) | 213 assertEquals("WeakMap", %_ClassOf(new WeakMap)) |
| 279 assertEquals("Object", %_ClassOf(WeakMap.prototype)) | 214 assertEquals("Object", %_ClassOf(WeakMap.prototype)) |
| 280 assertEquals("WeakSet", %_ClassOf(new WeakSet)) | 215 assertEquals("WeakSet", %_ClassOf(new WeakSet)) |
| 281 assertEquals("Object", %_ClassOf(WeakMap.prototype)) | 216 assertEquals("Object", %_ClassOf(WeakMap.prototype)) |
| 282 | 217 |
| 283 | 218 |
| 284 // Test name of constructor. | 219 // Test name of constructor. |
| 285 assertEquals("Set", Set.name); | |
| 286 assertEquals("Map", Map.name); | |
| 287 assertEquals("WeakMap", WeakMap.name); | 220 assertEquals("WeakMap", WeakMap.name); |
| 288 assertEquals("WeakSet", WeakSet.name); | 221 assertEquals("WeakSet", WeakSet.name); |
| 289 | 222 |
| 290 | 223 |
| 291 // Test prototype property of Set, Map, WeakMap and WeakSet. | 224 // Test prototype property of WeakMap and WeakSet. |
| 292 function TestPrototype(C) { | 225 function TestPrototype(C) { |
| 293 assertTrue(C.prototype instanceof Object); | 226 assertTrue(C.prototype instanceof Object); |
| 294 assertEquals({ | 227 assertEquals({ |
| 295 value: {}, | 228 value: {}, |
| 296 writable: true, // TODO(2793): This should be non-writable. | 229 writable: true, // TODO(2793): This should be non-writable. |
| 297 enumerable: false, | 230 enumerable: false, |
| 298 configurable: false | 231 configurable: false |
| 299 }, Object.getOwnPropertyDescriptor(C, "prototype")); | 232 }, Object.getOwnPropertyDescriptor(C, "prototype")); |
| 300 } | 233 } |
| 301 TestPrototype(Set); | |
| 302 TestPrototype(Map); | |
| 303 TestPrototype(WeakMap); | 234 TestPrototype(WeakMap); |
| 304 TestPrototype(WeakSet); | 235 TestPrototype(WeakSet); |
| 305 | 236 |
| 306 | 237 |
| 307 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype. | 238 // Test constructor property of the WeakMap and WeakSet prototype. |
| 308 function TestConstructor(C) { | 239 function TestConstructor(C) { |
| 309 assertFalse(C === Object.prototype.constructor); | 240 assertFalse(C === Object.prototype.constructor); |
| 310 assertSame(C, C.prototype.constructor); | 241 assertSame(C, C.prototype.constructor); |
| 311 assertSame(C, (new C).__proto__.constructor); | 242 assertSame(C, (new C).__proto__.constructor); |
| 312 } | 243 } |
| 313 TestConstructor(Set); | |
| 314 TestConstructor(Map); | |
| 315 TestConstructor(WeakMap); | 244 TestConstructor(WeakMap); |
| 316 TestConstructor(WeakSet); | 245 TestConstructor(WeakSet); |
| 317 | 246 |
| 318 | 247 |
| 319 // Test the Set, Map, WeakMap and WeakSet global properties themselves. | 248 // Test the WeakMap and WeakSet global properties themselves. |
| 320 function TestDescriptor(global, C) { | 249 function TestDescriptor(global, C) { |
| 321 assertEquals({ | 250 assertEquals({ |
| 322 value: C, | 251 value: C, |
| 323 writable: true, | 252 writable: true, |
| 324 enumerable: false, | 253 enumerable: false, |
| 325 configurable: true | 254 configurable: true |
| 326 }, Object.getOwnPropertyDescriptor(global, C.name)); | 255 }, Object.getOwnPropertyDescriptor(global, C.name)); |
| 327 } | 256 } |
| 328 TestDescriptor(this, Set); | |
| 329 TestDescriptor(this, Map); | |
| 330 TestDescriptor(this, WeakMap); | 257 TestDescriptor(this, WeakMap); |
| 331 TestDescriptor(this, WeakSet); | 258 TestDescriptor(this, WeakSet); |
| 332 | 259 |
| 333 | 260 |
| 334 // Regression test for WeakMap prototype. | 261 // Regression test for WeakMap prototype. |
| 335 assertTrue(WeakMap.prototype.constructor === WeakMap) | 262 assertTrue(WeakMap.prototype.constructor === WeakMap) |
| 336 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) | 263 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) |
| 337 | 264 |
| 338 | 265 |
| 339 // Regression test for issue 1617: The prototype of the WeakMap constructor | 266 // Regression test for issue 1617: The prototype of the WeakMap constructor |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 351 writable: true | 278 writable: true |
| 352 }}); | 279 }}); |
| 353 assertEquals(10, o.myValue); | 280 assertEquals(10, o.myValue); |
| 354 | 281 |
| 355 | 282 |
| 356 // Regression test for issue 1884: Invoking any of the methods for Harmony | 283 // 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 | 284 // maps, sets, or weak maps, with a wrong type of receiver should be throwing |
| 358 // a proper TypeError. | 285 // a proper TypeError. |
| 359 var alwaysBogus = [ undefined, null, true, "x", 23, {} ]; | 286 var alwaysBogus = [ undefined, null, true, "x", 23, {} ]; |
| 360 var bogusReceiversTestSet = [ | 287 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, | 288 { proto: WeakMap.prototype, |
| 370 funcs: [ 'get', 'set', 'has', 'delete' ], | 289 funcs: [ 'get', 'set', 'has', 'delete' ], |
| 371 receivers: alwaysBogus.concat([ new Set, new Map, new WeakSet ]), | 290 receivers: alwaysBogus.concat([ new WeakSet ]), |
| 372 }, | 291 }, |
| 373 { proto: WeakSet.prototype, | 292 { proto: WeakSet.prototype, |
| 374 funcs: [ 'add', 'has', 'delete' ], | 293 funcs: [ 'add', 'has', 'delete' ], |
| 375 receivers: alwaysBogus.concat([ new Set, new Map, new WeakMap ]), | 294 receivers: alwaysBogus.concat([ new WeakMap ]), |
| 376 }, | 295 }, |
| 377 ]; | 296 ]; |
| 378 function TestBogusReceivers(testSet) { | 297 function TestBogusReceivers(testSet) { |
| 379 for (var i = 0; i < testSet.length; i++) { | 298 for (var i = 0; i < testSet.length; i++) { |
| 380 var proto = testSet[i].proto; | 299 var proto = testSet[i].proto; |
| 381 var funcs = testSet[i].funcs; | 300 var funcs = testSet[i].funcs; |
| 382 var receivers = testSet[i].receivers; | 301 var receivers = testSet[i].receivers; |
| 383 for (var j = 0; j < funcs.length; j++) { | 302 for (var j = 0; j < funcs.length; j++) { |
| 384 var func = proto[funcs[j]]; | 303 var func = proto[funcs[j]]; |
| 385 for (var k = 0; k < receivers.length; k++) { | 304 for (var k = 0; k < receivers.length; k++) { |
| 386 assertThrows(function () { func.call(receivers[k], {}) }, TypeError); | 305 assertThrows(function () { func.call(receivers[k], {}) }, TypeError); |
| 387 } | 306 } |
| 388 } | 307 } |
| 389 } | 308 } |
| 390 } | 309 } |
| 391 TestBogusReceivers(bogusReceiversTestSet); | 310 TestBogusReceivers(bogusReceiversTestSet); |
| 392 | 311 |
| 393 | 312 |
| 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 | 313 // Test WeakMap clear |
| 466 (function() { | 314 (function() { |
| 467 var k = new Object(); | 315 var k = new Object(); |
| 468 var w = new WeakMap(); | 316 var w = new WeakMap(); |
| 469 w.set(k, 23); | 317 w.set(k, 23); |
| 470 assertTrue(w.has(k)); | 318 assertTrue(w.has(k)); |
| 471 assertEquals(23, w.get(k)); | 319 assertEquals(23, w.get(k)); |
| 472 w.clear(); | 320 w.clear(); |
| 473 assertFalse(w.has(k)); | 321 assertFalse(w.has(k)); |
| 474 assertEquals(undefined, w.get(k)); | 322 assertEquals(undefined, w.get(k)); |
| 475 })(); | 323 })(); |
| 476 | 324 |
| 477 | 325 |
| 478 // Test WeakSet clear | 326 // Test WeakSet clear |
| 479 (function() { | 327 (function() { |
| 480 var k = new Object(); | 328 var k = new Object(); |
| 481 var w = new WeakSet(); | 329 var w = new WeakSet(); |
| 482 w.add(k); | 330 w.add(k); |
| 483 assertTrue(w.has(k)); | 331 assertTrue(w.has(k)); |
| 484 w.clear(); | 332 w.clear(); |
| 485 assertFalse(w.has(k)); | 333 assertFalse(w.has(k)); |
| 486 })(); | 334 })(); |
| 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 |