| 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 TestTypedArrayIndexOf(constructor) { |
| 25 assertEquals(1, constructor.prototype.indexOf.length); |
| 26 |
| 27 // Negative cases. |
| 28 assertEquals(-1, constructor.of().indexOf(1)); |
| 29 var a = constructor.of(1, 2, 3, 1, 2, 3, 1); |
| 30 assertEquals(-1, a.indexOf(4)); |
| 31 assertEquals(-1, a.indexOf(3, a.length)); |
| 32 |
| 33 assertEquals(2, a.indexOf(3)); |
| 34 // Negative index out of range. |
| 35 assertEquals(1, a.indexOf(2, -17)); |
| 36 // Negative index in range. |
| 37 assertEquals(4, a.indexOf(2, -4)); |
| 38 // Index in range. |
| 39 assertEquals(3, a.indexOf(1, 1)); |
| 40 assertEquals(3, a.indexOf(1, 3)); |
| 41 assertEquals(6, a.indexOf(1, 4)); |
| 42 |
| 43 // Typed arrays cannot store "undefined", so even when creating one with |
| 44 // "undefined" values, those are coerced to numbers, which means indexOf() |
| 45 // cannot ever return a valid index for "undefined". |
| 46 assertEquals(-1, a.indexOf(undefined)); |
| 47 assertEquals(-1, constructor.of(undefined).indexOf(undefined)); |
| 48 |
| 49 // Neutering the buffer backing the typed array should make .indexOf() |
| 50 // always return -1 afterwards. |
| 51 a = constructor.of(1, 2, 3, 4, 5, 6); |
| 52 %ArrayBufferNeuter(a.buffer); |
| 53 CheckTypedArrayIsNeutered(a); |
| 54 assertEquals(-1, a.indexOf(4)); |
| 55 |
| 56 // The method must work for typed arrays created from ArrayBuffer. |
| 57 // The length of the ArrayBuffer is chosen so it is a multiple of |
| 58 // all lengths of the typed array items. |
| 59 a = new constructor(new ArrayBuffer(64)); |
| 60 a[4] = 42; |
| 61 assertEquals(4, a.indexOf(42)); |
| 62 |
| 63 // Externalizing the array by using the .buffer property should not |
| 64 // affect operation of .indexOf(). |
| 65 a = constructor.of(1, 2, 3, 4, 5, 6); |
| 66 var buffer = a.buffer; |
| 67 assertTrue(!!buffer); |
| 68 assertEquals("ArrayBuffer", %_ClassOf(buffer)); |
| 69 assertSame(buffer, a.buffer); |
| 70 assertEquals(2, a.indexOf(3)); |
| 71 |
| 72 // The %TypedArray%.prototype.indexOf() method should not work |
| 73 // when transplanted to objects that are not typed arrays. |
| 74 assertThrows(function () { constructor.prototype.indexOf.call([1, 2, 3], 0); }
, TypeError); |
| 75 assertThrows(function () { constructor.prototype.indexOf.call("abc", 0); }, Ty
peError); |
| 76 assertThrows(function () { constructor.prototype.indexOf.call({}, 0); }, TypeE
rror); |
| 77 assertThrows(function () { constructor.prototype.indexOf.call(0, 0); }, TypeEr
ror); |
| 78 |
| 79 // Method must be useable on instances of other typed arrays. |
| 80 for (var otherConstructor of typedArrayConstructors) { |
| 81 a = otherConstructor.of(1, 2, 3, 4); |
| 82 assertEquals(2, constructor.prototype.indexOf.call(a, 3)); |
| 83 } |
| 84 } |
| 85 |
| 86 for (var constructor of typedArrayConstructors) { |
| 87 TestTypedArrayIndexOf(constructor); |
| 88 } |
| OLD | NEW |