constant-folding-2.js 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


29
// Flags: --allow-natives-syntax --nostress-opt --opt
30
// Flags: --no-stress-flush-bytecode
31

32
function test(f, iterations) {
33
  %PrepareFunctionForOptimization(f);
34 35
  f();
  f();
36 37 38 39 40
  // Some of the tests need to learn until they stabilize.
  let n = iterations ? iterations : 1;
  for (let i = 0; i < n; i++) {
    %OptimizeFunctionOnNextCall(f);
    f();
41
    %PrepareFunctionForOptimization(f);
42 43 44
  }
  // Assert that the function finally stabilized.
  assertOptimized(f);
45 46 47 48 49
}

test(function add() {
  assertEquals(2, 1 + 1);
  assertEquals(2.5, 1.25 + 1.25);
50 51 52 53 54 55
  assertSame(Infinity, Infinity + Infinity);
  assertSame(Infinity, Infinity + 3);
  assertSame(NaN, Infinity + (-Infinity));
  assertSame(NaN, NaN + 2);
  assertSame(-Infinity, 1 / (-0.0 + (-0.0)));
  assertSame(Infinity, 1 / (-0.0 + 0.0));
56 57 58 59 60 61 62 63
});

test(function inc() {
  var a = 1;
  var b = Infinity;
  var c = -Infinity;
  var d = NaN;
  assertEquals(2, ++a);
64 65 66
  assertSame(Infinity, ++b);
  assertSame(-Infinity, ++c);
  assertSame(NaN, ++d);
67 68 69 70 71 72 73 74
});

test(function dec() {
  var a = 1;
  var b = Infinity;
  var c = -Infinity;
  var d = NaN;
  assertEquals(0, --a);
75 76 77
  assertSame(Infinity, --b);
  assertSame(-Infinity, --c);
  assertSame(NaN, --d);
78 79 80 81 82
});

test(function sub() {
  assertEquals(0, 1 - 1);
  assertEquals(0.5, 1.5 - 1);
83 84 85 86 87 88
  assertSame(Infinity, Infinity - (-Infinity));
  assertSame(Infinity, Infinity - 3);
  assertSame(NaN, Infinity - Infinity);
  assertSame(NaN, NaN - 2);
  assertSame(-Infinity, 1 / (-0.0 - 0.0));
  assertSame(Infinity, 1 / (0.0 - 0.0));
89 90 91 92 93
});

test(function mul() {
  assertEquals(1, 1 * 1);
  assertEquals(2.25, 1.5 * 1.5);
94 95 96 97 98 99 100
  assertSame(Infinity, Infinity * Infinity);
  assertSame(-Infinity, Infinity * (-Infinity));
  assertSame(Infinity, Infinity * 3);
  assertSame(-Infinity, Infinity * (-3));
  assertSame(NaN, NaN * 3);
  assertSame(-Infinity, 1 / (-0.0 * 0.0));
  assertSame(Infinity, 1 / (0.0 * 0.0));
101 102 103 104 105
});

test(function div() {
  assertEquals(1, 1 / 1);
  assertEquals(1.5, 2.25 / 1.5);
106 107 108 109 110
  assertSame(NaN, Infinity / Infinity);
  assertSame(Infinity, Infinity / 3);
  assertSame(-Infinity, Infinity / (-3));
  assertSame(NaN, NaN / 3);
  assertSame(-Infinity, 1 / (-0.0));
111
  assertSame(Infinity, Infinity / 0.0);
112 113 114 115 116 117
});

test(function mathMin() {
  assertEquals(1, Math.min(1, 10));
  assertEquals(1.5, Math.min(1.5, 2.5));
  assertEquals(0, Math.min(Infinity, 0));
118 119 120 121 122 123
  assertSame(Infinity, Math.min(Infinity, Infinity));
  assertSame(-Infinity, Math.min(Infinity, -Infinity));
  assertSame(NaN, Math.min(NaN, 1));
  assertSame(Infinity, 1 / Math.min(0.0, 0.0));
  assertSame(-Infinity, 1 / Math.min(-0.0, -0.0));
  assertSame(-Infinity, 1 / Math.min(0.0, -0.0));
124 125 126 127 128 129
});

