Index: test/mjsunit/harmony/weakmaps.js |
diff --git a/test/mjsunit/harmony/weakmaps.js b/test/mjsunit/harmony/weakmaps.js |
index 7b5dcaf0c1d8b03bb6dafd9f7e064f8143056588..5b0a759cd4bd27f68956ad9480d059f31f5b7293 100644 |
--- a/test/mjsunit/harmony/weakmaps.js |
+++ b/test/mjsunit/harmony/weakmaps.js |
@@ -25,104 +25,130 @@ |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
-// Flags: --harmony-weakmaps --expose-gc |
+// Flags: --harmony-collections --harmony-weakmaps --expose-gc |
// Test valid getter and setter calls |
-var m = new WeakMap; |
-assertDoesNotThrow(function () { m.get(new Object) }); |
-assertDoesNotThrow(function () { m.set(new Object) }); |
-assertDoesNotThrow(function () { m.has(new Object) }); |
-assertDoesNotThrow(function () { m.delete(new Object) }); |
- |
- |
-// Test invalid getter and setter calls |
-var m = new WeakMap; |
-assertThrows(function () { m.get(undefined) }, TypeError); |
-assertThrows(function () { m.set(undefined, 0) }, TypeError); |
-assertThrows(function () { m.get(0) }, TypeError); |
-assertThrows(function () { m.set(0, 0) }, TypeError); |
-assertThrows(function () { m.get('a-key') }, TypeError); |
-assertThrows(function () { m.set('a-key', 0) }, TypeError); |
+function TestValidCalls(m) { |
+ assertDoesNotThrow(function () { m.get(new Object) }); |
+ assertDoesNotThrow(function () { m.set(new Object) }); |
+ assertDoesNotThrow(function () { m.has(new Object) }); |
+ assertDoesNotThrow(function () { m.delete(new Object) }); |
+} |
+TestValidCalls(new Map); |
+TestValidCalls(new WeakMap); |
+ |
+ |
+// Test invalid getter and setter calls (for WeakMap only) |
+function TestInvalidCalls(m) { |
+ assertThrows(function () { m.get(undefined) }, TypeError); |
+ assertThrows(function () { m.set(undefined, 0) }, TypeError); |
+ assertThrows(function () { m.get(0) }, TypeError); |
+ assertThrows(function () { m.set(0, 0) }, TypeError); |
+ assertThrows(function () { m.get('a-key') }, TypeError); |
+ assertThrows(function () { m.set('a-key', 0) }, TypeError); |
+} |
+TestInvalidCalls(new WeakMap); |
// Test expected mapping behavior |
-var m = new WeakMap; |
function TestMapping(map, key, value) { |
map.set(key, value); |
assertSame(value, map.get(key)); |
} |
-TestMapping(m, new Object, 23); |
-TestMapping(m, new Object, 'the-value'); |
-TestMapping(m, new Object, new Object); |
+function TestBehavior(m) { |
+ TestMapping(m, new Object, 23); |
+ TestMapping(m, new Object, 'the-value'); |
+ TestMapping(m, new Object, new Object); |
+} |
+TestBehavior(new Map); |
+TestBehavior(new WeakMap); |
// Test expected querying behavior |
-var m = new WeakMap; |
-var key = new Object; |
-TestMapping(m, key, 'to-be-present'); |
-assertTrue(m.has(key)); |
-assertFalse(m.has(new Object)); |
-TestMapping(m, key, undefined); |
-assertFalse(m.has(key)); |
-assertFalse(m.has(new Object)); |
+function TestQuery(m) { |
+ var key = new Object; |
+ TestMapping(m, key, 'to-be-present'); |
+ assertTrue(m.has(key)); |
+ assertFalse(m.has(new Object)); |
+ TestMapping(m, key, undefined); |
+ assertFalse(m.has(key)); |
+ assertFalse(m.has(new Object)); |
+} |
+TestQuery(new Map); |
+TestQuery(new WeakMap); |
// Test expected deletion behavior |
-var m = new WeakMap; |
-var key = new Object; |
-TestMapping(m, key, 'to-be-deleted'); |
-assertTrue(m.delete(key)); |
-assertFalse(m.delete(key)); |
-assertFalse(m.delete(new Object)); |
-assertSame(m.get(key), undefined); |
+function TestDelete(m) { |
+ var key = new Object; |
+ TestMapping(m, key, 'to-be-deleted'); |
+ assertTrue(m.delete(key)); |
+ assertFalse(m.delete(key)); |
+ assertFalse(m.delete(new Object)); |
+ assertSame(m.get(key), undefined); |
+} |
+TestDelete(new Map); |
+TestDelete(new WeakMap); |
// Test GC of map with entry |
-var m = new WeakMap; |
-var key = new Object; |
-m.set(key, 'not-collected'); |
-gc(); |
-assertSame('not-collected', m.get(key)); |
+function TestGC1(m) { |
+ var key = new Object; |
+ m.set(key, 'not-collected'); |
+ gc(); |
+ assertSame('not-collected', m.get(key)); |
+} |
+TestGC1(new Map); |
+TestGC1(new WeakMap); |
// Test GC of map with chained entries |
-var m = new WeakMap; |
-var head = new Object; |
-for (key = head, i = 0; i < 10; i++, key = m.get(key)) { |
- m.set(key, new Object); |
+function TestGC2(m) { |
+ var head = new Object; |
+ for (key = head, i = 0; i < 10; i++, key = m.get(key)) { |
+ m.set(key, new Object); |
+ } |
+ gc(); |
+ var count = 0; |
+ for (key = head; key != undefined; key = m.get(key)) { |
+ count++; |
+ } |
+ assertEquals(11, count); |
} |
-gc(); |
-var count = 0; |
-for (key = head; key != undefined; key = m.get(key)) { |
- count++; |
-} |
-assertEquals(11, count); |
+TestGC2(new Map); |
+TestGC2(new WeakMap); |
// Test property attribute [[Enumerable]] |
-var m = new WeakMap; |
-function props(x) { |
- var array = []; |
- for (var p in x) array.push(p); |
- return array.sort(); |
+function TestEnumerable(m) { |
+ function props(x) { |
+ var array = []; |
+ for (var p in x) array.push(p); |
+ return array.sort(); |
+ } |
+ assertArrayEquals([], props(WeakMap)); |
+ assertArrayEquals([], props(WeakMap.prototype)); |
+ assertArrayEquals([], props(m)); |
} |
-assertArrayEquals([], props(WeakMap)); |
-assertArrayEquals([], props(WeakMap.prototype)); |
-assertArrayEquals([], props(m)); |
+TestEnumerable(new Map); |
+TestEnumerable(new WeakMap); |
// Test arbitrary properties on weak maps |
-var m = new WeakMap; |
-function TestProperty(map, property, value) { |
- map[property] = value; |
- assertEquals(value, map[property]); |
-} |
-for (i = 0; i < 20; i++) { |
- TestProperty(m, i, 'val' + i); |
- TestProperty(m, 'foo' + i, 'bar' + i); |
+function TestArbitrary(m) { |
+ function TestProperty(map, property, value) { |
+ map[property] = value; |
+ assertEquals(value, map[property]); |
+ } |
+ for (i = 0; i < 20; i++) { |
+ TestProperty(m, i, 'val' + i); |
+ TestProperty(m, 'foo' + i, 'bar' + i); |
+ } |
+ TestMapping(m, new Object, 'foobar'); |
} |
-TestMapping(m, new Object, 'foobar'); |
+TestArbitrary(new Map); |
+TestArbitrary(new WeakMap); |
// Test direct constructor call |