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

Side by Side Diff: test/mjsunit/harmony/typedarrays-foreach.js

Issue 583723002: Implement .forEach() on typed arrays (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Even more tests Created 6 years, 2 months 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/runtime/runtime-typedarray.cc ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --harmony-arrays --allow-natives-syntax
6
7 var typedArrayConstructors = [
8 Uint8Array,
9 Int8Array,
10 Uint16Array,
11 Int16Array,
12 Uint32Array,
13 Int32Array,
14 Uint8ClampedArray,
15 Float32Array,
16 Float64Array];
17
18 function CheckTypedArrayIsNeutered(array) {
19 assertEquals(0, array.byteLength);
20 assertEquals(0, array.byteOffset);
21 assertEquals(0, array.length);
22 }
23
24 function TestTypedArrayForEach(constructor) {
25 assertEquals(1, constructor.prototype.forEach.length);
26
27 var a = new constructor(2);
28 a[0] = 0;
29 a[1] = 1;
30
31 var count = 0;
32 a.forEach(function (n) { count++; });
33 assertEquals(2, count);
34
35 // Use specified object as this object when calling the function.
36 var o = { value: 42 };
37 var result = [];
38 a.forEach(function (n, index, array) { result.push(this.value); }, o);
39 assertArrayEquals([42, 42], result);
40
41 // Modify the original array.
42 count = 0;
43 a.forEach(function (n, index, array) { array[index] = n + 1; count++ });
44 assertEquals(2, count);
45 assertArrayEquals([1, 2], a);
46
47 // Check that values passed as second argument are wrapped into
48 // objects when calling into sloppy mode functions.
49 function CheckWrapping(value, wrapper) {
50 var wrappedValue = new wrapper(value);
51
52 a.forEach(function () {
53 assertEquals("object", typeof this);
54 assertEquals(wrappedValue, this);
55 }, value);
56
57 a.forEach(function () {
58 "use strict";
59 assertEquals(typeof value, typeof this);
60 assertEquals(value, this);
61 }, value);
62 }
63 CheckWrapping(true, Boolean);
64 CheckWrapping(false, Boolean);
65 CheckWrapping("xxx", String);
66 CheckWrapping(42, Number);
67 CheckWrapping(3.14, Number);
68 CheckWrapping({}, Object);
69
70 // Throw before completing iteration, only the first element
71 // should be modified when thorwing mid-way.
72 count = 0;
73 a[0] = 42;
74 a[1] = 42;
75 try {
76 a.forEach(function (n, index, array) {
77 if (count > 0) throw "meh";
78 array[index] = n + 1;
79 count++;
80 });
81 } catch (e) {
82 }
83 assertEquals(1, count);
84 assertEquals(43, a[0]);
85 assertEquals(42, a[1]);
86
87 // Neutering the buffer backing the typed array mid-way should
88 // still make .forEach() finish, and the array should keep being
89 // empty after neutering it.
90 count = 0;
91 a.forEach(function (n, index, array) {
92 if (count > 0) %ArrayBufferNeuter(array.buffer);
93 array[index] = n + 1;
94 count++;
95 });
96 assertEquals(2, count);
97 CheckTypedArrayIsNeutered(a);
98 assertEquals(undefined, a[0]);
99
100 // The method must work for typed arrays created from ArrayBuffer.
101 // The length of the ArrayBuffer is chosen so it is a multiple of
102 // all lengths of the typed array items.
103 a = new constructor(new ArrayBuffer(64));
104 count = 0;
105 a.forEach(function (n) { count++ });
106 assertEquals(a.length, count);
107
108 // Externalizing the array mid-way accessing the .buffer property
109 // should work.
110 a = new constructor(2);
111 count = 0;
112 var buffer = undefined;
113 a.forEach(function (n, index, array) {
114 if (count++ > 0)
115 buffer = array.buffer;
116 });
117 assertEquals(2, count);
118 assertTrue(!!buffer);
119 assertEquals("ArrayBuffer", %_ClassOf(buffer));
120 assertSame(buffer, a.buffer);
121
122 // The %TypedArray%.forEach() method should not work when
123 // transplanted to objects that are not typed arrays.
124 assertThrows(function () { constructor.prototype.forEach.call([1, 2, 3], funct ion (x) {}) }, TypeError);
125 assertThrows(function () { constructor.prototype.forEach.call("abc", function (x) {}) }, TypeError);
126 assertThrows(function () { constructor.prototype.forEach.call({}, function (x) {}) }, TypeError);
127 assertThrows(function () { constructor.prototype.forEach.call(0, function (x) {}) }, TypeError);
128
129 // Method must be useable on instances of other typed arrays.
130 for (var i = 0; i < typedArrayConstructors.length; i++) {
131 count = 0;
132 a = new typedArrayConstructors[i](4);
133 constructor.prototype.forEach.call(a, function (x) { count++ });
134 assertEquals(a.length, count);
135 }
136 }
137
138 for (i = 0; i < typedArrayConstructors.length; i++) {
139 TestTypedArrayForEach(typedArrayConstructors[i]);
140 }
OLDNEW
« no previous file with comments | « src/runtime/runtime-typedarray.cc ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698