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 29 // Flags: --nodead-code-elimination --fold-constants --allow-natives-syntax 30 31 function test(f) { 32 f(); 33 f(); 34 %OptimizeFunctionOnNextCall(f); 35 f(); 36 // Assert that there has been no deopt. 37 assertOptimized(f); 38 } 39 40 test(function add() { 41 assertEquals(2, 1 + 1); 42 assertEquals(2.5, 1.25 + 1.25); 43 assertEquals("Infinity", String(Infinity + Infinity)); 44 assertEquals("Infinity", String(Infinity + 3)); 45 assertEquals("NaN", String(Infinity + (-Infinity))); 46 assertEquals("NaN", String(NaN + 2)); 47 assertEquals("-Infinity", String(1 / (-0.0 + (-0.0)))); 48 assertEquals("Infinity", String(1 / (-0.0 + 0.0))); 49 }); 50 51 test(function inc() { 52 var a = 1; 53 var b = Infinity; 54 var c = -Infinity; 55 var d = NaN; 56 assertEquals(2, ++a); 57 assertEquals("Infinity", String(++b)); 58 assertEquals("-Infinity", String(++c)); 59 assertEquals("NaN", String(++d)); 60 }); 61 62 test(function dec() { 63 var a = 1; 64 var b = Infinity; 65 var c = -Infinity; 66 var d = NaN; 67 assertEquals(0, --a); 68 assertEquals("Infinity", String(--b)); 69 assertEquals("-Infinity", String(--c)); 70 assertEquals("NaN", String(--d)); 71 }); 72 73 test(function sub() { 74 assertEquals(0, 1 - 1); 75 assertEquals(0.5, 1.5 - 1); 76 assertEquals("Infinity", String(Infinity - (-Infinity))); 77 assertEquals("Infinity", String(Infinity - 3)); 78 assertEquals("NaN", String(Infinity - Infinity)); 79 assertEquals("NaN", String(NaN - 2)); 80 assertEquals("-Infinity", String(1 / (-0.0 - 0.0))); 81 assertEquals("Infinity", String(1 / (0.0 - 0.0))); 82 }); 83 84 test(function mul() { 85 assertEquals(1, 1 * 1); 86 assertEquals(2.25, 1.5 * 1.5); 87 assertEquals("Infinity", String(Infinity * Infinity)); 88 assertEquals("-Infinity", String(Infinity * (-Infinity))); 89 assertEquals("Infinity", String(Infinity * 3)); 90 assertEquals("-Infinity", String(Infinity * (-3))); 91 assertEquals("NaN", String(NaN * 3)); 92 assertEquals("-Infinity", String(1 / (-0.0 * 0.0))); 93 assertEquals("Infinity", String(1 / (0.0 * 0.0))); 94 }); 95 96 test(function div() { 97 assertEquals(1, 1 / 1); 98 assertEquals(1.5, 2.25 / 1.5); 99 assertEquals("NaN", String(Infinity / Infinity)); 100 assertEquals("Infinity", String(Infinity / 3)); 101 assertEquals("-Infinity", String(Infinity / (-3))); 102 assertEquals("NaN", String(NaN / 3)); 103 assertEquals("-Infinity", String(1 / (-0.0))); 104 assertEquals("Infinity", String(Infinity/0.0)); 105 }); 106 107 test(function mathMin() { 108 assertEquals(1, Math.min(1, 10)); 109 assertEquals(1.5, Math.min(1.5, 2.5)); 110 assertEquals(0, Math.min(Infinity, 0)); 111 assertEquals("Infinity", String(Math.min(Infinity, Infinity))); 112 assertEquals("-Infinity", String(Math.min(Infinity, -Infinity))); 113 assertEquals("NaN", String(Math.min(NaN, 1))); 114 assertEquals("Infinity", String(1 / Math.min(0.0, 0.0))); 115 assertEquals("-Infinity", String(1 / Math.min(-0.0, -0.0))); 116 assertEquals("-Infinity", String(1 / Math.min(0.0, -0.0))); 117 }); 118 119 test(function mathMax() { 120 assertEquals(10, Math.max(1, 10)); 121 assertEquals(2.5, Math.max(1.5, 2.5)); 122 assertEquals(Infinity, Math.max(Infinity, 0)); 123 assertEquals("-Infinity", String(Math.max(-Infinity, -Infinity))); 124 assertEquals("Infinity", String(Math.max(Infinity, -Infinity))); 125 assertEquals("NaN", String(Math.max(NaN, 1))); 126 assertEquals("Infinity", String(1 / Math.max(0.0, 0.0))); 127 assertEquals("-Infinity", String(1 / Math.max(-0.0, -0.0))); 128 assertEquals("Infinity", String(1 / Math.max(0.0, -0.0))); 129 }); 130 131 test(function mathSin() { 132 assertEquals(0.0, Math.sin(0.0)); 133 assertTrue(0.8 < Math.sin(1) && Math.sin(1) < 0.9); 134 assertEquals("NaN", String(Math.sin(Infinity))); 135 assertEquals("NaN", String(Math.sin(-Infinity))); 136 assertEquals("NaN", String(Math.sin(NaN))); 137 }); 138 139 test(function mathCos() { 140 assertEquals(1.0, Math.cos(0.0)); 141 assertTrue(0.5 < Math.cos(1) && Math.cos(1) < 0.6); 142 assertEquals("NaN", String(Math.cos(Infinity))); 143 assertEquals("NaN", String(Math.cos(-Infinity))); 144 assertEquals("NaN", String(Math.cos(NaN))); 145 }); 146 147 test(function mathTan() { 148 assertEquals(0.0, Math.tan(0.0)); 149 assertTrue(1.5 < Math.tan(1) && Math.tan(1) < 1.6); 150 assertEquals("NaN", String(Math.tan(Infinity))); 151 assertEquals("NaN", String(Math.tan(-Infinity))); 152 assertEquals("NaN", String(Math.tan(NaN))); 153 }); 154 155 test(function mathExp() { 156 assertEquals(1.0, Math.exp(0.0)); 157 assertTrue(2.7 < Math.exp(1) && Math.exp(1) < 2.8); 158 assertEquals("Infinity", String(Math.exp(Infinity))); 159 assertEquals("0", String(Math.exp(-Infinity))); 160 assertEquals("NaN", String(Math.exp(NaN))); 161 }); 162 163 test(function mathLog() { 164 assertEquals(0.0, Math.log(1.0)); 165 assertTrue(1 < Math.log(3) && Math.log(3) < 1.5); 166 assertEquals("Infinity", String(Math.log(Infinity))); 167 assertEquals("NaN", String(Math.log(-Infinity))); 168 assertEquals("NaN", String(Math.exp(NaN))); 169 }); 170 171 test(function mathSqrt() { 172 assertEquals(1.0, Math.sqrt(1.0)); 173 assertEquals("NaN", String(Math.sqrt(-1.0))); 174 assertEquals("Infinity", String(Math.sqrt(Infinity))); 175 assertEquals("NaN", String(Math.sqrt(-Infinity))); 176 assertEquals("NaN", String(Math.sqrt(NaN))); 177 }); 178 179 test(function mathPowHalf() { 180 assertEquals(1.0, Math.pow(1.0, 0.5)); 181 assertEquals("NaN", String(Math.sqrt(-1.0))); 182 assertEquals("Infinity", String(Math.pow(Infinity, 0.5))); 183 assertEquals("NaN", String(Math.sqrt(-Infinity, 0.5))); 184 assertEquals(0, Math.pow(Infinity, -0.5)); 185 assertEquals("NaN", String(Math.sqrt(-Infinity, -0.5))); 186 assertEquals("NaN", String(Math.sqrt(NaN, 0.5))); 187 }); 188 189 test(function mathAbs() { 190 assertEquals(1.5, Math.abs(1.5)); 191 assertEquals(1.5, Math.abs(-1.5)); 192 assertEquals("Infinity", String(Math.abs(Infinity))); 193 assertEquals("Infinity", String(Math.abs(-Infinity))); 194 assertEquals("NaN", String(Math.abs(NaN))); 195 }); 196 197 test(function mathRound() { 198 assertEquals(2, Math.round(1.5)); 199 assertEquals(-1, Math.round(-1.5)); 200 assertEquals("Infinity", String(Math.round(Infinity))); 201 assertEquals("-Infinity", String(Math.round(-Infinity))); 202 assertEquals("Infinity", String(1 / Math.round(0.0))); 203 assertEquals("-Infinity", String(1 / Math.round(-0.0))); 204 assertEquals("NaN", String(Math.round(NaN))); 205 assertEquals(Math.pow(2, 52) + 1, Math.round(Math.pow(2, 52) + 1)); 206 }); 207 208 test(function mathFloor() { 209 assertEquals(1, Math.floor(1.5)); 210 assertEquals(-2, Math.floor(-1.5)); 211 assertEquals("Infinity", String(Math.floor(Infinity))); 212 assertEquals("-Infinity", String(Math.floor(-Infinity))); 213 assertEquals("Infinity", String(1 / Math.floor(0.0))); 214 assertEquals("-Infinity", String(1 / Math.floor(-0.0))); 215 assertEquals("NaN", String(Math.floor(NaN))); 216 assertEquals(Math.pow(2, 52) + 1, Math.floor(Math.pow(2, 52) + 1)); 217 }); 218 219 test(function mathPow() { 220 assertEquals(2.25, Math.pow(1.5, 2)); 221 assertTrue(1.8 < Math.pow(1.5, 1.5) && Math.pow(1.5, 1.5) < 1.9); 222 assertEquals("Infinity", String(Math.pow(Infinity, 0.5))); 223 assertEquals("Infinity", String(Math.pow(-Infinity, 0.5))); 224 assertEquals(0, Math.pow(Infinity, -0.5)); 225 assertEquals(0, Math.pow(Infinity, -0.5)); 226 assertEquals("Infinity", String(Math.pow(Infinity, Infinity))); 227 assertEquals(0, Math.pow(Infinity, -Infinity)); 228 assertEquals("NaN", String(Math.pow(Infinity, NaN))); 229 assertEquals("NaN", String(Math.pow(NaN, 2))); 230 }); 231 232 test(function stringAdd() { 233 assertEquals("", "" + ""); 234 assertEquals("folded constant", "folded " + "constant"); 235 assertEquals("not folded constant1", "not folded constant" + 1); 236 }); 237 238 test(function stringLength() { 239 assertEquals(6, "abcdef".length); 240 assertEquals(0, "".length); 241 assertEquals(-5, { length: -5 }.length); 242 }); 243 244 test(function stringCharCodeAt() { 245 assertEquals(99, "abc".charCodeAt(2)); 246 assertEquals("NaN", String("abc".charCodeAt(-1))); 247 assertEquals("NaN", String("abc".charCodeAt(4))); 248 assertEquals(98, "abc".charCodeAt(1.1)); 249 assertEquals("NaN", String("abc".charCodeAt(4.1))); 250 }); 251 252 test(function stringCharAt() { 253 assertEquals("c", "abc".charAt(2)); 254 assertEquals("", "abc".charAt(-1)); 255 assertEquals("", "abc".charAt(4)); 256 assertEquals("b", "abc".charAt(1.1)); 257 assertEquals("", "abc".charAt(4.1)); 258 }); 259 260 261 test(function int32Mod() { 262 assertEquals(-0, -2147483648 % (-1)); 263 }); 264 265 test(function int32Div() { 266 assertEquals(2147483648, -2147483648 / (-1)); 267 }); 268