| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Joiner; |
| 20 |
import com.google.common.collect.ImmutableList; |
| 21 |
import com.google.common.collect.ImmutableMap; |
| 22 |
import com.google.common.collect.ImmutableSet; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
|
| 25 |
import java.util.List; |
| 26 |
import java.util.Map; |
| 27 |
import java.util.Set; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class PeepholeFoldConstantsTest extends CompilerTestCase { |
| 35 |
|
| 36 |
private boolean late; |
| 37 |
|
| 38 |
|
| 39 |
private PeepholeFoldConstantsTest(boolean compareAsTree) { |
| 40 |
super("", compareAsTree); |
| 41 |
} |
| 42 |
|
| 43 |
public PeepholeFoldConstantsTest() { |
| 44 |
super(""); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public void setUp() { |
| 49 |
late = false; |
| 50 |
enableLineNumberCheck(true); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public CompilerPass getProcessor(final Compiler compiler) { |
| 55 |
CompilerPass peepholePass = new PeepholeOptimizationsPass(compiler, |
| 56 |
new PeepholeFoldConstants(late)); |
| 57 |
return peepholePass; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
protected int getNumRepetitions() { |
| 62 |
|
| 63 |
return 2; |
| 64 |
} |
| 65 |
|
| 66 |
private void foldSame(String js) { |
| 67 |
testSame(js); |
| 68 |
} |
| 69 |
|
| 70 |
private void fold(String js, String expected) { |
| 71 |
test(js, expected); |
| 72 |
} |
| 73 |
|
| 74 |
private void fold(String js, String expected, DiagnosticType warning) { |
| 75 |
test(js, expected, null, warning); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private void assertResultString(String js, String expected) { |
| 81 |
PeepholeFoldConstantsTest scTest = new PeepholeFoldConstantsTest(false); |
| 82 |
|
| 83 |
scTest.test(js, expected); |
| 84 |
} |
| 85 |
|
| 86 |
public void testUndefinedComparison1() { |
| 87 |
fold("undefined == undefined", "true"); |
| 88 |
fold("undefined == null", "true"); |
| 89 |
fold("undefined == void 0", "true"); |
| 90 |
|
| 91 |
fold("undefined == 0", "false"); |
| 92 |
fold("undefined == 1", "false"); |
| 93 |
fold("undefined == 'hi'", "false"); |
| 94 |
fold("undefined == true", "false"); |
| 95 |
fold("undefined == false", "false"); |
| 96 |
|
| 97 |
fold("undefined === undefined", "true"); |
| 98 |
fold("undefined === null", "false"); |
| 99 |
fold("undefined === void 0", "true"); |
| 100 |
|
| 101 |
foldSame("undefined == this"); |
| 102 |
foldSame("undefined == x"); |
| 103 |
|
| 104 |
fold("undefined != undefined", "false"); |
| 105 |
fold("undefined != null", "false"); |
| 106 |
fold("undefined != void 0", "false"); |
| 107 |
|
| 108 |
fold("undefined != 0", "true"); |
| 109 |
fold("undefined != 1", "true"); |
| 110 |
fold("undefined != 'hi'", "true"); |
| 111 |
fold("undefined != true", "true"); |
| 112 |
fold("undefined != false", "true"); |
| 113 |
|
| 114 |
fold("undefined !== undefined", "false"); |
| 115 |
fold("undefined !== void 0", "false"); |
| 116 |
fold("undefined !== null", "true"); |
| 117 |
|
| 118 |
foldSame("undefined != this"); |
| 119 |
foldSame("undefined != x"); |
| 120 |
|
| 121 |
fold("undefined < undefined", "false"); |
| 122 |
fold("undefined > undefined", "false"); |
| 123 |
fold("undefined >= undefined", "false"); |
| 124 |
fold("undefined <= undefined", "false"); |
| 125 |
|
| 126 |
fold("0 < undefined", "false"); |
| 127 |
fold("true > undefined", "false"); |
| 128 |
fold("'hi' >= undefined", "false"); |
| 129 |
fold("null <= undefined", "false"); |
| 130 |
|
| 131 |
fold("undefined < 0", "false"); |
| 132 |
fold("undefined > true", "false"); |
| 133 |
fold("undefined >= 'hi'", "false"); |
| 134 |
fold("undefined <= null", "false"); |
| 135 |
|
| 136 |
fold("null == undefined", "true"); |
| 137 |
fold("0 == undefined", "false"); |
| 138 |
fold("1 == undefined", "false"); |
| 139 |
fold("'hi' == undefined", "false"); |
| 140 |
fold("true == undefined", "false"); |
| 141 |
fold("false == undefined", "false"); |
| 142 |
fold("null === undefined", "false"); |
| 143 |
fold("void 0 === undefined", "true"); |
| 144 |
|
| 145 |
fold("undefined == NaN", "false"); |
| 146 |
fold("NaN == undefined", "false"); |
| 147 |
fold("undefined == Infinity", "false"); |
| 148 |
fold("Infinity == undefined", "false"); |
| 149 |
fold("undefined == -Infinity", "false"); |
| 150 |
fold("-Infinity == undefined", "false"); |
| 151 |
fold("({}) == undefined", "false"); |
| 152 |
fold("undefined == ({})", "false"); |
| 153 |
fold("([]) == undefined", "false"); |
| 154 |
fold("undefined == ([])", "false"); |
| 155 |
fold("(/a/g) == undefined", "false"); |
| 156 |
fold("undefined == (/a/g)", "false"); |
| 157 |
fold("(function(){}) == undefined", "false"); |
| 158 |
fold("undefined == (function(){})", "false"); |
| 159 |
|
| 160 |
fold("undefined != NaN", "true"); |
| 161 |
fold("NaN != undefined", "true"); |
| 162 |
fold("undefined != Infinity", "true"); |
| 163 |
fold("Infinity != undefined", "true"); |
| 164 |
fold("undefined != -Infinity", "true"); |
| 165 |
fold("-Infinity != undefined", "true"); |
| 166 |
fold("({}) != undefined", "true"); |
| 167 |
fold("undefined != ({})", "true"); |
| 168 |
fold("([]) != undefined", "true"); |
| 169 |
fold("undefined != ([])", "true"); |
| 170 |
fold("(/a/g) != undefined", "true"); |
| 171 |
fold("undefined != (/a/g)", "true"); |
| 172 |
fold("(function(){}) != undefined", "true"); |
| 173 |
fold("undefined != (function(){})", "true"); |
| 174 |
|
| 175 |
foldSame("this == undefined"); |
| 176 |
foldSame("x == undefined"); |
| 177 |
} |
| 178 |
|
| 179 |
public void testUndefinedComparison2() { |
| 180 |
fold("\"123\" !== void 0", "true"); |
| 181 |
fold("\"123\" === void 0", "false"); |
| 182 |
|
| 183 |
fold("void 0 !== \"123\"", "true"); |
| 184 |
fold("void 0 === \"123\"", "false"); |
| 185 |
} |
| 186 |
|
| 187 |
public void testUndefinedComparison3() { |
| 188 |
fold("\"123\" !== undefined", "true"); |
| 189 |
fold("\"123\" === undefined", "false"); |
| 190 |
|
| 191 |
fold("undefined !== \"123\"", "true"); |
| 192 |
fold("undefined === \"123\"", "false"); |
| 193 |
} |
| 194 |
|
| 195 |
public void testUndefinedComparison4() { |
| 196 |
fold("1 !== void 0", "true"); |
| 197 |
fold("1 === void 0", "false"); |
| 198 |
|
| 199 |
fold("null !== void 0", "true"); |
| 200 |
fold("null === void 0", "false"); |
| 201 |
|
| 202 |
fold("undefined !== void 0", "false"); |
| 203 |
fold("undefined === void 0", "true"); |
| 204 |
} |
| 205 |
|
| 206 |
public void testNullComparison1() { |
| 207 |
fold("null == undefined", "true"); |
| 208 |
fold("null == null", "true"); |
| 209 |
fold("null == void 0", "true"); |
| 210 |
|
| 211 |
fold("null == 0", "false"); |
| 212 |
fold("null == 1", "false"); |
| 213 |
fold("null == 'hi'", "false"); |
| 214 |
fold("null == true", "false"); |
| 215 |
fold("null == false", "false"); |
| 216 |
|
| 217 |
fold("null === undefined", "false"); |
| 218 |
fold("null === null", "true"); |
| 219 |
fold("null === void 0", "false"); |
| 220 |
|
| 221 |
foldSame("null == this"); |
| 222 |
foldSame("null == x"); |
| 223 |
|
| 224 |
fold("null != undefined", "false"); |
| 225 |
fold("null != null", "false"); |
| 226 |
fold("null != void 0", "false"); |
| 227 |
|
| 228 |
fold("null != 0", "true"); |
| 229 |
fold("null != 1", "true"); |
| 230 |
fold("null != 'hi'", "true"); |
| 231 |
fold("null != true", "true"); |
| 232 |
fold("null != false", "true"); |
| 233 |
|
| 234 |
fold("null !== undefined", "true"); |
| 235 |
fold("null !== void 0", "true"); |
| 236 |
fold("null !== null", "false"); |
| 237 |
|
| 238 |
foldSame("null != this"); |
| 239 |
foldSame("null != x"); |
| 240 |
|
| 241 |
fold("null < null", "false"); |
| 242 |
fold("null > null", "false"); |
| 243 |
fold("null >= null", "true"); |
| 244 |
fold("null <= null", "true"); |
| 245 |
|
| 246 |
foldSame("0 < null"); |
| 247 |
fold("true > null", "true"); |
| 248 |
foldSame("'hi' >= null"); |
| 249 |
fold("null <= null", "true"); |
| 250 |
|
| 251 |
foldSame("null < 0"); |
| 252 |
fold("null > true", "false"); |
| 253 |
foldSame("null >= 'hi'"); |
| 254 |
fold("null <= null", "true"); |
| 255 |
|
| 256 |
fold("null == null", "true"); |
| 257 |
fold("0 == null", "false"); |
| 258 |
fold("1 == null", "false"); |
| 259 |
fold("'hi' == null", "false"); |
| 260 |
fold("true == null", "false"); |
| 261 |
fold("false == null", "false"); |
| 262 |
fold("null === null", "true"); |
| 263 |
fold("void 0 === null", "false"); |
| 264 |
|
| 265 |
fold("null == NaN", "false"); |
| 266 |
fold("NaN == null", "false"); |
| 267 |
fold("null == Infinity", "false"); |
| 268 |
fold("Infinity == null", "false"); |
| 269 |
fold("null == -Infinity", "false"); |
| 270 |
fold("-Infinity == null", "false"); |
| 271 |
fold("({}) == null", "false"); |
| 272 |
fold("null == ({})", "false"); |
| 273 |
fold("([]) == null", "false"); |
| 274 |
fold("null == ([])", "false"); |
| 275 |
fold("(/a/g) == null", "false"); |
| 276 |
fold("null == (/a/g)", "false"); |
| 277 |
fold("(function(){}) == null", "false"); |
| 278 |
fold("null == (function(){})", "false"); |
| 279 |
|
| 280 |
fold("null != NaN", "true"); |
| 281 |
fold("NaN != null", "true"); |
| 282 |
fold("null != Infinity", "true"); |
| 283 |
fold("Infinity != null", "true"); |
| 284 |
fold("null != -Infinity", "true"); |
| 285 |
fold("-Infinity != null", "true"); |
| 286 |
fold("({}) != null", "true"); |
| 287 |
fold("null != ({})", "true"); |
| 288 |
fold("([]) != null", "true"); |
| 289 |
fold("null != ([])", "true"); |
| 290 |
fold("(/a/g) != null", "true"); |
| 291 |
fold("null != (/a/g)", "true"); |
| 292 |
fold("(function(){}) != null", "true"); |
| 293 |
fold("null != (function(){})", "true"); |
| 294 |
|
| 295 |
foldSame("({a:f()}) == null"); |
| 296 |
foldSame("null == ({a:f()})"); |
| 297 |
foldSame("([f()]) == null"); |
| 298 |
foldSame("null == ([f()])"); |
| 299 |
|
| 300 |
foldSame("this == null"); |
| 301 |
foldSame("x == null"); |
| 302 |
} |
| 303 |
|
| 304 |
public void testUnaryOps() { |
| 305 |
|
| 306 |
foldSame("!foo()"); |
| 307 |
foldSame("~foo()"); |
| 308 |
foldSame("-foo()"); |
| 309 |
|
| 310 |
|
| 311 |
fold("a=!true", "a=false"); |
| 312 |
fold("a=!10", "a=false"); |
| 313 |
fold("a=!false", "a=true"); |
| 314 |
fold("a=!foo()", "a=!foo()"); |
| 315 |
fold("a=-0", "a=-0.0"); |
| 316 |
fold("a=-(0)", "a=-0.0"); |
| 317 |
fold("a=-Infinity", "a=-Infinity"); |
| 318 |
fold("a=-NaN", "a=NaN"); |
| 319 |
fold("a=-foo()", "a=-foo()"); |
| 320 |
fold("a=~~0", "a=0"); |
| 321 |
fold("a=~~10", "a=10"); |
| 322 |
fold("a=~-7", "a=6"); |
| 323 |
|
| 324 |
fold("a=+true", "a=1"); |
| 325 |
fold("a=+10", "a=10"); |
| 326 |
fold("a=+false", "a=0"); |
| 327 |
foldSame("a=+foo()"); |
| 328 |
foldSame("a=+f"); |
| 329 |
fold("a=+(f?true:false)", "a=+(f?1:0)"); |
| 330 |
fold("a=+0", "a=0"); |
| 331 |
fold("a=+Infinity", "a=Infinity"); |
| 332 |
fold("a=+NaN", "a=NaN"); |
| 333 |
fold("a=+-7", "a=-7"); |
| 334 |
fold("a=+.5", "a=.5"); |
| 335 |
|
| 336 |
fold("a=~0x100000000", "a=~0x100000000", |
| 337 |
PeepholeFoldConstants.BITWISE_OPERAND_OUT_OF_RANGE); |
| 338 |
fold("a=~-0x100000000", "a=~-0x100000000", |
| 339 |
PeepholeFoldConstants.BITWISE_OPERAND_OUT_OF_RANGE); |
| 340 |
testSame("a=~.5", PeepholeFoldConstants.FRACTIONAL_BITWISE_OPERAND); |
| 341 |
} |
| 342 |
|
| 343 |
public void testUnaryOpsStringCompare() { |
| 344 |
|
| 345 |
assertResultString("a=-1", "a=-1"); |
| 346 |
assertResultString("a=~0", "a=-1"); |
| 347 |
assertResultString("a=~1", "a=-2"); |
| 348 |
assertResultString("a=~101", "a=-102"); |
| 349 |
} |
| 350 |
|
| 351 |
public void testFoldLogicalOp() { |
| 352 |
fold("x = true && x", "x = x"); |
| 353 |
foldSame("x = [foo()] && x"); |
| 354 |
|
| 355 |
fold("x = false && x", "x = false"); |
| 356 |
fold("x = true || x", "x = true"); |
| 357 |
fold("x = false || x", "x = x"); |
| 358 |
fold("x = 0 && x", "x = 0"); |
| 359 |
fold("x = 3 || x", "x = 3"); |
| 360 |
fold("x = false || 0", "x = 0"); |
| 361 |
|
| 362 |
|
| 363 |
fold("a = x && true", "a=x&&true"); |
| 364 |
fold("a = x && false", "a=x&&false"); |
| 365 |
fold("a = x || 3", "a=x||3"); |
| 366 |
fold("a = x || false", "a=x||false"); |
| 367 |
fold("a = b ? c : x || false", "a=b?c:x||false"); |
| 368 |
fold("a = b ? x || false : c", "a=b?x||false:c"); |
| 369 |
fold("a = b ? c : x && true", "a=b?c:x&&true"); |
| 370 |
fold("a = b ? x && true : c", "a=b?x&&true:c"); |
| 371 |
|
| 372 |
|
| 373 |
foldSame("a = x || false ? b : c"); |
| 374 |
foldSame("a = x && true ? b : c"); |
| 375 |
|
| 376 |
fold("x = foo() || true || bar()", "x = foo()||true"); |
| 377 |
fold("x = foo() || false || bar()", "x = foo()||bar()"); |
| 378 |
fold("x = foo() || true && bar()", "x = foo()||bar()"); |
| 379 |
fold("x = foo() || false && bar()", "x = foo()||false"); |
| 380 |
fold("x = foo() && false && bar()", "x = foo()&&false"); |
| 381 |
fold("x = foo() && true && bar()", "x = foo()&&bar()"); |
| 382 |
fold("x = foo() && false || bar()", "x = foo()&&false||bar()"); |
| 383 |
|
| 384 |
fold("1 && b()", "b()"); |
| 385 |
fold("a() && (1 && b())", "a() && b()"); |
| 386 |
|
| 387 |
|
| 388 |
fold("(a() && 1) && b()", "(a() && 1) && b()"); |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
foldSame("x = foo() && true || bar()"); |
| 394 |
foldSame("foo() && true || bar()"); |
| 395 |
} |
| 396 |
|
| 397 |
public void testFoldBitwiseOp() { |
| 398 |
fold("x = 1 & 1", "x = 1"); |
| 399 |
fold("x = 1 & 2", "x = 0"); |
| 400 |
fold("x = 3 & 1", "x = 1"); |
| 401 |
fold("x = 3 & 3", "x = 3"); |
| 402 |
|
| 403 |
fold("x = 1 | 1", "x = 1"); |
| 404 |
fold("x = 1 | 2", "x = 3"); |
| 405 |
fold("x = 3 | 1", "x = 3"); |
| 406 |
fold("x = 3 | 3", "x = 3"); |
| 407 |
|
| 408 |
fold("x = 1 ^ 1", "x = 0"); |
| 409 |
fold("x = 1 ^ 2", "x = 3"); |
| 410 |
fold("x = 3 ^ 1", "x = 2"); |
| 411 |
fold("x = 3 ^ 3", "x = 0"); |
| 412 |
|
| 413 |
fold("x = -1 & 0", "x = 0"); |
| 414 |
fold("x = 0 & -1", "x = 0"); |
| 415 |
fold("x = 1 & 4", "x = 0"); |
| 416 |
fold("x = 2 & 3", "x = 2"); |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
fold("x = 1 & 1.1", "x = 1"); |
| 421 |
fold("x = 1.1 & 1", "x = 1"); |
| 422 |
fold("x = 1 & 3000000000", "x = 0"); |
| 423 |
fold("x = 3000000000 & 1", "x = 0"); |
| 424 |
|
| 425 |
|
| 426 |
fold("x = 1 | 4", "x = 5"); |
| 427 |
fold("x = 1 | 3", "x = 3"); |
| 428 |
fold("x = 1 | 1.1", "x = 1"); |
| 429 |
foldSame("x = 1 | 3E9"); |
| 430 |
fold("x = 1 | 3000000001", "x = -1294967295"); |
| 431 |
} |
| 432 |
|
| 433 |
public void testFoldBitwiseOp2() { |
| 434 |
fold("x = y & 1 & 1", "x = y & 1"); |
| 435 |
fold("x = y & 1 & 2", "x = y & 0"); |
| 436 |
fold("x = y & 3 & 1", "x = y & 1"); |
| 437 |
fold("x = 3 & y & 1", "x = y & 1"); |
| 438 |
fold("x = y & 3 & 3", "x = y & 3"); |
| 439 |
fold("x = 3 & y & 3", "x = y & 3"); |
| 440 |
|
| 441 |
fold("x = y | 1 | 1", "x = y | 1"); |
| 442 |
fold("x = y | 1 | 2", "x = y | 3"); |
| 443 |
fold("x = y | 3 | 1", "x = y | 3"); |
| 444 |
fold("x = 3 | y | 1", "x = y | 3"); |
| 445 |
fold("x = y | 3 | 3", "x = y | 3"); |
| 446 |
fold("x = 3 | y | 3", "x = y | 3"); |
| 447 |
|
| 448 |
fold("x = y ^ 1 ^ 1", "x = y ^ 0"); |
| 449 |
fold("x = y ^ 1 ^ 2", "x = y ^ 3"); |
| 450 |
fold("x = y ^ 3 ^ 1", "x = y ^ 2"); |
| 451 |
fold("x = 3 ^ y ^ 1", "x = y ^ 2"); |
| 452 |
fold("x = y ^ 3 ^ 3", "x = y ^ 0"); |
| 453 |
fold("x = 3 ^ y ^ 3", "x = y ^ 0"); |
| 454 |
|
| 455 |
fold("x = Infinity | NaN", "x=0"); |
| 456 |
fold("x = 12 | NaN", "x=12"); |
| 457 |
} |
| 458 |
|
| 459 |
public void testFoldingMixTypesLate() { |
| 460 |
late = true; |
| 461 |
fold("x = x + '2'", "x+='2'"); |
| 462 |
fold("x = +x + +'2'", "x = +x + 2"); |
| 463 |
fold("x = x - '2'", "x-=2"); |
| 464 |
fold("x = x ^ '2'", "x^=2"); |
| 465 |
fold("x = '2' ^ x", "x^=2"); |
| 466 |
fold("x = '2' & x", "x&=2"); |
| 467 |
fold("x = '2' | x", "x|=2"); |
| 468 |
|
| 469 |
fold("x = '2' | y", "x=2|y"); |
| 470 |
fold("x = y | '2'", "x=y|2"); |
| 471 |
fold("x = y | (a && '2')", "x=y|(a&&2)"); |
| 472 |
fold("x = y | (a,'2')", "x=y|(a,2)"); |
| 473 |
fold("x = y | (a?'1':'2')", "x=y|(a?1:2)"); |
| 474 |
fold("x = y | ('x'?'1':'2')", "x=y|('x'?1:2)"); |
| 475 |
} |
| 476 |
|
| 477 |
public void testFoldingMixTypesEarly() { |
| 478 |
late = false; |
| 479 |
foldSame("x = x + '2'"); |
| 480 |
fold("x = +x + +'2'", "x = +x + 2"); |
| 481 |
fold("x = x - '2'", "x = x - 2"); |
| 482 |
fold("x = x ^ '2'", "x = x ^ 2"); |
| 483 |
fold("x = '2' ^ x", "x = 2 ^ x"); |
| 484 |
fold("x = '2' & x", "x = 2 & x"); |
| 485 |
fold("x = '2' | x", "x = 2 | x"); |
| 486 |
|
| 487 |
fold("x = '2' | y", "x=2|y"); |
| 488 |
fold("x = y | '2'", "x=y|2"); |
| 489 |
fold("x = y | (a && '2')", "x=y|(a&&2)"); |
| 490 |
fold("x = y | (a,'2')", "x=y|(a,2)"); |
| 491 |
fold("x = y | (a?'1':'2')", "x=y|(a?1:2)"); |
| 492 |
fold("x = y | ('x'?'1':'2')", "x=y|('x'?1:2)"); |
| 493 |
} |
| 494 |
|
| 495 |
public void testFoldingAdd() { |
| 496 |
fold("x = null + true", "x=1"); |
| 497 |
foldSame("x = a + true"); |
| 498 |
} |
| 499 |
|
| 500 |
public void testFoldBitwiseOpStringCompare() { |
| 501 |
assertResultString("x = -1 | 0", "x=-1"); |
| 502 |
|
| 503 |
} |
| 504 |
|
| 505 |
public void testFoldBitShifts() { |
| 506 |
fold("x = 1 << 0", "x = 1"); |
| 507 |
fold("x = -1 << 0", "x = -1"); |
| 508 |
fold("x = 1 << 1", "x = 2"); |
| 509 |
fold("x = 3 << 1", "x = 6"); |
| 510 |
fold("x = 1 << 8", "x = 256"); |
| 511 |
|
| 512 |
fold("x = 1 >> 0", "x = 1"); |
| 513 |
fold("x = -1 >> 0", "x = -1"); |
| 514 |
fold("x = 1 >> 1", "x = 0"); |
| 515 |
fold("x = 2 >> 1", "x = 1"); |
| 516 |
fold("x = 5 >> 1", "x = 2"); |
| 517 |
fold("x = 127 >> 3", "x = 15"); |
| 518 |
fold("x = 3 >> 1", "x = 1"); |
| 519 |
fold("x = 3 >> 2", "x = 0"); |
| 520 |
fold("x = 10 >> 1", "x = 5"); |
| 521 |
fold("x = 10 >> 2", "x = 2"); |
| 522 |
fold("x = 10 >> 5", "x = 0"); |
| 523 |
|
| 524 |
fold("x = 10 >>> 1", "x = 5"); |
| 525 |
fold("x = 10 >>> 2", "x = 2"); |
| 526 |
fold("x = 10 >>> 5", "x = 0"); |
| 527 |
fold("x = -1 >>> 1", "x = 2147483647"); |
| 528 |
fold("x = -1 >>> 0", "x = 4294967295"); |
| 529 |
fold("x = -2 >>> 0", "x = 4294967294"); |
| 530 |
|
| 531 |
testSame("3000000000 << 1", |
| 532 |
PeepholeFoldConstants.BITWISE_OPERAND_OUT_OF_RANGE); |
| 533 |
testSame("1 << 32", |
| 534 |
PeepholeFoldConstants.SHIFT_AMOUNT_OUT_OF_BOUNDS); |
| 535 |
testSame("1 << -1", |
| 536 |
PeepholeFoldConstants.SHIFT_AMOUNT_OUT_OF_BOUNDS); |
| 537 |
testSame("3000000000 >> 1", |
| 538 |
PeepholeFoldConstants.BITWISE_OPERAND_OUT_OF_RANGE); |
| 539 |
testSame("1 >> 32", |
| 540 |
PeepholeFoldConstants.SHIFT_AMOUNT_OUT_OF_BOUNDS); |
| 541 |
testSame("1.5 << 0", |
| 542 |
PeepholeFoldConstants.FRACTIONAL_BITWISE_OPERAND); |
| 543 |
testSame("1 << .5", |
| 544 |
PeepholeFoldConstants.FRACTIONAL_BITWISE_OPERAND); |
| 545 |
testSame("1.5 >>> 0", |
| 546 |
PeepholeFoldConstants.FRACTIONAL_BITWISE_OPERAND); |
| 547 |
testSame("1 >>> .5", |
| 548 |
PeepholeFoldConstants.FRACTIONAL_BITWISE_OPERAND); |
| 549 |
testSame("1.5 >> 0", |
| 550 |
PeepholeFoldConstants.FRACTIONAL_BITWISE_OPERAND); |
| 551 |
testSame("1 >> .5", |
| 552 |
PeepholeFoldConstants.FRACTIONAL_BITWISE_OPERAND); |
| 553 |
} |
| 554 |
|
| 555 |
public void testFoldBitShiftsStringCompare() { |
| 556 |
|
| 557 |
assertResultString("x = -1 << 1", "x=-2"); |
| 558 |
assertResultString("x = -1 << 8", "x=-256"); |
| 559 |
assertResultString("x = -1 >> 1", "x=-1"); |
| 560 |
assertResultString("x = -2 >> 1", "x=-1"); |
| 561 |
assertResultString("x = -1 >> 0", "x=-1"); |
| 562 |
} |
| 563 |
|
| 564 |
public void testStringAdd() { |
| 565 |
fold("x = 'a' + \"bc\"", "x = \"abc\""); |
| 566 |
fold("x = 'a' + 5", "x = \"a5\""); |
| 567 |
fold("x = 5 + 'a'", "x = \"5a\""); |
| 568 |
fold("x = 'a' + ''", "x = \"a\""); |
| 569 |
fold("x = \"a\" + foo()", "x = \"a\"+foo()"); |
| 570 |
fold("x = foo() + 'a' + 'b'", "x = foo()+\"ab\""); |
| 571 |
fold("x = (foo() + 'a') + 'b'", "x = foo()+\"ab\""); |
| 572 |
fold("x = foo() + 'a' + 'b' + 'cd' + bar()", "x = foo()+\"abcd\"+bar()"); |
| 573 |
fold("x = foo() + 2 + 'b'", "x = foo()+2+\"b\""); |
| 574 |
fold("x = foo() + 'a' + 2", "x = foo()+\"a2\""); |
| 575 |
fold("x = '' + null", "x = \"null\""); |
| 576 |
fold("x = true + '' + false", "x = \"truefalse\""); |
| 577 |
fold("x = '' + []", "x = ''"); |
| 578 |
} |
| 579 |
|
| 580 |
public void testIssue821() { |
| 581 |
foldSame("var a =(Math.random()>0.5? '1' : 2 ) + 3 + 4;"); |
| 582 |
foldSame("var a = ((Math.random() ? 0 : 1) ||" + |
| 583 |
"(Math.random()>0.5? '1' : 2 )) + 3 + 4;"); |
| 584 |
} |
| 585 |
|
| 586 |
public void testFoldConstructor() { |
| 587 |
fold("x = this[new String('a')]", "x = this['a']"); |
| 588 |
fold("x = ob[new String(12)]", "x = ob['12']"); |
| 589 |
fold("x = ob[new String(false)]", "x = ob['false']"); |
| 590 |
fold("x = ob[new String(null)]", "x = ob['null']"); |
| 591 |
fold("x = 'a' + new String('b')", "x = 'ab'"); |
| 592 |
fold("x = 'a' + new String(23)", "x = 'a23'"); |
| 593 |
fold("x = 2 + new String(1)", "x = '21'"); |
| 594 |
foldSame("x = ob[new String(a)]"); |
| 595 |
foldSame("x = new String('a')"); |
| 596 |
foldSame("x = (new String('a'))[3]"); |
| 597 |
} |
| 598 |
|
| 599 |
public void testFoldArithmetic() { |
| 600 |
fold("x = 10 + 20", "x = 30"); |
| 601 |
fold("x = 2 / 4", "x = 0.5"); |
| 602 |
fold("x = 2.25 * 3", "x = 6.75"); |
| 603 |
fold("z = x * y", "z = x * y"); |
| 604 |
fold("x = y * 5", "x = y * 5"); |
| 605 |
fold("x = 1 / 0", "x = 1 / 0"); |
| 606 |
fold("x = 3 % 2", "x = 1"); |
| 607 |
fold("x = 3 % -2", "x = 1"); |
| 608 |
fold("x = -1 % 3", "x = -1"); |
| 609 |
fold("x = 1 % 0", "x = 1 % 0"); |
| 610 |
} |
| 611 |
|
| 612 |
public void testFoldArithmetic2() { |
| 613 |
foldSame("x = y + 10 + 20"); |
| 614 |
foldSame("x = y / 2 / 4"); |
| 615 |
fold("x = y * 2.25 * 3", "x = y * 6.75"); |
| 616 |
fold("z = x * y", "z = x * y"); |
| 617 |
fold("x = y * 5", "x = y * 5"); |
| 618 |
fold("x = y + (z * 24 * 60 * 60 * 1000)", "x = y + z * 864E5"); |
| 619 |
} |
| 620 |
|
| 621 |
public void testFoldArithmetic3() { |
| 622 |
fold("x = null * undefined", "x = NaN"); |
| 623 |
fold("x = null * 1", "x = 0"); |
| 624 |
fold("x = (null - 1) * 2", "x = -2"); |
| 625 |
fold("x = (null + 1) * 2", "x = 2"); |
| 626 |
} |
| 627 |
|
| 628 |
public void testFoldArithmeticInfinity() { |
| 629 |
fold("x=-Infinity-2", "x=-Infinity"); |
| 630 |
fold("x=Infinity-2", "x=Infinity"); |
| 631 |
fold("x=Infinity*5", "x=Infinity"); |
| 632 |
} |
| 633 |
|
| 634 |
public void testFoldArithmeticStringComp() { |
| 635 |
|
| 636 |
assertResultString("x = 10 - 20", "x=-10"); |
| 637 |
} |
| 638 |
|
| 639 |
public void testFoldComparison() { |
| 640 |
fold("x = 0 == 0", "x = true"); |
| 641 |
fold("x = 1 == 2", "x = false"); |
| 642 |
fold("x = 'abc' == 'def'", "x = false"); |
| 643 |
fold("x = 'abc' == 'abc'", "x = true"); |
| 644 |
fold("x = \"\" == ''", "x = true"); |
| 645 |
fold("x = foo() == bar()", "x = foo()==bar()"); |
| 646 |
|
| 647 |
fold("x = 1 != 0", "x = true"); |
| 648 |
fold("x = 'abc' != 'def'", "x = true"); |
| 649 |
fold("x = 'a' != 'a'", "x = false"); |
| 650 |
|
| 651 |
fold("x = 1 < 20", "x = true"); |
| 652 |
fold("x = 3 < 3", "x = false"); |
| 653 |
fold("x = 10 > 1.0", "x = true"); |
| 654 |
fold("x = 10 > 10.25", "x = false"); |
| 655 |
fold("x = y == y", "x = y==y"); |
| 656 |
fold("x = y < y", "x = false"); |
| 657 |
fold("x = y > y", "x = false"); |
| 658 |
fold("x = 1 <= 1", "x = true"); |
| 659 |
fold("x = 1 <= 0", "x = false"); |
| 660 |
fold("x = 0 >= 0", "x = true"); |
| 661 |
fold("x = -1 >= 9", "x = false"); |
| 662 |
|
| 663 |
fold("x = true == true", "x = true"); |
| 664 |
fold("x = false == false", "x = true"); |
| 665 |
fold("x = false == null", "x = false"); |
| 666 |
fold("x = false == true", "x = false"); |
| 667 |
fold("x = true == null", "x = false"); |
| 668 |
|
| 669 |
fold("0 == 0", "true"); |
| 670 |
fold("1 == 2", "false"); |
| 671 |
fold("'abc' == 'def'", "false"); |
| 672 |
fold("'abc' == 'abc'", "true"); |
| 673 |
fold("\"\" == ''", "true"); |
| 674 |
foldSame("foo() == bar()"); |
| 675 |
|
| 676 |
fold("1 != 0", "true"); |
| 677 |
fold("'abc' != 'def'", "true"); |
| 678 |
fold("'a' != 'a'", "false"); |
| 679 |
|
| 680 |
fold("1 < 20", "true"); |
| 681 |
fold("3 < 3", "false"); |
| 682 |
fold("10 > 1.0", "true"); |
| 683 |
fold("10 > 10.25", "false"); |
| 684 |
foldSame("x == x"); |
| 685 |
fold("x < x", "false"); |
| 686 |
fold("x > x", "false"); |
| 687 |
fold("1 <= 1", "true"); |
| 688 |
fold("1 <= 0", "false"); |
| 689 |
fold("0 >= 0", "true"); |
| 690 |
fold("-1 >= 9", "false"); |
| 691 |
|
| 692 |
fold("true == true", "true"); |
| 693 |
fold("false == null", "false"); |
| 694 |
fold("false == true", "false"); |
| 695 |
fold("true == null", "false"); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
public void testFoldComparison2() { |
| 700 |
fold("x = 0 === 0", "x = true"); |
| 701 |
fold("x = 1 === 2", "x = false"); |
| 702 |
fold("x = 'abc' === 'def'", "x = false"); |
| 703 |
fold("x = 'abc' === 'abc'", "x = true"); |
| 704 |
fold("x = \"\" === ''", "x = true"); |
| 705 |
fold("x = foo() === bar()", "x = foo()===bar()"); |
| 706 |
|
| 707 |
fold("x = 1 !== 0", "x = true"); |
| 708 |
fold("x = 'abc' !== 'def'", "x = true"); |
| 709 |
fold("x = 'a' !== 'a'", "x = false"); |
| 710 |
|
| 711 |
fold("x = y === y", "x = y===y"); |
| 712 |
|
| 713 |
fold("x = true === true", "x = true"); |
| 714 |
fold("x = false === false", "x = true"); |
| 715 |
fold("x = false === null", "x = false"); |
| 716 |
fold("x = false === true", "x = false"); |
| 717 |
fold("x = true === null", "x = false"); |
| 718 |
|
| 719 |
fold("0 === 0", "true"); |
| 720 |
fold("1 === 2", "false"); |
| 721 |
fold("'abc' === 'def'", "false"); |
| 722 |
fold("'abc' === 'abc'", "true"); |
| 723 |
fold("\"\" === ''", "true"); |
| 724 |
foldSame("foo() === bar()"); |
| 725 |
|
| 726 |
|
| 727 |
foldSame("1 === '1'"); |
| 728 |
foldSame("1 === true"); |
| 729 |
foldSame("1 !== '1'"); |
| 730 |
foldSame("1 !== true"); |
| 731 |
|
| 732 |
fold("1 !== 0", "true"); |
| 733 |
fold("'abc' !== 'def'", "true"); |
| 734 |
fold("'a' !== 'a'", "false"); |
| 735 |
|
| 736 |
foldSame("x === x"); |
| 737 |
|
| 738 |
fold("true === true", "true"); |
| 739 |
fold("false === null", "false"); |
| 740 |
fold("false === true", "false"); |
| 741 |
fold("true === null", "false"); |
| 742 |
} |
| 743 |
|
| 744 |
public void testFoldComparison3() { |
| 745 |
fold("x = !1 == !0", "x = false"); |
| 746 |
|
| 747 |
fold("x = !0 == !0", "x = true"); |
| 748 |
fold("x = !1 == !1", "x = true"); |
| 749 |
fold("x = !1 == null", "x = false"); |
| 750 |
fold("x = !1 == !0", "x = false"); |
| 751 |
fold("x = !0 == null", "x = false"); |
| 752 |
|
| 753 |
fold("!0 == !0", "true"); |
| 754 |
fold("!1 == null", "false"); |
| 755 |
fold("!1 == !0", "false"); |
| 756 |
fold("!0 == null", "false"); |
| 757 |
|
| 758 |
fold("x = !0 === !0", "x = true"); |
| 759 |
fold("x = !1 === !1", "x = true"); |
| 760 |
fold("x = !1 === null", "x = false"); |
| 761 |
fold("x = !1 === !0", "x = false"); |
| 762 |
fold("x = !0 === null", "x = false"); |
| 763 |
|
| 764 |
fold("!0 === !0", "true"); |
| 765 |
fold("!1 === null", "false"); |
| 766 |
fold("!1 === !0", "false"); |
| 767 |
fold("!0 === null", "false"); |
| 768 |
} |
| 769 |
|
| 770 |
public void testFoldGetElem() { |
| 771 |
fold("x = [,10][0]", "x = void 0"); |
| 772 |
fold("x = [10, 20][0]", "x = 10"); |
| 773 |
fold("x = [10, 20][1]", "x = 20"); |
| 774 |
|
| 775 |
testSame("x = [10, 20][0.5]", |
| 776 |
PeepholeFoldConstants.INVALID_GETELEM_INDEX_ERROR); |
| 777 |
testSame("x = [10, 20][-1]", |
| 778 |
PeepholeFoldConstants.INDEX_OUT_OF_BOUNDS_ERROR); |
| 779 |
testSame("x = [10, 20][2]", |
| 780 |
PeepholeFoldConstants.INDEX_OUT_OF_BOUNDS_ERROR); |
| 781 |
|
| 782 |
foldSame("x = [foo(), 0][1]"); |
| 783 |
fold("x = [0, foo()][1]", "x = foo()"); |
| 784 |
foldSame("x = [0, foo()][0]"); |
| 785 |
} |
| 786 |
|
| 787 |
public void testFoldComplex() { |
| 788 |
fold("x = (3 / 1.0) + (1 * 2)", "x = 5"); |
| 789 |
fold("x = (1 == 1.0) && foo() && true", "x = foo()&&true"); |
| 790 |
fold("x = 'abc' + 5 + 10", "x = \"abc510\""); |
| 791 |
} |
| 792 |
|
| 793 |
public void testFoldLeft() { |
| 794 |
foldSame("(+x - 1) + 2"); |
| 795 |
fold("(+x + 1) + 2", "+x + 3"); |
| 796 |
} |
| 797 |
|
| 798 |
public void testFoldArrayLength() { |
| 799 |
|
| 800 |
fold("x = [].length", "x = 0"); |
| 801 |
fold("x = [1,2,3].length", "x = 3"); |
| 802 |
fold("x = [a,b].length", "x = 2"); |
| 803 |
|
| 804 |
|
| 805 |
fold("x = [,,1].length", "x = 3"); |
| 806 |
|
| 807 |
|
| 808 |
fold("x = [foo(), 0].length", "x = [foo(),0].length"); |
| 809 |
fold("x = y.length", "x = y.length"); |
| 810 |
} |
| 811 |
|
| 812 |
public void testFoldStringLength() { |
| 813 |
|
| 814 |
fold("x = ''.length", "x = 0"); |
| 815 |
fold("x = '123'.length", "x = 3"); |
| 816 |
|
| 817 |
|
| 818 |
fold("x = '123\u01dc'.length", "x = 4"); |
| 819 |
} |
| 820 |
|
| 821 |
public void testFoldTypeof() { |
| 822 |
fold("x = typeof 1", "x = \"number\""); |
| 823 |
fold("x = typeof 'foo'", "x = \"string\""); |
| 824 |
fold("x = typeof true", "x = \"boolean\""); |
| 825 |
fold("x = typeof false", "x = \"boolean\""); |
| 826 |
fold("x = typeof null", "x = \"object\""); |
| 827 |
fold("x = typeof undefined", "x = \"undefined\""); |
| 828 |
fold("x = typeof void 0", "x = \"undefined\""); |
| 829 |
fold("x = typeof []", "x = \"object\""); |
| 830 |
fold("x = typeof [1]", "x = \"object\""); |
| 831 |
fold("x = typeof [1,[]]", "x = \"object\""); |
| 832 |
fold("x = typeof {}", "x = \"object\""); |
| 833 |
fold("x = typeof function() {}", "x = 'function'"); |
| 834 |
|
| 835 |
foldSame("x = typeof[1,[foo()]]"); |
| 836 |
foldSame("x = typeof{bathwater:baby()}"); |
| 837 |
} |
| 838 |
|
| 839 |
public void testFoldInstanceOf() { |
| 840 |
|
| 841 |
fold("64 instanceof Object", "false"); |
| 842 |
fold("64 instanceof Number", "false"); |
| 843 |
fold("'' instanceof Object", "false"); |
| 844 |
fold("'' instanceof String", "false"); |
| 845 |
fold("true instanceof Object", "false"); |
| 846 |
fold("true instanceof Boolean", "false"); |
| 847 |
fold("!0 instanceof Object", "false"); |
| 848 |
fold("!0 instanceof Boolean", "false"); |
| 849 |
fold("false instanceof Object", "false"); |
| 850 |
fold("null instanceof Object", "false"); |
| 851 |
fold("undefined instanceof Object", "false"); |
| 852 |
fold("NaN instanceof Object", "false"); |
| 853 |
fold("Infinity instanceof Object", "false"); |
| 854 |
|
| 855 |
|
| 856 |
fold("[] instanceof Object", "true"); |
| 857 |
fold("({}) instanceof Object", "true"); |
| 858 |
|
| 859 |
|
| 860 |
foldSame("new Foo() instanceof Object"); |
| 861 |
|
| 862 |
foldSame("[] instanceof Foo"); |
| 863 |
foldSame("({}) instanceof Foo"); |
| 864 |
|
| 865 |
fold("(function() {}) instanceof Object", "true"); |
| 866 |
|
| 867 |
|
| 868 |
foldSame("x instanceof Foo"); |
| 869 |
} |
| 870 |
|
| 871 |
public void testDivision() { |
| 872 |
|
| 873 |
fold("print(1/3)", "print(1/3)"); |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
fold("print(1/2)", "print(0.5)"); |
| 878 |
} |
| 879 |
|
| 880 |
public void testAssignOpsLate() { |
| 881 |
late = true; |
| 882 |
fold("x=x+y", "x+=y"); |
| 883 |
foldSame("x=y+x"); |
| 884 |
fold("x=x*y", "x*=y"); |
| 885 |
fold("x=y*x", "x*=y"); |
| 886 |
fold("x.y=x.y+z", "x.y+=z"); |
| 887 |
foldSame("next().x = next().x + 1"); |
| 888 |
|
| 889 |
fold("x=x-y", "x-=y"); |
| 890 |
foldSame("x=y-x"); |
| 891 |
fold("x=x|y", "x|=y"); |
| 892 |
fold("x=y|x", "x|=y"); |
| 893 |
fold("x=x*y", "x*=y"); |
| 894 |
fold("x=y*x", "x*=y"); |
| 895 |
fold("x.y=x.y+z", "x.y+=z"); |
| 896 |
foldSame("next().x = next().x + 1"); |
| 897 |
|
| 898 |
fold("({a:1}).a = ({a:1}).a + 1", "({a:1}).a = 2"); |
| 899 |
} |
| 900 |
|
| 901 |
public void testAssignOpsEarly() { |
| 902 |
late = false; |
| 903 |
foldSame("x=x+y"); |
| 904 |
foldSame("x=y+x"); |
| 905 |
foldSame("x=x*y"); |
| 906 |
foldSame("x=y*x"); |
| 907 |
foldSame("x.y=x.y+z"); |
| 908 |
foldSame("next().x = next().x + 1"); |
| 909 |
|
| 910 |
foldSame("x=x-y"); |
| 911 |
foldSame("x=y-x"); |
| 912 |
foldSame("x=x|y"); |
| 913 |
foldSame("x=y|x"); |
| 914 |
foldSame("x=x*y"); |
| 915 |
foldSame("x=y*x"); |
| 916 |
foldSame("x.y=x.y+z"); |
| 917 |
foldSame("next().x = next().x + 1"); |
| 918 |
|
| 919 |
fold("({a:1}).a = ({a:1}).a + 1", "({a:1}).a = 2"); |
| 920 |
} |
| 921 |
|
| 922 |
public void testFoldAdd1() { |
| 923 |
fold("x=false+1","x=1"); |
| 924 |
fold("x=true+1","x=2"); |
| 925 |
fold("x=1+false","x=1"); |
| 926 |
fold("x=1+true","x=2"); |
| 927 |
} |
| 928 |
|
| 929 |
public void testFoldLiteralNames() { |
| 930 |
foldSame("NaN == NaN"); |
| 931 |
foldSame("Infinity == Infinity"); |
| 932 |
foldSame("Infinity == NaN"); |
| 933 |
fold("undefined == NaN", "false"); |
| 934 |
fold("undefined == Infinity", "false"); |
| 935 |
|
| 936 |
foldSame("Infinity >= Infinity"); |
| 937 |
foldSame("NaN >= NaN"); |
| 938 |
} |
| 939 |
|
| 940 |
public void testFoldLiteralsTypeMismatches() { |
| 941 |
fold("true == true", "true"); |
| 942 |
fold("true == false", "false"); |
| 943 |
fold("true == null", "false"); |
| 944 |
fold("false == null", "false"); |
| 945 |
|
| 946 |
|
| 947 |
fold("null <= null", "true"); |
| 948 |
fold("null >= null", "true"); |
| 949 |
fold("null > null", "false"); |
| 950 |
fold("null < null", "false"); |
| 951 |
|
| 952 |
fold("false >= null", "true"); |
| 953 |
fold("false <= null", "true"); |
| 954 |
fold("false > null", "false"); |
| 955 |
fold("false < null", "false"); |
| 956 |
|
| 957 |
fold("true >= null", "true"); |
| 958 |
fold("true <= null", "false"); |
| 959 |
fold("true > null", "true"); |
| 960 |
fold("true < null", "false"); |
| 961 |
|
| 962 |
fold("true >= false", "true"); |
| 963 |
fold("true <= false", "false"); |
| 964 |
fold("true > false", "true"); |
| 965 |
fold("true < false", "false"); |
| 966 |
} |
| 967 |
|
| 968 |
public void testFoldLeftChildConcat() { |
| 969 |
foldSame("x +5 + \"1\""); |
| 970 |
fold("x+\"5\" + \"1\"", "x + \"51\""); |
| 971 |
|
| 972 |
fold("\"a\"+(\"b\"+c)","\"ab\"+c"); |
| 973 |
} |
| 974 |
|
| 975 |
public void testFoldLeftChildOp() { |
| 976 |
fold("x * Infinity * 2", "x * Infinity"); |
| 977 |
foldSame("x - Infinity - 2"); |
| 978 |
foldSame("x - 1 + Infinity"); |
| 979 |
foldSame("x - 2 + 1"); |
| 980 |
foldSame("x - 2 + 3"); |
| 981 |
foldSame("1 + x - 2 + 1"); |
| 982 |
foldSame("1 + x - 2 + 3"); |
| 983 |
foldSame("1 + x - 2 + 3 - 1"); |
| 984 |
foldSame("f(x)-0"); |
| 985 |
foldSame("x-0-0"); |
| 986 |
foldSame("x+2-2+2"); |
| 987 |
foldSame("x+2-2+2-2"); |
| 988 |
foldSame("x-2+2"); |
| 989 |
foldSame("x-2+2-2"); |
| 990 |
foldSame("x-2+2-2+2"); |
| 991 |
|
| 992 |
foldSame("1+x-0-NaN"); |
| 993 |
foldSame("1+f(x)-0-NaN"); |
| 994 |
foldSame("1+x-0+NaN"); |
| 995 |
foldSame("1+f(x)-0+NaN"); |
| 996 |
|
| 997 |
foldSame("1+x+NaN"); |
| 998 |
foldSame("x+2-2"); |
| 999 |
foldSame("x+2"); |
| 1000 |
foldSame("x-2"); |
| 1001 |
} |
| 1002 |
|
| 1003 |
public void testFoldSimpleArithmeticOp() { |
| 1004 |
foldSame("x*NaN"); |
| 1005 |
foldSame("NaN/y"); |
| 1006 |
foldSame("f(x)-0"); |
| 1007 |
foldSame("f(x)*1"); |
| 1008 |
foldSame("1*f(x)"); |
| 1009 |
foldSame("0+a+b"); |
| 1010 |
foldSame("0-a-b"); |
| 1011 |
foldSame("a+b-0"); |
| 1012 |
foldSame("(1+x)*NaN"); |
| 1013 |
|
| 1014 |
foldSame("(1+f(x))*NaN"); |
| 1015 |
} |
| 1016 |
|
| 1017 |
public void testFoldLiteralsAsNumbers() { |
| 1018 |
fold("x/'12'","x/12"); |
| 1019 |
fold("x/('12'+'6')", "x/126"); |
| 1020 |
fold("true*x", "1*x"); |
| 1021 |
fold("x/false", "x/0"); |
| 1022 |
} |
| 1023 |
|
| 1024 |
public void testNotFoldBackToTrueFalse() { |
| 1025 |
late = false; |
| 1026 |
fold("!0", "true"); |
| 1027 |
fold("!1", "false"); |
| 1028 |
fold("!3", "false"); |
| 1029 |
|
| 1030 |
late = true; |
| 1031 |
foldSame("!0"); |
| 1032 |
foldSame("!1"); |
| 1033 |
fold("!3", "false"); |
| 1034 |
foldSame("false"); |
| 1035 |
foldSame("true"); |
| 1036 |
} |
| 1037 |
|
| 1038 |
public void testFoldBangConstants() { |
| 1039 |
fold("1 + !0", "2"); |
| 1040 |
fold("1 + !1", "1"); |
| 1041 |
fold("'a ' + !1", "'a false'"); |
| 1042 |
fold("'a ' + !0", "'a true'"); |
| 1043 |
} |
| 1044 |
|
| 1045 |
public void testFoldMixed() { |
| 1046 |
fold("''+[1]", "'1'"); |
| 1047 |
foldSame("false+[]"); |
| 1048 |
} |
| 1049 |
|
| 1050 |
public void testFoldVoid() { |
| 1051 |
foldSame("void 0"); |
| 1052 |
fold("void 1", "void 0"); |
| 1053 |
fold("void x", "void 0"); |
| 1054 |
fold("void x()", "void x()"); |
| 1055 |
} |
| 1056 |
|
| 1057 |
public void testObjectLiteral() { |
| 1058 |
test("(!{})", "false"); |
| 1059 |
test("(!{a:1})", "false"); |
| 1060 |
testSame("(!{a:foo()})"); |
| 1061 |
testSame("(!{'a':foo()})"); |
| 1062 |
} |
| 1063 |
|
| 1064 |
public void testArrayLiteral() { |
| 1065 |
test("(![])", "false"); |
| 1066 |
test("(![1])", "false"); |
| 1067 |
test("(![a])", "false"); |
| 1068 |
testSame("(![foo()])"); |
| 1069 |
} |
| 1070 |
|
| 1071 |
public void testIssue601() { |
| 1072 |
testSame("'\\v' == 'v'"); |
| 1073 |
testSame("'v' == '\\v'"); |
| 1074 |
testSame("'\\u000B' == '\\v'"); |
| 1075 |
} |
| 1076 |
|
| 1077 |
public void testFoldObjectLiteralRef1() { |
| 1078 |
|
| 1079 |
testSame("var x = ({a:foo(),b:bar()}).a"); |
| 1080 |
testSame("var x = ({a:1,b:bar()}).a"); |
| 1081 |
testSame("function f() { return {b:foo(), a:2}.a; }"); |
| 1082 |
|
| 1083 |
|
| 1084 |
testSame("({a:x}).a = 1"); |
| 1085 |
test("({a:x}).a += 1", "({a:x}).a = x + 1"); |
| 1086 |
testSame("({a:x}).a ++"); |
| 1087 |
testSame("({a:x}).a --"); |
| 1088 |
|
| 1089 |
|
| 1090 |
testSame("({a:function(){return this}}).a"); |
| 1091 |
testSame("({get a() {return this}}).a"); |
| 1092 |
testSame("({set a(b) {return this}}).a"); |
| 1093 |
|
| 1094 |
|
| 1095 |
testSame("({}).a"); |
| 1096 |
|
| 1097 |
|
| 1098 |
testSame("({}).a"); |
| 1099 |
testSame("({set a(b) {}}).a"); |
| 1100 |
|
| 1101 |
test("({a:1,set a(b) {}}).a", "1"); |
| 1102 |
|
| 1103 |
|
| 1104 |
test("({get a() {}}).a", "(function (){})()"); |
| 1105 |
|
| 1106 |
test("({get a() {},set a(b) {}}).a", "(function (){})()"); |
| 1107 |
|
| 1108 |
|
| 1109 |
test("var x = ({a:function(){return 1}}).a", |
| 1110 |
"var x = function(){return 1}"); |
| 1111 |
|
| 1112 |
test("var x = ({a:1}).a", "var x = 1"); |
| 1113 |
test("var x = ({a:1, a:2}).a", "var x = 2"); |
| 1114 |
test("var x = ({a:1, a:foo()}).a", "var x = foo()"); |
| 1115 |
test("var x = ({a:foo()}).a", "var x = foo()"); |
| 1116 |
|
| 1117 |
test("function f() { return {a:1, b:2}.a; }", |
| 1118 |
"function f() { return 1; }"); |
| 1119 |
|
| 1120 |
|
| 1121 |
test("var x = ({'a':1})['a']", "var x = 1"); |
| 1122 |
} |
| 1123 |
|
| 1124 |
public void testFoldObjectLiteralRef2() { |
| 1125 |
late = false; |
| 1126 |
test("({a:x}).a += 1", "({a:x}).a = x + 1"); |
| 1127 |
late = true; |
| 1128 |
testSame("({a:x}).a += 1"); |
| 1129 |
} |
| 1130 |
|
| 1131 |
public void testIEString() { |
| 1132 |
testSame("!+'\\v1'"); |
| 1133 |
} |
| 1134 |
|
| 1135 |
public void testIssue522() { |
| 1136 |
testSame("[][1] = 1;"); |
| 1137 |
} |
| 1138 |
|
| 1139 |
private static final List<String> LITERAL_OPERANDS = |
| 1140 |
ImmutableList.of( |
| 1141 |
"null", |
| 1142 |
"undefined", |
| 1143 |
"void 0", |
| 1144 |
"true", |
| 1145 |
"false", |
| 1146 |
"!0", |
| 1147 |
"!1", |
| 1148 |
"0", |
| 1149 |
"1", |
| 1150 |
"''", |
| 1151 |
"'123'", |
| 1152 |
"'abc'", |
| 1153 |
"'def'", |
| 1154 |
"NaN", |
| 1155 |
"Infinity", |
| 1156 |
|
| 1157 |
"-Infinity" |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
); |
| 1164 |
|
| 1165 |
public void testInvertibleOperators() { |
| 1166 |
Map<String, String> inverses = ImmutableMap.<String, String>builder() |
| 1167 |
.put("==", "!=") |
| 1168 |
.put("===", "!==") |
| 1169 |
.put("<=", ">") |
| 1170 |
.put("<", ">=") |
| 1171 |
.put(">=", "<") |
| 1172 |
.put(">", "<=") |
| 1173 |
.put("!=", "==") |
| 1174 |
.put("!==", "===") |
| 1175 |
.build(); |
| 1176 |
Set<String> comparators = ImmutableSet.of("<=", "<", ">=", ">"); |
| 1177 |
Set<String> equalitors = ImmutableSet.of("==", "==="); |
| 1178 |
Set<String> uncomparables = ImmutableSet.of("undefined", "void 0"); |
| 1179 |
List<String> operators = ImmutableList.copyOf(inverses.values()); |
| 1180 |
for (int iOperandA = 0; iOperandA < LITERAL_OPERANDS.size(); iOperandA++) { |
| 1181 |
for (int iOperandB = 0; |
| 1182 |
iOperandB < LITERAL_OPERANDS.size(); |
| 1183 |
iOperandB++) { |
| 1184 |
for (int iOp = 0; iOp < operators.size(); iOp++) { |
| 1185 |
String a = LITERAL_OPERANDS.get(iOperandA); |
| 1186 |
String b = LITERAL_OPERANDS.get(iOperandB); |
| 1187 |
String op = operators.get(iOp); |
| 1188 |
String inverse = inverses.get(op); |
| 1189 |
|
| 1190 |
|
| 1191 |
if (comparators.contains(op) && |
| 1192 |
(uncomparables.contains(a) || uncomparables.contains(b))) { |
| 1193 |
assertSameResults(join(a, op, b), "false"); |
| 1194 |
assertSameResults(join(a, inverse, b), "false"); |
| 1195 |
} else if (a.equals(b) && equalitors.contains(op)) { |
| 1196 |
if (a.equals("NaN") || |
| 1197 |
a.equals("Infinity") || |
| 1198 |
a.equals("-Infinity")) { |
| 1199 |
foldSame(join(a, op, b)); |
| 1200 |
foldSame(join(a, inverse, b)); |
| 1201 |
} else { |
| 1202 |
assertSameResults(join(a, op, b), "true"); |
| 1203 |
assertSameResults(join(a, inverse, b), "false"); |
| 1204 |
} |
| 1205 |
} else { |
| 1206 |
assertNotSameResults(join(a, op, b), join(a, inverse, b)); |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
public void testCommutativeOperators() { |
| 1214 |
late = true; |
| 1215 |
List<String> operators = |
| 1216 |
ImmutableList.of( |
| 1217 |
"==", |
| 1218 |
"!=", |
| 1219 |
"===", |
| 1220 |
"!==", |
| 1221 |
"*", |
| 1222 |
"|", |
| 1223 |
"&", |
| 1224 |
"^"); |
| 1225 |
for (int iOperandA = 0; iOperandA < LITERAL_OPERANDS.size(); iOperandA++) { |
| 1226 |
for (int iOperandB = iOperandA; |
| 1227 |
iOperandB < LITERAL_OPERANDS.size(); |
| 1228 |
iOperandB++) { |
| 1229 |
for (int iOp = 0; iOp < operators.size(); iOp++) { |
| 1230 |
String a = LITERAL_OPERANDS.get(iOperandA); |
| 1231 |
String b = LITERAL_OPERANDS.get(iOperandB); |
| 1232 |
String op = operators.get(iOp); |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
assertSameResultsOrUncollapsed(join(a, op, b), join(b, op, a)); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
public void testConvertToNumberNegativeInf() { |
| 1243 |
foldSame("var x = 3 * (r ? Infinity : -Infinity);"); |
| 1244 |
} |
| 1245 |
|
| 1246 |
private String join(String operandA, String op, String operandB) { |
| 1247 |
return operandA + " " + op + " " + operandB; |
| 1248 |
} |
| 1249 |
|
| 1250 |
private void assertSameResultsOrUncollapsed(String exprA, String exprB) { |
| 1251 |
String resultA = process(exprA); |
| 1252 |
if (resultA.equals(print(exprA))) { |
| 1253 |
foldSame(exprA); |
| 1254 |
foldSame(exprB); |
| 1255 |
} else { |
| 1256 |
assertSameResults(exprA, exprB); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
private void assertSameResults(String exprA, String exprB) { |
| 1261 |
assertEquals( |
| 1262 |
"Expressions did not fold the same\nexprA: " + |
| 1263 |
exprA + "\nexprB: " + exprB, |
| 1264 |
process(exprA), process(exprB)); |
| 1265 |
} |
| 1266 |
|
| 1267 |
private void assertNotSameResults(String exprA, String exprB) { |
| 1268 |
assertFalse( |
| 1269 |
"Expressions folded the same\nexprA: " + |
| 1270 |
exprA + "\nexprB: " + exprB, |
| 1271 |
process(exprA).equals(process(exprB))); |
| 1272 |
} |
| 1273 |
|
| 1274 |
private String process(String js) { |
| 1275 |
return printHelper(js, true); |
| 1276 |
} |
| 1277 |
|
| 1278 |
private String print(String js) { |
| 1279 |
return printHelper(js, false); |
| 1280 |
} |
| 1281 |
|
| 1282 |
private String printHelper(String js, boolean runProcessor) { |
| 1283 |
Compiler compiler = createCompiler(); |
| 1284 |
CompilerOptions options = getOptions(); |
| 1285 |
compiler.init( |
| 1286 |
ImmutableList.<SourceFile>of(), |
| 1287 |
ImmutableList.of(SourceFile.fromCode("testcode", js)), |
| 1288 |
options); |
| 1289 |
Node root = compiler.parseInputs(); |
| 1290 |
assertTrue("Unexpected parse error(s): " + |
| 1291 |
Joiner.on("\n").join(compiler.getErrors()) + |
| 1292 |
"\nEXPR: " + js, |
| 1293 |
root != null); |
| 1294 |
Node externsRoot = root.getFirstChild(); |
| 1295 |
Node mainRoot = externsRoot.getNext(); |
| 1296 |
if (runProcessor) { |
| 1297 |
getProcessor(compiler).process(externsRoot, mainRoot); |
| 1298 |
} |
| 1299 |
return compiler.toSource(mainRoot); |
| 1300 |
} |
| 1301 |
} |