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("test that comparison operators work correctly.") 25 26function makeTest(start, end, expression, relationship, override, invert) { 27 var resultValue = eval(relationship + expression + 0) || !!override; 28 if (invert) 29 resultValue = !resultValue; 30 var expr = start + expression + end; 31 var result = []; 32 function func(content) { var f = new Function(content); f.toString = function(){ return content}; return f; } 33 result.push([new func("return " + expr + ";"), resultValue]); 34 result.push([new func("if (" + expr + ") return true; return false;"), resultValue]); 35 result.push([new func("var k = 0; while (" + expr + ") if (k++) return true; return false;"), resultValue]); 36 result.push([new func("var k = 0; for (; " + expr + "; ) if (k++) return true; return false;"), resultValue]); 37 return result; 38} 39function doTest(lhs, rhs, relationship) { 40 var expressionParts = [["(",")"],["(", ") || 1", true],["(", ") && 1"],["(", ") || 1", true],["1 || (",")", true],["1 && (",")"]]; 41 var expressions = []; 42 var tests = []; 43 for (var i = 0; i < expressionParts.length; i++) { 44 var start = expressionParts[i][0] + lhs; 45 var end = String(rhs) + expressionParts[i][1]; 46 tests.push.apply(tests, makeTest(start, end, "==", relationship, expressionParts[i][2])); 47 tests.push.apply(tests, makeTest(start, end, "!=", relationship, expressionParts[i][2])); 48 tests.push.apply(tests, makeTest(start, end, "===", relationship, expressionParts[i][2])); 49 tests.push.apply(tests, makeTest(start, end, "!==", relationship, expressionParts[i][2])); 50 } 51 for (var i = 0; i < tests.length; i++) { 52 if ((r=tests[i][0]()) == tests[i][1]) 53 testPassed(tests[i][0] + " is " + tests[i][1]); 54 else 55 testFailed(tests[i][0] + " is " + r + " and should be " + tests[i][1] + "."); 56 } 57} 58 59var letterA = "a"; 60var letterB = "b"; 61var One = 1; 62var Zero = 0; 63doTest('"a"', '"b"', -1); 64doTest('"a"', '"a"', 0); 65doTest('"b"', '"a"', 1); 66doTest('letterA', '"b"', -1); 67doTest('letterA', '"a"', 0); 68doTest('"b"', '"a"', 1); 69doTest('letterA', '"b"', -1); 70doTest('letterA', 'letterA', 0); 71doTest('"b"', 'letterA', 1); 72doTest('"a"', '"b"', -1); 73doTest('"a"', 'letterA', 0); 74doTest('"b"', 'letterA', 1); 75 76doTest('"a"', '0', NaN); 77doTest('0', '"a"', NaN); 78doTest('letterA', '0', NaN); 79doTest('letterA', '"a"', 0); 80doTest('0', '"a"', NaN); 81doTest('letterA', 'letterA', 0); 82doTest('0', 'letterA', NaN); 83doTest('"a"', 'letterA', 0); 84doTest('0', 'letterA', NaN); 85 86 87doTest('0', '1', -1); 88doTest('0', '0', 0); 89doTest('1', '0', 1); 90doTest('Zero', '1', -1); 91doTest('Zero', '0', 0); 92doTest('1', 'Zero', 1); 93doTest('0', 'One', -1); 94doTest('One', '0', 1); 95