Debian Codegraph @

test/com/google/javascript/jscomp/ReorderConstantExpressionTest.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 /**
20 * Unit test for {@ReorderConstantExpression}
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 * This set has a mutable on the right and an Immutable on the left.
78 * Applies for relational and symmetric operations.
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 * This set has a mutable on the right and an Immutable on the left.
99 * Applies only for symmetric operations.
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 * This set has an the immutable on the left already, or both non-immutable.
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 }