OLD | NEW |
---|---|
(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 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, [], new constructor([]), sum, 0); | |
123 testReduce("reduce", "EmptyReduceProd", 1, [], new constructor([]), prod, 1); | |
124 testReduce("reduce", "EmptyReduceDec", 0, [], new constructor([]), dec, 0); | |
125 testReduce("reduce", "EmptyReduceAccumulate", [], [], new constructor([]), acc umulate, []); | |
126 | |
127 testReduce("reduce", "EmptyReduceSumNoInit", 0, [], new constructor([0]), sum) ; | |
128 testReduce("reduce", "EmptyReduceProdNoInit", 1, [], new constructor([1]), pro d); | |
129 testReduce("reduce", "EmptyReduceDecNoInit", 0, [], new constructor([0]), dec) ; | |
130 | |
131 // ---- Test ReduceRight | |
132 | |
133 testReduce("reduceRight", "SimpleReduceRightSum", 12, | |
134 [[0, 6, 2, simpleArray, 6], | |
135 [6, 4, 1, simpleArray, 10], | |
136 [10, 2, 0, simpleArray, 12]], | |
137 simpleArray, sum, 0); | |
138 | |
139 testReduce("reduceRight", "SimpleReduceRightProd", 48, | |
140 [[1, 6, 2, simpleArray, 6], | |
141 [6, 4, 1, simpleArray, 24], | |
142 [24, 2, 0, simpleArray, 48]], | |
143 simpleArray, prod, 1); | |
144 | |
145 testReduce("reduceRight", "SimpleReduceRightDec", 246, | |
146 [[0, 6, 2, simpleArray, 6], | |
147 [6, 4, 1, simpleArray, 46], | |
148 [46, 2, 0, simpleArray, 246]], | |
149 simpleArray, dec, 0); | |
150 | |
151 | |
152 testReduce("reduceRight", "EmptyReduceRightSum", 0, [], new constructor([]), s um, 0); | |
153 testReduce("reduceRight", "EmptyReduceRightProd", 1, [], new constructor([]), prod, 1); | |
154 testReduce("reduceRight", "EmptyReduceRightDec", 0, [], new constructor([]), d ec, 0); | |
155 testReduce("reduceRight", "EmptyReduceRightAccumulate", [], | |
156 [], new constructor([]), accumulate, []); | |
157 | |
158 testReduce("reduceRight", "EmptyReduceRightSumNoInit", 0, [], new constructor( [0]), sum); | |
159 testReduce("reduceRight", "EmptyReduceRightProdNoInit", 1, [], new constructor ([1]), prod); | |
160 testReduce("reduceRight", "EmptyReduceRightDecNoInit", 0, [], new constructor( [0]), dec); | |
161 | |
162 // Ignore non-array properties: | |
163 | |
164 var arrayPlus = new constructor([1,2,3]); | |
165 arrayPlus[-1] = NaN; | |
166 arrayPlus["00"] = NaN; | |
167 arrayPlus["02"] = NaN; | |
168 arrayPlus["-0"] = NaN; | |
169 arrayPlus.x = NaN; | |
170 | |
171 testReduce("reduce", "ArrayWithNonElementPropertiesReduce", 6, | |
172 [[0, 1, 0, arrayPlus, 1], | |
173 [1, 2, 1, arrayPlus, 3], | |
174 [3, 3, 2, arrayPlus, 6], | |
175 ], arrayPlus, sum, 0); | |
176 | |
177 testReduce("reduceRight", "ArrayWithNonElementPropertiesReduceRight", 6, | |
178 [[0, 3, 2, arrayPlus, 3], | |
179 [3, 2, 1, arrayPlus, 5], | |
180 [5, 1, 0, arrayPlus, 6], | |
181 ], arrayPlus, sum, 0); | |
182 | |
183 | |
184 // Test error conditions: | |
185 | |
186 var exception = false; | |
187 try { | |
188 new constructor([1]).reduce("not a function"); | |
189 } catch (e) { | |
190 exception = true; | |
191 assertTrue(e instanceof TypeError, | |
192 "reduce callback not a function not throwing TypeError"); | |
193 assertTrue(e.message.indexOf(" is not a function") >= 0, | |
194 "reduce non function TypeError type"); | |
195 } | |
196 assertTrue(exception); | |
197 | |
198 exception = false; | |
199 try { | |
200 new constructor([1]).reduceRight("not a function"); | |
201 } catch (e) { | |
202 exception = true; | |
203 assertTrue(e instanceof TypeError, | |
204 "reduceRight callback not a function not throwing TypeError"); | |
205 assertTrue(e.message.indexOf(" is not a function") >= 0, | |
206 "reduceRight non function TypeError type"); | |
207 } | |
208 assertTrue(exception); | |
209 | |
210 exception = false; | |
211 try { | |
212 new constructor([]).reduce(sum); | |
213 } catch (e) { | |
214 exception = true; | |
215 assertTrue(e instanceof TypeError, | |
216 "reduce no initial value not throwing TypeError"); | |
217 assertEquals("Reduce of empty array with no initial value", e.message, | |
218 "reduce no initial TypeError type"); | |
219 } | |
220 assertTrue(exception); | |
221 | |
222 exception = false; | |
223 try { | |
224 new constructor([]).reduceRight(sum); | |
225 } catch (e) { | |
226 exception = true; | |
227 assertTrue(e instanceof TypeError, | |
228 "reduceRight no initial value not throwing TypeError"); | |
229 assertEquals("Reduce of empty array with no initial value", e.message, | |
230 "reduceRight no initial TypeError type"); | |
231 } | |
232 assertTrue(exception); | |
233 | |
234 // Reduce fails when called on non-TypedArrays | |
235 assertThrows(function() { | |
236 constructor.prototype.reduce.call([], function() {}, null); | |
237 }, TypeError); | |
238 assertThrows(function() { | |
239 constructor.prototype.reduceRight.call([], function() {}, null); | |
240 }, TypeError); | |
241 | |
242 // Shadowing length doesn't affect every, unlike Array.prototype.every | |
adamk
2015/06/04 16:10:10
Stray comment, fine to fix this in a followup (wan
| |
243 var a = new constructor([1, 2]); | |
244 Object.defineProperty(a, 'length', {value: 1}); | |
245 assertEquals(a.reduce(sum, 0), 3); | |
246 assertEquals(Array.prototype.reduce.call(a, sum, 0), 1); | |
247 assertEquals(a.reduceRight(sum, 0), 3); | |
248 assertEquals(Array.prototype.reduceRight.call(a, sum, 0), 1); | |
249 | |
250 assertEquals(1, constructor.prototype.reduce.length); | |
251 assertEquals(1, constructor.prototype.reduceRight.length); | |
252 } | |
OLD | NEW |