Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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 public class PeepholeCollectPropertyAssignmentsTest extends CompilerTestCase {
20
21 @Override
22 protected CompilerPass getProcessor(final Compiler compiler) {
23 return new PeepholeOptimizationsPass(
24 compiler, new PeepholeCollectPropertyAssignments());
25 }
26
27 public final void testArrayOptimization1() {
28 test("var a = []; a[0] = 1; a[1] = 2; a[2] = 3;",
29 "var a = [1, 2, 3];");
30 }
31
32 public final void testArrayOptimization2() {
33 test("var a; a = []; a[0] = 1; a[1] = 2; a[2] = 3;",
34 "var a; a = [1, 2, 3];");
35 }
36
37 public final void testArrayOptimization3() {
38 testSame("var a; a.b = []; a.b[0] = 1; a.b[1] = 2; a.b[2] = 3;");
39 }
40
41 public final void testCompoundAssignment() {
42 testSame("var x, a; a = []; a[0] *= x;");
43 }
44
45 public final void testNegativeArrayIndex1() {
46 testSame("var a = []; a[-1] = 1;");
47 }
48
49 public final void testNegativeArrayIndex2() {
50 testSame("var a; a = []; a[-1] = 1;");
51 }
52
53 public final void testFractionalArrayIndex1() {
54 testSame("var a = []; a[0.5] = 1;");
55 }
56
57 public final void testFractionalArrayIndex2() {
58 testSame("var a; a = []; a[0.5] = 1;");
59 }
60
61 public final void testArrayOptimizationOfPartiallyBuiltArray1() {
62 test("var a = [1, 2]; a[2] = 3;",
63 "var a = [1, 2, 3];");
64 }
65
66 public final void testArrayOptimizationOfPartiallyBuiltArray2() {
67 test("var a; a = [1, 2]; a[2] = 3;",
68 "var a; a = [1, 2, 3];");
69 }
70
71 public final void testArrayOptimizationWithAHole1() {
72 test("var a = []; a[0] = 1; a[1] = 2; a[3] = 4;",
73 "var a = [1, 2, , 4];");
74 }
75
76 public final void testArrayOptimizationWithAHole2() {
77 test("var a; a = []; a[0] = 1; a[1] = 2; a[3] = 4;",
78 "var a; a = [1, 2, , 4];");
79 }
80
81 public final void testEarlyUsage1() {
82 testSame(
83 "function c() {return sum(a)};"
84 + "var a = [1,2,3];"
85 + "a[4] = c();");
86 }
87
88 public final void testEarlyUsage2() {
89 testSame(
90 "function c() {return sum(a)};"
91 + "var a; a = [1,2,3];"
92 + "a[4] = c();");
93 }
94
95 public final void testArrayTooSparseOptimization1() {
96 test("var a = []; a[0] = 1; a[1] = 2; a[100] = 4;",
97 "var a = [1, 2]; a[100] = 4;");
98 }
99
100 public final void testArrayTooSparseOptimization2() {
101 test("var a; a = []; a[0] = 1; a[1] = 2; a[100] = 4;",
102 "var a; a = [1, 2]; a[100] = 4;");
103 }
104
105 public final void testArrayOutOfOrder() {
106 test("var a = []; a[1] = 1; a[0] = 0;",
107 "var a = [0, 1];");
108 test("var a; a = []; a[1] = 1; a[0] = 0;",
109 "var a; a = [0, 1];");
110 // We cannot change the order of side-effects.
111 // The below should not be
112 // var x = 0; var a = [x++, x++]
113 // since that would produce
114 // var a = [0, 1], x = 2;
115 // instead of
116 // var a = [1, 0], x = 2;
117 testSame("var x = 0; var a = []; a[1] = x++; a[0] = x++;");
118 testSame("var x; x = 0; var a = []; a[1] = x++; a[0] = x++;");
119 }
120
121 public final void testMultipleNames1() {
122 test("var b = []; b[0] = 2; var a = []; a[0] = 1;",
123 "var b = [2]; var a = [1];");
124 }
125
126 public final void testMultipleNames2() {
127 test("var b; b = []; b[0] = 2; var a = []; a[0] = 1;",
128 "var b; b = [2]; var a = [1];");
129 }
130
131
132 public final void testArrayReassignedInValue1() {
133 test("var a = []; a[0] = 1; a[1] = (a = []); a[3] = 4;",
134 "var a = [1]; a[1] = (a = []); a[3] = 4;");
135 }
136
137 public final void testArrayReassignedInValue2() {
138 test("var a; a = []; a[0] = 1; a[1] = (a = []); a[3] = 4;",
139 "var a; a = [1]; a[1] = (a = []); a[3] = 4;");
140 }
141
142 public final void testArrayReassignedInSubsequentVar1() {
143 testSame("var a = []; a[0] = a = []; a[1] = 2;");
144 }
145
146 public final void testArrayReassignedInSubsequentVar2() {
147 testSame("var a; a = []; a[0] = a = []; a[1] = 2;");
148 }
149
150 public final void testForwardReference1() {
151 test("var a; a = []; a[0] = 1; a[1] = a;",
152 "var a; a = [1]; a[1] = a;");
153 }
154
155 public final void testForwardReference2() {
156 test("var a; a = []; a[0] = 1; a[1] = a;",
157 "var a; a = [1]; a[1] = a;");
158 }
159
160 public final void testObjectOptimization1() {
161 test("var o = {}; o.x = 0; o['y'] = 1; o[2] = 2;",
162 "var o = { x: 0, \"y\": 1, \"2\": 2 };");
163 }
164
165 public final void testObjectOptimization2() {
166 test("var o; o = {}; o.x = 0; o['y'] = 1; o[2] = 2;",
167 "var o; o = { x: 0, \"y\": 1, \"2\": 2 };");
168 }
169
170 public final void testObjectReassignedInValue1() {
171 test("var o = {}; o.x = 1; o.y = (o = {}); o.z = 4;",
172 "var o = {x:1}; o.y = (o = {}); o.z = 4;");
173 }
174
175
176 public final void testObjectReassignedInValue2() {
177 test("var o; o = {}; o.x = 1; o.y = (o = {}); o.z = 4;",
178 "var o; o = {x:1}; o.y = (o = {}); o.z = 4;");
179 }
180
181 public final void testObjectFunctionRollup1() {
182 test("var o; o = {};" +
183 "o.x = function() {};",
184 "var o; o = {x:function () {}};");
185 }
186
187 public final void testObjectFunctionRollup2() {
188 testSame(
189 "var o; o = {};" +
190 "o.x = (function() {return o})();");
191 }
192
193 public final void testObjectFunctionRollup3() {
194 test("var o; o = {};" +
195 "o.x = function() {return o};",
196 "var o; o = {x:function () {return o}};");
197 }
198
199 public final void testObjectFunctionRollup4() {
200 testSame(
201 "function f() {return o};" +
202 "var o; o = {};" +
203 "o.x = f();");
204 }
205
206 public final void testObjectFunctionRollup5() {
207 test("var o; o = {};" +
208 "o.x = function() {return o};" +
209 "o.y = [function() {return o}];" +
210 "o.z = {a:function() {return o}};",
211
212 "var o; o = {" +
213 "x:function () {return o}, " +
214 "y:[function () {return o}], " +
215 "z:{a:function () {return o}}};");
216 }
217
218 }