OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 assertEquals(37, x[0], "longlength first"); | 202 assertEquals(37, x[0], "longlength first"); |
203 assertEquals(42, x[1], "longlength second"); | 203 assertEquals(42, x[1], "longlength second"); |
204 assertFalse(2 in x,"longlength third"); | 204 assertFalse(2 in x,"longlength third"); |
205 } | 205 } |
206 | 206 |
207 TestNonArrayLongerLength(4); | 207 TestNonArrayLongerLength(4); |
208 TestNonArrayLongerLength(10); | 208 TestNonArrayLongerLength(10); |
209 TestNonArrayLongerLength(1000); | 209 TestNonArrayLongerLength(1000); |
210 TestNonArrayLongerLength(500000); | 210 TestNonArrayLongerLength(500000); |
211 TestNonArrayLongerLength(Math.pow(2,32) - 1); | 211 TestNonArrayLongerLength(Math.pow(2,32) - 1); |
| 212 |
| 213 |
| 214 function TestInheritedElementSort(depth) { |
| 215 var length = depth * 2 + 3; |
| 216 var obj = {length: length}; |
| 217 obj[depth * 2 + 1] = 0; |
| 218 for (var i = 0; i < depth; i++) { |
| 219 var newObj = {}; |
| 220 newObj.__proto__ = obj; |
| 221 obj[i] = undefined; |
| 222 obj[i + depth + 1] = depth - i; |
| 223 obj = newObj; |
| 224 } |
| 225 // expected (inherited) object: [undef1,...undefdepth,hole,1,...,depth,0,hole] |
| 226 |
| 227 Array.prototype.sort.call(obj, function(a,b) { return (b < a) - (a < b); }); |
| 228 // expected result: [0,1,...,depth,undef1,...,undefdepth,undef,hole] |
| 229 var name = "SortInherit("+depth+")-"; |
| 230 |
| 231 assertEquals(length, obj.length, name+"length"); |
| 232 for (var i = 0; i <= depth; i++) { |
| 233 assertTrue(obj.hasOwnProperty(i), name + "hasvalue" + i); |
| 234 assertEquals(i, obj[i], name + "value" + i); |
| 235 } |
| 236 for (var i = depth + 1; i <= depth * 2 + 1; i++) { |
| 237 assertEquals(undefined, obj[i], name + "undefined" + i); |
| 238 assertTrue(obj.hasOwnProperty(i), name + "hasundefined" + i); |
| 239 } |
| 240 assertTrue(!obj.hasOwnProperty(depth * 2 + 2), name + "hashole"); |
| 241 } |
| 242 |
| 243 TestInheritedElementSort(5); |
| 244 TestInheritedElementSort(15); |
| 245 |
| 246 function TestSparseInheritedElementSort(scale) { |
| 247 var length = scale * 10; |
| 248 var x = {length: length}; |
| 249 var y = {}; |
| 250 y.__proto__ = x; |
| 251 |
| 252 for (var i = 0; i < 5; i++) { |
| 253 x[i * 2 * scale] = 2 * (4 - i); |
| 254 y[(i * 2 + 1) * scale] = 2 * (4 - i) + 1; |
| 255 } |
| 256 |
| 257 var name = "SparseSortInherit(" + scale + ")-"; |
| 258 |
| 259 Array.prototype.sort.call(y); |
| 260 |
| 261 assertEquals(length, y.length, name+"length"); |
| 262 |
| 263 for (var i = 0; i < 10; i++) { |
| 264 assertTrue(y.hasOwnProperty(i), name + "hasvalue" + i); |
| 265 assertEquals(i, y[i], name + "value" + i); |
| 266 } |
| 267 for (var i = 10; i < length; i++) { |
| 268 assertEquals(x.hasOwnProperty(i), y.hasOwnProperty(i), name+"hasundef"+i); |
| 269 assertEquals(undefined, y[i], name+"undefined"+i); |
| 270 if (x.hasOwnProperty(i)) { |
| 271 assertTrue(0 == i % (2 * scale), name+"new_x"+i); |
| 272 } |
| 273 } |
| 274 } |
| 275 |
| 276 TestSparseInheritedElementSort(10); |
| 277 TestSparseInheritedElementSort(100); |
| 278 TestSparseInheritedElementSort(1000); |
| 279 TestSparseInheritedElementSort(10000); |
| 280 |
| 281 function TestSpecialCasesInheritedElementSort() { |
| 282 |
| 283 var x = { |
| 284 1:"d1", |
| 285 2:"c1", |
| 286 3:"b1", |
| 287 4: undefined, |
| 288 __proto__: { |
| 289 length: 10000, |
| 290 1: "e2", |
| 291 10: "a2", |
| 292 100: "b2", |
| 293 1000: "c2", |
| 294 2000: undefined, |
| 295 8000: "d2", |
| 296 12000: "XX", |
| 297 __proto__: { |
| 298 0: "e3", |
| 299 1: "d3", |
| 300 2: "c3", |
| 301 3: "b3", |
| 302 4: "f3", |
| 303 5: "a3", |
| 304 6: undefined, |
| 305 } |
| 306 } |
| 307 }; |
| 308 Array.prototype.sort.call(x); |
| 309 |
| 310 var name = "SpecialInherit-"; |
| 311 |
| 312 assertEquals(10000, x.length, name + "length"); |
| 313 var sorted = ["a2", "a3", "b1", "b2", "c1", "c2", "d1", "d2", "e3", |
| 314 undefined, undefined, undefined]; |
| 315 for (var i = 0; i < sorted.length; i++) { |
| 316 assertTrue(x[0], x.hasOwnProperty(i) + "has" + i) |
| 317 assertEquals(sorted[i], x[i], name + i); |
| 318 } |
| 319 assertFalse(x.hasOwnProperty(sorted.length), name + "haspost"); |
| 320 assertFalse(sorted.length in x, name + "haspost2"); |
| 321 assertEquals(undefined, x[12000], name + "XX12000"); |
| 322 |
| 323 assertTrue(x.hasOwnProperty(10), name + "hasundefined10"); |
| 324 assertEquals(undefined, x[10], name + "undefined10"); |
| 325 assertTrue(x.hasOwnProperty(100), name + "hasundefined100"); |
| 326 assertEquals(undefined, x[100], name + "undefined100"); |
| 327 assertTrue(x.hasOwnProperty(1000), name + "hasundefined1000"); |
| 328 assertEquals(undefined, x[1000], name + "undefined1000"); |
| 329 assertTrue(x.hasOwnProperty(2000), name + "hasundefined2000"); |
| 330 assertEquals(undefined, x[2000], name + "undefined2000"); |
| 331 assertTrue(x.hasOwnProperty(8000), name + "hasundefined8000"); |
| 332 assertEquals(undefined, x[8000], name + "undefined8000"); |
| 333 |
| 334 assertFalse(x.hasOwnProperty(11), name + "hasundefined11"); |
| 335 assertEquals(undefined, x[11], name + "undefined11"); |
| 336 |
| 337 assertFalse(x.hasOwnProperty(12000), name + "has12000"); |
| 338 assertEquals("XX", x[12000], name + "XX12000"); |
| 339 |
| 340 } |
OLD | NEW |