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

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

Issue 8372027: Implement Harmony sets and maps. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments by Andreas Rossberg. Created 9 years, 1 month 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
(Empty)
1 // Copyright 2011 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
4 // met:
5 //
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.
15 //
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.
27
28 // Flags: --harmony-collections --expose-gc
29
30
31 // Test valid getter and setter calls on Sets.
32 function TestValidSetCalls(m) {
33 assertDoesNotThrow(function () { m.add(new Object) });
34 assertDoesNotThrow(function () { m.has(new Object) });
35 assertDoesNotThrow(function () { m.delete(new Object) });
36 }
37 TestValidSetCalls(new Set);
38
39
40 // Test valid getter and setter calls on Maps and WeakMaps
41 function TestValidMapCalls(m) {
42 assertDoesNotThrow(function () { m.get(new Object) });
43 assertDoesNotThrow(function () { m.set(new Object) });
44 assertDoesNotThrow(function () { m.has(new Object) });
45 assertDoesNotThrow(function () { m.delete(new Object) });
46 }
47 TestValidMapCalls(new Map);
48 TestValidMapCalls(new WeakMap);
49
50
51 // Test invalid getter and setter calls for WeakMap only
52 function TestInvalidCalls(m) {
53 assertThrows(function () { m.get(undefined) }, TypeError);
54 assertThrows(function () { m.set(undefined, 0) }, TypeError);
55 assertThrows(function () { m.get(0) }, TypeError);
56 assertThrows(function () { m.set(0, 0) }, TypeError);
57 assertThrows(function () { m.get('a-key') }, TypeError);
58 assertThrows(function () { m.set('a-key', 0) }, TypeError);
59 }
60 TestInvalidCalls(new WeakMap);
61
62
63 // Test expected behavior for Sets
64 function TestSet(set, key) {
65 assertFalse(set.has(key));
66 set.add(key);
67 assertTrue(set.has(key));
68 set.delete(key);
69 assertFalse(set.has(key));
70 }
71 function TestSetBehavior(set) {
72 for (i = 0; i < 20; i++) {
arv (Not doing code reviews) 2011/10/26 17:09:44 missing var
Michael Starzinger 2011/10/27 15:36:05 Done (will include in next CL).
73 TestSet(set, new Object);
74 }
75 }
76 TestSet(new Set, 23);
77 TestSet(new Set, 'foo');
78 TestSetBehavior(new Set);
79
80
81 // Test expected mapping behavior for Maps and WeakMaps
82 function TestMapping(map, key, value) {
83 map.set(key, value);
84 assertSame(value, map.get(key));
85 }
86 function TestMapBehavior1(m) {
87 TestMapping(m, new Object, 23);
88 TestMapping(m, new Object, 'the-value');
89 TestMapping(m, new Object, new Object);
90 }
91 TestMapBehavior1(new Map);
92 TestMapBehavior1(new WeakMap);
93
94
95 // Test expected mapping behavior for Maps only
96 function TestMapBehavior2(m) {
97 for (var i = 0; i < 20; i++) {
98 TestMapping(m, i, new Object);
99 TestMapping(m, i / 10, new Object);
100 TestMapping(m, 'key-' + i, new Object);
101 }
102 var keys = [ +0, -0, +Infinity, -Infinity, true, false ];
arv (Not doing code reviews) 2011/10/26 17:09:44 is it worth testing NaN, undefined and null too?
Michael Starzinger 2011/10/27 15:36:05 Currently "undefined" and "null" cannot be used as
103 for (var i = 0; i < keys.length; i++) {
104 TestMapping(m, keys[i], new Object);
105 }
106 }
107 TestMapBehavior2(new Map);
108
109
110 // Test expected querying behavior of Maps and WeakMaps
111 function TestQuery(m) {
112 var key = new Object;
113 TestMapping(m, key, 'to-be-present');
114 assertTrue(m.has(key));
115 assertFalse(m.has(new Object));
116 TestMapping(m, key, undefined);
117 assertFalse(m.has(key));
118 assertFalse(m.has(new Object));
119 }
120 TestQuery(new Map);
121 TestQuery(new WeakMap);
122
123
124 // Test expected deletion behavior of Maps and WeakMaps
125 function TestDelete(m) {
126 var key = new Object;
127 TestMapping(m, key, 'to-be-deleted');
128 assertTrue(m.delete(key));
129 assertFalse(m.delete(key));
130 assertFalse(m.delete(new Object));
131 assertSame(m.get(key), undefined);
132 }
133 TestDelete(new Map);
134 TestDelete(new WeakMap);
135
136
137 // Test GC of Maps and WeakMaps with entry
138 function TestGC1(m) {
139 var key = new Object;
140 m.set(key, 'not-collected');
141 gc();
142 assertSame('not-collected', m.get(key));
143 }
144 TestGC1(new Map);
145 TestGC1(new WeakMap);
146
147
148 // Test GC of Maps and WeakMaps with chained entries
149 function TestGC2(m) {
150 var head = new Object;
151 for (key = head, i = 0; i < 10; i++, key = m.get(key)) {
152 m.set(key, new Object);
153 }
154 gc();
155 var count = 0;
156 for (key = head; key != undefined; key = m.get(key)) {
157 count++;
158 }
159 assertEquals(11, count);
160 }
161 TestGC2(new Map);
162 TestGC2(new WeakMap);
163
164
165 // Test property attribute [[Enumerable]]
166 function TestEnumerable(func) {
167 function props(x) {
168 var array = [];
169 for (var p in x) array.push(p);
170 return array.sort();
171 }
172 assertArrayEquals([], props(func));
173 assertArrayEquals([], props(func.prototype));
174 assertArrayEquals([], props(new func()));
175 }
176 TestEnumerable(Set);
177 TestEnumerable(Map);
178 TestEnumerable(WeakMap);
179
180
181 // Test arbitrary properties on Maps and WeakMaps
182 function TestArbitrary(m) {
183 function TestProperty(map, property, value) {
184 map[property] = value;
185 assertEquals(value, map[property]);
186 }
187 for (i = 0; i < 20; i++) {
arv (Not doing code reviews) 2011/10/26 17:09:44 var
Michael Starzinger 2011/10/27 15:36:05 Done (will include in next CL).
188 TestProperty(m, i, 'val' + i);
189 TestProperty(m, 'foo' + i, 'bar' + i);
190 }
191 TestMapping(m, new Object, 'foobar');
192 }
193 TestArbitrary(new Map);
194 TestArbitrary(new WeakMap);
195
196
197 // Test direct constructor call
198 assertTrue(Set() instanceof Set);
199 assertTrue(Map() instanceof Map);
200 assertTrue(WeakMap() instanceof WeakMap);
201
202
203 // Test whether NaN values as keys are treated correctly.
204 var s = new Set;
205 assertFalse(s.has(NaN));
206 assertFalse(s.has(NaN + 1));
207 assertFalse(s.has(23));
208 s.add(NaN);
209 assertTrue(s.has(NaN));
210 assertTrue(s.has(NaN + 1));
211 assertFalse(s.has(23));
212 var m = new Map;
213 assertFalse(m.has(NaN));
214 assertFalse(m.has(NaN + 1));
215 assertFalse(m.has(23));
216 m.set(NaN, 'a-value');
217 assertTrue(m.has(NaN));
218 assertTrue(m.has(NaN + 1));
219 assertFalse(m.has(23));
220
221
222 // Test some common JavaScript idioms for Sets
223 var s = new Set;
224 assertTrue(s instanceof Set);
225 assertTrue(Set.prototype.add instanceof Function)
226 assertTrue(Set.prototype.has instanceof Function)
227 assertTrue(Set.prototype.delete instanceof Function)
228
229
230 // Test some common JavaScript idioms for Maps
231 var m = new Map;
232 assertTrue(m instanceof Map);
233 assertTrue(Map.prototype.set instanceof Function)
234 assertTrue(Map.prototype.get instanceof Function)
235 assertTrue(Map.prototype.has instanceof Function)
236 assertTrue(Map.prototype.delete instanceof Function)
237
238
239 // Test some common JavaScript idioms for WeakMaps
240 var m = new WeakMap;
241 assertTrue(m instanceof WeakMap);
242 assertTrue(WeakMap.prototype.set instanceof Function)
243 assertTrue(WeakMap.prototype.get instanceof Function)
244 assertTrue(WeakMap.prototype.has instanceof Function)
245 assertTrue(WeakMap.prototype.delete instanceof Function)
246
247
248 // Regression test for WeakMap prototype.
249 assertTrue(WeakMap.prototype.constructor === WeakMap)
250 assertTrue(Object.getPrototypeOf(WeakMap.prototype) === Object.prototype)
251
252
253 // Regression test for issue 1617: The prototype of the WeakMap constructor
254 // needs to be unique (i.e. different from the one of the Object constructor).
255 assertFalse(WeakMap.prototype === Object.prototype);
256 var o = Object.create({});
257 assertFalse("get" in o);
258 assertFalse("set" in o);
259 assertEquals(undefined, o.get);
260 assertEquals(undefined, o.set);
261 var o = Object.create({}, { myValue: {
262 value: 10,
263 enumerable: false,
264 configurable: true,
265 writable: true
266 }});
267 assertEquals(10, o.myValue);
268
269
270 // Stress Test
271 // There is a proposed stress-test available at the es-discuss mailing list
272 // which cannot be reasonably automated. Check it out by hand if you like:
273 // https://mail.mozilla.org/pipermail/es-discuss/2011-May/014096.html
OLDNEW
« src/objects.h ('K') | « src/weakmap.js ('k') | test/mjsunit/harmony/proxies-hash.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698