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 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 TestValidSetCalls(new Set); | 44 TestValidSetCalls(new Set); |
45 TestValidSetCalls(new WeakSet); | 45 TestValidSetCalls(new WeakSet); |
46 | 46 |
47 | 47 |
48 // Test valid getter and setter calls on Maps and WeakMaps | 48 // Test valid getter and setter calls on Maps and WeakMaps |
49 function TestValidMapCalls(m) { | 49 function TestValidMapCalls(m) { |
50 assertDoesNotThrow(function () { m.get(new Object) }); | 50 assertDoesNotThrow(function () { m.get(new Object) }); |
51 assertDoesNotThrow(function () { m.set(new Object) }); | 51 assertDoesNotThrow(function () { m.set(new Object) }); |
52 assertDoesNotThrow(function () { m.has(new Object) }); | 52 assertDoesNotThrow(function () { m.has(new Object) }); |
53 assertDoesNotThrow(function () { m.delete(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()) }); |
54 } | 69 } |
55 TestValidMapCalls(new Map); | 70 TestValidMapCalls(new Map); |
56 TestValidMapCalls(new WeakMap); | 71 TestValidMapCalls(new WeakMap); |
57 | 72 |
58 | 73 |
59 // Test invalid getter and setter calls for WeakMap only | 74 // Test invalid getter and setter calls for WeakMap only |
60 function TestInvalidCalls(m) { | 75 function TestInvalidCalls(m) { |
61 assertThrows(function () { m.get(undefined) }, TypeError); | |
62 assertThrows(function () { m.set(undefined, 0) }, TypeError); | 76 assertThrows(function () { m.set(undefined, 0) }, TypeError); |
63 assertThrows(function () { m.get(null) }, TypeError); | |
64 assertThrows(function () { m.set(null, 0) }, TypeError); | 77 assertThrows(function () { m.set(null, 0) }, TypeError); |
65 assertThrows(function () { m.get(0) }, TypeError); | |
66 assertThrows(function () { m.set(0, 0) }, TypeError); | 78 assertThrows(function () { m.set(0, 0) }, TypeError); |
67 assertThrows(function () { m.get('a-key') }, TypeError); | |
68 assertThrows(function () { m.set('a-key', 0) }, TypeError); | 79 assertThrows(function () { m.set('a-key', 0) }, TypeError); |
| 80 assertThrows(function () { m.set(Symbol(), 0) }, TypeError); |
69 } | 81 } |
70 TestInvalidCalls(new WeakMap); | 82 TestInvalidCalls(new WeakMap); |
71 | 83 |
72 | 84 |
73 // Test expected behavior for Sets and WeakSets | 85 // Test expected behavior for Sets and WeakSets |
74 function TestSet(set, key) { | 86 function TestSet(set, key) { |
75 assertFalse(set.has(key)); | 87 assertFalse(set.has(key)); |
76 assertSame(set, set.add(key)); | 88 assertFalse(set.delete(key)); |
77 assertTrue(set.has(key)); | 89 if (typeof key === 'object' && !(set instanceof WeakSet)) { |
78 assertTrue(set.delete(key)); | 90 assertSame(set, set.add(key)); |
| 91 assertTrue(set.has(key)); |
| 92 assertTrue(set.delete(key)); |
| 93 } |
79 assertFalse(set.has(key)); | 94 assertFalse(set.has(key)); |
80 assertFalse(set.delete(key)); | 95 assertFalse(set.delete(key)); |
81 assertFalse(set.has(key)); | 96 assertFalse(set.has(key)); |
82 } | 97 } |
83 function TestSetBehavior(set) { | 98 function TestSetBehavior(set) { |
| 99 // Fill |
84 for (var i = 0; i < 20; i++) { | 100 for (var i = 0; i < 20; i++) { |
85 TestSet(set, new Object); | 101 TestSet(set, new Object); |
86 TestSet(set, i); | 102 TestSet(set, i); |
87 TestSet(set, i / 100); | 103 TestSet(set, i / 100); |
88 TestSet(set, 'key-' + i); | 104 TestSet(set, 'key-' + i); |
| 105 TestSet(set, Symbol(i)); |
89 } | 106 } |
90 var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; | 107 |
| 108 var keys = [ |
| 109 -0, +0, 1, 1/3, 10, +Infinity, -Infinity, NaN, true, false, null, undefined, |
| 110 'x', Symbol(), {}, function(){} |
| 111 ]; |
91 for (var i = 0; i < keys.length; i++) { | 112 for (var i = 0; i < keys.length; i++) { |
92 TestSet(set, keys[i]); | 113 TestSet(set, keys[i]); |
93 } | 114 } |
94 } | 115 } |
95 TestSetBehavior(new Set); | 116 TestSetBehavior(new Set); |
96 TestSet(new WeakSet, new Object); | 117 TestSetBehavior(new WeakSet); |
97 | 118 |
98 | 119 |
99 // Test expected mapping behavior for Maps and WeakMaps | 120 // Test expected mapping behavior for Maps and WeakMaps |
100 function TestMapping(map, key, value) { | 121 function TestMapping(map, key, value) { |
101 assertSame(map, map.set(key, value)); | 122 assertFalse(map.has(key)); |
102 assertSame(value, map.get(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)); |
| 130 } |
| 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)); |
103 } | 136 } |
104 function TestMapBehavior1(m) { | 137 function TestMapBehavior(m) { |
| 138 // Fill |
105 TestMapping(m, new Object, 23); | 139 TestMapping(m, new Object, 23); |
106 TestMapping(m, new Object, 'the-value'); | 140 TestMapping(m, new Object, 'the-value'); |
107 TestMapping(m, new Object, new Object); | 141 TestMapping(m, new Object, new Object); |
108 } | |
109 TestMapBehavior1(new Map); | |
110 TestMapBehavior1(new WeakMap); | |
111 | |
112 | |
113 // Test expected mapping behavior for Maps only | |
114 function TestMapBehavior2(m) { | |
115 for (var i = 0; i < 20; i++) { | 142 for (var i = 0; i < 20; i++) { |
116 TestMapping(m, i, new Object); | 143 TestMapping(m, i, new Object); |
117 TestMapping(m, i / 10, new Object); | 144 TestMapping(m, i / 10, new Object); |
118 TestMapping(m, 'key-' + i, new Object); | 145 TestMapping(m, 'key-' + i, new Object); |
| 146 TestMapping(m, Symbol(i), new Object); |
119 } | 147 } |
120 // -0 is handled in TestMinusZeroMap | 148 |
121 var keys = [ 0, +Infinity, -Infinity, true, false, null, undefined ]; | 149 var keys = [ |
| 150 -0, +0, 1, 1/3, 10, +Infinity, -Infinity, NaN, true, false, null, undefined, |
| 151 'x', Symbol(), {}, function(){} |
| 152 ]; |
122 for (var i = 0; i < keys.length; i++) { | 153 for (var i = 0; i < keys.length; i++) { |
| 154 TestMapping(m, keys[i], 23); |
| 155 TestMapping(m, keys[i], 'the-value'); |
123 TestMapping(m, keys[i], new Object); | 156 TestMapping(m, keys[i], new Object); |
124 } | 157 } |
125 } | 158 } |
126 TestMapBehavior2(new Map); | 159 TestMapBehavior(new Map); |
| 160 TestMapBehavior(new WeakMap); |
127 | 161 |
128 | 162 |
129 // Test expected querying behavior of Maps and WeakMaps | 163 // Test expected querying behavior of Maps and WeakMaps |
130 function TestQuery(m) { | 164 function TestQuery(m) { |
131 var key = new Object; | 165 var key = new Object; |
132 var values = [ 'x', 0, +Infinity, -Infinity, true, false, null, undefined ]; | 166 var values = [ 'x', 0, +Infinity, -Infinity, true, false, null, undefined ]; |
133 for (var i = 0; i < values.length; i++) { | 167 for (var i = 0; i < values.length; i++) { |
134 TestMapping(m, key, values[i]); | 168 TestMapping(m, key, values[i]); |
135 assertTrue(m.has(key)); | |
136 assertFalse(m.has(new Object)); | |
137 } | 169 } |
138 } | 170 } |
139 TestQuery(new Map); | 171 TestQuery(new Map); |
140 TestQuery(new WeakMap); | 172 TestQuery(new WeakMap); |
141 | 173 |
142 | 174 |
143 // Test expected deletion behavior of Maps and WeakMaps | 175 // Test expected deletion behavior of Maps and WeakMaps |
144 function TestDelete(m) { | 176 function TestDelete(m) { |
145 var key = new Object; | 177 var key = new Object; |
146 TestMapping(m, key, 'to-be-deleted'); | 178 TestMapping(m, key, 'to-be-deleted'); |
147 assertTrue(m.delete(key)); | |
148 assertFalse(m.delete(key)); | 179 assertFalse(m.delete(key)); |
149 assertFalse(m.delete(new Object)); | 180 assertFalse(m.delete(new Object)); |
150 assertSame(m.get(key), undefined); | 181 assertSame(m.get(key), undefined); |
151 } | 182 } |
152 TestDelete(new Map); | 183 TestDelete(new Map); |
153 TestDelete(new WeakMap); | 184 TestDelete(new WeakMap); |
154 | 185 |
155 | 186 |
156 // Test GC of Maps and WeakMaps with entry | 187 // Test GC of Maps and WeakMaps with entry |
157 function TestGC1(m) { | 188 function TestGC1(m) { |
(...skipping 1278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1436 assertEquals(adderName + 'iterator', log); | 1467 assertEquals(adderName + 'iterator', log); |
1437 | 1468 |
1438 Object.defineProperty(ctor.prototype, adderName, { | 1469 Object.defineProperty(ctor.prototype, adderName, { |
1439 value: adderFunction | 1470 value: adderFunction |
1440 }); | 1471 }); |
1441 } | 1472 } |
1442 TestConstructorOrderOfAdderIterator(Map, 'set'); | 1473 TestConstructorOrderOfAdderIterator(Map, 'set'); |
1443 TestConstructorOrderOfAdderIterator(Set, 'add'); | 1474 TestConstructorOrderOfAdderIterator(Set, 'add'); |
1444 TestConstructorOrderOfAdderIterator(WeakMap, 'set'); | 1475 TestConstructorOrderOfAdderIterator(WeakMap, 'set'); |
1445 TestConstructorOrderOfAdderIterator(WeakSet, 'add'); | 1476 TestConstructorOrderOfAdderIterator(WeakSet, 'add'); |
OLD | NEW |