• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// This test will not pass on FLOAT32 due to precision issues
16
17var func = function(a, b) {
18  return a + b;
19}
20
21// check function type
22try {
23  [0].reduceRight(new Object());
24  assert(false);
25} catch(e) {
26  assert(e instanceof TypeError);
27}
28
29// check for init value
30try {
31  [].reduceRight(func);
32  assert(false);
33} catch(e) {
34  assert(e instanceof TypeError);
35}
36
37try {
38  var a = new Array();
39  a.length = 10;
40  a.reduceRight(func);
41  assert (false);
42} catch (e) {
43  assert (e instanceof TypeError)
44}
45
46// various checks
47assert([].reduceRight(func, 1) === 1);
48
49assert([].reduceRight(func, undefined) === undefined);
50
51assert([0].reduceRight(func) === 0);
52
53assert([0, 1].reduceRight(func) === 1);
54
55assert([0, 1].reduceRight(func, 1) === 2);
56
57assert([0, 1, 2, 3].reduceRight(func, 1) === 7);
58
59assert (["A","B"].reduceRight(func) === "BA");
60
61assert (["A","B"].reduceRight(func, "Init:") === "Init:BA");
62
63assert ([0, 1].reduceRight(func, 3.2) === 4.2);
64
65assert ([0, "x", 1].reduceRight(func) === "1x0");
66
67assert ([0, "x", 1].reduceRight(func, 3.2) === "4.2x0");
68
69var long_array = [0, 1];
70assert (long_array.reduceRight(func,10) === 11);
71
72long_array[10000] = 1;
73assert (long_array.reduceRight(func,10) === 12);
74
75var accessed = false;
76function callbackfn(prevVal, curVal, idx, obj) {
77    accessed = true;
78    return typeof prevVal === "undefined";
79}
80
81var obj = { 0: 11, length: 1 };
82
83assert (Array.prototype.reduceRight.call(obj, callbackfn, undefined) === true && accessed);
84