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 24var wasPostTestScriptParsed = false; 25 26function removeLink(text) 27{ 28 return text.replace(/<a[^>]*>/g, "").replace(/<\/a>/g, ""); 29} 30 31function description(msg) 32{ 33 print(removeLink(msg)); 34 print("\nOn success, you will see a series of \"PASS\" messages, followed by \"TEST COMPLETE\".\n"); 35 print(); 36} 37 38function debug(msg) 39{ 40 print(msg); 41} 42 43function escapeString(text) 44{ 45 return text.replace(/\0/g, ""); 46} 47 48function testPassed(msg) 49{ 50 print("PASS", escapeString(msg)); 51} 52 53function testFailed(msg) 54{ 55 print("FAIL", escapeString(msg)); 56} 57 58function areArraysEqual(_a, _b) 59{ 60 if (Object.prototype.toString.call(_a) != Object.prototype.toString.call([])) 61 return false; 62 if (_a.length !== _b.length) 63 return false; 64 for (var i = 0; i < _a.length; i++) 65 if (_a[i] !== _b[i]) 66 return false; 67 return true; 68} 69 70function isMinusZero(n) 71{ 72 // the only way to tell 0 from -0 in JS is the fact that 1/-0 is 73 // -Infinity instead of Infinity 74 return n === 0 && 1/n < 0; 75} 76 77function isResultCorrect(_actual, _expected) 78{ 79 if (_expected === 0) 80 return _actual === _expected && (1/_actual) === (1/_expected); 81 if (_actual === _expected) 82 return true; 83 if (typeof(_expected) == "number" && isNaN(_expected)) 84 return typeof(_actual) == "number" && isNaN(_actual); 85 if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([])) 86 return areArraysEqual(_actual, _expected); 87 return false; 88} 89 90function stringify(v) 91{ 92 if (v === 0 && 1/v < 0) 93 return "-0"; 94 else return "" + v; 95} 96 97function shouldBe(_a, _b) 98{ 99 if (typeof _a != "string" || typeof _b != "string") 100 debug("WARN: shouldBe() expects string arguments"); 101 var exception; 102 var _av; 103 try { 104 _av = eval(_a); 105 } catch (e) { 106 exception = e; 107 } 108 var _bv = eval(_b); 109 110 if (exception) 111 testFailed(_a + " should be " + _bv + ". Threw exception " + exception); 112 else if (isResultCorrect(_av, _bv)) 113 testPassed(_a + " is " + _b); 114 else if (typeof(_av) == typeof(_bv)) 115 testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); 116 else 117 testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ")."); 118} 119 120function shouldBeTrue(_a) { shouldBe(_a, "true"); } 121function shouldBeFalse(_a) { shouldBe(_a, "false"); } 122function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } 123function shouldBeNull(_a) { shouldBe(_a, "null"); } 124 125function shouldBeEqualToString(a, b) 126{ 127 if (typeof a !== "string" || typeof b !== "string") 128 debug("WARN: shouldBeEqualToString() expects string arguments"); 129 var unevaledString = JSON.stringify(b); 130 shouldBe(a, unevaledString); 131} 132 133function shouldBeUndefined(_a) 134{ 135 var exception; 136 var _av; 137 try { 138 _av = eval(_a); 139 } catch (e) { 140 exception = e; 141 } 142 143 if (exception) 144 testFailed(_a + " should be undefined. Threw exception " + exception); 145 else if (typeof _av == "undefined") 146 testPassed(_a + " is undefined."); 147 else 148 testFailed(_a + " should be undefined. Was " + _av); 149} 150 151 152function shouldThrow(_a, _e) 153{ 154 var exception; 155 var _av; 156 try { 157 _av = eval(_a); 158 } catch (e) { 159 exception = e; 160 } 161 162 var _ev; 163 if (_e) 164 _ev = eval(_e); 165 166 if (exception) { 167 if (typeof _e == "undefined" || exception == _ev) 168 testPassed(_a + " threw exception " + exception + "."); 169 else 170 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + "."); 171 } else if (typeof _av == "undefined") 172 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined."); 173 else 174 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + "."); 175} 176 177function isSuccessfullyParsed() 178{ 179 successfullyParsed = true; 180 shouldBeTrue("successfullyParsed"); 181 debug("\nTEST COMPLETE\n"); 182} 183 184// It's possible for an async test to call finishJSTest() before js-test-post.js 185// has been parsed. 186function finishJSTest() 187{ 188 wasFinishJSTestCalled = true; 189 if (!wasPostTestScriptParsed) 190 return; 191 isSuccessfullyParsed(); 192} 193