blob: 1e2f17a3a4eb03a31b95c8f8b95bf71c2e3cd855 [file] [log] [blame]
Jake Wharton3d656f72019-12-12 23:11:15 -05001// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
Clément Béra7c503c02024-12-03 10:54:54 +01005package backport;
Søren Gjessec0a7afb2022-02-09 09:16:41 +01006
Jake Wharton3d656f72019-12-12 23:11:15 -05007import com.android.tools.r8.TestParameters;
8import com.android.tools.r8.TestRuntime.CfVm;
Jake Wharton3d656f72019-12-12 23:11:15 -05009import com.android.tools.r8.ToolHelper.DexVm.Version;
Clément Béra7c503c02024-12-03 10:54:54 +010010import com.android.tools.r8.desugar.backports.AbstractBackportTest;
Jake Wharton3d656f72019-12-12 23:11:15 -050011import com.android.tools.r8.utils.AndroidApiLevel;
Clément Béra7c503c02024-12-03 10:54:54 +010012import java.util.NoSuchElementException;
Jake Wharton3d656f72019-12-12 23:11:15 -050013import java.util.OptionalInt;
Jake Wharton3d656f72019-12-12 23:11:15 -050014import org.junit.runner.RunWith;
15import org.junit.runners.Parameterized;
16import org.junit.runners.Parameterized.Parameters;
17
Jake Wharton3d656f72019-12-12 23:11:15 -050018@RunWith(Parameterized.class)
19public final class OptionalIntBackportJava10Test extends AbstractBackportTest {
20 @Parameters(name = "{0}")
21 public static Iterable<?> data() {
22 return getTestParameters()
23 .withDexRuntimesStartingFromIncluding(Version.V7_0_0)
24 .withApiLevelsStartingAtIncluding(AndroidApiLevel.N)
25 .withCfRuntimesStartingFromIncluding(CfVm.JDK10)
Søren Gjessecd3636c2023-04-13 10:47:17 +020026 .enableApiLevelsForCf()
Jake Wharton3d656f72019-12-12 23:11:15 -050027 .build();
28 }
29
Jake Wharton3d656f72019-12-12 23:11:15 -050030 public OptionalIntBackportJava10Test(TestParameters parameters) {
Clément Béra7c503c02024-12-03 10:54:54 +010031 super(parameters, OptionalInt.class, OptionalIntBackportJava10Main.class);
Søren Gjessec0a7afb2022-02-09 09:16:41 +010032 // Note: The methods in this test exist in android.jar from Android T. When R8 builds targeting
33 // Java 11 move these tests to OptionalBackportTest (out of examplesJava10).
Jake Wharton3d656f72019-12-12 23:11:15 -050034
Søren Gjessec0a7afb2022-02-09 09:16:41 +010035 // Available since N.
Jake Wharton3d656f72019-12-12 23:11:15 -050036 ignoreInvokes("empty");
37 ignoreInvokes("getAsInt");
38 ignoreInvokes("of");
Søren Gjessec0a7afb2022-02-09 09:16:41 +010039
40 registerTarget(AndroidApiLevel.T, 2);
Jake Wharton3d656f72019-12-12 23:11:15 -050041 }
Clément Béra7c503c02024-12-03 10:54:54 +010042
43 public static class OptionalIntBackportJava10Main {
44
45 public static void main(String[] args) {
46 testOrElseThrow();
47 }
48
49 private static void testOrElseThrow() {
50 OptionalInt present = OptionalInt.of(2);
51 assertEquals(2, present.orElseThrow());
52
53 OptionalInt absent = OptionalInt.empty();
54 try {
55 throw new AssertionError(absent.orElseThrow());
56 } catch (NoSuchElementException expected) {
57 }
58 }
59
60 private static void assertEquals(Object expected, Object actual) {
61 if (expected != actual && !expected.equals(actual)) {
62 throw new AssertionError("Expected <" + expected + "> but was <" + actual + ">");
63 }
64 }
65 }
Jake Wharton3d656f72019-12-12 23:11:15 -050066}