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 every() method on Array objects." 26); 27 28debug("1.0 Single Argument Testing"); 29function isBigEnough(element, index, array) { 30 return (element >= 10); 31} 32shouldBeFalse("[12, 5, 8, 130, 44].every(isBigEnough)"); 33shouldBeTrue("[12, 54, 18, 130, 44].every(isBigEnough)"); 34debug(""); 35 36debug("2.0 Two Argument Testing"); 37var predicate = { 38 comparison: 11, 39 isBigEnough: function(s) { 40 return (s >= comparison); 41 } 42}; 43shouldBeFalse("[12, 5, 10, 130, 44].every(isBigEnough, predicate)"); 44shouldBeTrue("[12, 54, 18, 130, 44].every(isBigEnough, predicate)"); 45debug(""); 46 47debug("3.0 Array Mutation Tests"); 48debug(""); 49 50debug("3.1 Array Element Removal"); 51function isBigEnoughAndPop(element, index, array) { 52 array.pop(); 53 return (element >= 10); 54} 55shouldBeFalse("[12, 5, 8, 130, 44].every(isBigEnoughAndPop)"); 56shouldBeTrue("[12, 54, 18, 130, 44].every(isBigEnoughAndPop)"); 57debug(""); 58 59debug("3.2 Array Element Changing"); 60function isBigEnoughAndChange(element, index, array) { 61 array[array.length-1-index]= 5; 62 return (element >= 10); 63} 64shouldBeFalse("[12, 5, 8, 130, 44].every(isBigEnoughAndChange)"); 65shouldBeFalse("[12, 54, 18, 130, 44].every(isBigEnoughAndChange)"); 66debug(""); 67 68debug("3.3 Array Element Addition"); 69function isBigEnoughAndPush(element, index, array) { 70 array.push(131); 71 return (element >= 131); 72} 73shouldBeFalse("[12, 5, 8, 130, 44].every(isBigEnoughAndPush)"); 74shouldBeFalse("[12, 54, 18, 130, 44].every(isBigEnoughAndPush)"); 75debug(""); 76 77debug("4.0 Exception Test"); 78function isBigEnoughAndException(element, index, array) { 79 if(index==1) throw "exception from function"; 80 return (element >= 10); 81} 82shouldThrow("[12, 5, 8, 130, 44].every(isBigEnoughAndException)", '"exception from function"'); 83shouldThrow("[12, 54, 18, 130, 44].every(isBigEnoughAndException)", '"exception from function"'); 84debug(""); 85 86debug("5.0 Wrong Type for Callback Test"); 87shouldThrow("[12, 5, 8, 130, 44].every(5)"); 88shouldThrow("[12, 5, 8, 130, 44].every('wrong')"); 89shouldThrow("[12, 5, 8, 130, 44].every(new Object())"); 90shouldThrow("[12, 5, 8, 130, 44].every(null)"); 91shouldThrow("[12, 5, 8, 130, 44].every(undefined)"); 92shouldThrow("[12, 5, 8, 130, 44].every()"); 93debug(""); 94 95debug('6.0 Early Exit ("Short Circuiting")'); 96var accumulator = new Array(); 97function isBigEnoughShortCircuit(element, index, array) { 98 accumulator.push(element); 99 return (element >= 10); 100} 101shouldBeFalse("[12, 5, 8, 130, 44].every(isBigEnoughShortCircuit)"); 102shouldBe("accumulator.toString()", "[12, 5].toString()"); 103accumulator.length = 0; 104shouldBeTrue("[12, 54, 18, 130, 44].every(isBigEnoughShortCircuit)"); 105shouldBe("accumulator.toString()", "[12, 54, 18, 130, 44].toString()"); 106debug(""); 107 108debug('7.0 Behavior for Holes in Arrays'); 109var arr = [5, 5, 5, 5]; 110delete arr[1]; 111function isNotUndefined(element, index, array) { 112 return typeof element !== "undefined"; 113} 114shouldBeTrue("arr.every(isNotUndefined)"); 115arr = new Array(20); 116shouldBeTrue("arr.every(isNotUndefined)"); 117