| Index: test/mjsunit/harmony/typedarrays-every.js
|
| diff --git a/test/mjsunit/harmony/typedarrays-foreach.js b/test/mjsunit/harmony/typedarrays-every.js
|
| similarity index 61%
|
| copy from test/mjsunit/harmony/typedarrays-foreach.js
|
| copy to test/mjsunit/harmony/typedarrays-every.js
|
| index 4bfa655449363e50e6817fe0fb38bc3656035a71..ab5397b1efdaa2617a1e371e34a9a4aa366745c2 100644
|
| --- a/test/mjsunit/harmony/typedarrays-foreach.js
|
| +++ b/test/mjsunit/harmony/typedarrays-every.js
|
| @@ -4,6 +4,11 @@
|
|
|
| // Flags: --harmony-arrays --allow-natives-syntax
|
|
|
| +// The tests here are essentially the same as the ones for
|
| +// %TypedArray%.prototype.forEach, modified to take into account
|
| +// that callbacks must return a boolean value, plus an extra test
|
| +// to check whether returning "false" stops iteration.
|
| +
|
| var typedArrayConstructors = [
|
| Uint8Array,
|
| Int8Array,
|
| @@ -21,43 +26,51 @@ function CheckTypedArrayIsNeutered(array) {
|
| assertEquals(0, array.length);
|
| }
|
|
|
| -function TestTypedArrayForEach(constructor) {
|
| - assertEquals(1, constructor.prototype.forEach.length);
|
| +function TestTypedArrayEvery(constructor) {
|
| + assertEquals(1, constructor.prototype.every.length);
|
|
|
| var a = new constructor(2);
|
| a[0] = 0;
|
| a[1] = 1;
|
|
|
| var count = 0;
|
| - a.forEach(function (n) { count++; });
|
| + a.every(function (n) { count++; return true; });
|
| assertEquals(2, count);
|
|
|
| // Use specified object as this object when calling the function.
|
| var o = { value: 42 };
|
| var result = [];
|
| - a.forEach(function (n, index, array) { result.push(this.value); }, o);
|
| + a.every(function (n, index, array) { result.push(this.value); return true; }, o);
|
| assertArrayEquals([42, 42], result);
|
|
|
| // Modify the original array.
|
| count = 0;
|
| - a.forEach(function (n, index, array) { array[index] = n + 1; count++ });
|
| + a.every(function (n, index, array) { array[index] = n + 1; count++; return true; });
|
| assertEquals(2, count);
|
| assertArrayEquals([1, 2], a);
|
|
|
| + // Returning "false" stops the iteration.
|
| + count = 0;
|
| + a.every(function (n, index, array) { array[index] = 42; count++; return false; });
|
| + assertEquals(1, count);
|
| + assertArrayEquals([42, 2], a);
|
| +
|
| // Check that values passed as second argument are wrapped into
|
| // objects when calling into sloppy mode functions.
|
| function CheckWrapping(value, wrapper) {
|
| var wrappedValue = new wrapper(value);
|
|
|
| - a.forEach(function () {
|
| + a.every(function () {
|
| assertEquals("object", typeof this);
|
| assertEquals(wrappedValue, this);
|
| + return true;
|
| }, value);
|
|
|
| - a.forEach(function () {
|
| + a.every(function () {
|
| "use strict";
|
| assertEquals(typeof value, typeof this);
|
| assertEquals(value, this);
|
| + return true;
|
| }, value);
|
| }
|
| CheckWrapping(true, Boolean);
|
| @@ -73,10 +86,11 @@ function TestTypedArrayForEach(constructor) {
|
| a[0] = 42;
|
| a[1] = 42;
|
| try {
|
| - a.forEach(function (n, index, array) {
|
| + a.every(function (n, index, array) {
|
| if (count > 0) throw "meh";
|
| array[index] = n + 1;
|
| count++;
|
| + return true;
|
| });
|
| } catch (e) {
|
| }
|
| @@ -85,13 +99,14 @@ function TestTypedArrayForEach(constructor) {
|
| assertEquals(42, a[1]);
|
|
|
| // Neutering the buffer backing the typed array mid-way should
|
| - // still make .forEach() finish, and the array should keep being
|
| + // still make .every() finish, and the array should keep being
|
| // empty after neutering it.
|
| count = 0;
|
| - a.forEach(function (n, index, array) {
|
| + a.every(function (n, index, array) {
|
| if (count > 0) %ArrayBufferNeuter(array.buffer);
|
| array[index] = n + 1;
|
| count++;
|
| + return true;
|
| });
|
| assertEquals(2, count);
|
| CheckTypedArrayIsNeutered(a);
|
| @@ -102,7 +117,7 @@ function TestTypedArrayForEach(constructor) {
|
| // all lengths of the typed array items.
|
| a = new constructor(new ArrayBuffer(64));
|
| count = 0;
|
| - a.forEach(function (n) { count++ });
|
| + a.every(function (n) { count++; return true; });
|
| assertEquals(a.length, count);
|
|
|
| // Externalizing the array mid-way accessing the .buffer property
|
| @@ -110,31 +125,32 @@ function TestTypedArrayForEach(constructor) {
|
| a = new constructor(2);
|
| count = 0;
|
| var buffer = undefined;
|
| - a.forEach(function (n, index, array) {
|
| + a.every(function (n, index, array) {
|
| if (count++ > 0)
|
| buffer = array.buffer;
|
| + return true;
|
| });
|
| assertEquals(2, count);
|
| assertTrue(!!buffer);
|
| assertEquals("ArrayBuffer", %_ClassOf(buffer));
|
| assertSame(buffer, a.buffer);
|
|
|
| - // The %TypedArray%.forEach() method should not work when
|
| + // The %TypedArray%.every() method should not work when
|
| // transplanted to objects that are not typed arrays.
|
| - assertThrows(function () { constructor.prototype.forEach.call([1, 2, 3], function (x) {}) }, TypeError);
|
| - assertThrows(function () { constructor.prototype.forEach.call("abc", function (x) {}) }, TypeError);
|
| - assertThrows(function () { constructor.prototype.forEach.call({}, function (x) {}) }, TypeError);
|
| - assertThrows(function () { constructor.prototype.forEach.call(0, function (x) {}) }, TypeError);
|
| + assertThrows(function () { constructor.prototype.every.call([1, 2, 3], function (x) {}) }, TypeError);
|
| + assertThrows(function () { constructor.prototype.every.call("abc", function (x) {}) }, TypeError);
|
| + assertThrows(function () { constructor.prototype.every.call({}, function (x) {}) }, TypeError);
|
| + assertThrows(function () { constructor.prototype.every.call(0, function (x) {}) }, TypeError);
|
|
|
| // Method must be useable on instances of other typed arrays.
|
| - for (var i = 0; i < typedArrayConstructors.length; i++) {
|
| + for (var otherConstructor of typedArrayConstructors) {
|
| count = 0;
|
| - a = new typedArrayConstructors[i](4);
|
| - constructor.prototype.forEach.call(a, function (x) { count++ });
|
| + a = new otherConstructor(4);
|
| + constructor.prototype.every.call(a, function (x) { count++; return true; });
|
| assertEquals(a.length, count);
|
| }
|
| }
|
|
|
| -for (i = 0; i < typedArrayConstructors.length; i++) {
|
| - TestTypedArrayForEach(typedArrayConstructors[i]);
|
| +for (var constructor of typedArrayConstructors) {
|
| + TestTypedArrayEvery(constructor);
|
| }
|
|
|