Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(588)

Unified Diff: test/mjsunit/harmony/typedarrays-indexof.js

Issue 728973005: Implement .indexOf() and .lastIndexOf() on typed arrays Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/harmony-typedarray.js ('k') | test/mjsunit/harmony/typedarrays-lastindexof.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
+}
« no previous file with comments | « src/harmony-typedarray.js ('k') | test/mjsunit/harmony/typedarrays-lastindexof.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698