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

Unified Diff: test/mjsunit/harmony/typedarrays-lastindexof.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 | « test/mjsunit/harmony/typedarrays-indexof.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/typedarrays-lastindexof.js
diff --git a/test/mjsunit/harmony/typedarrays-lastindexof.js b/test/mjsunit/harmony/typedarrays-lastindexof.js
new file mode 100644
index 0000000000000000000000000000000000000000..4f1fcb374205b9c9fc42101fd0ee0c822ac7ac10
--- /dev/null
+++ b/test/mjsunit/harmony/typedarrays-lastindexof.js
@@ -0,0 +1,87 @@
+// 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 TestTypedArrayLastIndexOf(constructor) {
+ assertEquals(1, constructor.prototype.lastIndexOf.length);
+
+ // Negative cases.
+ assertEquals(-1, constructor.of().lastIndexOf(1));
+ var a = constructor.of(1, 2, 3, 1, 2, 3, 1);
+ assertEquals(-1, a.lastIndexOf(4));
+
+ assertEquals(5, a.lastIndexOf(3));
+ // Index out of range.
+ assertEquals(4, a.lastIndexOf(2, a.length));
+ // Index in range.
+ assertEquals(1, a.lastIndexOf(2, 2));
+ // Negative index in range.
+ assertEquals(0, a.lastIndexOf(1, -5));
+ // Negative index out of range.
+ assertEquals(-1, a.lastIndexOf(1, -10));
+
+ // Typed arrays cannot store "undefined", so even when creating one
+ // with "undefined" values, those are coerced to numbers, which means
+ // .lastIndexOf() cannot ever return a valid index for "undefined".
+ assertEquals(-1, a.lastIndexOf(undefined));
+ assertEquals(-1, constructor.of(undefined).lastIndexOf(undefined));
+
+ // Neutering the buffer backing the typed array should make
+ // .lastIndexOf() always return -1 afterwards.
+ a = constructor.of(1, 2, 3, 4, 5, 6);
+ %ArrayBufferNeuter(a.buffer);
+ CheckTypedArrayIsNeutered(a);
+ assertEquals(-1, a.lastIndexOf(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.lastIndexOf(42));
+
+ // Externalizing the array by using the .buffer property should not
+ // affect operation of .lastIndexOf().
+ 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.lastIndexOf(3));
+
+ // The %TypedArray%.prototype.lastIndexOf() method should not work
+ // when transplanted to objects that are not typed arrays.
+ assertThrows(function () { constructor.prototype.lastIndexOf.call([1, 2, 3], 0); }, TypeError);
+ assertThrows(function () { constructor.prototype.lastIndexOf.call("abc", 0); }, TypeError);
+ assertThrows(function () { constructor.prototype.lastIndexOf.call({}, 0); }, TypeError);
+ assertThrows(function () { constructor.prototype.lastIndexOf.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.lastIndexOf.call(a, 3));
+ }
+}
+
+for (var constructor of typedArrayConstructors) {
+ TestTypedArrayLastIndexOf(constructor);
+}
« no previous file with comments | « test/mjsunit/harmony/typedarrays-indexof.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698