| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class ReorderConstantExpressionTest extends CompilerTestCase { |
| 24 |
|
| 25 |
@Override |
| 26 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 27 |
return new PeepholeOptimizationsPass(compiler, |
| 28 |
new ReorderConstantExpression()); |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
protected void setUp() throws Exception { |
| 33 |
super.setUp(); |
| 34 |
super.enableLineNumberCheck(true); |
| 35 |
disableTypeCheck(); |
| 36 |
} |
| 37 |
|
| 38 |
public void testSymmetricOperations() throws Exception { |
| 39 |
set1Tests("=="); |
| 40 |
set2Tests("=="); |
| 41 |
set3Tests("=="); |
| 42 |
|
| 43 |
set1Tests("!="); |
| 44 |
set2Tests("!="); |
| 45 |
set3Tests("!="); |
| 46 |
|
| 47 |
set1Tests("==="); |
| 48 |
set2Tests("==="); |
| 49 |
set3Tests("==="); |
| 50 |
|
| 51 |
set1Tests("!=="); |
| 52 |
set2Tests("!=="); |
| 53 |
set3Tests("!=="); |
| 54 |
|
| 55 |
set1Tests("*"); |
| 56 |
set2Tests("*"); |
| 57 |
set3Tests("*"); |
| 58 |
} |
| 59 |
|
| 60 |
public void testRelationalOperations() throws Exception { |
| 61 |
set1Tests(">", "<"); |
| 62 |
set3Tests(">"); |
| 63 |
set1Tests("<", ">"); |
| 64 |
set3Tests("<"); |
| 65 |
|
| 66 |
set1Tests(">=", "<="); |
| 67 |
set3Tests(">="); |
| 68 |
set1Tests("<=", ">="); |
| 69 |
set3Tests("<="); |
| 70 |
} |
| 71 |
|
| 72 |
private void set1Tests(String op) throws Exception { |
| 73 |
set1Tests(op, op); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private void set1Tests(String op1, String op2) throws Exception { |
| 81 |
test("a " + op1 + " 0", "0 " + op2 + " a"); |
| 82 |
test("a " + op1 + " '0'", "'0' " + op2 + " a"); |
| 83 |
test("a " + op1 + " ''", "'' " + op2 + " a"); |
| 84 |
test("a " + op1 + " -1.0", "-1.0 " + op2 + " a"); |
| 85 |
|
| 86 |
test("function f(a){a " + op1 + " 0}", |
| 87 |
"function f(a){0 " + op2 + " a}"); |
| 88 |
test("f() " + op1 + " 0", "0 " + op2 + " f()"); |
| 89 |
test("(a + b) " + op1 + " 0", "0 " + op2 + " (a + b)"); |
| 90 |
test("(a + 1) " + op1 + " 0", "0 " + op2 + " (a + 1)"); |
| 91 |
|
| 92 |
test("x++ " + op1 + " 0", "0 " + op2 + " x++"); |
| 93 |
test("x = 0; function f(){x++; return x}; f() " + op1 + " 0", |
| 94 |
"x = 0; function f(){x++; return x}; 0 " + op2 + " f()"); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
private void set2Tests(String op) throws Exception { |
| 102 |
test("a " + op + " NaN", "NaN " + op + " a"); |
| 103 |
test("a " + op + " Infinity", "Infinity " + op + " a"); |
| 104 |
|
| 105 |
testSame("NaN " + op + " a"); |
| 106 |
testSame("Infinity " + op + " a"); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private void set3Tests(String op) throws Exception { |
| 113 |
testSame("0 " + op + " a"); |
| 114 |
testSame("'0' " + op + " a"); |
| 115 |
testSame("'' " + op + " a"); |
| 116 |
testSame("-1.0 " + op + " a"); |
| 117 |
testSame("-1.0 " + op + " a"); |
| 118 |
|
| 119 |
testSame("0 " + op + " 1"); |
| 120 |
|
| 121 |
testSame("a " + op + " b"); |
| 122 |
} |
| 123 |
} |