test(function mathMax() {
  assertEquals(10, Math.max(1, 10));
  assertEquals(2.5, Math.max(1.5, 2.5));
  assertEquals(Infinity, Math.max(Infinity, 0));
130 131 132 133 134 135
  assertSame(-Infinity, Math.max(-Infinity, -Infinity));
  assertSame(Infinity, Math.max(Infinity, -Infinity));
  assertSame(NaN, Math.max(NaN, 1));
  assertSame(Infinity, 1 / Math.max(0.0, 0.0));
  assertSame(-Infinity, 1 / Math.max(-0.0, -0.0));
  assertSame(Infinity, 1 / Math.max(0.0, -0.0));
136 137 138 139 140
});

test(function mathExp() {
  assertEquals(1.0, Math.exp(0.0));
  assertTrue(2.7 < Math.exp(1) && Math.exp(1) < 2.8);
141
  assertSame(Infinity, Math.exp(Infinity));
142
  assertEquals("0", String(Math.exp(-Infinity)));
143
  assertSame(NaN, Math.exp(NaN));
144 145 146 147 148
});

test(function mathLog() {
  assertEquals(0.0, Math.log(1.0));
  assertTrue(1 < Math.log(3) && Math.log(3) < 1.5);
149 150 151
  assertSame(Infinity, Math.log(Infinity));
  assertSame(NaN, Math.log(-Infinity));
  assertSame(NaN, Math.exp(NaN));
152 153 154 155
});

test(function mathSqrt() {
  assertEquals(1.0, Math.sqrt(1.0));
156 157 158 159
  assertSame(NaN, Math.sqrt(-1.0));
  assertSame(Infinity, Math.sqrt(Infinity));
  assertSame(NaN, Math.sqrt(-Infinity));
  assertSame(NaN, Math.sqrt(NaN));
160 161 162 163
});

test(function mathPowHalf() {
  assertEquals(1.0, Math.pow(1.0, 0.5));
164 165 166
  assertSame(NaN, Math.sqrt(-1.0));
  assertSame(Infinity, Math.pow(Infinity, 0.5));
  assertSame(NaN, Math.sqrt(-Infinity, 0.5));
167
  assertEquals(0, Math.pow(Infinity, -0.5));
168 169
  assertSame(NaN, Math.sqrt(-Infinity, -0.5));
  assertSame(NaN, Math.sqrt(NaN, 0.5));
170 171 172 173 174
});

test(function mathAbs() {
  assertEquals(1.5, Math.abs(1.5));
  assertEquals(1.5, Math.abs(-1.5));
175 176 177
  assertSame(Infinity, Math.abs(Infinity));
  assertSame(Infinity, Math.abs(-Infinity));
  assertSame(NaN, Math.abs(NaN));
178 179 180 181 182
});

test(function mathRound() {
  assertEquals(2, Math.round(1.5));
  assertEquals(-1, Math.round(-1.5));
183 184 185 186 187
  assertSame(Infinity, Math.round(Infinity));
  assertSame(-Infinity, Math.round(-Infinity));
  assertSame(Infinity, 1 / Math.round(0.0));
  assertSame(-Infinity, 1 / Math.round(-0.0));
  assertSame(NaN, Math.round(NaN));
188 189 190
  assertEquals(Math.pow(2, 52) + 1, Math.round(Math.pow(2, 52) + 1));
});

191 192
test(function mathFround() {
  assertTrue(isNaN(Math.fround(NaN)));
193 194
  assertSame(Infinity, 1 / Math.fround(0));
  assertSame(-Infinity, 1 / Math.fround(-0));
195 196 197 198
  assertSame(Infinity, Math.fround(Infinity));
  assertSame(-Infinity, Math.fround(-Infinity));
  assertSame(Infinity, Math.fround(1E200));
  assertSame(-Infinity, Math.fround(-1E200));
199 200 201
  assertEquals(3.1415927410125732, Math.fround(Math.PI));
});

202 203 204
test(function mathFloor() {
  assertEquals(1, Math.floor(1.5));
  assertEquals(-2, Math.floor(-1.5));
205 206 207 208 209
  assertSame(Infinity, Math.floor(Infinity));
  assertSame(-Infinity, Math.floor(-Infinity));
  assertSame(Infinity, 1 / Math.floor(0.0));
  assertSame(-Infinity, 1 / Math.floor(-0.0));
  assertSame(NaN, Math.floor(NaN));
210 211 212 213 214 215
  assertEquals(Math.pow(2, 52) + 1, Math.floor(Math.pow(2, 52) + 1));
});

