1919import com .google .gwt .dev .jjs .ast .JMethod ;
2020import com .google .gwt .dev .jjs .ast .JProgram ;
2121
22+ import java .util .Locale ;
23+
2224/**
2325 * Tests {@link DeadCodeElimination}.
2426 */
@@ -255,8 +257,22 @@ public void testIfStatementToBoolean_ThenOptimization() throws Exception {
255257 "EntryPoint.b && (EntryPoint.i = 1);" );
256258 }
257259
260+ public void testForOptimizations () {
261+ // We need a helper to inline the false from, so JDT doesn't alert us to unreachable code and fail
262+ runMethodInliner = true ;
263+ addSnippetClassDecl (
264+ "static class B { "
265+ + "static boolean isFalse() { return false; } "
266+ + "}" );
267+
268+ optimize ("void" , "for (i = 1; B.isFalse(); i++) { i = 2; }" )
269+ .intoString ("EntryPoint.i = 1;" );
270+ optimize ("void" , "for (i = 1; i < 5; i++) { i = 2; }" )
271+ .intoString ("for (EntryPoint.i = 1; EntryPoint.i < 5; EntryPoint.i++) {\n EntryPoint.i = 2;\n }" );
272+ }
273+
258274 /**
259- * BUG: JInstance was marked as not having side effects whereas it all depends on the
275+ * BUG: JInstanceOf was marked as not having side effects whereas it all depends on
260276 * whether the expression on the left has side effects.
261277 *
262278 * Reproduces Issue:7818.
@@ -309,7 +325,7 @@ public void testStringOptimizations() throws Exception {
309325 " final static String s4 = null;" ,
310326 "}" );
311327
312- // TODO(rluble): This test is not 100% meaninful as the JDT performs some optimizations for us.
328+ // TODO(rluble): This test is not 100% meaningful as the JDT performs some optimizations for us.
313329 optimizeExpressions (false , "boolean" , "\" a\" .equals(\" a\" )" )
314330 .into ("return true;" );
315331 optimizeExpressions (false , "boolean" , "\" a\" == \" a\" " )
@@ -334,11 +350,10 @@ public void testStringOptimizations() throws Exception {
334350 .into ("return true;" );
335351 optimizeExpressions (false , "boolean" , "A.s1.equals(A.s2)" )
336352 .into ("return true;" );
337- // Next two are not directly optimizable because of inserted clinits.
338- // optimizeExpressions(false, "boolean", "\"a\" != A.s4")
339- // .into("return true;");
340- // optimizeExpressions(false, "boolean", "A.s4 == null")
341- // .into("return true;");
353+ optimizeExpressions (false , "boolean" , "\" a\" != A.s4" )
354+ .intoString ("return (EntryPoint$A.$clinit(), true);" );
355+ optimizeExpressions (false , "boolean" , "A.s4 == null" )
356+ .intoString ("return (EntryPoint$A.$clinit(), true);" );
342357 }
343358
344359 public void testStringOptimizations_withSpecializer () throws Exception {
@@ -419,7 +434,7 @@ public void testMultiExpressionOptimization() throws Exception {
419434
420435 public void testOptimizeStringCalls () throws Exception {
421436 // Note: we're limited here by the methods declared in the mock String in
422- // JJSTestBase#addBuiltinClasses
437+ // JavaResourceBase#getStandardResources().
423438
424439 // String.length
425440 optimize ("int" , "return \" abc\" .length();" ).intoString ("return 3;" );
@@ -434,8 +449,8 @@ public void testOptimizeStringCalls() throws Exception {
434449 optimize ("String" , "return s.toString();" ).intoString ("return EntryPoint.s.toString();" );
435450 optimize ("String" , "return o.toString();" ).intoString ("return EntryPoint.o.toString();" );
436451
437- // String.hashCode: never optimized
438- optimize ("int" , "return \" abc\" .hashCode();" ).intoString ("return \" abc \" .hashCode() ;" );
452+ // String.hashCode
453+ optimize ("int" , "return \" abc\" .hashCode();" ).intoString ("return 96354 ;" );
439454 optimize ("int" , "return s.hashCode();" ).intoString ("return EntryPoint.s.hashCode();" );
440455
441456 // String.equals
@@ -464,9 +479,9 @@ public void testSubtractFromZero() throws Exception {
464479
465480 public void testFloatingPoint () throws Exception {
466481 // Internally we represent float literals as double, so here we make sure that 1.1f is
467- // is printed as a double with the right precision.
468- optimize ("float" , "return 1.1f;" ).intoString ("return " + String . format ( "%.16g" , ( double ) 1.1f ) +
469- ";" );
482+ // printed as a double with the right precision.
483+ optimize ("float" , "return 1.1f;" ).intoString ("return "
484+ + String . format ( Locale . ROOT , "%.16g" , ( double ) 1.1f ) + ";" );
470485 optimize ("boolean" , "return 2d > 1;" ).intoString ("return true;" );
471486 optimize ("boolean" , "return 1 < 2d;" ).intoString ("return true;" );
472487 }
0 commit comments