1 // Copyright 2010 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 function MaxLT(x, y) { 29 if (x < y) return y; 30 return x; 31 } 32 33 function MaxLE(x, y) { 34 if (x <= y) return y; 35 return x; 36 } 37 38 function MaxGE(x, y) { 39 if (x >= y) return x; 40 return y; 41 } 42 43 function MaxGT(x, y) { 44 if (x > y) return x; 45 return y; 46 } 47 48 49 // First test primitive values. 50 function TestPrimitive(max, x, y) { 51 assertEquals(max, MaxLT(x, y), "MaxLT - primitive"); 52 assertEquals(max, MaxLE(x, y), "MaxLE - primitive"); 53 assertEquals(max, MaxGE(x, y), "MaxGE - primitive"); 54 assertEquals(max, MaxGT(x, y), "MaxGT - primitive"); 55 } 56 57 TestPrimitive(1, 0, 1); 58 TestPrimitive(1, 1, 0); 59 TestPrimitive(4, 3, 4); 60 TestPrimitive(4, 4, 3); 61 TestPrimitive(0, -1, 0); 62 TestPrimitive(0, 0, -1) 63 TestPrimitive(-2, -2, -3); 64 TestPrimitive(-2, -3, -2); 65 66 TestPrimitive(1, 0.1, 1); 67 TestPrimitive(1, 1, 0.1); 68 TestPrimitive(4, 3.1, 4); 69 TestPrimitive(4, 4, 3.1); 70 TestPrimitive(0, -1.1, 0); 71 TestPrimitive(0, 0, -1.1) 72 TestPrimitive(-2, -2, -3.1); 73 TestPrimitive(-2, -3.1, -2); 74 75 76 // Test non-primitive values and watch for valueOf call order. 77 function TestNonPrimitive(order, f) { 78 var result = ""; 79 var x = { valueOf: function() { result += "x"; } }; 80 var y = { valueOf: function() { result += "y"; } }; 81 f(x, y); 82 assertEquals(order, result); 83 } 84 85 TestNonPrimitive("xy", MaxLT); 86 TestNonPrimitive("yx", MaxLE); 87 TestNonPrimitive("xy", MaxGE); 88 TestNonPrimitive("yx", MaxGT); 89 90 // Test compare in case of aliased registers. 91 function CmpX(x) { if (x == x) return 42; } 92 assertEquals(42, CmpX(0)); 93 94 function CmpXY(x) { var y = x; if (x == y) return 42; } 95 assertEquals(42, CmpXY(0)); 96 97 98 // Test compare against null. 99 function CmpNullValue(x) { return x == null; } 100 assertEquals(false, CmpNullValue(42)); 101 102 function CmpNullTest(x) { if (x == null) return 42; return 0; } 103 assertEquals(42, CmpNullTest(null)); 104 105 var g1 = 0; 106 function CmpNullEffect() { (g1 = 42) == null; } 107 CmpNullEffect(); 108 assertEquals(42, g1); 109