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 |
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-collections | 28 // Flags: --harmony-collections --harmony-iteration |
29 // Flags: --expose-gc --allow-natives-syntax | 29 // Flags: --expose-gc --allow-natives-syntax |
30 | 30 |
31 | 31 |
32 // Test valid getter and setter calls on Sets and WeakSets | 32 // Test valid getter and setter calls on Sets and WeakSets |
33 function TestValidSetCalls(m) { | 33 function TestValidSetCalls(m) { |
34 assertDoesNotThrow(function () { m.add(new Object) }); | 34 assertDoesNotThrow(function () { m.add(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 } | 37 } |
38 TestValidSetCalls(new Set); | 38 TestValidSetCalls(new Set); |
(...skipping 941 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
980 | 980 |
981 if (v === 4) { | 981 if (v === 4) { |
982 for (var i = 5; i < 16; i++) { | 982 for (var i = 5; i < 16; i++) { |
983 map.delete(i); | 983 map.delete(i); |
984 } | 984 } |
985 } | 985 } |
986 }); | 986 }); |
987 | 987 |
988 assertArrayEquals([0, 1, 2, 3, 4], buffer); | 988 assertArrayEquals([0, 1, 2, 3, 4], buffer); |
989 })(); | 989 })(); |
| 990 |
| 991 |
| 992 (function TestSetConstructor() { |
| 993 var s = new Set(null); |
| 994 assertEquals(s.size, 0); |
| 995 |
| 996 s = new Set(undefined); |
| 997 assertEquals(s.size, 0); |
| 998 |
| 999 // No @@iterator |
| 1000 assertThrows(function() { |
| 1001 new Set({}); |
| 1002 }, TypeError); |
| 1003 |
| 1004 // @@iterator not callable |
| 1005 assertThrows(function() { |
| 1006 var object = {}; |
| 1007 object[Symbol.iterator] = 42; |
| 1008 new Set(object); |
| 1009 }, TypeError); |
| 1010 |
| 1011 // @@iterator result not object |
| 1012 assertThrows(function() { |
| 1013 var object = {}; |
| 1014 object[Symbol.iterator] = function() { |
| 1015 return 42; |
| 1016 }; |
| 1017 new Set(object); |
| 1018 }, TypeError); |
| 1019 |
| 1020 var s2 = new Set(); |
| 1021 s2.add('a'); |
| 1022 s2.add('b'); |
| 1023 s2.add('c'); |
| 1024 s = new Set(s2.values()); |
| 1025 assertEquals(s.size, 3); |
| 1026 assertTrue(s.has('a')); |
| 1027 assertTrue(s.has('b')); |
| 1028 assertTrue(s.has('c')); |
| 1029 })(); |
| 1030 |
| 1031 |
| 1032 (function TestSetConstructorAddNotCallable() { |
| 1033 var originalSetPrototypeAdd = Set.prototype.add; |
| 1034 assertThrows(function() { |
| 1035 Set.prototype.add = 42; |
| 1036 new Set([1, 2].values()); |
| 1037 }, TypeError); |
| 1038 Set.prototype.add = originalSetPrototypeAdd; |
| 1039 })(); |
| 1040 |
| 1041 |
| 1042 (function TestSetConstructorGetAddOnce() { |
| 1043 var originalSetPrototypeAdd = Set.prototype.add; |
| 1044 var getAddCount = 0; |
| 1045 Object.defineProperty(Set.prototype, 'add', { |
| 1046 get: function() { |
| 1047 getAddCount++; |
| 1048 return function() {}; |
| 1049 } |
| 1050 }); |
| 1051 var s = new Set([1, 2].values()); |
| 1052 assertEquals(getAddCount, 1); |
| 1053 assertEquals(s.size, 0); |
| 1054 Object.defineProperty(Set.prototype, 'add', { |
| 1055 value: originalSetPrototypeAdd, |
| 1056 writable: true |
| 1057 }); |
| 1058 })(); |
| 1059 |
| 1060 |
| 1061 (function TestSetConstructorAddReplaced() { |
| 1062 var originalSetPrototypeAdd = Set.prototype.add; |
| 1063 var addCount = 0; |
| 1064 Set.prototype.add = function(value) { |
| 1065 addCount++; |
| 1066 originalSetPrototypeAdd.call(this, value); |
| 1067 Set.prototype.add = null; |
| 1068 }; |
| 1069 var s = new Set([1, 2].values()); |
| 1070 assertEquals(addCount, 2); |
| 1071 assertEquals(s.size, 2); |
| 1072 Set.prototype.add = originalSetPrototypeAdd; |
| 1073 })(); |
| 1074 |
| 1075 |
| 1076 (function TestSetConstructorOrderOfDoneValue() { |
| 1077 var valueCount = 0, doneCount = 0; |
| 1078 var iterator = { |
| 1079 next: function() { |
| 1080 return { |
| 1081 get value() { |
| 1082 valueCount++; |
| 1083 }, |
| 1084 get done() { |
| 1085 doneCount++; |
| 1086 throw new Error(); |
| 1087 } |
| 1088 }; |
| 1089 } |
| 1090 }; |
| 1091 iterator[Symbol.iterator] = function() { |
| 1092 return this; |
| 1093 }; |
| 1094 assertThrows(function() { |
| 1095 new Set(iterator); |
| 1096 }); |
| 1097 assertEquals(doneCount, 1); |
| 1098 assertEquals(valueCount, 0); |
| 1099 })(); |
| 1100 |
| 1101 |
| 1102 (function TestSetConstructorNextNotAnObject() { |
| 1103 var iterator = { |
| 1104 next: function() { |
| 1105 return 'abc'; |
| 1106 } |
| 1107 }; |
| 1108 iterator[Symbol.iterator] = function() { |
| 1109 return this; |
| 1110 }; |
| 1111 assertThrows(function() { |
| 1112 new Set(iterator); |
| 1113 }, TypeError); |
| 1114 })(); |
| 1115 |
| 1116 |
| 1117 (function TestMapConstructor() { |
| 1118 var m = new Map(null); |
| 1119 assertEquals(m.size, 0); |
| 1120 |
| 1121 m = new Map(undefined); |
| 1122 assertEquals(m.size, 0); |
| 1123 |
| 1124 // No @@iterator |
| 1125 assertThrows(function() { |
| 1126 new Map({}); |
| 1127 }, TypeError); |
| 1128 |
| 1129 // @@iterator not callable |
| 1130 assertThrows(function() { |
| 1131 var object = {}; |
| 1132 object[Symbol.iterator] = 42; |
| 1133 new Map(object); |
| 1134 }, TypeError); |
| 1135 |
| 1136 // @@iterator result not object |
| 1137 assertThrows(function() { |
| 1138 var object = {}; |
| 1139 object[Symbol.iterator] = function() { |
| 1140 return 42; |
| 1141 }; |
| 1142 new Map(object); |
| 1143 }, TypeError); |
| 1144 |
| 1145 var m2 = new Map(); |
| 1146 m2.set(0, 'a'); |
| 1147 m2.set(1, 'b'); |
| 1148 m2.set(2, 'c'); |
| 1149 m = new Map(m2.entries()); |
| 1150 assertEquals(m.size, 3); |
| 1151 assertEquals(m.get(0), 'a'); |
| 1152 assertEquals(m.get(1), 'b'); |
| 1153 assertEquals(m.get(2), 'c'); |
| 1154 })(); |
| 1155 |
| 1156 |
| 1157 (function TestMapConstructorSetNotCallable() { |
| 1158 var originalMapPrototypeSet = Map.prototype.set; |
| 1159 assertThrows(function() { |
| 1160 Map.prototype.set = 42; |
| 1161 new Map([1, 2].entries()); |
| 1162 }, TypeError); |
| 1163 Map.prototype.set = originalMapPrototypeSet; |
| 1164 })(); |
| 1165 |
| 1166 |
| 1167 (function TestMapConstructorGetAddOnce() { |
| 1168 var originalMapPrototypeSet = Map.prototype.set; |
| 1169 var getSetCount = 0; |
| 1170 Object.defineProperty(Map.prototype, 'set', { |
| 1171 get: function() { |
| 1172 getSetCount++; |
| 1173 return function() {}; |
| 1174 } |
| 1175 }); |
| 1176 var m = new Map([1, 2].entries()); |
| 1177 assertEquals(getSetCount, 1); |
| 1178 assertEquals(m.size, 0); |
| 1179 Object.defineProperty(Map.prototype, 'set', { |
| 1180 value: originalMapPrototypeSet, |
| 1181 writable: true |
| 1182 }); |
| 1183 })(); |
| 1184 |
| 1185 |
| 1186 (function TestMapConstructorSetReplaced() { |
| 1187 var originalMapPrototypeSet = Map.prototype.set; |
| 1188 var setCount = 0; |
| 1189 Map.prototype.set = function(key, value) { |
| 1190 setCount++; |
| 1191 originalMapPrototypeSet.call(this, key, value); |
| 1192 Map.prototype.set = null; |
| 1193 }; |
| 1194 var m = new Map([1, 2].entries()); |
| 1195 assertEquals(setCount, 2); |
| 1196 assertEquals(m.size, 2); |
| 1197 Map.prototype.set = originalMapPrototypeSet; |
| 1198 })(); |
| 1199 |
| 1200 |
| 1201 (function TestMapConstructorOrderOfDoneValue() { |
| 1202 var valueCount = 0, doneCount = 0; |
| 1203 function FakeError() {} |
| 1204 var iterator = { |
| 1205 next: function() { |
| 1206 return { |
| 1207 get value() { |
| 1208 valueCount++; |
| 1209 }, |
| 1210 get done() { |
| 1211 doneCount++; |
| 1212 throw new FakeError(); |
| 1213 } |
| 1214 }; |
| 1215 } |
| 1216 }; |
| 1217 iterator[Symbol.iterator] = function() { |
| 1218 return this; |
| 1219 }; |
| 1220 assertThrows(function() { |
| 1221 new Map(iterator); |
| 1222 }, FakeError); |
| 1223 assertEquals(doneCount, 1); |
| 1224 assertEquals(valueCount, 0); |
| 1225 })(); |
| 1226 |
| 1227 |
| 1228 (function TestMapConstructorNextNotAnObject() { |
| 1229 var iterator = { |
| 1230 next: function() { |
| 1231 return 'abc'; |
| 1232 } |
| 1233 }; |
| 1234 iterator[Symbol.iterator] = function() { |
| 1235 return this; |
| 1236 }; |
| 1237 assertThrows(function() { |
| 1238 new Map(iterator); |
| 1239 }, TypeError); |
| 1240 })(); |
| 1241 |
| 1242 |
| 1243 (function TestMapConstructorIteratorNotObjectValues() { |
| 1244 assertThrows(function() { |
| 1245 new Map([1, 2].values()); |
| 1246 }, TypeError); |
| 1247 })(); |
OLD | NEW |