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