test(function mathPow() {
  assertEquals(2.25, Math.pow(1.5, 2));
  assertTrue(1.8 < Math.pow(1.5, 1.5) && Math.pow(1.5, 1.5) < 1.9);
216 217
  assertSame(Infinity, Math.pow(Infinity, 0.5));
  assertSame(Infinity, Math.pow(-Infinity, 0.5));
218 219
  assertEquals(0, Math.pow(Infinity, -0.5));
  assertEquals(0, Math.pow(Infinity, -0.5));
220
  assertSame(Infinity, Math.pow(Infinity, Infinity));
221
  assertEquals(0, Math.pow(Infinity, -Infinity));
222 223
  assertSame(NaN, Math.pow(Infinity, NaN));
  assertSame(NaN, Math.pow(NaN, 2));
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
});

test(function stringAdd() {
  assertEquals("", "" + "");
  assertEquals("folded constant", "folded " + "constant");
  assertEquals("not folded constant1", "not folded constant" + 1);
});

test(function stringLength() {
  assertEquals(6, "abcdef".length);
  assertEquals(0, "".length);
  assertEquals(-5, { length: -5 }.length);
});

test(function stringCharAt() {
  assertEquals("c", "abc".charAt(2));
  assertEquals("", "abc".charAt(-1));
  assertEquals("", "abc".charAt(4));
  assertEquals("b", "abc".charAt(1.1));
  assertEquals("", "abc".charAt(4.1));
244 245 246 247
  assertEquals("", "abc".charAt(Infinity));
  assertEquals("", "abc".charAt(-Infinity));
  assertEquals("a", "abc".charAt(-0));
  assertEquals("a", "abc".charAt(+0));
248
  assertEquals("", "".charAt());
249 250 251 252 253 254 255 256 257 258 259 260 261
  assertEquals("", "abc".charAt(1 + 4294967295));
}, 10);

test(function stringCharCodeAt() {
  assertSame(99, "abc".charCodeAt(2));
  assertSame(NaN, "abc".charCodeAt(-1));
  assertSame(NaN, "abc".charCodeAt(4));
  assertSame(98, "abc".charCodeAt(1.1));
  assertSame(NaN, "abc".charCodeAt(4.1));
  assertSame(NaN, "abc".charCodeAt(Infinity));
  assertSame(NaN, "abc".charCodeAt(-Infinity));
  assertSame(97, "abc".charCodeAt(-0));
  assertSame(97, "abc".charCodeAt(+0));
262
  assertSame(NaN, "".charCodeAt());
263 264
  assertSame(NaN, "abc".charCodeAt(1 + 4294967295));
}, 10);
265

266
test(function stringCodePointAt() {
267 268
  assertSame(65533, "äϠ�𝌆".codePointAt(2));
  assertSame(119558, "äϠ�𝌆".codePointAt(3));
269 270 271 272 273 274 275 276
  assertSame(undefined, "äϠ�".codePointAt(-1));
  assertSame(undefined, "äϠ�".codePointAt(4));
  assertSame(992, "äϠ�".codePointAt(1.1));
  assertSame(undefined, "äϠ�".codePointAt(4.1));
  assertSame(undefined, "äϠ�".codePointAt(Infinity));
  assertSame(undefined, "äϠ�".codePointAt(-Infinity));
  assertSame(228, "äϠ�".codePointAt(-0));
  assertSame(97, "aϠ�".codePointAt(+0));
277
  assertSame(undefined, "".codePointAt());
278 279
  assertSame(undefined, "äϠ�".codePointAt(1 + 4294967295));
}, 10);
280

281 282 283 284 285 286 287 288 289 290 291 292 293
test(function stringFromCodePoint() {
  assertEquals(String.fromCodePoint(), "");
  assertEquals(String.fromCodePoint(-0), "\0");
  assertEquals(String.fromCodePoint(0), "\0");
  assertEquals(String.fromCodePoint(0x1D306), "\uD834\uDF06");
  assertEquals(
    String.fromCodePoint(0x1D306, 0x61, 0x1D307),
    "\uD834\uDF06a\uD834\uDF07");
  assertEquals(String.fromCodePoint(0x61, 0x62, 0x1D307), "ab\uD834\uDF07");
  assertEquals(String.fromCodePoint(false), "\0");
  assertEquals(String.fromCodePoint(null), "\0");
}, 5);

294 295 296 297
test(function stringFromCharCode() {
  assertEquals("!", String.fromCharCode(0x10FF01));
}, 2);

298 299 300 301 302 303 304
test(function int32Mod() {
  assertEquals(-0, -2147483648 % (-1));
});

test(function int32Div() {
  assertEquals(2147483648, -2147483648 / (-1));
});