|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 //----------------------------------------------------------------------------- |
|
7 var BUGNUMBER = 363040; |
|
8 var summary = 'Array.prototype.reduce, Array.prototype.reduceRight'; |
|
9 var actual = ''; |
|
10 var expect = ''; |
|
11 |
|
12 |
|
13 //----------------------------------------------------------------------------- |
|
14 test(); |
|
15 //----------------------------------------------------------------------------- |
|
16 |
|
17 function test() |
|
18 { |
|
19 enterFunc ('test'); |
|
20 printBugNumber(BUGNUMBER); |
|
21 printStatus (summary); |
|
22 |
|
23 function f(x,y) { return '(' + x + '+' + y + ')';} |
|
24 |
|
25 var testdesc; |
|
26 var arr0elms = []; |
|
27 var arr1elms = [1]; |
|
28 var arr2elms = [1, 2]; |
|
29 |
|
30 testdesc = 'Test reduce of empty array without initializer.'; |
|
31 try |
|
32 { |
|
33 expect = 'TypeError: reduce of empty array with no initial value'; |
|
34 arr0elms.reduce(f); |
|
35 } |
|
36 catch(ex) |
|
37 { |
|
38 actual = ex + ''; |
|
39 } |
|
40 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
41 |
|
42 testdesc = 'Test reduceRight of empty array without initializer.'; |
|
43 try |
|
44 { |
|
45 expect = 'TypeError: reduce of empty array with no initial value'; |
|
46 arr0elms.reduceRight(f); |
|
47 } |
|
48 catch(ex) |
|
49 { |
|
50 actual = ex + ''; |
|
51 } |
|
52 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
53 |
|
54 testdesc = 'Test reduce of empty array with initial value.'; |
|
55 expect = 'a'; |
|
56 actual = arr0elms.reduce(f, 'a'); |
|
57 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
58 |
|
59 testdesc = 'Test reduceRight of empty array with initial value.'; |
|
60 expect = 'a'; |
|
61 actual = arr0elms.reduceRight(f, 'a'); |
|
62 reportCompare(expect + '', actual + '', testdesc +' : ' + expect); |
|
63 |
|
64 testdesc = 'Test reduce of 1 element array with no initializer.'; |
|
65 expect = '1'; |
|
66 actual = arr1elms.reduce(f); |
|
67 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
68 |
|
69 testdesc = 'Test reduceRight of 1 element array with no initializer.'; |
|
70 expect = '1'; |
|
71 actual = arr1elms.reduceRight(f); |
|
72 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
73 |
|
74 testdesc = 'Test reduce of 2 element array with no initializer.'; |
|
75 expect = '(1+2)'; |
|
76 actual = arr2elms.reduce(f); |
|
77 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
78 |
|
79 testdesc = 'Test reduce of 2 element array with initializer.'; |
|
80 expect = '((a+1)+2)'; |
|
81 actual = arr2elms.reduce(f,'a'); |
|
82 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
83 |
|
84 testdesc = 'Test reduceRight of 2 element array with no initializer.'; |
|
85 expect = '(2+1)'; |
|
86 actual = arr2elms.reduceRight(f); |
|
87 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
88 |
|
89 testdesc = 'Test reduceRight of 2 element array with no initializer.'; |
|
90 expect = '((a+2)+1)'; |
|
91 actual = arr2elms.reduceRight(f,'a'); |
|
92 reportCompare(expect + '', actual + '', testdesc + ' : ' + expect); |
|
93 |
|
94 exitFunc ('test'); |
|
95 } |