1 // Copyright 2013 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 // Flags: --allow-natives-syntax 29 30 var test_id = 0; 31 32 function testTrunc(expected, input) { 33 var test = new Function('n', 34 '"' + (test_id++) + '";return Math.trunc(n)'); 35 assertEquals(expected, test(input)); 36 assertEquals(expected, test(input)); 37 assertEquals(expected, test(input)); 38 %OptimizeFunctionOnNextCall(test); 39 assertEquals(expected, test(input)); 40 41 var test_double_input = new Function( 42 'n', 43 '"' + (test_id++) + '";return Math.trunc(+n)'); 44 assertEquals(expected, test_double_input(input)); 45 assertEquals(expected, test_double_input(input)); 46 assertEquals(expected, test_double_input(input)); 47 %OptimizeFunctionOnNextCall(test_double_input); 48 assertEquals(expected, test_double_input(input)); 49 50 var test_double_output = new Function( 51 'n', 52 '"' + (test_id++) + '";return Math.trunc(n) + -0.0'); 53 assertEquals(expected, test_double_output(input)); 54 assertEquals(expected, test_double_output(input)); 55 assertEquals(expected, test_double_output(input)); 56 %OptimizeFunctionOnNextCall(test_double_output); 57 assertEquals(expected, test_double_output(input)); 58 } 59 60 function test() { 61 // Ensure that a negative zero coming from Math.trunc is properly handled 62 // by other operations. 63 function itrunc(x) { 64 return 1 / Math.trunc(x); 65 } 66 assertEquals(Infinity, itrunc(0)); 67 assertEquals(-Infinity, itrunc(-0)); 68 assertEquals(Infinity, itrunc(Math.PI / 4)); 69 assertEquals(-Infinity, itrunc(-Math.sqrt(2) / 2)); 70 assertEquals(-Infinity, itrunc({valueOf: function() { return "-0.1"; }})); 71 %OptimizeFunctionOnNextCall(itrunc); 72 73 testTrunc(100, 100); 74 testTrunc(-199, -199); 75 testTrunc(100, 100.1); 76 testTrunc(4503599627370495.0, 4503599627370495.0); 77 testTrunc(4503599627370496.0, 4503599627370496.0); 78 testTrunc(-4503599627370495.0, -4503599627370495.0); 79 testTrunc(-4503599627370496.0, -4503599627370496.0); 80 testTrunc(9007199254740991.0, 9007199254740991.0); 81 testTrunc(-9007199254740991.0, -9007199254740991.0); 82 testTrunc(0, []); 83 testTrunc(1, [1]); 84 testTrunc(-100, [-100.1]); 85 testTrunc(-100, {toString: function() { return "-100.3"; }}); 86 testTrunc(10, {toString: function() { return 10.1; }}); 87 testTrunc(-1, {valueOf: function() { return -1.1; }}); 88 testTrunc(-Infinity, -Infinity); 89 testTrunc(Infinity, Infinity); 90 testTrunc(-Infinity, "-Infinity"); 91 testTrunc(Infinity, "Infinity"); 92 93 assertTrue(isNaN(Math.trunc("abc"))); 94 assertTrue(isNaN(Math.trunc({}))); 95 assertTrue(isNaN(Math.trunc([1, 1]))); 96 } 97 98 // Test in a loop to cover the custom IC and GC-related issues. 99 for (var i = 0; i < 10; i++) { 100 test(); 101 new Array(i * 10000); 102 } 103