blob: 5e5f50c1827fb93c16d7e06ac77ffe28a6f60019 [file] [log] [blame]
// Copyright (c) 2021, 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.ifs;
import static com.android.tools.r8.utils.codeinspector.CodeMatchers.invokesMethodWithName;
import static com.android.tools.r8.utils.codeinspector.Matchers.isAbsent;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assume.assumeTrue;
import com.android.tools.r8.NeverInline;
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 TrivialObjectEqualsTest extends TestBase {
private final TestParameters parameters;
@Parameterized.Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters().withAllRuntimesAndApiLevels().build();
}
public TrivialObjectEqualsTest(TestParameters parameters) {
this.parameters = parameters;
}
@Test
public void testD8() throws Exception {
assumeTrue(parameters.isDexRuntime());
testForD8(parameters.getBackend())
.addInnerClasses(getClass())
.setMinApi(parameters.getApiLevel())
.compile()
.inspect(
inspector ->
assertThat(
inspector.clazz(Main.class).uniqueMethodWithName("dead"),
not(invokesMethodWithName("dead"))))
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutputLines("Hello world!");
}
@Test
public void testR8() throws Exception {
testForR8(parameters.getBackend())
.addInnerClasses(getClass())
.addKeepMainRule(Main.class)
.enableInliningAnnotations()
.setMinApi(parameters.getApiLevel())
.compile()
.inspect(
inspector ->
assertThat(inspector.clazz(Main.class).uniqueMethodWithName("dead"), isAbsent()))
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutputLines("Hello world!");
}
static class Main {
public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();
if (o1 == o1) {
System.out.print("Hello");
} else {
dead();
}
if (o1 == o2) {
dead();
} else {
System.out.println(" world!");
}
}
@NeverInline
static void dead() {
System.out.println("Unexpected!");
}
}
}