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 typedArrayConstructors = [ |
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 Float32Array, | 13 Float32Array, |
14 Float64Array]; | 14 Float64Array]; |
15 | 15 |
16 for (var constructor of typedArrayConstructors) { | 16 for (var constructor of typedArrayConstructors) { |
17 assertEquals(1, constructor.prototype.fill.length); | 17 assertEquals(1, constructor.prototype.fill.length); |
18 | 18 |
19 assertArrayEquals(new constructor([0]).fill(), [0]); | |
rongjie
2017/03/14 11:17:15
This line fails with "Failure (array element at in
rongjie
2017/03/14 12:54:01
Test fixed.
| |
19 assertArrayEquals(new constructor([]).fill(8), []); | 20 assertArrayEquals(new constructor([]).fill(8), []); |
20 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8), [8, 8, 8, 8, 8]); | 21 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8), [8, 8, 8, 8, 8]); |
21 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 1), [0, 8, 8, 8, 8] ); | 22 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 1), [0, 8, 8, 8, 8] ); |
22 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 10), [0, 0, 0, 0, 0 ]); | 23 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 10), [0, 0, 0, 0, 0 ]); |
23 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -5), [8, 8, 8, 8, 8 ]); | 24 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -5), [8, 8, 8, 8, 8 ]); |
24 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 1, 4), [0, 8, 8, 8, 0]); | 25 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 1, 4), [0, 8, 8, 8, 0]); |
25 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 1, -1), [0, 8, 8, 8 , 0]); | 26 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 1, -1), [0, 8, 8, 8 , 0]); |
26 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 1, 42), [0, 8, 8, 8 , 8]); | 27 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 1, 42), [0, 8, 8, 8 , 8]); |
27 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -3, 42), [0, 0, 8, 8, 8]); | 28 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -3, 42), [0, 0, 8, 8, 8]); |
28 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -3, 4), [0, 0, 8, 8 , 0]); | 29 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -3, 4), [0, 0, 8, 8 , 0]); |
29 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -2, -1), [0, 0, 0, 8, 0]); | 30 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -2, -1), [0, 0, 0, 8, 0]); |
30 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -1, -3), [0, 0, 0, 0, 0]); | 31 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -1, -3), [0, 0, 0, 0, 0]); |
31 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 0, 4), [8, 8, 8, 8, 0]); | 32 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 0, 4), [8, 8, 8, 8, 0]); |
32 | 33 |
34 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, Infinity), [0, 0, 0 , 0, 0]); | |
35 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, -Infinity), [8, 8, 8, 8, 8]); | |
36 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 0, Infinity), [8, 8 , 8, 8, 8]); | |
37 assertArrayEquals(new constructor([0, 0, 0, 0, 0]).fill(8, 0, -Infinity), [0, 0, 0, 0, 0]); | |
38 | |
33 // Test exceptions | 39 // Test exceptions |
34 assertThrows('constructor.prototype.fill.call(null)', TypeError); | 40 assertThrows('constructor.prototype.fill.call(null)', TypeError); |
35 assertThrows('constructor.prototype.fill.call(undefined)', TypeError); | 41 assertThrows('constructor.prototype.fill.call(undefined)', TypeError); |
36 assertThrows('constructor.prototype.fill.call([])', TypeError); | 42 assertThrows('constructor.prototype.fill.call([])', TypeError); |
37 | 43 |
44 // Test ToNumber | |
45 var s = ""; | |
46 var p = new Proxy({}, {get(t,k) { s += k.toString() + '\n'; return Reflect.get (t, k)}}) | |
47 new constructor(3).fill(p); | |
48 assertEquals(`Symbol(Symbol.toPrimitive) | |
49 valueOf | |
50 toString | |
51 Symbol(Symbol.toStringTag) | |
52 Symbol(Symbol.toPrimitive) | |
53 valueOf | |
54 toString | |
55 Symbol(Symbol.toStringTag) | |
56 Symbol(Symbol.toPrimitive) | |
57 valueOf | |
58 toString | |
59 Symbol(Symbol.toStringTag) | |
60 `, s); | |
61 | |
38 // Shadowing length doesn't affect fill, unlike Array.prototype.fill | 62 // Shadowing length doesn't affect fill, unlike Array.prototype.fill |
39 var a = new constructor([2, 2]); | 63 var a = new constructor([2, 2]); |
40 Object.defineProperty(a, 'length', {value: 1}); | 64 Object.defineProperty(a, 'length', {value: 1}); |
41 a.fill(3); | 65 a.fill(3); |
42 assertArrayEquals([a[0], a[1]], [3, 3]); | 66 assertArrayEquals([a[0], a[1]], [3, 3]); |
43 Array.prototype.fill.call(a, 4); | 67 Array.prototype.fill.call(a, 4); |
44 assertArrayEquals([a[0], a[1]], [4, 3]); | 68 assertArrayEquals([a[0], a[1]], [4, 3]); |
45 } | 69 } |
70 | |
71 // Clamping | |
72 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(-10), [0, 0, 0, 0, 0]); | |
73 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(1000), [255, 255, 255, 255, 255]); | |
74 | |
75 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0.50001), [1, 1, 1 , 1, 1]); | |
76 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0.50000), [0, 0, 0 , 0, 0]); | |
77 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(0.49999), [0, 0, 0 , 0, 0]); | |
78 // Check round half to even | |
79 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(1.50000), [2, 2, 2 , 2, 2]); | |
80 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(2.50000), [2, 2, 2 , 2, 2]); | |
81 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(2.50001), [3, 3, 3 , 3, 3]); | |
82 // Check infinity clamping. | |
83 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(-Infinity), [0, 0, 0, 0, 0]); | |
84 assertArrayEquals(new Uint8ClampedArray([0, 0, 0, 0, 0]).fill(Infinity), [255, 2 55, 255, 255, 255]); | |
OLD | NEW |