OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var typedArrayConstructors = [ | 5 var intArrayConstructors = [ |
6 Uint8Array, | 6 Uint8Array, |
7 Int8Array, | 7 Int8Array, |
8 Uint16Array, | 8 Uint16Array, |
9 Int16Array, | 9 Int16Array, |
10 Uint32Array, | 10 Uint32Array, |
11 Int32Array, | 11 Int32Array, |
12 Uint8ClampedArray, | 12 Uint8ClampedArray |
| 13 ]; |
| 14 |
| 15 var floatArrayConstructors = [ |
13 Float32Array, | 16 Float32Array, |
14 Float64Array]; | 17 Float64Array |
| 18 ]; |
| 19 |
| 20 var typedArrayConstructors = [...intArrayConstructors, ...floatArrayConstructors
]; |
15 | 21 |
16 for (var constructor of typedArrayConstructors) { | 22 for (var constructor of typedArrayConstructors) { |
17 assertEquals(1, constructor.prototype.fill.length); | 23 assertEquals(1, constructor.prototype.fill.length); |
18 | 24 |
19 assertArrayEquals([], new constructor([]).fill(8)); | 25 assertArrayEquals([], new constructor([]).fill(8)); |
20 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8)); | 26 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8)); |
21 assertArrayEquals([0, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, 1)
); | 27 assertArrayEquals([0, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, 1)
); |
22 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 10
)); | 28 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 10
)); |
23 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, -5
)); | 29 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, -5
)); |
24 assertArrayEquals([0, 8, 8, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 1,
4)); | 30 assertArrayEquals([0, 8, 8, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 1,
4)); |
25 assertArrayEquals([0, 8, 8, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 1,
-1)); | 31 assertArrayEquals([0, 8, 8, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 1,
-1)); |
26 assertArrayEquals([0, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, 1,
42)); | 32 assertArrayEquals([0, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, 1,
42)); |
27 assertArrayEquals([0, 0, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, -3
, 42)); | 33 assertArrayEquals([0, 0, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, -3
, 42)); |
28 assertArrayEquals([0, 0, 8, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, -3
, 4)); | 34 assertArrayEquals([0, 0, 8, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, -3
, 4)); |
29 assertArrayEquals([0, 0, 0, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, -2
, -1)); | 35 assertArrayEquals([0, 0, 0, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, -2
, -1)); |
30 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(8, -1
, -3)); | 36 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(8, -1
, -3)); |
31 assertArrayEquals([8, 8, 8, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 0,
4)); | 37 assertArrayEquals([8, 8, 8, 8, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 0,
4)); |
32 | 38 |
33 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(8, In
finity)); | 39 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(8, In
finity)); |
34 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, -I
nfinity)); | 40 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, -I
nfinity)); |
35 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, 0,
Infinity)); | 41 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill(8, 0,
Infinity)); |
36 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 0,
-Infinity)); | 42 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(8, 0,
-Infinity)); |
37 | 43 |
38 // Test exceptions | 44 // Test exceptions |
39 assertThrows('constructor.prototype.fill.call(null)', TypeError); | 45 assertThrows('constructor.prototype.fill.call(null)', TypeError); |
40 assertThrows('constructor.prototype.fill.call(undefined)', TypeError); | 46 assertThrows('constructor.prototype.fill.call(undefined)', TypeError); |
41 assertThrows('constructor.prototype.fill.call([])', TypeError); | 47 assertThrows('constructor.prototype.fill.call([])', TypeError); |
42 | 48 |
| 49 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(false
)); |
| 50 assertArrayEquals([1, 1, 1, 1, 1], new constructor([0, 0, 0, 0, 0]).fill(true)
); |
| 51 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(null)
); |
| 52 assertArrayEquals([8, 8, 8, 8, 8], new constructor([0, 0, 0, 0, 0]).fill("8"))
; |
| 53 |
43 // Test ToNumber | 54 // Test ToNumber |
44 var s = ""; | 55 var s = ""; |
45 var p = new Proxy({}, {get(t,k) { s += k.toString() + '\n'; return Reflect.get
(t, k)}}) | 56 var p = new Proxy({}, {get(t,k) { s += k.toString() + '\n'; return Reflect.get
(t, k)}}) |
46 new constructor(3).fill(p); | 57 new constructor(3).fill(p); |
47 assertEquals(s, `Symbol(Symbol.toPrimitive) | 58 assertEquals(s, `Symbol(Symbol.toPrimitive) |
48 valueOf | 59 valueOf |
49 toString | 60 toString |
50 Symbol(Symbol.toStringTag) | 61 Symbol(Symbol.toStringTag) |
51 Symbol(Symbol.toPrimitive) | 62 Symbol(Symbol.toPrimitive) |
52 valueOf | 63 valueOf |
53 toString | 64 toString |
54 Symbol(Symbol.toStringTag) | 65 Symbol(Symbol.toStringTag) |
55 Symbol(Symbol.toPrimitive) | 66 Symbol(Symbol.toPrimitive) |
56 valueOf | 67 valueOf |
57 toString | 68 toString |
58 Symbol(Symbol.toStringTag) | 69 Symbol(Symbol.toStringTag) |
59 `); | 70 `); |
60 | 71 |
61 // Shadowing length doesn't affect fill, unlike Array.prototype.fill | 72 // Shadowing length doesn't affect fill, unlike Array.prototype.fill |
62 var a = new constructor([2, 2]); | 73 var a = new constructor([2, 2]); |
63 Object.defineProperty(a, 'length', {value: 1}); | 74 Object.defineProperty(a, 'length', {value: 1}); |
64 a.fill(3); | 75 a.fill(3); |
65 assertArrayEquals([3, 3], [a[0], a[1]]); | 76 assertArrayEquals([3, 3], [a[0], a[1]]); |
66 Array.prototype.fill.call(a, 4); | 77 Array.prototype.fill.call(a, 4); |
67 assertArrayEquals([4, 3], [a[0], a[1]]); | 78 assertArrayEquals([4, 3], [a[0], a[1]]); |
68 } | 79 } |
69 | 80 |
70 // Empty args | 81 for (var constructor of intArrayConstructors) { |
71 assertArrayEquals([0], new Uint8Array([0]).fill()); | 82 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill(undef
ined)); |
72 assertArrayEquals([NaN], new Float32Array([0]).fill()); | 83 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill()); |
| 84 assertArrayEquals([0, 0, 0, 0, 0], new constructor([0, 0, 0, 0, 0]).fill("abcd
")); |
| 85 } |
| 86 |
| 87 for (var constructor of floatArrayConstructors) { |
| 88 assertArrayEquals([NaN, NaN, NaN, NaN, NaN], new constructor([0, 0, 0, 0, 0]).
fill(undefined)); |
| 89 assertArrayEquals([NaN, NaN, NaN, NaN, NaN], new constructor([0, 0, 0, 0, 0]).
fill()); |
| 90 assertArrayEquals([NaN, NaN, NaN, NaN, NaN], new constructor([0, 0, 0, 0, 0]).
fill("abcd")); |
| 91 } |
73 | 92 |
74 // Clamping | 93 // Clamping |
75 assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(-
10)); | 94 assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(-
10)); |
76 assertArrayEquals([255, 255, 255, 255, 255], new Uint8ClampedArray([0, 0, 0, 0,
0]).fill(1000)); | 95 assertArrayEquals([255, 255, 255, 255, 255], new Uint8ClampedArray([0, 0, 0, 0,
0]).fill(1000)); |
77 | 96 |
78 assertArrayEquals([1, 1, 1, 1, 1], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0
.50001)); | 97 assertArrayEquals([1, 1, 1, 1, 1], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0
.50001)); |
79 assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0
.50000)); | 98 assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0
.50000)); |
80 assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0
.49999)); | 99 assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0
.49999)); |
81 // Check round half to even | 100 // Check round half to even |
82 assertArrayEquals([2, 2, 2, 2, 2], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(1
.50000)); | 101 assertArrayEquals([2, 2, 2, 2, 2], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(1
.50000)); |
83 assertArrayEquals([2, 2, 2, 2, 2], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(2
.50000)); | 102 assertArrayEquals([2, 2, 2, 2, 2], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(2
.50000)); |
84 assertArrayEquals([3, 3, 3, 3, 3], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(2
.50001)); | 103 assertArrayEquals([3, 3, 3, 3, 3], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(2
.50001)); |
85 // Check infinity clamping. | 104 // Check infinity clamping. |
86 assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(-
Infinity)); | 105 assertArrayEquals([0, 0, 0, 0, 0], new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(-
Infinity)); |
87 assertArrayEquals([255, 255, 255, 255, 255], new Uint8ClampedArray([0, 0, 0, 0,
0]).fill(Infinity)); | 106 assertArrayEquals([255, 255, 255, 255, 255], new Uint8ClampedArray([0, 0, 0, 0,
0]).fill(Infinity)); |
OLD | NEW |