Reproduce StringBuilder optimization bug
Change-Id: I5323cce28908b15e615d0d75895dc3308fb149c7
Bug: 163230758
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/string/StringBuilderInDoWhileLoopTest.java b/src/test/java/com/android/tools/r8/ir/optimize/string/StringBuilderInDoWhileLoopTest.java
new file mode 100644
index 0000000..1d7bb20
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/string/StringBuilderInDoWhileLoopTest.java
@@ -0,0 +1,49 @@
+// Copyright (c) 2020, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.ir.optimize.string;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class StringBuilderInDoWhileLoopTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimesAndApiLevels().build();
+ }
+
+ public StringBuilderInDoWhileLoopTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(StringBuilderInDoWhileLoopTest.class)
+ .addKeepMainRule(TestClass.class)
+ .setMinApi(parameters.getApiLevel())
+ .run(parameters.getRuntime(), TestClass.class)
+ // TODO(b/163230758): Should succeed with "000".
+ .assertSuccessWithOutputLines(parameters.isCfRuntime() ? "000" : "0");
+ }
+
+ static class TestClass {
+ public static void main(String[] args) {
+ StringBuilder builder = new StringBuilder();
+ int i = 0;
+ do {
+ builder.append("0");
+ i++;
+ } while (i < 3);
+ System.out.println(builder.toString());
+ }
+ }
+}