Debian Codegraph @

test/com/google/javascript/jscomp/PeepholeReplaceKnownMethodsTest.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 tests for {#link {@link PeepholeReplaceKnownMethods}
21 *
22 */
23 public class PeepholeReplaceKnownMethodsTest extends CompilerTestCase {
24
25 private boolean late = true;
26
27 public PeepholeReplaceKnownMethodsTest() {
28 super("");
29 }
30
31 @Override
32 public void setUp() {
33 enableLineNumberCheck(true);
34 }
35
36 @Override
37 public CompilerPass getProcessor(final Compiler compiler) {
38 CompilerPass peepholePass = new PeepholeOptimizationsPass(compiler,
39 new PeepholeReplaceKnownMethods(late));
40 return peepholePass;
41 }
42
43 public void testStringIndexOf() {
44 fold("x = 'abcdef'.indexOf('b')", "x = 1");
45 fold("x = 'abcdefbe'.indexOf('b', 2)", "x = 6");
46 fold("x = 'abcdef'.indexOf('bcd')", "x = 1");
47 fold("x = 'abcdefsdfasdfbcdassd'.indexOf('bcd', 4)", "x = 13");
48
49 fold("x = 'abcdef'.lastIndexOf('b')", "x = 1");
50 fold("x = 'abcdefbe'.lastIndexOf('b')", "x = 6");
51 fold("x = 'abcdefbe'.lastIndexOf('b', 5)", "x = 1");
52
53 // Both elements must be strings. Don't do anything if either one is not
54 // string.
55 fold("x = 'abc1def'.indexOf(1)", "x = 3");
56 fold("x = 'abcNaNdef'.indexOf(NaN)", "x = 3");
57 fold("x = 'abcundefineddef'.indexOf(undefined)", "x = 3");
58 fold("x = 'abcnulldef'.indexOf(null)", "x = 3");
59 fold("x = 'abctruedef'.indexOf(true)", "x = 3");
60
61 // The following test case fails with JSC_PARSE_ERROR. Hence omitted.
62 // foldSame("x = 1.indexOf('bcd');");
63 foldSame("x = NaN.indexOf('bcd')");
64 foldSame("x = undefined.indexOf('bcd')");
65 foldSame("x = null.indexOf('bcd')");
66 foldSame("x = true.indexOf('bcd')");
67 foldSame("x = false.indexOf('bcd')");
68
69 // Avoid dealing with regex or other types.
70 foldSame("x = 'abcdef'.indexOf(/b./)");
71 foldSame("x = 'abcdef'.indexOf({a:2})");
72 foldSame("x = 'abcdef'.indexOf([1,2])");
73 }
74
75 public void testStringJoinAddSparse() {
76 fold("x = [,,'a'].join(',')", "x = ',,a'");
77 }
78
79 public void testNoStringJoin() {
80 foldSame("x = [].join(',',2)");
81 foldSame("x = [].join(f)");
82 }
83
84 public void testStringJoinAdd() {
85 fold("x = ['a', 'b', 'c'].join('')", "x = \"abc\"");
86 fold("x = [].join(',')", "x = \"\"");
87 fold("x = ['a'].join(',')", "x = \"a\"");
88 fold("x = ['a', 'b', 'c'].join(',')", "x = \"a,b,c\"");
89 fold("x = ['a', foo, 'b', 'c'].join(',')",
90 "x = [\"a\",foo,\"b,c\"].join()");
91 fold("x = [foo, 'a', 'b', 'c'].join(',')",
92 "x = [foo,\"a,b,c\"].join()");
93 fold("x = ['a', 'b', 'c', foo].join(',')",
94 "x = [\"a,b,c\",foo].join()");
95
96 // Works with numbers
97 fold("x = ['a=', 5].join('')", "x = \"a=5\"");
98 fold("x = ['a', '5'].join(7)", "x = \"a75\"");
99
100 // Works on boolean
101 fold("x = ['a=', false].join('')", "x = \"a=false\"");
102 fold("x = ['a', '5'].join(true)", "x = \"atrue5\"");
103 fold("x = ['a', '5'].join(false)", "x = \"afalse5\"");
104
105 // Only optimize if it's a size win.
106 fold("x = ['a', '5', 'c'].join('a very very very long chain')",
107 "x = [\"a\",\"5\",\"c\"].join(\"a very very very long chain\")");
108
109 // TODO(user): Its possible to fold this better.
110 foldSame("x = ['', foo].join('-')");
111 foldSame("x = ['', foo, ''].join()");
112
113 fold("x = ['', '', foo, ''].join(',')",
114 "x = [',', foo, ''].join()");
115 fold("x = ['', '', foo, '', ''].join(',')",
116 "x = [',', foo, ','].join()");
117
118 fold("x = ['', '', foo, '', '', bar].join(',')",
119 "x = [',', foo, ',', bar].join()");
120
121 fold("x = [1,2,3].join('abcdef')",
122 "x = '1abcdef2abcdef3'");
123
124 fold("x = [1,2].join()", "x = '1,2'");
125 fold("x = [null,undefined,''].join(',')", "x = ',,'");
126 fold("x = [null,undefined,0].join(',')", "x = ',,0'");
127 // This can be folded but we don't currently.
128 foldSame("x = [[1,2],[3,4]].join()"); // would like: "x = '1,2,3,4'"
129 }
130
131 public void testStringJoinAdd_b1992789() {
132 fold("x = ['a'].join('')", "x = \"a\"");
133 fold("x = [foo()].join('')", "x = '' + foo()");
134 fold("[foo()].join('')", "'' + foo()");
135 }
136
137 public void testFoldStringSubstr() {
138 fold("x = 'abcde'.substr(0,2)", "x = 'ab'");
139 fold("x = 'abcde'.substr(1,2)", "x = 'bc'");
140 fold("x = 'abcde'['substr'](1,3)", "x = 'bcd'");
141 fold("x = 'abcde'.substr(2)", "x = 'cde'");
142
143 // we should be leaving negative indexes alone for now
144 foldSame("x = 'abcde'.substr(-1)");
145 foldSame("x = 'abcde'.substr(1, -2)");
146 foldSame("x = 'abcde'.substr(1, 2, 3)");
147 foldSame("x = 'a'.substr(0, 2)");
148 }
149
150 public void testFoldStringSubstring() {
151 fold("x = 'abcde'.substring(0,2)", "x = 'ab'");
152 fold("x = 'abcde'.substring(1,2)", "x = 'b'");
153 fold("x = 'abcde'['substring'](1,3)", "x = 'bc'");
154 fold("x = 'abcde'.substring(2)", "x = 'cde'");
155
156 // we should be leaving negative indexes alone for now
157 foldSame("x = 'abcde'.substring(-1)");
158 foldSame("x = 'abcde'.substring(1, -2)");
159 foldSame("x = 'abcde'.substring(1, 2, 3)");
160 foldSame("x = 'a'.substring(0, 2)");
161 }
162
163 public void testFoldStringCharAt() {
164 fold("x = 'abcde'.charAt(0)", "x = 'a'");
165 fold("x = 'abcde'.charAt(1)", "x = 'b'");
166 fold("x = 'abcde'.charAt(2)", "x = 'c'");
167 fold("x = 'abcde'.charAt(3)", "x = 'd'");
168 fold("x = 'abcde'.charAt(4)", "x = 'e'");
169 foldSame("x = 'abcde'.charAt(5)"); // or x = ''
170 foldSame("x = 'abcde'.charAt(-1)"); // or x = ''
171 foldSame("x = 'abcde'.charAt(y)");
172 foldSame("x = 'abcde'.charAt()"); // or x = 'a'
173 foldSame("x = 'abcde'.charAt(0, ++z)"); // or (++z, 'a')
174 foldSame("x = 'abcde'.charAt(null)"); // or x = 'a'
175 foldSame("x = 'abcde'.charAt(true)"); // or x = 'b'
176 fold("x = '\\ud834\udd1e'.charAt(0)", "x = '\\ud834'");
177 fold("x = '\\ud834\udd1e'.charAt(1)", "x = '\\udd1e'");
178 }
179
180 public void testFoldStringCharCodeAt() {
181 fold("x = 'abcde'.charCodeAt(0)", "x = 97");
182 fold("x = 'abcde'.charCodeAt(1)", "x = 98");
183 fold("x = 'abcde'.charCodeAt(2)", "x = 99");
184 fold("x = 'abcde'.charCodeAt(3)", "x = 100");
185 fold("x = 'abcde'.charCodeAt(4)", "x = 101");
186 foldSame("x = 'abcde'.charCodeAt(5)"); // or x = (0/0)
187 foldSame("x = 'abcde'.charCodeAt(-1)"); // or x = (0/0)
188 foldSame("x = 'abcde'.charCodeAt(y)");
189 foldSame("x = 'abcde'.charCodeAt()"); // or x = 97
190 foldSame("x = 'abcde'.charCodeAt(0, ++z)"); // or (++z, 97)
191 foldSame("x = 'abcde'.charCodeAt(null)"); // or x = 97
192 foldSame("x = 'abcde'.charCodeAt(true)"); // or x = 98
193 fold("x = '\\ud834\udd1e'.charCodeAt(0)", "x = 55348");
194 fold("x = '\\ud834\udd1e'.charCodeAt(1)", "x = 56606");
195 }
196
197 public void testFoldStringSplit() {
198 late = false;
199 fold("x = 'abcde'.split('foo')", "x = ['abcde']");
200 fold("x = 'abcde'.split()", "x = ['abcde']");
201 fold("x = 'abcde'.split(null)", "x = ['abcde']");
202 fold("x = 'a b c d e'.split(' ')", "x = ['a','b','c','d','e']");
203 fold("x = 'a b c d e'.split(' ', 0)", "x = []");
204 fold("x = 'abcde'.split('cd')", "x = ['ab','e']");
205 fold("x = 'a b c d e'.split(' ', 1)", "x = ['a']");
206 fold("x = 'a b c d e'.split(' ', 3)", "x = ['a','b','c']");
207 fold("x = 'a b c d e'.split(null, 1)", "x = ['a b c d e']");
208 fold("x = 'aaaaa'.split('a')", "x = ['', '', '', '', '', '']");
209 fold("x = 'xyx'.split('x')", "x = ['', 'y', '']");
210
211 // Empty separator
212 fold("x = 'abcde'.split('')", "x = ['a','b','c','d','e']");
213 fold("x = 'abcde'.split('', 3)", "x = ['a','b','c']");
214
215 // Empty separator AND empty string
216 fold("x = ''.split('')", "x = []");
217
218 // Separator equals string
219 fold("x = 'aaa'.split('aaa')", "x = ['','']");
220 fold("x = ' '.split(' ')", "x = ['','']");
221
222 foldSame("x = 'abcde'.split(/ /)");
223 foldSame("x = 'abcde'.split(' ', -1)");
224
225 late = true;
226 foldSame("x = 'a b c d e'.split(' ')");
227 }
228
229 public void testJoinBug() {
230 fold("var x = [].join();", "var x = '';");
231 fold("var x = [x].join();", "var x = '' + x;");
232 foldSame("var x = [x,y].join();");
233 foldSame("var x = [x,y,z].join();");
234
235 foldSame("shape['matrix'] = [\n" +
236 " Number(headingCos2).toFixed(4),\n" +
237 " Number(-headingSin2).toFixed(4),\n" +
238 " Number(headingSin2 * yScale).toFixed(4),\n" +
239 " Number(headingCos2 * yScale).toFixed(4),\n" +
240 " 0,\n" +
241 " 0\n" +
242 " ].join()");
243 }
244
245 public void testToUpper() {
246 fold("'a'.toUpperCase()", "'A'");
247 fold("'A'.toUpperCase()", "'A'");
248 fold("'aBcDe'.toUpperCase()", "'ABCDE'");
249 }
250
251 public void testToLower() {
252 fold("'A'.toLowerCase()", "'a'");
253 fold("'a'.toLowerCase()", "'a'");
254 fold("'aBcDe'.toLowerCase()", "'abcde'");
255 }
256
257 public void testFoldParseNumbers() {
258 enableNormalize();
259 enableEcmaScript5(true);
260
261 fold("x = parseInt('123')", "x = 123");
262 fold("x = parseInt(' 123')", "x = 123");
263 fold("x = parseInt('123', 10)", "x = 123");
264 fold("x = parseInt('0xA')", "x = 10");
265 fold("x = parseInt('0xA', 16)", "x = 10");
266 fold("x = parseInt('07', 8)", "x = 7");
267 fold("x = parseInt('08')", "x = 8");
268 fold("x = parseInt('0')", "x = 0");
269 fold("x = parseFloat('0')", "x = 0");
270 fold("x = parseFloat('1.23')", "x = 1.23");
271 fold("x = parseFloat('1.2300')", "x = 1.23");
272 fold("x = parseFloat(' 0.3333')", "x = 0.3333");
273 fold("x = parseFloat('0100')", "x = 100");
274 fold("x = parseFloat('0100.000')", "x = 100");
275
276 //Mozilla Dev Center test cases
277 fold("x = parseInt(' 0xF', 16)", "x = 15");
278 fold("x = parseInt(' F', 16)", "x = 15");
279 fold("x = parseInt('17', 8)", "x = 15");
280 fold("x = parseInt('015', 10)", "x = 15");
281 fold("x = parseInt('1111', 2)", "x = 15");
282 fold("x = parseInt('12', 13)", "x = 15");
283 fold("x = parseInt(021, 8)", "x = 15");
284 fold("x = parseInt(15.99, 10)", "x = 15");
285 fold("x = parseFloat('3.14')", "x = 3.14");
286 fold("x = parseFloat(3.14)", "x = 3.14");
287
288 //Valid calls - unable to fold
289 foldSame("x = parseInt('FXX123', 16)");
290 foldSame("x = parseInt('15*3', 10)");
291 foldSame("x = parseInt('15e2', 10)");
292 foldSame("x = parseInt('15px', 10)");
293 foldSame("x = parseInt('-0x08')");
294 foldSame("x = parseInt('1', -1)");
295 foldSame("x = parseFloat('3.14more non-digit characters')");
296 foldSame("x = parseFloat('314e-2')");
297 foldSame("x = parseFloat('0.0314E+2')");
298 foldSame("x = parseFloat('3.333333333333333333333333')");
299
300 //Invalid calls
301 foldSame("x = parseInt('0xa', 10)");
302 foldSame("x = parseInt('')");
303
304 enableEcmaScript5(false);
305 foldSame("x = parseInt('08')");
306 }
307
308 @Override
309 protected int getNumRepetitions() {
310 // Reduce this to 2 if we get better expression evaluators.
311 return 2;
312 }
313
314 private void foldSame(String js) {
315 testSame(js);
316 }
317
318 private void fold(String js, String expected) {
319 test(js, expected);
320 }
321 }