Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(290)

Side by Side Diff: test/mjsunit/harmony/collections.js

Issue 19678023: ES6: Implement WeakSet (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Reitveld is acting up Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
(...skipping 17 matching lines...) Expand all
28 // Flags: --harmony-collections --expose-gc --allow-natives-syntax 28 // Flags: --harmony-collections --expose-gc --allow-natives-syntax
29 29
30 30
31 // Test valid getter and setter calls on Sets. 31 // Test valid getter and setter calls on Sets.
32 function TestValidSetCalls(m) { 32 function TestValidSetCalls(m) {
33 assertDoesNotThrow(function () { m.add(new Object) }); 33 assertDoesNotThrow(function () { m.add(new Object) });
34 assertDoesNotThrow(function () { m.has(new Object) }); 34 assertDoesNotThrow(function () { m.has(new Object) });
35 assertDoesNotThrow(function () { m.delete(new Object) }); 35 assertDoesNotThrow(function () { m.delete(new Object) });
36 } 36 }
37 TestValidSetCalls(new Set); 37 TestValidSetCalls(new Set);
38 TestValidSetCalls(new WeakSet);
38 39
39 40
40 // Test valid getter and setter calls on Maps and WeakMaps 41 // Test valid getter and setter calls on Maps and WeakMaps
41 function TestValidMapCalls(m) { 42 function TestValidMapCalls(m) {
42 assertDoesNotThrow(function () { m.get(new Object) }); 43 assertDoesNotThrow(function () { m.get(new Object) });
43 assertDoesNotThrow(function () { m.set(new Object) }); 44 assertDoesNotThrow(function () { m.set(new Object) });
44 assertDoesNotThrow(function () { m.has(new Object) }); 45 assertDoesNotThrow(function () { m.has(new Object) });
45 assertDoesNotThrow(function () { m.delete(new Object) }); 46 assertDoesNotThrow(function () { m.delete(new Object) });
46 } 47 }
47 TestValidMapCalls(new Map); 48 TestValidMapCalls(new Map);
(...skipping 30 matching lines...) Expand all
78 TestSet(set, i); 79 TestSet(set, i);
79 TestSet(set, i / 100); 80 TestSet(set, i / 100);
80 TestSet(set, 'key-' + i); 81 TestSet(set, 'key-' + i);
81 } 82 }
82 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; 83 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ];
83 for (var i = 0; i < keys.length; i++) { 84 for (var i = 0; i < keys.length; i++) {
84 TestSet(set, keys[i]); 85 TestSet(set, keys[i]);
85 } 86 }
86 } 87 }
87 TestSetBehavior(new Set); 88 TestSetBehavior(new Set);
89 TestSet(new WeakSet, new Object);
88 90
89 91
90 // Test expected mapping behavior for Maps and WeakMaps 92 // Test expected mapping behavior for Maps and WeakMaps
91 function TestMapping(map, key, value) { 93 function TestMapping(map, key, value) {
92 assertSame(undefined, map.set(key, value)); 94 assertSame(undefined, map.set(key, value));
93 assertSame(value, map.get(key)); 95 assertSame(value, map.get(key));
94 } 96 }
95 function TestMapBehavior1(m) { 97 function TestMapBehavior1(m) {
96 TestMapping(m, new Object, 23); 98 TestMapping(m, new Object, 23);
97 TestMapping(m, new Object, 'the-value'); 99 TestMapping(m, new Object, 'the-value');
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 for (var p in x) array.push(p); 180 for (var p in x) array.push(p);
179 return array.sort(); 181 return array.sort();
180 } 182 }
181 assertArrayEquals([], props(func)); 183 assertArrayEquals([], props(func));
182 assertArrayEquals([], props(func.prototype)); 184 assertArrayEquals([], props(func.prototype));
183 assertArrayEquals([], props(new func())); 185 assertArrayEquals([], props(new func()));
184 } 186 }
185 TestEnumerable(Set); 187 TestEnumerable(Set);
186 TestEnumerable(Map); 188 TestEnumerable(Map);
187 TestEnumerable(WeakMap); 189 TestEnumerable(WeakMap);
190 TestEnumerable(WeakSet);
188 191
189 192
190 // Test arbitrary properties on Maps and WeakMaps 193 // Test arbitrary properties on Maps and WeakMaps
191 function TestArbitrary(m) { 194 function TestArbitrary(m) {
192 function TestProperty(map, property, value) { 195 function TestProperty(map, property, value) {
193 map[property] = value; 196 map[property] = value;
194 assertEquals(value, map[property]); 197 assertEquals(value, map[property]);
195 } 198 }
196 for (var i = 0; i < 20; i++) { 199 for (var i = 0; i < 20; i++) {
197 TestProperty(m, i, 'val' + i); 200 TestProperty(m, i, 'val' + i);
198 TestProperty(m, 'foo' + i, 'bar' + i); 201 TestProperty(m, 'foo' + i, 'bar' + i);
199 } 202 }
200 TestMapping(m, new Object, 'foobar'); 203 TestMapping(m, new Object, 'foobar');
201 } 204 }
202 TestArbitrary(new Map); 205 TestArbitrary(new Map);
203 TestArbitrary(new WeakMap); 206 TestArbitrary(new WeakMap);
204 207
205 208
206 // Test direct constructor call 209 // Test direct constructor call
207 assertTrue(Set() instanceof Set); 210 assertTrue(Set() instanceof Set);
208 assertTrue(Map() instanceof Map); 211 assertTrue(Map() instanceof Map);
209 assertTrue(WeakMap() instanceof WeakMap); 212 assertTrue(WeakMap() instanceof WeakMap);
213 assertTrue(WeakSet() instanceof WeakSet);
210 214
211 215
212 // Test whether NaN values as keys are treated correctly. 216 // Test whether NaN values as keys are treated correctly.
213 var s = new Set; 217 var s = new Set;
214 assertFalse(s.has(NaN)); 218 assertFalse(s.has(NaN));
215 assertFalse(s.has(NaN + 1)); 219 assertFalse(s.has(NaN + 1));
216 assertFalse(s.has(23)); 220 assertFalse(s.has(23));
217 s.add(NaN); 221 s.add(NaN);
218 assertTrue(s.has(NaN)); 222 assertTrue(s.has(NaN));
219 assertTrue(s.has(NaN + 1)); 223 assertTrue(s.has(NaN + 1));
220 assertFalse(s.has(23)); 224 assertFalse(s.has(23));
221 var m = new Map; 225 var m = new Map;
222 assertFalse(m.has(NaN)); 226 assertFalse(m.has(NaN));
223 assertFalse(m.has(NaN + 1)); 227 assertFalse(m.has(NaN + 1));
224 assertFalse(m.has(23)); 228 assertFalse(m.has(23));
225 m.set(NaN, 'a-value'); 229 m.set(NaN, 'a-value');
226 assertTrue(m.has(NaN)); 230 assertTrue(m.has(NaN));
227 assertTrue(m.has(NaN + 1)); 231 assertTrue(m.has(NaN + 1));
228 assertFalse(m.has(23)); 232 assertFalse(m.has(23));
229 233
230 234
231 // Test some common JavaScript idioms for Sets 235 // Test some common JavaScript idioms for Sets
232 var s = new Set; 236 var s = new Set;
233 assertTrue(s instanceof Set); 237 assertTrue(s instanceof Set);
234 assertTrue(Set.prototype.add instanceof Function) 238 assertTrue(Set.prototype.add instanceof Function)
235 assertTrue(Set.prototype.has instanceof Function) 239 assertTrue(Set.prototype.has instanceof Function)
236 assertTrue(Set.prototype.delete instanceof Function) 240 assertTrue(Set.prototype.delete instanceof Function)
241 assertTrue(Set.prototype.clear instanceof Function)
237 242
238 243
239 // Test some common JavaScript idioms for Maps 244 // Test some common JavaScript idioms for Maps
240 var m = new Map; 245 var m = new Map;
241 assertTrue(m instanceof Map); 246 assertTrue(m instanceof Map);
242 assertTrue(Map.prototype.set instanceof Function) 247 assertTrue(Map.prototype.set instanceof Function)
243 assertTrue(Map.prototype.get instanceof Function) 248 assertTrue(Map.prototype.get instanceof Function)
244 assertTrue(Map.prototype.has instanceof Function) 249 assertTrue(Map.prototype.has instanceof Function)
245 assertTrue(Map.prototype.delete instanceof Function) 250 assertTrue(Map.prototype.delete instanceof Function)
251 assertTrue(Map.prototype.clear instanceof Function)
246 252
247 253
248 // Test some common JavaScript idioms for WeakMaps 254 // Test some common JavaScript idioms for WeakMaps
249 var m = new WeakMap; 255 var m = new WeakMap;
250 assertTrue(m instanceof WeakMap); 256 assertTrue(m instanceof WeakMap);
251 assertTrue(WeakMap.prototype.set instanceof Function) 257 assertTrue(WeakMap.prototype.set instanceof Function)
252 assertTrue(WeakMap.prototype.get instanceof Function) 258 assertTrue(WeakMap.prototype.get instanceof Function)
253 assertTrue(WeakMap.prototype.has instanceof Function) 259 assertTrue(WeakMap.prototype.has instanceof Function)
254 assertTrue(WeakMap.prototype.delete instanceof Function) 260 assertTrue(WeakMap.prototype.delete instanceof Function)
261 assertTrue(WeakMap.prototype.clear instanceof Function)
255 262
256 263
257 // Test class of the Set, Map and WeakMap instance and prototype. 264 // Test some common JavaScript idioms for WeakSets
265 var s = new WeakSet;
266 assertTrue(s instanceof WeakSet);
267 assertTrue(WeakSet.prototype.add instanceof Function)
268 assertTrue(WeakSet.prototype.has instanceof Function)
269 assertTrue(WeakSet.prototype.delete instanceof Function)
270 assertTrue(WeakSet.prototype.clear instanceof Function)
271
272
273 // Test class of instance and prototype.
258 assertEquals("Set", %_ClassOf(new Set)) 274 assertEquals("Set", %_ClassOf(new Set))
259 assertEquals("Object", %_ClassOf(Set.prototype)) 275 assertEquals("Object", %_ClassOf(Set.prototype))
260 assertEquals("Map", %_ClassOf(new Map)) 276 assertEquals("Map", %_ClassOf(new Map))
261 assertEquals("Object", %_ClassOf(Map.prototype)) 277 assertEquals("Object", %_ClassOf(Map.prototype))
262 assertEquals("WeakMap", %_ClassOf(new WeakMap)) 278 assertEquals("WeakMap", %_ClassOf(new WeakMap))
263 assertEquals("Object", %_ClassOf(WeakMap.prototype)) 279 assertEquals("Object", %_ClassOf(WeakMap.prototype))
280 assertEquals("WeakSet", %_ClassOf(new WeakSet))
281 assertEquals("Object", %_ClassOf(WeakMap.prototype))
264 282
265 283
266 // Test constructor property of the Set, Map and WeakMap prototype. 284 // Test name of constructor.
285 assertEquals("Set", Set.name);
286 assertEquals("Map", Map.name);
287 assertEquals("WeakMap", WeakMap.name);
288 assertEquals("WeakSet", WeakSet.name);
289
290
291 // Test constructor property of the Set, Map, WeakMap and WeakSet prototype.
267 function TestConstructor(C) { 292 function TestConstructor(C) {
268 assertFalse(C === Object.prototype.constructor); 293 assertFalse(C === Object.prototype.constructor);
269 assertSame(C, C.prototype.constructor); 294 assertSame(C, C.prototype.constructor);
270 assertSame(C, C().__proto__.constructor); 295 assertSame(C, C().__proto__.constructor);
271 assertSame(C, (new C).__proto__.constructor); 296 assertSame(C, (new C).__proto__.constructor);
272 } 297 }
273 TestConstructor(Set); 298 TestConstructor(Set);
274 TestConstructor(Map); 299 TestConstructor(Map);
275 TestConstructor(WeakMap); 300 TestConstructor(WeakMap);
301 TestConstructor(WeakSet);
302
303
304 function TestDescriptor(global, C) {
305 assertEquals({
306 value: C,
307 writable: true,
308 enumerable: false,
309 configurable: true
310 }, Object.getOwnPropertyDescriptor(global, C.name));
311 }
312 TestDescriptor(this, Set);
313 TestDescriptor(this, Map);
314 TestDescriptor(this, WeakMap);
315 TestDescriptor(this, WeakSet);
276 316
277 317
278 // Regression test for WeakMap prototype. 318 // Regression test for WeakMap prototype.
279 assertTrue(WeakMap.prototype.constructor === WeakMap) 319 assertTrue(WeakMap.prototype.constructor === WeakMap)
280 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype) 320 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype)
281 321
282 322
283 // Regression test for issue 1617: The prototype of the WeakMap constructor 323 // Regression test for issue 1617: The prototype of the WeakMap constructor
284 // needs to be unique (i.e. different from the one of the Object constructor). 324 // needs to be unique (i.e. different from the one of the Object constructor).
285 assertFalse(WeakMap.prototype === Object.prototype); 325 assertFalse(WeakMap.prototype === Object.prototype);
(...skipping 11 matching lines...) Expand all
297 assertEquals(10, o.myValue); 337 assertEquals(10, o.myValue);
298 338
299 339
300 // Regression test for issue 1884: Invoking any of the methods for Harmony 340 // Regression test for issue 1884: Invoking any of the methods for Harmony
301 // maps, sets, or weak maps, with a wrong type of receiver should be throwing 341 // maps, sets, or weak maps, with a wrong type of receiver should be throwing
302 // a proper TypeError. 342 // a proper TypeError.
303 var alwaysBogus = [ undefined, null, true, "x", 23, {} ]; 343 var alwaysBogus = [ undefined, null, true, "x", 23, {} ];
304 var bogusReceiversTestSet = [ 344 var bogusReceiversTestSet = [
305 { proto: Set.prototype, 345 { proto: Set.prototype,
306 funcs: [ 'add', 'has', 'delete' ], 346 funcs: [ 'add', 'has', 'delete' ],
307 receivers: alwaysBogus.concat([ new Map, new WeakMap ]), 347 receivers: alwaysBogus.concat([ new Map, new WeakMap, new WeakSet ]),
308 }, 348 },
309 { proto: Map.prototype, 349 { proto: Map.prototype,
310 funcs: [ 'get', 'set', 'has', 'delete' ], 350 funcs: [ 'get', 'set', 'has', 'delete' ],
311 receivers: alwaysBogus.concat([ new Set, new WeakMap ]), 351 receivers: alwaysBogus.concat([ new Set, new WeakMap, new WeakSet ]),
312 }, 352 },
313 { proto: WeakMap.prototype, 353 { proto: WeakMap.prototype,
314 funcs: [ 'get', 'set', 'has', 'delete' ], 354 funcs: [ 'get', 'set', 'has', 'delete' ],
315 receivers: alwaysBogus.concat([ new Set, new Map ]), 355 receivers: alwaysBogus.concat([ new Set, new Map, new WeakSet ]),
356 },
357 { proto: WeakSet.prototype,
358 funcs: [ 'add', 'has', 'delete' ],
359 receivers: alwaysBogus.concat([ new Set, new Map, new WeakMap ]),
316 }, 360 },
317 ]; 361 ];
318 function TestBogusReceivers(testSet) { 362 function TestBogusReceivers(testSet) {
319 for (var i = 0; i < testSet.length; i++) { 363 for (var i = 0; i < testSet.length; i++) {
320 var proto = testSet[i].proto; 364 var proto = testSet[i].proto;
321 var funcs = testSet[i].funcs; 365 var funcs = testSet[i].funcs;
322 var receivers = testSet[i].receivers; 366 var receivers = testSet[i].receivers;
323 for (var j = 0; j < funcs.length; j++) { 367 for (var j = 0; j < funcs.length; j++) {
324 var func = proto[funcs[j]]; 368 var func = proto[funcs[j]];
325 for (var k = 0; k < receivers.length; k++) { 369 for (var k = 0; k < receivers.length; k++) {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 (function() { 450 (function() {
407 var k = new Object(); 451 var k = new Object();
408 var w = new WeakMap(); 452 var w = new WeakMap();
409 w.set(k, 23); 453 w.set(k, 23);
410 assertTrue(w.has(k)); 454 assertTrue(w.has(k));
411 assertEquals(23, w.get(k)); 455 assertEquals(23, w.get(k));
412 w.clear(); 456 w.clear();
413 assertFalse(w.has(k)); 457 assertFalse(w.has(k));
414 assertEquals(undefined, w.get(k)); 458 assertEquals(undefined, w.get(k));
415 })(); 459 })();
460
461
462 // Test WeakSet clear
463 (function() {
464 var k = new Object();
465 var w = new WeakSet();
466 w.add(k);
467 assertTrue(w.has(k));
468 w.clear();
469 assertFalse(w.has(k));
470 })();
OLDNEW
« src/objects-printer.cc ('K') | « test/cctest/test-weaksets.cc ('k') | tools/grokdump.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698