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 } 49 for (var i = 0; i < tests.length; i++) { 50 if ((r=tests[i][0]()) == tests[i][1]) 51 testPassed(tests[i][0] + " is " + tests[i][1]); 52 else 53 testFailed(tests[i][0] + " is " + r + " and should be " + tests[i][1] + "."); 54 } 55} 56 57var letterA = "a"; 58var letterB = "b"; 59var letter0 = "0"; 60var One = 1; 61var Zero = 0; 62doTest('"a"', '"b"', -1); 63doTest('"a"', '"a"', 0); 64doTest('"b"', '"a"', 1); 65doTest('letterA', '"b"', -1); 66doTest('letterA', '"a"', 0); 67doTest('"b"', '"a"', 1); 68doTest('letterA', '"b"', -1); 69doTest('letterA', 'letterA', 0); 70doTest('"b"', 'letterA', 1); 71doTest('"a"', '"b"', -1); 72doTest('"a"', 'letterA', 0); 73doTest('"b"', 'letterA', 1); 74 75doTest('"a"', '0', NaN); 76doTest('0', '"a"', NaN); 77doTest('letterA', '0', NaN); 78doTest('letterA', '"a"', 0); 79doTest('0', '"a"', NaN); 80doTest('letterA', 'letterA', 0); 81doTest('0', 'letterA', NaN); 82doTest('"a"', 'letterA', 0); 83doTest('0', 'letterA', NaN); 84 85 86doTest('0', '"0"', 0); 87doTest('Zero', 'letter0', 0); 88doTest('0', 'letter0', 0); 89doTest('Zero', '"0"', 0); 90doTest('-1', '"0"', -1); 91doTest('1', '"0"', 1); 92doTest('0', '"-1"', 1); 93doTest('0', '"1"', -1); 94 95doTest('0', '1', -1); 96doTest('0', '0', 0); 97doTest('1', '0', 1); 98doTest('Zero', '1', -1); 99doTest('Zero', '0', 0); 100doTest('1', 'Zero', 1); 101doTest('0', 'One', -1); 102doTest('One', '0', 1); 103 104shouldBeTrue("0 <= 0"); 105shouldBeTrue("0 <= 1"); 106shouldBeTrue("1 <= 1"); 107shouldBeTrue("0 < 1"); 108shouldBeFalse("1 <= 0"); 109shouldBeFalse("2 <= 1"); 110shouldBeFalse("1 < 1"); 111shouldBeFalse("2 < 1"); 112 113 shouldBeTrue("if (0 <= 0 || 0) true; else false"); 114 shouldBeTrue("if (0 <= 1 || 0) true; else false"); 115 shouldBeTrue("if (1 <= 1 || 0) true; else false"); 116 shouldBeTrue("if (0 < 1 || 0) true; else false"); 117shouldBeFalse("if (1 <= 0 || 0) true; else false"); 118shouldBeFalse("if (2 <= 1 || 0) true; else false"); 119shouldBeFalse("if (1 < 1 || 0) true; else false"); 120shouldBeFalse("if (2 < 1 || 0) true; else false"); 121 122shouldBeTrue("0 <= 0.5"); 123shouldBeTrue("0 <= 1.5"); 124shouldBeTrue("1 <= 1.5"); 125shouldBeTrue("0 < 1.5"); 126shouldBeFalse("1 <= 0.5"); 127shouldBeFalse("2 <= 1.5"); 128shouldBeTrue("1 < 1.5"); 129shouldBeFalse("2 < 1.5"); 130 131 shouldBeTrue("if (0 <= 0.5 || 0) true; else false"); 132 shouldBeTrue("if (0 <= 1.5 || 0) true; else false"); 133 shouldBeTrue("if (1 <= 1.5 || 0) true; else false"); 134 shouldBeTrue("if (0 < 1.5 || 0) true; else false"); 135shouldBeFalse("if (1 <= 0.5 || 0) true; else false"); 136shouldBeFalse("if (2 <= 1.5 || 0) true; else false"); 137 shouldBeTrue("if (1 < 1.5 || 0) true; else false"); 138shouldBeFalse("if (2 < 1.5 || 0) true; else false"); 139 140var zero = 0; 141var one = 1; 142var two = 2; 143var p5 = 0.5; 144var p15 = 1.5; 145 146 147shouldBeTrue("zero <= zero"); 148shouldBeTrue("zero <= 1"); 149shouldBeTrue("1 <= 1"); 150shouldBeTrue("zero < 1"); 151shouldBeFalse("1 <= zero"); 152shouldBeFalse("2 <= 1"); 153shouldBeFalse("1 < 1"); 154shouldBeFalse("2 < 1"); 155 156 shouldBeTrue("if (zero <= zero || 0) true; else false"); 157 shouldBeTrue("if (zero <= 1 || zero) true; else false"); 158 shouldBeTrue("if (1 <= 1 || zero) true; else false"); 159 shouldBeTrue("if (zero < 1 || zero) true; else false"); 160shouldBeFalse("if (1 <= zero || zero) true; else false"); 161shouldBeFalse("if (2 <= 1 || zero) true; else false"); 162shouldBeFalse("if (1 < 1 || zero) true; else false"); 163shouldBeFalse("if (2 < 1 || zero) true; else false"); 164 165shouldBeTrue("0 <= p5"); 166shouldBeTrue("0 <= p15"); 167shouldBeTrue("1 <= p15"); 168shouldBeTrue("0 < p15"); 169shouldBeFalse("1 <= p5"); 170shouldBeFalse("2 <= p15"); 171shouldBeTrue("1 < p15"); 172shouldBeFalse("2 < p15"); 173 174 shouldBeTrue("if (0 <= p5 || 0) true; else false"); 175 shouldBeTrue("if (0 <= p15 || 0) true; else false"); 176 shouldBeTrue("if (1 <= p15 || 0) true; else false"); 177 shouldBeTrue("if (0 < p15 || 0) true; else false"); 178shouldBeFalse("if (1 <= p5 || 0) true; else false"); 179shouldBeFalse("if (2 <= p15 || 0) true; else false"); 180 shouldBeTrue("if (1 < p15 || 0) true; else false"); 181shouldBeFalse("if (2 < p15 || 0) true; else false"); 182 183 184shouldBeTrue("zero <= zero"); 185shouldBeTrue("zero <= one"); 186shouldBeTrue("one <= one"); 187shouldBeTrue("zero < one"); 188shouldBeFalse("one <= zero"); 189shouldBeFalse("two <= one"); 190shouldBeFalse("one < one"); 191shouldBeFalse("two < one"); 192 193 shouldBeTrue("if (zero <= zero || 0) true; else false"); 194 shouldBeTrue("if (zero <= one || zero) true; else false"); 195 shouldBeTrue("if (one <= one || zero) true; else false"); 196 shouldBeTrue("if (zero < one || zero) true; else false"); 197shouldBeFalse("if (one <= zero || zero) true; else false"); 198shouldBeFalse("if (two <= one || zero) true; else false"); 199shouldBeFalse("if (one < one || zero) true; else false"); 200shouldBeFalse("if (two < one || zero) true; else false"); 201 202shouldBeTrue("0 <= p5"); 203shouldBeTrue("0 <= p15"); 204shouldBeTrue("one <= p15"); 205shouldBeTrue("0 < p15"); 206shouldBeFalse("one <= p5"); 207shouldBeFalse("two <= p15"); 208shouldBeTrue("one < p15"); 209shouldBeFalse("two < p15"); 210 211 shouldBeTrue("if (0 <= p5 || 0) true; else false"); 212 shouldBeTrue("if (0 <= p15 || 0) true; else false"); 213 shouldBeTrue("if (one <= p15 || 0) true; else false"); 214 shouldBeTrue("if (0 < p15 || 0) true; else false"); 215shouldBeFalse("if (one <= p5 || 0) true; else false"); 216shouldBeFalse("if (two <= p15 || 0) true; else false"); 217 shouldBeTrue("if (one < p15 || 0) true; else false"); 218shouldBeFalse("if (two < p15 || 0) true; else false"); 219 220 221shouldBeFalse(" 0 < NaN"); 222shouldBeFalse(" NaN < NaN"); 223shouldBeFalse(" 0 <= NaN"); 224shouldBeFalse(" NaN <= NaN"); 225shouldBeFalse(" 0.5 < NaN"); 226shouldBeFalse(" NaN < NaN"); 227shouldBeFalse(" 0.5 <= NaN"); 228shouldBeFalse(" NaN <= NaN"); 229shouldBeFalse(" zero < NaN"); 230shouldBeFalse("zero <= NaN"); 231shouldBeFalse(" NaN <= NaN"); 232shouldBeFalse(" p5 < NaN"); 233shouldBeFalse("NaN < NaN"); 234shouldBeFalse("p5 <= NaN"); 235shouldBeFalse("NaN <= NaN"); 236 237 238shouldBeFalse("if ( 0 < NaN) true; else false;"); 239shouldBeFalse("if ( NaN < NaN) true; else false;"); 240shouldBeFalse("if ( 0 <= NaN) true; else false;"); 241shouldBeFalse("if ( NaN <= NaN) true; else false;"); 242shouldBeFalse("if ( 0.5 < NaN) true; else false;"); 243shouldBeFalse("if ( NaN < NaN) true; else false;"); 244shouldBeFalse("if ( 0.5 <= NaN) true; else false;"); 245shouldBeFalse("if ( NaN <= NaN) true; else false;"); 246shouldBeFalse("if ( zero < NaN) true; else false;"); 247shouldBeFalse("if (zero <= NaN) true; else false;"); 248shouldBeFalse("if ( NaN <= NaN) true; else false;"); 249shouldBeFalse("if ( p5 < NaN) true; else false;"); 250shouldBeFalse("if (NaN < NaN) true; else false;"); 251shouldBeFalse("if (p5 <= NaN) true; else false;"); 252shouldBeFalse("if (NaN <= NaN) true; else false;"); 253