1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25"This test checks the behavior of the reduceRight() method on a number of objects."
26);
27
28function toObject(array) {
29    var o = {};
30    for (var i in array)
31        o[i] = array[i];
32    o.length = array.length;
33    o.reduceRight = Array.prototype.reduceRight;
34    return o;
35}
36function toUnorderedObject(array) {
37    var o = {};
38    var props = [];
39    for (var i in array)
40        props.push(i);
41    for (var i = props.length - 1; i >= 0; i--)
42        o[props[i]] = array[props[i]];
43    o.length = array.length;
44    o.reduceRight = Array.prototype.reduceRight;
45    return o;
46}
47
48shouldBe("[0,1,2,3].reduceRight(function(a,b){ return a + b; })", "6");
49shouldBe("[1,2,3].reduceRight(function(a,b){ return a + b; })", "6");
50shouldBe("[0,1,2,3].reduceRight(function(a,b){ return a + b; }, 4)", "10");
51shouldBe("[1,2,3].reduceRight(function(a,b){ return a + b; }, 4)", "10");
52shouldBe("toObject([0,1,2,3]).reduceRight(function(a,b){ return a + b; })", "6");
53shouldBe("toObject([1,2,3]).reduceRight(function(a,b){ return a + b; })", "6");
54shouldBe("toObject([0,1,2,3]).reduceRight(function(a,b){ return a + b; }, 4)", "10");
55shouldBe("toObject([1,2,3]).reduceRight(function(a,b){ return a + b; }, 4)", "10");
56shouldBe("toUnorderedObject([0,1,2,3]).reduceRight(function(a,b){ return a + b; })", "6");
57shouldBe("toUnorderedObject([1,2,3]).reduceRight(function(a,b){ return a + b; })", "6");
58shouldBe("toUnorderedObject([0,1,2,3]).reduceRight(function(a,b){ return a + b; }, 4)", "10");
59shouldBe("toUnorderedObject([1,2,3]).reduceRight(function(a,b){ return a + b; }, 4)", "10");
60sparseArray = [];
61sparseArray[10] = 10;
62shouldBe("sparseArray.reduceRight(function(a,b){ return a + b; }, 0)", "10");
63shouldBe("toObject(sparseArray).reduceRight(function(a,b){ return a + b; }, 0)", "10");
64var callCount = 0;
65shouldBe("sparseArray.reduceRight(function(a,b){ callCount++; }); callCount", "0");
66callCount = 0;
67shouldBe("toObject(sparseArray).reduceRight(function(a,b){ callCount++; }); callCount", "0");
68var callCount = 0;
69shouldBe("sparseArray.reduceRight(function(a,b){ callCount++; }, 0); callCount", "1");
70callCount = 0;
71shouldBe("toObject(sparseArray).reduceRight(function(a,b){ callCount++; }, 0); callCount", "1");
72callCount = 0;
73shouldBe("[0,1,2,3,4].reduceRight(function(a,b){ callCount++; }, 0); callCount", "5");
74callCount = 0;
75shouldBe("[0,1,2,3,4].reduceRight(function(a,b){ callCount++; }); callCount", "4");
76callCount = 0;
77shouldBe("[1, 2, 3, 4].reduceRight(function(a,b, i, thisObj){ thisObj.length--; callCount++; return a + b; }, 0); callCount", "4");
78callCount = 0;
79shouldBe("[1, 2, 3, 4].reduceRight(function(a,b, i, thisObj){ thisObj.length = 1; callCount++; return a + b; }, 0); callCount", "2");
80callCount = 0;
81shouldBe("[1, 2, 3, 4].reduceRight(function(a,b, i, thisObj){ thisObj.length++; callCount++; return a + b; }, 0); callCount", "4");
82callCount = 0;
83shouldBe("toObject([1, 2, 3, 4]).reduceRight(function(a,b, i, thisObj){ thisObj.length--; callCount++; return a + b; }, 0); callCount", "4");
84callCount = 0;
85shouldBe("toObject([1, 2, 3, 4]).reduceRight(function(a,b, i, thisObj){ thisObj.length++; callCount++; return a + b; }, 0); callCount", "4");
86
87shouldBe("[[0,1], [2,3], [4,5]].reduceRight(function(a,b) {return a.concat(b);}, [])", "[4,5,2,3,0,1]");
88shouldBe("toObject([[0,1], [2,3], [4,5]]).reduceRight(function(a,b) {return a.concat(b);}, [])", "[4,5,2,3,0,1]");
89shouldBe("toObject([0,1,2,3,4,5]).reduceRight(function(a,b,i) {return a.concat([i,b]);}, [])", "[5,5,4,4,3,3,2,2,1,1,0,0]");
90shouldBe("toUnorderedObject([[0,1], [2,3], [4,5]]).reduceRight(function(a,b) {return a.concat(b);}, [])", "[4,5,2,3,0,1]");
91shouldBe("toUnorderedObject([0,1,2,3,4,5]).reduceRight(function(a,b,i) {return a.concat([i,b]);}, [])", "[5,5,4,4,3,3,2,2,1,1,0,0]");
92shouldBe("[0,1,2,3,4,5].reduceRight(function(a,b,i) {return a.concat([i,b]);}, [])", "[5,5,4,4,3,3,2,2,1,1,0,0]");
93shouldBe("[2,3].reduceRight(function() {'use strict'; return this;})", "undefined");
94