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].reduce(new Object()); 24 assert(false); 25} 26catch(e) { 27 assert(e instanceof TypeError); 28} 29 30// check for init value 31try { 32 [].reduce(func); 33 assert(false); 34} 35catch(e) { 36 assert(e instanceof TypeError); 37} 38 39// various checks 40assert ([].reduce(func, 1) === 1); 41 42assert ([].reduce(func, undefined) === undefined); 43 44assert ([0].reduce(func) === 0); 45 46assert ([0, 1].reduce(func) === 1); 47 48assert ([0, 1].reduce(func, 1) === 2); 49 50assert ([0, 1, 2, 3].reduce(func, 1) === 7); 51 52assert (["A","B"].reduce(func) === "AB"); 53 54assert (["A","B"].reduce(func, "Init:") === "Init:AB"); 55 56assert ([0, 1].reduce(func, 3.2) === 4.2); 57 58assert ([0, "x", 1].reduce(func) === "0x1"); 59 60assert ([0, "x", 1].reduce(func, 3.2) === "3.2x1"); 61 62var long_array = [0, 1]; 63assert (long_array.reduce(func,10) === 11); 64 65long_array[10000] = 1; 66assert (long_array.reduce(func,10) === 12); 67 68var accessed = false; 69function callbackfn(prevVal, curVal, idx, obj) { 70 accessed = true; 71 return typeof prevVal === "undefined"; 72} 73 74var obj = { 0: 11, length: 1 }; 75 76assert (Array.prototype.reduce.call(obj, callbackfn, undefined) === true && accessed); 77