OLD | NEW |
| (Empty) |
1 // Copyright 2015 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-arrays | |
6 | |
7 function ArrayMaker(x) { | |
8 return x; | |
9 } | |
10 ArrayMaker.prototype = Array.prototype; | |
11 | |
12 var arrayConstructors = [ | |
13 Uint8Array, | |
14 Int8Array, | |
15 Uint16Array, | |
16 Int16Array, | |
17 Uint32Array, | |
18 Int32Array, | |
19 Uint8ClampedArray, | |
20 Float32Array, | |
21 Float64Array, | |
22 ArrayMaker // Also test arrays | |
23 ]; | |
24 | |
25 function assertArrayLikeEquals(value, expected, type) { | |
26 assertEquals(value.__proto__, type.prototype); | |
27 assertEquals(expected.length, value.length); | |
28 for (var i = 0; i < value.length; ++i) { | |
29 assertEquals(expected[i], value[i]); | |
30 } | |
31 } | |
32 | |
33 for (var constructor of arrayConstructors) { | |
34 // Test reversing both even and odd length arrays | |
35 var a = new constructor([1, 2, 3]); | |
36 assertArrayLikeEquals(a.reverse(), [3, 2, 1], constructor); | |
37 assertArrayLikeEquals(a, [3, 2, 1], constructor); | |
38 | |
39 a = new constructor([1, 2, 3, 4]); | |
40 assertArrayLikeEquals(a.reverse(), [4, 3, 2, 1], constructor); | |
41 assertArrayLikeEquals(a, [4, 3, 2, 1], constructor); | |
42 | |
43 if (constructor != ArrayMaker) { | |
44 // Cannot be called on objects which are not TypedArrays | |
45 assertThrows(function () { a.reverse.call({ length: 0 }); }, TypeError); | |
46 } else { | |
47 // Array.reverse works on array-like objects | |
48 var x = { length: 2, 1: 5 }; | |
49 a.reverse.call(x); | |
50 assertEquals(2, x.length); | |
51 assertFalse(Object.hasOwnProperty(x, '1')); | |
52 assertEquals(5, x[0]); | |
53 } | |
54 | |
55 assertEquals(0, a.reverse.length); | |
56 } | |
OLD | NEW |