Index: test/mjsunit/harmony/typedarrays-foreach.js |
diff --git a/test/mjsunit/harmony/typedarrays-foreach.js b/test/mjsunit/harmony/typedarrays-foreach.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8c22043b673a0b4dfbf2a87d6e3db1292353a079 |
--- /dev/null |
+++ b/test/mjsunit/harmony/typedarrays-foreach.js |
@@ -0,0 +1,58 @@ |
+// 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-typedarrays |
+ |
+var typedArrayConstructors = [ |
+ Uint8Array, |
+ Int8Array, |
+ Uint16Array, |
+ Int16Array, |
+ Uint32Array, |
+ Int32Array, |
+ Uint8ClampedArray, |
+ Float32Array, |
+ Float64Array]; |
+ |
+function TestTypedArrayForEach(constructor) { |
+ assertEquals(1, constructor.prototype.forEach.length); |
+ |
+ var a = new constructor(2); |
+ a[0] = 0; |
+ a[1] = 1; |
+ |
+ var count = 0; |
+ a.forEach(function (n) { count++; }); |
+ 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); |
+ assertArrayEquals([42, 42], result); |
+ |
+ // Modify the original array. |
+ count = 0; |
+ a.forEach(function (n, index, array) { array[index] = n + 1; count++ }); |
+ assertEquals(2, count); |
+ assertArrayEquals([1, 2], a); |
+ |
Dmitry Lomov (no reviews)
2014/10/08 13:57:11
Add test for a function that throws exception midw
|
+ // The %TypedArray%.forEach() method should not work when |
+ // transplanted to objects that are not typed arrays. |
+ a = [1, 2, 3]; |
+ a.forEach = constructor.prototype.forEach; |
+ assertThrows(function () { a.forEach(function (x) {}) }, TypeError); |
Dmitry Lomov (no reviews)
2014/10/08 13:57:11
Also test for non-array (e.g. {}).
Use construct
|
+} |
+ |
+for (i = 0; i < typedArrayConstructors.length; i++) { |
+ TestTypedArrayForEach(typedArrayConstructors[i]); |
+} |
+ |
+// Transplanting the method from one typed array to another |
+// typed array should work normally. |
+var a = new Int8Array(4); |
+a.forEach = Uint32Array.prototype.forEach; |
Dmitry Lomov (no reviews)
2014/10/08 13:57:11
Use Uint32Array.prototype.forEach.call - more stra
|
+var count = 0; |
+a.forEach(function (x) { count++ }); |
+assertEquals(a.length, count); |