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

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

Issue 1139663005: Implement %TypedArray%.prototype.{map,filter,some,reduce,reduceRight} (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fixes 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
« src/array.js ('K') | « test/mjsunit/harmony/typedarray-iteration.js ('k') | no next file » | 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 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 --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
19 function clone(v) {
20 // Shallow-copies arrays, returns everything else verbatim.
21 if (v instanceof Array) {
22 // Shallow-copy an array.
23 var newArray = new Array(v.length);
24 for (var i in v) {
25 newArray[i] = v[i];
26 }
27 return newArray;
28 }
29 return v;
30 }
31
32
33 // Creates a callback function for reduce/reduceRight that tests the number
34 // of arguments and otherwise behaves as "func", but which also
35 // records all calls in an array on the function (as arrays of arguments
36 // followed by result).
37 function makeRecorder(func, testName) {
38 var record = [];
39 var f = function recorder(a, b, i, s) {
40 assertEquals(4, arguments.length,
41 testName + "(number of arguments: " + arguments.length + ")");
42 assertEquals("number", typeof(i), testName + "(index must be number)");
43 assertEquals(s[i], b, testName + "(current argument is at index)");
44 if (record.length > 0) {
45 var prevRecord = record[record.length - 1];
46 var prevResult = prevRecord[prevRecord.length - 1];
47 assertEquals(prevResult, a,
48 testName + "(prev result -> current input)");
49 }
50 var args = [clone(a), clone(b), i, clone(s)];
51 var result = func.apply(this, arguments);
52 args.push(clone(result));
53 record.push(args);
54 return result;
55 };
56 f.record = record;
57 return f;
58 }
59
60
61 function testReduce(type,
62 testName,
63 expectedResult,
64 expectedCalls,
65 array,
66 combine,
67 init) {
68 var rec = makeRecorder(combine);
69 var result;
70 var performsCall;
71 if (arguments.length > 6) {
72 result = array[type](rec, init);
73 } else {
74 result = array[type](rec);
75 }
76 var calls = rec.record;
77 assertEquals(expectedCalls.length, calls.length,
78 testName + " (number of calls)");
79 for (var i = 0; i < expectedCalls.length; i++) {
80 assertEquals(expectedCalls[i], calls[i],
81 testName + " (call " + (i + 1) + ")");
82 }
83 assertEquals(expectedResult, result, testName + " (result)");
84 }
85
86
87 function sum(a, b) { return a + b; }
88 function prod(a, b) { return a * b; }
89 function dec(a, b, i, arr) { return a + b * Math.pow(10, arr.length - i - 1); }
90 function accumulate(acc, elem, i) { acc[i] = elem; return acc; }
91
92 for (var constructor of typedArrayConstructors) {
93 // ---- Test Reduce[Left]
94
95 var simpleArray = new constructor([2,4,6])
96
97 testReduce("reduce", "SimpleReduceSum", 12,
98 [[0, 2, 0, simpleArray, 2],
99 [2, 4, 1, simpleArray, 6],
100 [6, 6, 2, simpleArray, 12]],
101 simpleArray, sum, 0);
102
103 testReduce("reduce", "SimpleReduceProd", 48,
104 [[1, 2, 0, simpleArray, 2],
105 [2, 4, 1, simpleArray, 8],
106 [8, 6, 2, simpleArray, 48]],
107 simpleArray, prod, 1);
108
109 testReduce("reduce", "SimpleReduceDec", 246,
110 [[0, 2, 0, simpleArray, 200],
111 [200, 4, 1, simpleArray, 240],
112 [240, 6, 2, simpleArray, 246]],
113 simpleArray, dec, 0);
114
115 testReduce("reduce", "SimpleReduceAccumulate", [2, 4, 6],
116 [[[], 2, 0, simpleArray, [2]],
117 [[2], 4, 1, simpleArray, [2, 4]],
118 [[2,4], 6, 2, simpleArray, [2, 4, 6]]],
119 simpleArray, accumulate, []);
120
121
122 testReduce("reduce", "EmptyReduceSum", 0, [], [], sum, 0);
123 testReduce("reduce", "EmptyReduceProd", 1, [], [], prod, 1);
124 testReduce("reduce", "EmptyReduceDec", 0, [], [], dec, 0);
125 testReduce("reduce", "EmptyReduceAccumulate", [], [], [], accumulate, []);
126
127 testReduce("reduce", "EmptyReduceSumNoInit", 0, [], [0], sum);
128 testReduce("reduce", "EmptyReduceProdNoInit", 1, [], [1], prod);
129 testReduce("reduce", "EmptyReduceDecNoInit", 0, [], [0], dec);
130 testReduce("reduce", "EmptyReduceAccumulateNoInit", [], [], [[]], accumulate);
131
132 // ---- Test ReduceRight
133
134 testReduce("reduceRight", "SimpleReduceRightSum", 12,
135 [[0, 6, 2, simpleArray, 6],
136 [6, 4, 1, simpleArray, 10],
137 [10, 2, 0, simpleArray, 12]],
138 simpleArray, sum, 0);
139
140 testReduce("reduceRight", "SimpleReduceRightProd", 48,
141 [[1, 6, 2, simpleArray, 6],
142 [6, 4, 1, simpleArray, 24],
143 [24, 2, 0, simpleArray, 48]],
144 simpleArray, prod, 1);
145
146 testReduce("reduceRight", "SimpleReduceRightDec", 246,
147 [[0, 6, 2, simpleArray, 6],
148 [6, 4, 1, simpleArray, 46],
149 [46, 2, 0, simpleArray, 246]],
150 simpleArray, dec, 0);
151
152
153 testReduce("reduceRight", "EmptyReduceRightSum", 0, [], [], sum, 0);
154 testReduce("reduceRight", "EmptyReduceRightProd", 1, [], [], prod, 1);
155 testReduce("reduceRight", "EmptyReduceRightDec", 0, [], [], dec, 0);
156 testReduce("reduceRight", "EmptyReduceRightAccumulate", [],
157 [], [], accumulate, []);
158
159 testReduce("reduceRight", "EmptyReduceRightSumNoInit", 0, [], [0], sum);
160 testReduce("reduceRight", "EmptyReduceRightProdNoInit", 1, [], [1], prod);
161 testReduce("reduceRight", "EmptyReduceRightDecNoInit", 0, [], [0], dec);
162 testReduce("reduceRight", "EmptyReduceRightAccumulateNoInit",
163 [], [], [[]], accumulate);
164
165 // Ignore non-array properties:
166
167 var arrayPlus = [1,2,3];
168 arrayPlus[-1] = NaN;
169 arrayPlus[Math.pow(2,32)] = NaN;
170 arrayPlus[NaN] = NaN;
171 arrayPlus["00"] = NaN;
172 arrayPlus["02"] = NaN;
173 arrayPlus["-0"] = NaN;
174
175 testReduce("reduce", "ArrayWithNonElementPropertiesReduce", 6,
176 [[0, 1, 0, arrayPlus, 1],
177 [1, 2, 1, arrayPlus, 3],
178 [3, 3, 2, arrayPlus, 6],
179 ], arrayPlus, sum, 0);
180
181 testReduce("reduceRight", "ArrayWithNonElementPropertiesReduceRight", 6,
182 [[0, 3, 2, arrayPlus, 3],
183 [3, 2, 1, arrayPlus, 5],
184 [5, 1, 0, arrayPlus, 6],
185 ], arrayPlus, sum, 0);
186
187
188 // Test error conditions:
189
190 var exception = false;
191 try {
192 new constructor([1]).reduce("not a function");
193 } catch (e) {
194 exception = true;
195 assertTrue(e instanceof TypeError,
196 "reduce callback not a function not throwing TypeError");
197 assertTrue(e.message.indexOf(" is not a function") >= 0,
198 "reduce non function TypeError type");
199 }
200 assertTrue(exception);
201
202 exception = false;
203 try {
204 new constructor([1]).reduceRight("not a function");
205 } catch (e) {
206 exception = true;
207 assertTrue(e instanceof TypeError,
208 "reduceRight callback not a function not throwing TypeError");
209 assertTrue(e.message.indexOf(" is not a function") >= 0,
210 "reduceRight non function TypeError type");
211 }
212 assertTrue(exception);
213
214 exception = false;
215 try {
216 new constructor([]).reduce(sum);
217 } catch (e) {
218 exception = true;
219 assertTrue(e instanceof TypeError,
220 "reduce no initial value not throwing TypeError");
221 assertEquals("Reduce of empty array with no initial value", e.message,
222 "reduce no initial TypeError type");
223 }
224 assertTrue(exception);
225
226 exception = false;
227 try {
228 new constructor([]).reduceRight(sum);
229 } catch (e) {
230 exception = true;
231 assertTrue(e instanceof TypeError,
232 "reduceRight no initial value not throwing TypeError");
233 assertEquals("Reduce of empty array with no initial value", e.message,
234 "reduceRight no initial TypeError type");
235 }
236 assertTrue(exception);
237
238 // Reduce fails when called on non-TypedArrays
239 assertThrows(function() {
240 constructor.prototype.reduce.call([], function() {}, null);
241 }, TypeError);
242 assertThrows(function() {
243 constructor.prototype.reduceRight.call([], function() {}, null);
244 }, TypeError);
245
246 // Shadowing length doesn't affect every, unlike Array.prototype.every
247 var a = new constructor([1, 2]);
248 Object.defineProperty(a, 'length', {value: 1});
249 assertEquals(a.reduce(sum, 0), 3);
250 assertEquals(Array.prototype.reduce.call(a, sum, 0), 1);
251 assertEquals(a.reduceRight(sum, 0), 3);
252 assertEquals(Array.prototype.reduceRight.call(a, sum, 0), 1);
253
254 assertEquals(1, constructor.prototype.reduce.length);
255 assertEquals(1, constructor.prototype.reduceRight.length);
256 }
OLDNEW
« src/array.js ('K') | « test/mjsunit/harmony/typedarray-iteration.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698