Introduce ThrowBlockOutlinerTestBase
Change-Id: Ic9cc7c164ee4abb37f991abf3f7332a8b045bce2
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerArrayUseTypeTest.java b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerArrayUseTypeTest.java
index d946ca0..b5ee677 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerArrayUseTypeTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerArrayUseTypeTest.java
@@ -3,63 +3,38 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.ir.optimize.outliner.exceptions;
-import static com.google.common.base.Predicates.alwaysTrue;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import com.android.tools.r8.TestBase;
-import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.TestParametersCollection;
-import com.android.tools.r8.utils.BooleanBox;
+import com.android.tools.r8.graph.DexItemFactory;
import java.util.Collection;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-import org.junit.runners.Parameterized.Parameters;
-@RunWith(Parameterized.class)
-public class ThrowBlockOutlinerArrayUseTypeTest extends TestBase {
-
- @Parameter(0)
- public TestParameters parameters;
-
- @Parameters(name = "{0}")
- public static TestParametersCollection data() {
- return getTestParameters().withDexRuntimesAndAllApiLevels().build();
- }
+public class ThrowBlockOutlinerArrayUseTypeTest extends ThrowBlockOutlinerTestBase {
@Test
public void test() throws Exception {
- BooleanBox receivedCallback = new BooleanBox();
testForD8(parameters)
.addInnerClasses(getClass())
- .addOptionsModification(
- options -> {
- assertFalse(options.getThrowBlockOutlinerOptions().enable);
- options.getThrowBlockOutlinerOptions().enable = true;
- options.getThrowBlockOutlinerOptions().outlineConsumerForTesting =
- outlines -> {
- inspectOutlines(outlines);
- receivedCallback.set();
- };
- options.getThrowBlockOutlinerOptions().outlineStrategyForTesting = alwaysTrue();
- })
+ .addOptionsModification(this::configure)
.release()
.compile()
.run(parameters.getRuntime(), Main.class)
.assertFailureWithErrorThatThrows(MyException.class);
- assertTrue(receivedCallback.isTrue());
}
- private void inspectOutlines(Collection<ThrowBlockOutline> outlines) {
+ @Override
+ public void inspectOutlines(Collection<ThrowBlockOutline> outlines, DexItemFactory factory) {
assertEquals(1, outlines.size());
ThrowBlockOutline outline = outlines.iterator().next();
assertEquals(1, outline.getProto().getParameters().size());
assertEquals(Main[].class.getTypeName(), outline.getProto().getParameter(0).getTypeName());
}
+ @Override
+ public boolean shouldOutline(ThrowBlockOutline outline) {
+ return true;
+ }
+
static class Main {
public static void main(String[] args) {
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerConstArgumentTest.java b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerConstArgumentTest.java
index b60d7e8..a01c432 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerConstArgumentTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerConstArgumentTest.java
@@ -8,15 +8,11 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestCompileResult;
-import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.synthesis.SyntheticItemsTestUtils;
-import com.android.tools.r8.utils.BooleanBox;
import com.android.tools.r8.utils.codeinspector.ClassSubject;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
import com.android.tools.r8.utils.codeinspector.MethodSubject;
@@ -24,45 +20,18 @@
import it.unimi.dsi.fastutil.ints.IntSet;
import java.util.Collection;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-import org.junit.runners.Parameterized.Parameters;
-@RunWith(Parameterized.class)
-public class ThrowBlockOutlinerConstArgumentTest extends TestBase {
-
- @Parameter(0)
- public TestParameters parameters;
-
- @Parameters(name = "{0}")
- public static TestParametersCollection data() {
- return getTestParameters().withDexRuntimesAndAllApiLevels().build();
- }
+public class ThrowBlockOutlinerConstArgumentTest extends ThrowBlockOutlinerTestBase {
@Test
public void test() throws Exception {
- BooleanBox receivedCallback = new BooleanBox();
TestCompileResult<?, ?> compileResult =
testForD8(parameters)
.addInnerClasses(getClass())
- .addOptionsModification(
- options -> {
- assertFalse(options.getThrowBlockOutlinerOptions().enable);
- options.getThrowBlockOutlinerOptions().enable = true;
- options.getThrowBlockOutlinerOptions().outlineConsumerForTesting =
- outlines -> {
- inspectOutlines(outlines);
- receivedCallback.set();
- };
- options.getThrowBlockOutlinerOptions().outlineStrategyForTesting =
- outline -> outline.getNumberOfUsers() >= 2;
- })
+ .addOptionsModification(this::configure)
.release()
.compile()
.inspect(this::inspectOutput);
- assertTrue(receivedCallback.isTrue());
-
for (int i = 0; i < 3; i++) {
compileResult
.run(parameters.getRuntime(), Main.class, Integer.toString(i))
@@ -77,7 +46,8 @@
.assertSuccessWithEmptyOutput();
}
- private void inspectOutlines(Collection<ThrowBlockOutline> outlines) {
+ @Override
+ public void inspectOutlines(Collection<ThrowBlockOutline> outlines, DexItemFactory factory) {
// Verify that we have two outlines with one and three users, respectively.
assertEquals(2, outlines.size());
IntSet numberOfUsers = new IntArraySet();
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerNoArgumentsTest.java b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerNoArgumentsTest.java
index 824130d..9cb2f4a 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerNoArgumentsTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerNoArgumentsTest.java
@@ -7,14 +7,11 @@
import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestCompileResult;
-import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.synthesis.SyntheticItemsTestUtils;
-import com.android.tools.r8.utils.BooleanBox;
import com.android.tools.r8.utils.BooleanUtils;
import com.android.tools.r8.utils.codeinspector.ClassSubject;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
@@ -24,50 +21,33 @@
import java.util.Collection;
import java.util.List;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
-@RunWith(Parameterized.class)
-public class ThrowBlockOutlinerNoArgumentsTest extends TestBase {
-
- @Parameter(0)
- public boolean minimizeSyntheticNames;
+public class ThrowBlockOutlinerNoArgumentsTest extends ThrowBlockOutlinerTestBase {
@Parameter(1)
- public TestParameters parameters;
+ public boolean minimizeSyntheticNames;
- @Parameters(name = "{1}, minimizeSyntheticNames: {0}")
- public static List<Object[]> data() {
+ @Parameters(name = "{0}, minimizeSyntheticNames: {1}")
+ public static List<Object[]> extraData() {
return buildParameters(
- BooleanUtils.values(), getTestParameters().withDexRuntimesAndAllApiLevels().build());
+ getTestParameters().withDexRuntimesAndAllApiLevels().build(), BooleanUtils.values());
}
@Test
public void test() throws Exception {
- BooleanBox receivedCallback = new BooleanBox();
TestCompileResult<?, ?> compileResult =
testForD8(parameters)
.addInnerClasses(getClass())
+ .addOptionsModification(this::configure)
.addOptionsModification(
- options -> {
- options.desugarSpecificOptions().minimizeSyntheticNames = minimizeSyntheticNames;
- assertFalse(options.getThrowBlockOutlinerOptions().enable);
- options.getThrowBlockOutlinerOptions().enable = true;
- options.getThrowBlockOutlinerOptions().outlineConsumerForTesting =
- outlines -> {
- inspectOutlines(outlines);
- receivedCallback.set();
- };
- options.getThrowBlockOutlinerOptions().outlineStrategyForTesting =
- outline -> outline.getNumberOfUsers() >= 2;
- })
+ options ->
+ options.desugarSpecificOptions().minimizeSyntheticNames =
+ minimizeSyntheticNames)
.release()
.compile()
.inspect(this::inspectOutput);
- assertTrue(receivedCallback.isTrue());
-
for (int i = 0; i < 3; i++) {
compileResult
.run(parameters.getRuntime(), Main.class, Integer.toString(i))
@@ -81,7 +61,8 @@
.assertSuccessWithEmptyOutput();
}
- private void inspectOutlines(Collection<ThrowBlockOutline> outlines) {
+ @Override
+ public void inspectOutlines(Collection<ThrowBlockOutline> outlines, DexItemFactory factory) {
// Verify that we have two outlines with one and three users, respectively.
assertEquals(2, outlines.size());
IntSet numberOfUsers = new IntArraySet();
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerSharedStringBuilderTest.java b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerSharedStringBuilderTest.java
index f8a93c3..dfe625c 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerSharedStringBuilderTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerSharedStringBuilderTest.java
@@ -8,17 +8,12 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestCompileResult;
-import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.TestParametersCollection;
import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.ir.analysis.value.AbstractValue;
import com.android.tools.r8.synthesis.SyntheticItemsTestUtils;
-import com.android.tools.r8.utils.BooleanBox;
import com.android.tools.r8.utils.ListUtils;
import com.android.tools.r8.utils.codeinspector.ClassSubject;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
@@ -26,45 +21,18 @@
import com.google.common.collect.Lists;
import java.util.Collection;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-import org.junit.runners.Parameterized.Parameters;
-@RunWith(Parameterized.class)
-public class ThrowBlockOutlinerSharedStringBuilderTest extends TestBase {
-
- @Parameter(0)
- public TestParameters parameters;
-
- @Parameters(name = "{0}")
- public static TestParametersCollection data() {
- return getTestParameters().withDexRuntimesAndAllApiLevels().build();
- }
+public class ThrowBlockOutlinerSharedStringBuilderTest extends ThrowBlockOutlinerTestBase {
@Test
public void test() throws Exception {
- BooleanBox receivedCallback = new BooleanBox();
TestCompileResult<?, ?> compileResult =
testForD8(parameters)
.addInnerClasses(getClass())
- .addOptionsModification(
- options -> {
- assertFalse(options.getThrowBlockOutlinerOptions().enable);
- options.getThrowBlockOutlinerOptions().enable = true;
- options.getThrowBlockOutlinerOptions().outlineConsumerForTesting =
- outlines -> {
- inspectOutlines(outlines, options.dexItemFactory());
- receivedCallback.set();
- };
- options.getThrowBlockOutlinerOptions().outlineStrategyForTesting =
- outline -> outline.getNumberOfUsers() >= 2;
- })
+ .addOptionsModification(this::configure)
.release()
.compile()
.inspect(this::inspectOutput);
- assertTrue(receivedCallback.isTrue());
-
compileResult
.run(parameters.getRuntime(), Main.class, "0", "1")
.assertFailureWithErrorThatThrows(IllegalArgumentException.class)
@@ -75,7 +43,8 @@
.assertFailureWithErrorThatMatches(containsString("j=0, i=1"));
}
- private void inspectOutlines(Collection<ThrowBlockOutline> outlines, DexItemFactory factory) {
+ @Override
+ public void inspectOutlines(Collection<ThrowBlockOutline> outlines, DexItemFactory factory) {
// Verify that we have a single outline with two users.
assertEquals(1, outlines.size());
ThrowBlockOutline outline = outlines.iterator().next();
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerTestBase.java b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerTestBase.java
new file mode 100644
index 0000000..314a7ef
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerTestBase.java
@@ -0,0 +1,63 @@
+// Copyright (c) 2025, 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.outliner.exceptions;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.graph.DexItemFactory;
+import com.android.tools.r8.utils.BooleanBox;
+import com.android.tools.r8.utils.InternalOptions;
+import java.util.Collection;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public abstract class ThrowBlockOutlinerTestBase extends TestBase {
+
+ @Parameter(0)
+ public TestParameters parameters;
+
+ private final BooleanBox receivedCallback = new BooleanBox();
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withDexRuntimesAndAllApiLevels().build();
+ }
+
+ @Before
+ public void before() {
+ receivedCallback.unset();
+ }
+
+ @After
+ public void after() {
+ assertTrue(receivedCallback.isTrue());
+ }
+
+ public void configure(InternalOptions options) {
+ assertFalse(options.getThrowBlockOutlinerOptions().enable);
+ options.getThrowBlockOutlinerOptions().enable = true;
+ options.getThrowBlockOutlinerOptions().outlineConsumerForTesting =
+ outlines -> {
+ inspectOutlines(outlines, options.dexItemFactory());
+ receivedCallback.set();
+ };
+ options.getThrowBlockOutlinerOptions().outlineStrategyForTesting = this::shouldOutline;
+ }
+
+ public abstract void inspectOutlines(
+ Collection<ThrowBlockOutline> outlineCollection, DexItemFactory factory);
+
+ public boolean shouldOutline(ThrowBlockOutline outline) {
+ return outline.getNumberOfUsers() >= 2;
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerUseTypeTest.java b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerUseTypeTest.java
index 8c816dc..1d93270 100644
--- a/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerUseTypeTest.java
+++ b/src/test/java/com/android/tools/r8/ir/optimize/outliner/exceptions/ThrowBlockOutlinerUseTypeTest.java
@@ -3,63 +3,38 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.ir.optimize.outliner.exceptions;
-import static com.google.common.base.Predicates.alwaysTrue;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import com.android.tools.r8.TestBase;
-import com.android.tools.r8.TestParameters;
-import com.android.tools.r8.TestParametersCollection;
-import com.android.tools.r8.utils.BooleanBox;
+import com.android.tools.r8.graph.DexItemFactory;
import java.util.Collection;
import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-import org.junit.runners.Parameterized.Parameters;
-@RunWith(Parameterized.class)
-public class ThrowBlockOutlinerUseTypeTest extends TestBase {
-
- @Parameter(0)
- public TestParameters parameters;
-
- @Parameters(name = "{0}")
- public static TestParametersCollection data() {
- return getTestParameters().withDexRuntimesAndAllApiLevels().build();
- }
+public class ThrowBlockOutlinerUseTypeTest extends ThrowBlockOutlinerTestBase {
@Test
public void test() throws Exception {
- BooleanBox receivedCallback = new BooleanBox();
testForD8(parameters)
.addInnerClasses(getClass())
- .addOptionsModification(
- options -> {
- assertFalse(options.getThrowBlockOutlinerOptions().enable);
- options.getThrowBlockOutlinerOptions().enable = true;
- options.getThrowBlockOutlinerOptions().outlineConsumerForTesting =
- outlines -> {
- inspectOutlines(outlines);
- receivedCallback.set();
- };
- options.getThrowBlockOutlinerOptions().outlineStrategyForTesting = alwaysTrue();
- })
+ .addOptionsModification(this::configure)
.release()
.compile()
.run(parameters.getRuntime(), Main.class)
.assertFailureWithErrorThatThrows(MyException.class);
- assertTrue(receivedCallback.isTrue());
}
- private void inspectOutlines(Collection<ThrowBlockOutline> outlines) {
+ @Override
+ public void inspectOutlines(Collection<ThrowBlockOutline> outlines, DexItemFactory factory) {
assertEquals(1, outlines.size());
ThrowBlockOutline outline = outlines.iterator().next();
assertEquals(1, outline.getProto().getParameters().size());
assertEquals(Main.class.getTypeName(), outline.getProto().getParameter(0).getTypeName());
}
+ @Override
+ public boolean shouldOutline(ThrowBlockOutline outline) {
+ return true;
+ }
+
static class Main {
public static void main(String[] args) {