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

Side by Side Diff: test/mjsunit/harmony/typedarray-iteration.js

Issue 1139663005: Implement %TypedArray%.prototype.{map,filter,some,reduce,reduceRight} (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 7 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
OLDNEW
(Empty)
1 // Copyright 2015 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
6
7 // Tests for standard TypedArray array iteration functions.
8
9 var typedArrayConstructors = [
10 Uint8Array,
11 Int8Array,
12 Uint16Array,
13 Int16Array,
14 Uint32Array,
15 Int32Array,
16 Uint8ClampedArray,
17 Float32Array,
18 Float64Array
19 ];
20
21 function assertArrayLikeEquals(expected, value, type) {
22 assertEquals(value.__proto__, type.prototype);
23 assertEquals(expected.length, value.length);
24 for (var i = 0; i < value.length; ++i) {
25 assertEquals(expected[i], value[i]);
26 }
27 }
28
29 for (var constructor of typedArrayConstructors) {
30 //
31 // %TypedArray%.prototype.filter
32 //
33 (function() {
34 // Simple use.
35 var a = new constructor([0,1]);
36 assertArrayLikeEquals([0], a.filter(function(n) { return n == 0; }),
37 constructor);
38 assertArrayLikeEquals([0,1], a, constructor);
39
40 // Use specified object as this object when calling the function.
41 var o = { value: 42 }
42 a = new constructor([1,42,3,42,4]);
arv (Not doing code reviews) 2015/05/18 22:54:23 space after comma
dehrenberg 2015/05/19 00:13:45 Done.
43 assertArrayLikeEquals([42,42], a.filter(function(n) {
44 return this.value == n
45 }, o), constructor);
46
47 // Modify original array.
48 a = new constructor([1,42,3,42,4]);
49 assertArrayLikeEquals([42,42], a.filter(function(n, index, array) {
50 array[index] = 43; return 42 == n;
51 }), constructor);
52 assertArrayLikeEquals([43,43,43,43,43], a, constructor);
53
54 // Create a new object in each function call when receiver is a
55 // primitive value. See ECMA-262, Annex C.
56 a = [];
57 new constructor([1, 2]).filter(function() { a.push(this) }, "");
58 assertTrue(a[0] !== a[1]);
59
60 // Do not create a new object otherwise.
61 a = [];
62 new constructor([1, 2]).filter(function() { a.push(this) }, {});
63 assertEquals(a[0], a[1]);
64
65 // In strict mode primitive values should not be coerced to an object.
66 a = [];
67 new constructor([1, 2]).filter(function() {
68 'use strict';
69 a.push(this);
70 }, "");
arv (Not doing code reviews) 2015/05/18 22:54:23 stick to either " or '
dehrenberg 2015/05/19 00:13:45 Done.
71 assertEquals("", a[0]);
72 assertEquals(a[0], a[1]);
73
74 // Calling this method on other types is a TypeError
75 assertThrows(function() {
76 constructor.prototype.filter.call([], function() {});
77 }, TypeError);
78
79 // Shadowing the length property doesn't change anything
80 a = new constructor([1, 2]);
81 Object.defineProperty(a, 'length', { value: 1 });
82 assertArrayLikeEquals([2], a.filter(function(elt) {
83 return elt == 2;
84 }), constructor);
85 })();
86
87 //
88 // %TypedArray%.prototype.map
arv (Not doing code reviews) 2015/05/18 22:54:24 Other tests tend to use the following format: (fu
dehrenberg 2015/05/19 00:13:45 Done.
89 //
90 (function() {
91 var a = new constructor([0,1,2,3,4]);
92
93 // Simple use.
94 var result = [1,2,3,4,5];
95 assertArrayLikeEquals(result, a.map(function(n) { return n + 1; }),
96 constructor);
97 assertEquals(a, a);
98
99 // Use specified object as this object when calling the function.
100 var o = { delta: 42 }
101 result = [42,43,44,45,46];
102 assertArrayLikeEquals(result, a.map(function(n) {
103 return this.delta + n;
104 }, o), constructor);
105
106 // Modify original array.
107 a = new constructor([0,1,2,3,4]);
108 result = [1,2,3,4,5];
109 assertArrayLikeEquals(result, a.map(function(n, index, array) {
110 array[index] = n + 1;
111 return n + 1;
112 }), constructor);
113 assertArrayLikeEquals(result, a, constructor);
114
115 // Create a new object in each function call when receiver is a
116 // primitive value. See ECMA-262, Annex C.
117 a = [];
118 new constructor([1, 2]).map(function() { a.push(this) }, "");
119 assertTrue(a[0] !== a[1]);
120
121 // Do not create a new object otherwise.
122 a = [];
123 new constructor([1, 2]).map(function() { a.push(this) }, {});
124 assertEquals(a[0], a[1]);
125
126 // In strict mode primitive values should not be coerced to an object.
127 a = [];
128 new constructor([1, 2]).map(function() { 'use strict'; a.push(this); }, "");
129 assertEquals("", a[0]);
130 assertEquals(a[0], a[1]);
131 })();
132
arv (Not doing code reviews) 2015/05/18 22:54:24 map is a bit special since the return value might
dehrenberg 2015/05/19 00:13:45 Done.
133 //
134 // %TypedArray%.prototype.some
135 //
136 (function() {
137 var a = new constructor([0,1,2,3,4]);
138
139 // Simple use.
140 assertTrue(a.some(function(n) { return n == 3}));
141 assertFalse(a.some(function(n) { return n == 5}));
142
143 // Use specified object as this object when calling the function.
144 var o = { element: 42 };
145 a = new constructor([1,42,3]);
146 assertTrue(a.some(function(n) { return this.element == n; }, o));
147 a = new constructor([1]);
148 assertFalse(a.some(function(n) { return this.element == n; }, o));
149
150 // Modify original array.
151 a = new constructor([0,1,2,3]);
152 assertTrue(a.some(function(n, index, array) {
153 array[index] = n + 1;
154 return n == 2;
155 }));
156 assertArrayLikeEquals([1,2,3,3], a, constructor);
157
158 // Create a new object in each function call when receiver is a
159 // primitive value. See ECMA-262, Annex C.
160 a = [];
161 new constructor([1, 2]).some(function() { a.push(this) }, "");
162 assertTrue(a[0] !== a[1]);
163
164 // Do not create a new object otherwise.
165 a = [];
166 new constructor([1, 2]).some(function() { a.push(this) }, {});
167 assertEquals(a[0], a[1]);
168
169 // In strict mode primitive values should not be coerced to an object.
170 a = [];
171 new constructor([1, 2]).some(function() {
172 'use strict';
173 a.push(this);
174 }, "");
175 assertEquals("", a[0]);
176 assertEquals(a[0], a[1]);
177
178 // Calling this method on other types is a TypeError
179 assertThrows(function() {
180 constructor.prototype.some.call([], function() {});
181 }, TypeError);
182
183 // Shadowing the length property doesn't change anything
184 a = new constructor([1, 2]);
185 Object.defineProperty(a, 'length', { value: 1 });
186 assertEquals(true, a.some(function(elt) { return elt == 2; }));
187 assertEquals(false, Array.prototype.some.call(a, function(elt) {
188 return elt == 2;
189 }));
190 })();
191
192 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698