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

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

Issue 8372027: Implement Harmony sets and maps. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 2 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 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --harmony-weakmaps --expose-gc 28 // Flags: --harmony-collections --harmony-weakmaps --expose-gc
29 29
30 30
31 // Test valid getter and setter calls 31 // Test valid getter and setter calls
32 var m = new WeakMap; 32 function TestValidCalls(m) {
33 assertDoesNotThrow(function () { m.get(new Object) }); 33 assertDoesNotThrow(function () { m.get(new Object) });
34 assertDoesNotThrow(function () { m.set(new Object) }); 34 assertDoesNotThrow(function () { m.set(new Object) });
35 assertDoesNotThrow(function () { m.has(new Object) }); 35 assertDoesNotThrow(function () { m.has(new Object) });
36 assertDoesNotThrow(function () { m.delete(new Object) }); 36 assertDoesNotThrow(function () { m.delete(new Object) });
37 }
38 TestValidCalls(new Map);
39 TestValidCalls(new WeakMap);
37 40
38 41
39 // Test invalid getter and setter calls 42 // Test invalid getter and setter calls (for WeakMap only)
40 var m = new WeakMap; 43 function TestInvalidCalls(m) {
41 assertThrows(function () { m.get(undefined) }, TypeError); 44 assertThrows(function () { m.get(undefined) }, TypeError);
42 assertThrows(function () { m.set(undefined, 0) }, TypeError); 45 assertThrows(function () { m.set(undefined, 0) }, TypeError);
43 assertThrows(function () { m.get(0) }, TypeError); 46 assertThrows(function () { m.get(0) }, TypeError);
44 assertThrows(function () { m.set(0, 0) }, TypeError); 47 assertThrows(function () { m.set(0, 0) }, TypeError);
45 assertThrows(function () { m.get('a-key') }, TypeError); 48 assertThrows(function () { m.get('a-key') }, TypeError);
46 assertThrows(function () { m.set('a-key', 0) }, TypeError); 49 assertThrows(function () { m.set('a-key', 0) }, TypeError);
50 }
51 TestInvalidCalls(new WeakMap);
47 52
48 53
49 // Test expected mapping behavior 54 // Test expected mapping behavior
50 var m = new WeakMap;
51 function TestMapping(map, key, value) { 55 function TestMapping(map, key, value) {
52 map.set(key, value); 56 map.set(key, value);
53 assertSame(value, map.get(key)); 57 assertSame(value, map.get(key));
54 } 58 }
55 TestMapping(m, new Object, 23); 59 function TestBehavior(m) {
56 TestMapping(m, new Object, 'the-value'); 60 TestMapping(m, new Object, 23);
57 TestMapping(m, new Object, new Object); 61 TestMapping(m, new Object, 'the-value');
62 TestMapping(m, new Object, new Object);
63 }
64 TestBehavior(new Map);
65 TestBehavior(new WeakMap);
58 66
59 67
60 // Test expected querying behavior 68 // Test expected querying behavior
61 var m = new WeakMap; 69 function TestQuery(m) {
62 var key = new Object; 70 var key = new Object;
63 TestMapping(m, key, 'to-be-present'); 71 TestMapping(m, key, 'to-be-present');
64 assertTrue(m.has(key)); 72 assertTrue(m.has(key));
65 assertFalse(m.has(new Object)); 73 assertFalse(m.has(new Object));
66 TestMapping(m, key, undefined); 74 TestMapping(m, key, undefined);
67 assertFalse(m.has(key)); 75 assertFalse(m.has(key));
68 assertFalse(m.has(new Object)); 76 assertFalse(m.has(new Object));
77 }
78 TestQuery(new Map);
79 TestQuery(new WeakMap);
69 80
70 81
71 // Test expected deletion behavior 82 // Test expected deletion behavior
72 var m = new WeakMap; 83 function TestDelete(m) {
73 var key = new Object; 84 var key = new Object;
74 TestMapping(m, key, 'to-be-deleted'); 85 TestMapping(m, key, 'to-be-deleted');
75 assertTrue(m.delete(key)); 86 assertTrue(m.delete(key));
76 assertFalse(m.delete(key)); 87 assertFalse(m.delete(key));
77 assertFalse(m.delete(new Object)); 88 assertFalse(m.delete(new Object));
78 assertSame(m.get(key), undefined); 89 assertSame(m.get(key), undefined);
90 }
91 TestDelete(new Map);
92 TestDelete(new WeakMap);
79 93
80 94
81 // Test GC of map with entry 95 // Test GC of map with entry
82 var m = new WeakMap; 96 function TestGC1(m) {
83 var key = new Object; 97 var key = new Object;
84 m.set(key, 'not-collected'); 98 m.set(key, 'not-collected');
85 gc(); 99 gc();
86 assertSame('not-collected', m.get(key)); 100 assertSame('not-collected', m.get(key));
101 }
102 TestGC1(new Map);
103 TestGC1(new WeakMap);
87 104
88 105
89 // Test GC of map with chained entries 106 // Test GC of map with chained entries
90 var m = new WeakMap; 107 function TestGC2(m) {
91 var head = new Object; 108 var head = new Object;
92 for (key = head, i = 0; i < 10; i++, key = m.get(key)) { 109 for (key = head, i = 0; i < 10; i++, key = m.get(key)) {
93 m.set(key, new Object); 110 m.set(key, new Object);
111 }
112 gc();
113 var count = 0;
114 for (key = head; key != undefined; key = m.get(key)) {
115 count++;
116 }
117 assertEquals(11, count);
94 } 118 }
95 gc(); 119 TestGC2(new Map);
96 var count = 0; 120 TestGC2(new WeakMap);
97 for (key = head; key != undefined; key = m.get(key)) {
98 count++;
99 }
100 assertEquals(11, count);
101 121
102 122
103 // Test property attribute [[Enumerable]] 123 // Test property attribute [[Enumerable]]
104 var m = new WeakMap; 124 function TestEnumerable(m) {
105 function props(x) { 125 function props(x) {
106 var array = []; 126 var array = [];
107 for (var p in x) array.push(p); 127 for (var p in x) array.push(p);
108 return array.sort(); 128 return array.sort();
129 }
130 assertArrayEquals([], props(WeakMap));
131 assertArrayEquals([], props(WeakMap.prototype));
132 assertArrayEquals([], props(m));
109 } 133 }
110 assertArrayEquals([], props(WeakMap)); 134 TestEnumerable(new Map);
111 assertArrayEquals([], props(WeakMap.prototype)); 135 TestEnumerable(new WeakMap);
112 assertArrayEquals([], props(m));
113 136
114 137
115 // Test arbitrary properties on weak maps 138 // Test arbitrary properties on weak maps
116 var m = new WeakMap; 139 function TestArbitrary(m) {
117 function TestProperty(map, property, value) { 140 function TestProperty(map, property, value) {
118 map[property] = value; 141 map[property] = value;
119 assertEquals(value, map[property]); 142 assertEquals(value, map[property]);
143 }
144 for (i = 0; i < 20; i++) {
145 TestProperty(m, i, 'val' + i);
146 TestProperty(m, 'foo' + i, 'bar' + i);
147 }
148 TestMapping(m, new Object, 'foobar');
120 } 149 }
121 for (i = 0; i < 20; i++) { 150 TestArbitrary(new Map);
122 TestProperty(m, i, 'val' + i); 151 TestArbitrary(new WeakMap);
123 TestProperty(m, 'foo' + i, 'bar' + i);
124 }
125 TestMapping(m, new Object, 'foobar');
126 152
127 153
128 // Test direct constructor call 154 // Test direct constructor call
129 var m = WeakMap(); 155 var m = WeakMap();
130 assertTrue(m instanceof WeakMap); 156 assertTrue(m instanceof WeakMap);
131 157
132 158
133 // Test some common JavaScript idioms 159 // Test some common JavaScript idioms
134 var m = new WeakMap; 160 var m = new WeakMap;
135 assertTrue(m instanceof WeakMap); 161 assertTrue(m instanceof WeakMap);
(...skipping 22 matching lines...) Expand all
158 configurable: true, 184 configurable: true,
159 writable: true 185 writable: true
160 }}); 186 }});
161 assertEquals(10, o.myValue); 187 assertEquals(10, o.myValue);
162 188
163 189
164 // Stress Test 190 // Stress Test
165 // There is a proposed stress-test available at the es-discuss mailing list 191 // There is a proposed stress-test available at the es-discuss mailing list
166 // which cannot be reasonably automated. Check it out by hand if you like: 192 // which cannot be reasonably automated. Check it out by hand if you like:
167 // https://mail.mozilla.org/pipermail/es-discuss/2011-May/014096.html 193 // https://mail.mozilla.org/pipermail/es-discuss/2011-May/014096.html
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698