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

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