Home | History | Annotate | Download | only in webkit
      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("test that comparison operators work correctly.")
     25 
     26 function 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 }
     39 function 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 
     59 var letterA = "a";
     60 var letterB = "b";
     61 var One = 1;
     62 var Zero = 0;
     63 doTest('"a"', '"b"', -1);
     64 doTest('"a"', '"a"', 0);
     65 doTest('"b"', '"a"', 1);
     66 doTest('letterA', '"b"', -1);
     67 doTest('letterA', '"a"', 0);
     68 doTest('"b"', '"a"', 1);
     69 doTest('letterA', '"b"', -1);
     70 doTest('letterA', 'letterA', 0);
     71 doTest('"b"', 'letterA', 1);
     72 doTest('"a"', '"b"', -1);
     73 doTest('"a"', 'letterA', 0);
     74 doTest('"b"', 'letterA', 1);
     75 
     76 doTest('"a"', '0', NaN);
     77 doTest('0', '"a"', NaN);
     78 doTest('letterA', '0', NaN);
     79 doTest('letterA', '"a"', 0);
     80 doTest('0', '"a"', NaN);
     81 doTest('letterA', 'letterA', 0);
     82 doTest('0', 'letterA', NaN);
     83 doTest('"a"', 'letterA', 0);
     84 doTest('0', 'letterA', NaN);
     85 
     86 
     87 doTest('0', '1', -1);
     88 doTest('0', '0', 0);
     89 doTest('1', '0', 1);
     90 doTest('Zero', '1', -1);
     91 doTest('Zero', '0', 0);
     92 doTest('1', 'Zero', 1);
     93 doTest('0', 'One', -1);
     94 doTest('One', '0', 1);
     95