| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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 --allow-natives-syntax |
| 6 |
| 7 var typedArrayConstructors = [ |
| 8 Uint8Array, |
| 9 Int8Array, |
| 10 Uint16Array, |
| 11 Int16Array, |
| 12 Uint32Array, |
| 13 Int32Array, |
| 14 Uint8ClampedArray, |
| 15 Float32Array, |
| 16 Float64Array]; |
| 17 |
| 18 function CheckTypedArrayIsNeutered(array) { |
| 19 assertEquals(0, array.byteLength); |
| 20 assertEquals(0, array.byteOffset); |
| 21 assertEquals(0, array.length); |
| 22 } |
| 23 |
| 24 function TestTypedArrayFill(constructor) { |
| 25 assertEquals(1, constructor.prototype.fill.length); |
| 26 |
| 27 assertArrayEquals([], (new constructor(0)).fill(8)); |
| 28 |
| 29 assertArrayEquals([8, 8, 8, 8, 8], (new constructor(5).fill(8))); |
| 30 assertArrayEquals([0, 8, 8, 8, 8], (new constructor(5)).fill(8, 1)); |
| 31 assertArrayEquals([0, 0, 0, 0, 0], (new constructor(5)).fill(8, 10)); |
| 32 assertArrayEquals([8, 8, 8, 8, 8], (new constructor(5)).fill(8, -5)); |
| 33 assertArrayEquals([0, 8, 8, 8, 0], (new constructor(5)).fill(8, 1, 4)); |
| 34 assertArrayEquals([0, 8, 8, 8, 0], (new constructor(5)).fill(8, 1, -1)); |
| 35 assertArrayEquals([0, 8, 8, 8, 8], (new constructor(5)).fill(8, 1, 42)); |
| 36 assertArrayEquals([0, 0, 8, 8, 8], (new constructor(5)).fill(8, -3, 42)); |
| 37 assertArrayEquals([0, 0, 8, 8, 0], (new constructor(5)).fill(8, -3, 4)); |
| 38 assertArrayEquals([0, 0, 0, 8, 0], (new constructor(5)).fill(8, -2, -1)); |
| 39 assertArrayEquals([0, 0, 0, 0, 0], (new constructor(5)).fill(8, -1, -3)); |
| 40 assertArrayEquals([8, 8, 8, 8, 0], (new constructor(5)).fill(8, undefined, 4))
; |
| 41 |
| 42 // Typed arrays with float numbers are by default filled with NaN, |
| 43 // the ones with integral numbers are filled with zeroes. |
| 44 var D = 0; |
| 45 if (constructor == Float32Array || constructor == Float64Array) |
| 46 D = NaN; |
| 47 assertArrayEquals([D, D, D, D, D], (new constructor(5).fill())); |
| 48 |
| 49 // Using .fill() on a neutered array must not cause errors, |
| 50 // and the array must remain neutered afterwards. |
| 51 var a = new constructor(5); |
| 52 %ArrayBufferNeuter(a.buffer); |
| 53 a.fill(8); |
| 54 CheckTypedArrayIsNeutered(a); |
| 55 |
| 56 // Test exceptions |
| 57 assertThrows(function () { |
| 58 constructor.prototype.fill.call(null); |
| 59 }, TypeError); |
| 60 assertThrows(function () { |
| 61 constructor.prototype.fill.call(undefined); |
| 62 }, TypeError); |
| 63 |
| 64 // Check superficial features of %TypedArray%.prototype.fill. |
| 65 var desc = Object.getOwnPropertyDescriptor(constructor.prototype, "fill"); |
| 66 |
| 67 assertEquals(false, desc.configurable); |
| 68 assertEquals(false, desc.enumerable); |
| 69 assertEquals(false, desc.writable); |
| 70 assertEquals(1, constructor.prototype.fill.length); |
| 71 |
| 72 // The %TypedArray%.prototype.fill() method should not work |
| 73 // when transplanted to objects that are not typed arrays. |
| 74 assertThrows(function () { constructor.prototype.fill.call([1, 2, 3], 8) }, Ty
peError); |
| 75 assertThrows(function () { constructor.prototype.fill.call("abc", 8) }, TypeEr
ror); |
| 76 assertThrows(function () { constructor.prototype.fill.call({}, 8) }, TypeError
); |
| 77 assertThrows(function () { constructor.prototype.fill.call({}, 8) }, TypeError
); |
| 78 assertThrows(function () { constructor.prototype.fill.call(0, 8) }, TypeError)
; |
| 79 |
| 80 // The method should be work on instances of other typed arrays. |
| 81 for (var otherConstructor of typedArrayConstructors) { |
| 82 a = new otherConstructor(5); |
| 83 constructor.prototype.fill.call(a, 8); |
| 84 assertArrayEquals([8, 8, 8, 8, 8], a); |
| 85 } |
| 86 } |
| 87 |
| 88 |
| 89 for (var x of typedArrayConstructors) { |
| 90 TestTypedArrayFill(x); |
| 91 } |
| OLD | NEW |