1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
28 // Flags: --expose-gc --allow-natives-syntax --harmony-tostring
31 function assertSize(expected, collection) {
32 if (collection instanceof Map || collection instanceof Set) {
33 assertEquals(expected, collection.size);
38 // Test valid getter and setter calls on Sets and WeakSets
39 function TestValidSetCalls(m) {
40 assertDoesNotThrow(function () { m.add(new Object) });
41 assertDoesNotThrow(function () { m.has(new Object) });
42 assertDoesNotThrow(function () { m.delete(new Object) });
44 TestValidSetCalls(new Set);
45 TestValidSetCalls(new WeakSet);
48 // Test valid getter and setter calls on Maps and WeakMaps
49 function TestValidMapCalls(m) {
50 assertDoesNotThrow(function () { m.get(new Object) });
51 assertDoesNotThrow(function () { m.set(new Object) });
52 assertDoesNotThrow(function () { m.has(new Object) });
53 assertDoesNotThrow(function () { m.delete(new Object) });
54 assertDoesNotThrow(function () { m.get(undefined) });
55 assertDoesNotThrow(function () { m.get(null) });
56 assertDoesNotThrow(function () { m.get(0) });
57 assertDoesNotThrow(function () { m.get('a-key') });
58 assertDoesNotThrow(function () { m.get(Symbol()) });
59 assertDoesNotThrow(function () { m.has(undefined) });
60 assertDoesNotThrow(function () { m.has(null) });
61 assertDoesNotThrow(function () { m.has(0) });
62 assertDoesNotThrow(function () { m.has('a-key') });
63 assertDoesNotThrow(function () { m.has(Symbol()) });
64 assertDoesNotThrow(function () { m.delete(undefined) });
65 assertDoesNotThrow(function () { m.delete(null) });
66 assertDoesNotThrow(function () { m.delete(0) });
67 assertDoesNotThrow(function () { m.delete('a-key') });
68 assertDoesNotThrow(function () { m.delete(Symbol()) });
70 TestValidMapCalls(new Map);
71 TestValidMapCalls(new WeakMap);
74 // Test invalid getter and setter calls for WeakMap only
75 function TestInvalidCalls(m) {
76 assertThrows(function () { m.set(undefined, 0) }, TypeError);
77 assertThrows(function () { m.set(null, 0) }, TypeError);
78 assertThrows(function () { m.set(0, 0) }, TypeError);
79 assertThrows(function () { m.set('a-key', 0) }, TypeError);
80 assertThrows(function () { m.set(Symbol(), 0) }, TypeError);
82 TestInvalidCalls(new WeakMap);
85 // Test expected behavior for Sets and WeakSets
86 function TestSet(set, key) {
87 assertFalse(set.has(key));
88 assertFalse(set.delete(key));
89 if (typeof key === 'object' && !(set instanceof WeakSet)) {
90 assertSame(set, set.add(key));
91 assertTrue(set.has(key));
92 assertTrue(set.delete(key));
94 assertFalse(set.has(key));
95 assertFalse(set.delete(key));
96 assertFalse(set.has(key));
98 function TestSetBehavior(set) {
100 for (var i = 0; i < 20; i++) {
101 TestSet(set, new Object);
103 TestSet(set, i / 100);
104 TestSet(set, 'key-' + i);
105 TestSet(set, Symbol(i));
109 -0, +0, 1, 1/3, 10, +Infinity, -Infinity, NaN, true, false, null, undefined,
110 'x', Symbol(), {}, function(){}
112 for (var i = 0; i < keys.length; i++) {
113 TestSet(set, keys[i]);
116 TestSetBehavior(new Set);
117 TestSetBehavior(new WeakSet);
120 // Test expected mapping behavior for Maps and WeakMaps
121 function TestMapping(map, key, value) {
122 assertFalse(map.has(key));
123 assertSame(undefined, map.get(key));
124 assertFalse(map.delete(key));
125 if (typeof key === 'object' && !(map instanceof WeakMap)) {
126 assertSame(map, map.set(key, value));
127 assertSame(value, map.get(key));
128 assertTrue(map.has(key));
129 assertTrue(map.delete(key));
131 assertFalse(map.has(key));
132 assertSame(undefined, map.get(key));
133 assertFalse(map.delete(key));
134 assertFalse(map.has(key));
135 assertSame(undefined, map.get(key));
137 function TestMapBehavior(m) {
139 TestMapping(m, new Object, 23);
140 TestMapping(m, new Object, 'the-value');
141 TestMapping(m, new Object, new Object);
142 for (var i = 0; i < 20; i++) {
143 TestMapping(m, i, new Object);
144 TestMapping(m, i / 10, new Object);
145 TestMapping(m, 'key-' + i, new Object);
146 TestMapping(m, Symbol(i), new Object);
150 -0, +0, 1, 1/3, 10, +Infinity, -Infinity, NaN, true, false, null, undefined,
151 'x', Symbol(), {}, function(){}
153 for (var i = 0; i < keys.length; i++) {
154 TestMapping(m, keys[i], 23);
155 TestMapping(m, keys[i], 'the-value');
156 TestMapping(m, keys[i], new Object);
159 TestMapBehavior(new Map);
160 TestMapBehavior(new WeakMap);
163 // Test expected querying behavior of Maps and WeakMaps
164 function TestQuery(m) {
165 var key = new Object;
166 var values = [ 'x', 0, +Infinity, -Infinity, true, false, null, undefined ];
167 for (var i = 0; i < values.length; i++) {
168 TestMapping(m, key, values[i]);
172 TestQuery(new WeakMap);
175 // Test expected deletion behavior of Maps and WeakMaps
176 function TestDelete(m) {
177 var key = new Object;
178 TestMapping(m, key, 'to-be-deleted');
179 assertFalse(m.delete(key));
180 assertFalse(m.delete(new Object));
181 assertSame(m.get(key), undefined);
184 TestDelete(new WeakMap);
187 // Test GC of Maps and WeakMaps with entry
188 function TestGC1(m) {
189 var key = new Object;
190 m.set(key, 'not-collected');
192 assertSame('not-collected', m.get(key));
195 TestGC1(new WeakMap);
198 // Test GC of Maps and WeakMaps with chained entries
199 function TestGC2(m) {
200 var head = new Object;
201 for (key = head, i = 0; i < 10; i++, key = m.get(key)) {
202 m.set(key, new Object);
206 for (key = head; key != undefined; key = m.get(key)) {
209 assertEquals(11, count);
212 TestGC2(new WeakMap);
215 // Test property attribute [[Enumerable]]
216 function TestEnumerable(func) {
219 for (var p in x) array.push(p);
222 assertArrayEquals([], props(func));
223 assertArrayEquals([], props(func.prototype));
224 assertArrayEquals([], props(new func()));
228 TestEnumerable(WeakMap);
229 TestEnumerable(WeakSet);
232 // Test arbitrary properties on Maps and WeakMaps
233 function TestArbitrary(m) {
234 function TestProperty(map, property, value) {
235 map[property] = value;
236 assertEquals(value, map[property]);
238 for (var i = 0; i < 20; i++) {
239 TestProperty(m, i, 'val' + i);
240 TestProperty(m, 'foo' + i, 'bar' + i);
242 TestMapping(m, new Object, 'foobar');
244 TestArbitrary(new Map);
245 TestArbitrary(new WeakMap);
248 // Test direct constructor call
249 assertThrows(function() { Set(); }, TypeError);
250 assertThrows(function() { Map(); }, TypeError);
251 assertThrows(function() { WeakMap(); }, TypeError);
252 assertThrows(function() { WeakSet(); }, TypeError);
255 // Test whether NaN values as keys are treated correctly.
257 assertFalse(s.has(NaN));
258 assertFalse(s.has(NaN + 1));
259 assertFalse(s.has(23));
261 assertTrue(s.has(NaN));
262 assertTrue(s.has(NaN + 1));
263 assertFalse(s.has(23));
265 assertFalse(m.has(NaN));
266 assertFalse(m.has(NaN + 1));
267 assertFalse(m.has(23));
268 m.set(NaN, 'a-value');
269 assertTrue(m.has(NaN));
270 assertTrue(m.has(NaN + 1));
271 assertFalse(m.has(23));
274 // Test some common JavaScript idioms for Sets
276 assertTrue(s instanceof Set);
277 assertTrue(Set.prototype.add instanceof Function)
278 assertTrue(Set.prototype.has instanceof Function)
279 assertTrue(Set.prototype.delete instanceof Function)
280 assertTrue(Set.prototype.clear instanceof Function)
283 // Test some common JavaScript idioms for Maps
285 assertTrue(m instanceof Map);
286 assertTrue(Map.prototype.set instanceof Function)
287 assertTrue(Map.prototype.get instanceof Function)
288 assertTrue(Map.prototype.has instanceof Function)
289 assertTrue(Map.prototype.delete instanceof Function)
290 assertTrue(Map.prototype.clear instanceof Function)
293 // Test some common JavaScript idioms for WeakMaps
295 assertTrue(m instanceof WeakMap);
296 assertTrue(WeakMap.prototype.set instanceof Function)
297 assertTrue(WeakMap.prototype.get instanceof Function)
298 assertTrue(WeakMap.prototype.has instanceof Function)
299 assertTrue(WeakMap.prototype.delete instanceof Function)
302 // Test some common JavaScript idioms for WeakSets
304 assertTrue(s instanceof WeakSet);
305 assertTrue(WeakSet.prototype.add instanceof Function)
306 assertTrue(WeakSet.prototype.has instanceof Function)
307 assertTrue(WeakSet.prototype.delete instanceof Function)
310 // Test class of instance and prototype.
311 assertEquals("Set", %_ClassOf(new Set))
312 assertEquals("Object", %_ClassOf(Set.prototype))
313 assertEquals("Map", %_ClassOf(new Map))
314 assertEquals("Object", %_ClassOf(Map.prototype))
315 assertEquals("WeakMap", %_ClassOf(new WeakMap))
316 assertEquals("Object", %_ClassOf(WeakMap.prototype))
317 assertEquals("WeakSet", %_ClassOf(new WeakSet))
318 assertEquals("Object", %_ClassOf(WeakMap.prototype))
321 // Test name of constructor.
322 assertEquals("Set", Set.name);
323 assertEquals("Map", Map.name);
324 assertEquals("WeakMap", WeakMap.name);
325 assertEquals("WeakSet", WeakSet.name);
328 // Test prototype property of Set, Map, WeakMap and WeakSet.
329 function TestPrototype(C) {
330 assertTrue(C.prototype instanceof Object);
336 }, Object.getOwnPropertyDescriptor(C, "prototype"));
340 TestPrototype(WeakMap);
341 TestPrototype(WeakSet);
344 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype.
345 function TestConstructor(C) {
346 assertFalse(C === Object.prototype.constructor);
347 assertSame(C, C.prototype.constructor);
348 assertSame(C, (new C).__proto__.constructor);
349 assertEquals(1, C.length);
351 TestConstructor(Set);
352 TestConstructor(Map);
353 TestConstructor(WeakMap);
354 TestConstructor(WeakSet);
357 // Test the Set, Map, WeakMap and WeakSet global properties themselves.
358 function TestDescriptor(global, C) {
364 }, Object.getOwnPropertyDescriptor(global, C.name));
366 TestDescriptor(this, Set);
367 TestDescriptor(this, Map);
368 TestDescriptor(this, WeakMap);
369 TestDescriptor(this, WeakSet);
372 // Regression test for WeakMap prototype.
373 assertTrue(WeakMap.prototype.constructor === WeakMap)
374 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype)
377 // Regression test for issue 1617: The prototype of the WeakMap constructor
378 // needs to be unique (i.e. different from the one of the Object constructor).
379 assertFalse(WeakMap.prototype === Object.prototype);
380 var o = Object.create({});
381 assertFalse("get" in o);
382 assertFalse("set" in o);
383 assertEquals(undefined, o.get);
384 assertEquals(undefined, o.set);
385 var o = Object.create({}, { myValue: {
391 assertEquals(10, o.myValue);
394 // Regression test for issue 1884: Invoking any of the methods for Harmony
395 // maps, sets, or weak maps, with a wrong type of receiver should be throwing
396 // a proper TypeError.
397 var alwaysBogus = [ undefined, null, true, "x", 23, {} ];
398 var bogusReceiversTestSet = [
399 { proto: Set.prototype,
400 funcs: [ 'add', 'has', 'delete' ],
401 receivers: alwaysBogus.concat([ new Map, new WeakMap, new WeakSet ]),
403 { proto: Map.prototype,
404 funcs: [ 'get', 'set', 'has', 'delete' ],
405 receivers: alwaysBogus.concat([ new Set, new WeakMap, new WeakSet ]),
407 { proto: WeakMap.prototype,
408 funcs: [ 'get', 'set', 'has', 'delete' ],
409 receivers: alwaysBogus.concat([ new Set, new Map, new WeakSet ]),
411 { proto: WeakSet.prototype,
412 funcs: [ 'add', 'has', 'delete' ],
413 receivers: alwaysBogus.concat([ new Set, new Map, new WeakMap ]),
416 function TestBogusReceivers(testSet) {
417 for (var i = 0; i < testSet.length; i++) {
418 var proto = testSet[i].proto;
419 var funcs = testSet[i].funcs;
420 var receivers = testSet[i].receivers;
421 for (var j = 0; j < funcs.length; j++) {
422 var func = proto[funcs[j]];
423 for (var k = 0; k < receivers.length; k++) {
424 assertThrows(function () { func.call(receivers[k], {}) }, TypeError);
429 TestBogusReceivers(bogusReceiversTestSet);
433 // There is a proposed stress-test available at the es-discuss mailing list
434 // which cannot be reasonably automated. Check it out by hand if you like:
435 // https://mail.mozilla.org/pipermail/es-discuss/2011-May/014096.html
438 // Set and Map size getters
439 var setSizeDescriptor = Object.getOwnPropertyDescriptor(Set.prototype, 'size');
440 assertEquals(undefined, setSizeDescriptor.value);
441 assertEquals(undefined, setSizeDescriptor.set);
442 assertTrue(setSizeDescriptor.get instanceof Function);
443 assertEquals(undefined, setSizeDescriptor.get.prototype);
444 assertFalse(setSizeDescriptor.enumerable);
445 assertTrue(setSizeDescriptor.configurable);
448 assertFalse(s.hasOwnProperty('size'));
449 for (var i = 0; i < 10; i++) {
450 assertEquals(i, s.size);
453 for (var i = 9; i >= 0; i--) {
455 assertEquals(i, s.size);
459 var mapSizeDescriptor = Object.getOwnPropertyDescriptor(Map.prototype, 'size');
460 assertEquals(undefined, mapSizeDescriptor.value);
461 assertEquals(undefined, mapSizeDescriptor.set);
462 assertTrue(mapSizeDescriptor.get instanceof Function);
463 assertEquals(undefined, mapSizeDescriptor.get.prototype);
464 assertFalse(mapSizeDescriptor.enumerable);
465 assertTrue(mapSizeDescriptor.configurable);
468 assertFalse(m.hasOwnProperty('size'));
469 for (var i = 0; i < 10; i++) {
470 assertEquals(i, m.size);
473 for (var i = 9; i >= 0; i--) {
475 assertEquals(i, m.size);
483 assertTrue(s.has(42));
484 assertEquals(1, s.size);
486 assertFalse(s.has(42));
487 assertEquals(0, s.size);
495 assertTrue(m.has(42));
496 assertEquals(1, m.size);
498 assertFalse(m.has(42));
499 assertEquals(0, m.size);
503 (function TestMinusZeroSet() {
506 assertSame(0, s.values().next().value);
508 assertEquals(1, s.size);
509 assertTrue(s.has(0));
510 assertTrue(s.has(-0));
514 (function TestMinusZeroMap() {
517 assertSame(0, m.keys().next().value);
519 assertEquals(1, m.size);
520 assertTrue(m.has(0));
521 assertTrue(m.has(-0));
522 assertEquals('plus', m.get(0));
523 assertEquals('plus', m.get(-0));
527 (function TestSetForEachInvalidTypes() {
528 assertThrows(function() {
529 Set.prototype.set.forEach.call({});
533 assertThrows(function() {
539 (function TestSetForEach() {
547 set.forEach(function(v, k, s) {
550 assertSame(this, receiver);
557 } else if (v === 'c') {
563 assertEquals('acdfb', buffer);
567 (function TestSetForEachAddAtEnd() {
573 set.forEach(function(v) {
580 assertEquals('abc', buffer);
584 (function TestSetForEachDeleteNext() {
591 set.forEach(function(v) {
598 assertEquals('ab', buffer);
602 (function TestSetForEachDeleteVisitedAndAddAgain() {
609 set.forEach(function(v) {
613 } else if (v === 'c') {
618 assertEquals('abca', buffer);
622 (function TestSetForEachClear() {
629 set.forEach(function(v) {
638 assertEquals('ade', buffer);
642 (function TestSetForEachNested() {
649 set.forEach(function(v) {
651 set.forEach(function(v) {
659 assertEquals('aaccac', buffer);
663 (function TestSetForEachEarlyExit() {
672 set.forEach(function(v) {
679 assertEquals('a', buffer);
683 (function TestSetForEachGC() {
685 for (var i = 0; i < 100; i++) {
690 set.forEach(function(v) {
696 assertEquals(4950, accumulated);
700 (function TestSetForEachReceiverAsObject() {
701 var set = new Set(["1", "2"]);
703 // Create a new object in each function call when receiver is a
704 // primitive value. See ECMA-262, Annex C.
706 set.forEach(function() { a.push(this) }, "");
707 assertTrue(a[0] !== a[1]);
709 // Do not create a new object otherwise.
711 set.forEach(function() { a.push(this); }, {});
712 assertEquals(a[0], a[1]);
716 (function TestSetForEachReceiverAsObjectInStrictMode() {
717 var set = new Set(["1", "2"]);
719 // In strict mode primitive values should not be coerced to an object.
721 set.forEach(function() { 'use strict'; a.push(this); }, "");
722 assertTrue(a[0] === "" && a[0] === a[1]);
726 (function TestMapForEachInvalidTypes() {
727 assertThrows(function() {
728 Map.prototype.map.forEach.call({});
732 assertThrows(function() {
738 (function TestMapForEach() {
746 map.forEach(function(v, k, m) {
747 assertEquals(map, m);
748 assertEquals(this, receiver);
755 } else if (k === 2) {
761 assertArrayEquals([0, 'a', 2, 'c', 3, 'd', 5, 'f', 1, 'B'], buffer);
765 (function TestMapForEachAddAtEnd() {
771 map.forEach(function(v, k) {
778 assertArrayEquals([0, 'a', 1, 'b', 2, 'c'], buffer);
782 (function TestMapForEachDeleteNext() {
789 map.forEach(function(v, k) {
796 assertArrayEquals([0, 'a', 1, 'b'], buffer);
800 (function TestSetForEachDeleteVisitedAndAddAgain() {
807 map.forEach(function(v, k) {
811 } else if (k === 2) {
816 assertArrayEquals([0, 'a', 1, 'b', 2, 'c', 0, 'a'], buffer);
820 (function TestMapForEachClear() {
827 map.forEach(function(v, k) {
836 assertArrayEquals([0, 'a', 3, 'd', 4, 'e'], buffer);
840 (function TestMapForEachNested() {
847 map.forEach(function(v, k) {
849 map.forEach(function(v, k) {
857 assertArrayEquals([0, 'a', 0, 'a', 2, 'c', 2, 'c', 0, 'a', 2, 'c'], buffer);
861 (function TestMapForEachEarlyExit() {
870 map.forEach(function(v, k) {
877 assertArrayEquals([0, 'a'], buffer);
881 (function TestMapForEachGC() {
883 for (var i = 0; i < 100; i++) {
888 map.forEach(function(v) {
894 assertEquals(4950, accumulated);
898 (function TestMapForEachAllRemovedTransition() {
903 map.forEach(function(v) {
906 for (var i = 1; i < 4; i++) {
912 for (var i = 0; i < 4; i++) {
915 for (var i = 4; i < 8; i++) {
921 assertArrayEquals([0, 1, 2, 3, 4, 5, 6, 7], buffer);
925 (function TestMapForEachClearTransition() {
931 map.forEach(function(v) {
934 for (var j = 0; j < 5; j++) {
941 assertArrayEquals([0, 1, 2, 3, 4], buffer);
945 (function TestMapForEachNestedNonTrivialTransition() {
955 map.forEach(function(v) {
956 if (++i > 10) return;
962 for (var j = 4; j < 10; j++) {
965 for (var j = 4; j < 10; j += 2) {
970 for (var j = 10; j < 20; j++) {
973 for (var j = 10; j < 20; j += 2) {
981 assertArrayEquals([1, 2, 3, 5, 7, 9, 11, 13, 15, 17], buffer);
985 (function TestMapForEachAllRemovedTransitionNoClear() {
990 map.forEach(function(v) {
993 for (var i = 1; i < 8; i++) {
999 for (var i = 0; i < 8; i++) {
1005 assertArrayEquals([0, 1, 2, 3, 4], buffer);
1009 (function TestMapForEachNoMoreElementsAfterTransition() {
1014 map.forEach(function(v) {
1017 for (var i = 1; i < 16; i++) {
1023 for (var i = 5; i < 16; i++) {
1029 assertArrayEquals([0, 1, 2, 3, 4], buffer);
1033 (function TestMapForEachReceiverAsObject() {
1034 var map = new Map();
1035 map.set("key1", "value1");
1036 map.set("key2", "value2");
1038 // Create a new object in each function call when receiver is a
1039 // primitive value. See ECMA-262, Annex C.
1041 map.forEach(function() { a.push(this) }, "");
1042 assertTrue(a[0] !== a[1]);
1044 // Do not create a new object otherwise.
1046 map.forEach(function() { a.push(this); }, {});
1047 assertEquals(a[0], a[1]);
1051 (function TestMapForEachReceiverAsObjectInStrictMode() {
1052 var map = new Map();
1053 map.set("key1", "value1");
1054 map.set("key2", "value2");
1056 // In strict mode primitive values should not be coerced to an object.
1058 map.forEach(function() { 'use strict'; a.push(this); }, "");
1059 assertTrue(a[0] === "" && a[0] === a[1]);
1063 // Allows testing iterator-based constructors easily.
1064 var oneAndTwo = new Map();
1068 oneAndTwo.set(k1, 1);
1069 oneAndTwo.set(k2, 2);
1072 function TestSetConstructor(ctor) {
1073 var s = new ctor(null);
1076 s = new ctor(undefined);
1080 assertThrows(function() {
1083 assertThrows(function() {
1087 // @@iterator not callable
1088 assertThrows(function() {
1090 object[Symbol.iterator] = 42;
1094 // @@iterator result not object
1095 assertThrows(function() {
1097 object[Symbol.iterator] = function() {
1107 s = new ctor(s2.values());
1109 assertTrue(s.has(k0));
1110 assertTrue(s.has(k1));
1111 assertTrue(s.has(k2));
1113 TestSetConstructor(Set);
1114 TestSetConstructor(WeakSet);
1117 function TestSetConstructorAddNotCallable(ctor) {
1118 var originalPrototypeAdd = ctor.prototype.add;
1119 assertThrows(function() {
1120 ctor.prototype.add = 42;
1121 new ctor(oneAndTwo.values());
1123 ctor.prototype.add = originalPrototypeAdd;
1125 TestSetConstructorAddNotCallable(Set);
1126 TestSetConstructorAddNotCallable(WeakSet);
1129 function TestSetConstructorGetAddOnce(ctor) {
1130 var originalPrototypeAdd = ctor.prototype.add;
1131 var getAddCount = 0;
1132 Object.defineProperty(ctor.prototype, 'add', {
1135 return function() {};
1138 var s = new ctor(oneAndTwo.values());
1139 assertEquals(1, getAddCount);
1141 Object.defineProperty(ctor.prototype, 'add', {
1142 value: originalPrototypeAdd,
1146 TestSetConstructorGetAddOnce(Set);
1147 TestSetConstructorGetAddOnce(WeakSet);
1150 function TestSetConstructorAddReplaced(ctor) {
1151 var originalPrototypeAdd = ctor.prototype.add;
1153 ctor.prototype.add = function(value) {
1155 originalPrototypeAdd.call(this, value);
1156 ctor.prototype.add = null;
1158 var s = new ctor(oneAndTwo.keys());
1159 assertEquals(2, addCount);
1161 ctor.prototype.add = originalPrototypeAdd;
1163 TestSetConstructorAddReplaced(Set);
1164 TestSetConstructorAddReplaced(WeakSet);
1167 function TestSetConstructorOrderOfDoneValue(ctor) {
1168 var valueCount = 0, doneCount = 0;
1182 iterator[Symbol.iterator] = function() {
1185 assertThrows(function() {
1188 assertEquals(1, doneCount);
1189 assertEquals(0, valueCount);
1191 TestSetConstructorOrderOfDoneValue(Set);
1192 TestSetConstructorOrderOfDoneValue(WeakSet);
1195 function TestSetConstructorNextNotAnObject(ctor) {
1201 iterator[Symbol.iterator] = function() {
1204 assertThrows(function() {
1208 TestSetConstructorNextNotAnObject(Set);
1209 TestSetConstructorNextNotAnObject(WeakSet);
1212 (function TestWeakSetConstructorNonObjectKeys() {
1213 assertThrows(function() {
1219 function TestSetConstructorIterableValue(ctor) {
1221 // Strict mode is required to prevent implicit wrapping in the getter.
1222 Object.defineProperty(Number.prototype, Symbol.iterator, {
1224 assertEquals('number', typeof this);
1226 assertEquals('number', typeof this);
1227 return oneAndTwo.keys();
1233 var set = new ctor(42);
1235 assertTrue(set.has(k1));
1236 assertTrue(set.has(k2));
1238 delete Number.prototype[Symbol.iterator];
1240 TestSetConstructorIterableValue(Set);
1241 TestSetConstructorIterableValue(WeakSet);
1244 (function TestSetConstructorStringValue() {
1245 var s = new Set('abc');
1247 assertTrue(s.has('a'));
1248 assertTrue(s.has('b'));
1249 assertTrue(s.has('c'));
1253 function TestMapConstructor(ctor) {
1254 var m = new ctor(null);
1257 m = new ctor(undefined);
1261 assertThrows(function() {
1264 assertThrows(function() {
1268 // @@iterator not callable
1269 assertThrows(function() {
1271 object[Symbol.iterator] = 42;
1275 // @@iterator result not object
1276 assertThrows(function() {
1278 object[Symbol.iterator] = function() {
1288 m = new ctor(m2.entries());
1290 assertEquals('a', m.get(k0));
1291 assertEquals('b', m.get(k1));
1292 assertEquals('c', m.get(k2));
1294 TestMapConstructor(Map);
1295 TestMapConstructor(WeakMap);
1298 function TestMapConstructorSetNotCallable(ctor) {
1299 var originalPrototypeSet = ctor.prototype.set;
1300 assertThrows(function() {
1301 ctor.prototype.set = 42;
1302 new ctor(oneAndTwo.entries());
1304 ctor.prototype.set = originalPrototypeSet;
1306 TestMapConstructorSetNotCallable(Map);
1307 TestMapConstructorSetNotCallable(WeakMap);
1310 function TestMapConstructorGetAddOnce(ctor) {
1311 var originalPrototypeSet = ctor.prototype.set;
1312 var getSetCount = 0;
1313 Object.defineProperty(ctor.prototype, 'set', {
1316 return function() {};
1319 var m = new ctor(oneAndTwo.entries());
1320 assertEquals(1, getSetCount);
1322 Object.defineProperty(ctor.prototype, 'set', {
1323 value: originalPrototypeSet,
1327 TestMapConstructorGetAddOnce(Map);
1328 TestMapConstructorGetAddOnce(WeakMap);
1331 function TestMapConstructorSetReplaced(ctor) {
1332 var originalPrototypeSet = ctor.prototype.set;
1334 ctor.prototype.set = function(key, value) {
1336 originalPrototypeSet.call(this, key, value);
1337 ctor.prototype.set = null;
1339 var m = new ctor(oneAndTwo.entries());
1340 assertEquals(2, setCount);
1342 ctor.prototype.set = originalPrototypeSet;
1344 TestMapConstructorSetReplaced(Map);
1345 TestMapConstructorSetReplaced(WeakMap);
1348 function TestMapConstructorOrderOfDoneValue(ctor) {
1349 var valueCount = 0, doneCount = 0;
1350 function FakeError() {}
1359 throw new FakeError();
1364 iterator[Symbol.iterator] = function() {
1367 assertThrows(function() {
1370 assertEquals(1, doneCount);
1371 assertEquals(0, valueCount);
1373 TestMapConstructorOrderOfDoneValue(Map);
1374 TestMapConstructorOrderOfDoneValue(WeakMap);
1377 function TestMapConstructorNextNotAnObject(ctor) {
1383 iterator[Symbol.iterator] = function() {
1386 assertThrows(function() {
1390 TestMapConstructorNextNotAnObject(Map);
1391 TestMapConstructorNextNotAnObject(WeakMap);
1394 function TestMapConstructorIteratorNotObjectValues(ctor) {
1395 assertThrows(function() {
1396 new ctor(oneAndTwo.values());
1399 TestMapConstructorIteratorNotObjectValues(Map);
1400 TestMapConstructorIteratorNotObjectValues(WeakMap);
1403 (function TestWeakMapConstructorNonObjectKeys() {
1404 assertThrows(function() {
1405 new WeakMap([[1, 2]])
1410 function TestMapConstructorIterableValue(ctor) {
1412 // Strict mode is required to prevent implicit wrapping in the getter.
1413 Object.defineProperty(Number.prototype, Symbol.iterator, {
1415 assertEquals('number', typeof this);
1417 assertEquals('number', typeof this);
1418 return oneAndTwo.entries();
1424 var map = new ctor(42);
1426 assertEquals(1, map.get(k1));
1427 assertEquals(2, map.get(k2));
1429 delete Number.prototype[Symbol.iterator];
1431 TestMapConstructorIterableValue(Map);
1432 TestMapConstructorIterableValue(WeakMap);
1434 function TestCollectionToString(C) {
1435 assertEquals("[object " + C.name + "]",
1436 Object.prototype.toString.call(new C()));
1438 TestCollectionToString(Map);
1439 TestCollectionToString(Set);
1440 TestCollectionToString(WeakMap);
1441 TestCollectionToString(WeakSet);
1444 function TestConstructorOrderOfAdderIterator(ctor, adderName) {
1445 var iterable = new Map();
1446 iterable.set({}, {});
1447 iterable.set({}, {});
1448 var iterableFunction = iterable[Symbol.iterator];
1449 Object.defineProperty(iterable, Symbol.iterator, {
1452 return iterableFunction;
1457 var adderFunction = ctor.prototype[adderName];
1459 Object.defineProperty(ctor.prototype, adderName, {
1462 return adderFunction;
1467 assertEquals(adderName + 'iterator', log);
1469 Object.defineProperty(ctor.prototype, adderName, {
1470 value: adderFunction
1473 TestConstructorOrderOfAdderIterator(Map, 'set');
1474 TestConstructorOrderOfAdderIterator(Set, 'add');
1475 TestConstructorOrderOfAdderIterator(WeakMap, 'set');
1476 TestConstructorOrderOfAdderIterator(WeakSet, 'add');