| Index: test/mjsunit/harmony/typedarrays-indexof.js
|
| diff --git a/test/mjsunit/harmony/typedarrays-indexof.js b/test/mjsunit/harmony/typedarrays-indexof.js
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5baf09ef87fa8b185add5c872b39aadc557289c4
|
| --- /dev/null
|
| +++ b/test/mjsunit/harmony/typedarrays-indexof.js
|
| @@ -0,0 +1,88 @@
|
| +// Copyright 2014 the V8 project authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +// Flags: --harmony-arrays --allow-natives-syntax
|
| +
|
| +var typedArrayConstructors = [
|
| + Uint8Array,
|
| + Int8Array,
|
| + Uint16Array,
|
| + Int16Array,
|
| + Uint32Array,
|
| + Int32Array,
|
| + Uint8ClampedArray,
|
| + Float32Array,
|
| + Float64Array];
|
| +
|
| +function CheckTypedArrayIsNeutered(array) {
|
| + assertEquals(0, array.byteLength);
|
| + assertEquals(0, array.byteOffset);
|
| + assertEquals(0, array.length);
|
| +}
|
| +
|
| +function TestTypedArrayIndexOf(constructor) {
|
| + assertEquals(1, constructor.prototype.indexOf.length);
|
| +
|
| + // Negative cases.
|
| + assertEquals(-1, constructor.of().indexOf(1));
|
| + var a = constructor.of(1, 2, 3, 1, 2, 3, 1);
|
| + assertEquals(-1, a.indexOf(4));
|
| + assertEquals(-1, a.indexOf(3, a.length));
|
| +
|
| + assertEquals(2, a.indexOf(3));
|
| + // Negative index out of range.
|
| + assertEquals(1, a.indexOf(2, -17));
|
| + // Negative index in range.
|
| + assertEquals(4, a.indexOf(2, -4));
|
| + // Index in range.
|
| + assertEquals(3, a.indexOf(1, 1));
|
| + assertEquals(3, a.indexOf(1, 3));
|
| + assertEquals(6, a.indexOf(1, 4));
|
| +
|
| + // Typed arrays cannot store "undefined", so even when creating one with
|
| + // "undefined" values, those are coerced to numbers, which means indexOf()
|
| + // cannot ever return a valid index for "undefined".
|
| + assertEquals(-1, a.indexOf(undefined));
|
| + assertEquals(-1, constructor.of(undefined).indexOf(undefined));
|
| +
|
| + // Neutering the buffer backing the typed array should make .indexOf()
|
| + // always return -1 afterwards.
|
| + a = constructor.of(1, 2, 3, 4, 5, 6);
|
| + %ArrayBufferNeuter(a.buffer);
|
| + CheckTypedArrayIsNeutered(a);
|
| + assertEquals(-1, a.indexOf(4));
|
| +
|
| + // The method must work for typed arrays created from ArrayBuffer.
|
| + // The length of the ArrayBuffer is chosen so it is a multiple of
|
| + // all lengths of the typed array items.
|
| + a = new constructor(new ArrayBuffer(64));
|
| + a[4] = 42;
|
| + assertEquals(4, a.indexOf(42));
|
| +
|
| + // Externalizing the array by using the .buffer property should not
|
| + // affect operation of .indexOf().
|
| + a = constructor.of(1, 2, 3, 4, 5, 6);
|
| + var buffer = a.buffer;
|
| + assertTrue(!!buffer);
|
| + assertEquals("ArrayBuffer", %_ClassOf(buffer));
|
| + assertSame(buffer, a.buffer);
|
| + assertEquals(2, a.indexOf(3));
|
| +
|
| + // The %TypedArray%.prototype.indexOf() method should not work
|
| + // when transplanted to objects that are not typed arrays.
|
| + assertThrows(function () { constructor.prototype.indexOf.call([1, 2, 3], 0); }, TypeError);
|
| + assertThrows(function () { constructor.prototype.indexOf.call("abc", 0); }, TypeError);
|
| + assertThrows(function () { constructor.prototype.indexOf.call({}, 0); }, TypeError);
|
| + assertThrows(function () { constructor.prototype.indexOf.call(0, 0); }, TypeError);
|
| +
|
| + // Method must be useable on instances of other typed arrays.
|
| + for (var otherConstructor of typedArrayConstructors) {
|
| + a = otherConstructor.of(1, 2, 3, 4);
|
| + assertEquals(2, constructor.prototype.indexOf.call(a, 3));
|
| + }
|
| +}
|
| +
|
| +for (var constructor of typedArrayConstructors) {
|
| + TestTypedArrayIndexOf(constructor);
|
| +}
|
|
|