Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 1 | // 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éra | 7c503c0 | 2024-12-03 10:54:54 +0100 | [diff] [blame] | 5 | package backport; |
Søren Gjesse | c0a7afb | 2022-02-09 09:16:41 +0100 | [diff] [blame] | 6 | |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 7 | import com.android.tools.r8.TestParameters; |
| 8 | import com.android.tools.r8.TestRuntime.CfVm; |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 9 | import com.android.tools.r8.ToolHelper.DexVm.Version; |
Clément Béra | 7c503c0 | 2024-12-03 10:54:54 +0100 | [diff] [blame] | 10 | import com.android.tools.r8.desugar.backports.AbstractBackportTest; |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 11 | import com.android.tools.r8.utils.AndroidApiLevel; |
Clément Béra | 7c503c0 | 2024-12-03 10:54:54 +0100 | [diff] [blame] | 12 | import java.util.NoSuchElementException; |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 13 | import java.util.OptionalInt; |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 14 | import org.junit.runner.RunWith; |
| 15 | import org.junit.runners.Parameterized; |
| 16 | import org.junit.runners.Parameterized.Parameters; |
| 17 | |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 18 | @RunWith(Parameterized.class) |
| 19 | public 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 Gjesse | cd3636c | 2023-04-13 10:47:17 +0200 | [diff] [blame] | 26 | .enableApiLevelsForCf() |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 27 | .build(); |
| 28 | } |
| 29 | |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 30 | public OptionalIntBackportJava10Test(TestParameters parameters) { |
Clément Béra | 7c503c0 | 2024-12-03 10:54:54 +0100 | [diff] [blame] | 31 | super(parameters, OptionalInt.class, OptionalIntBackportJava10Main.class); |
Søren Gjesse | c0a7afb | 2022-02-09 09:16:41 +0100 | [diff] [blame] | 32 | // 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 Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 34 | |
Søren Gjesse | c0a7afb | 2022-02-09 09:16:41 +0100 | [diff] [blame] | 35 | // Available since N. |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 36 | ignoreInvokes("empty"); |
| 37 | ignoreInvokes("getAsInt"); |
| 38 | ignoreInvokes("of"); |
Søren Gjesse | c0a7afb | 2022-02-09 09:16:41 +0100 | [diff] [blame] | 39 | |
| 40 | registerTarget(AndroidApiLevel.T, 2); |
Jake Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 41 | } |
Clément Béra | 7c503c0 | 2024-12-03 10:54:54 +0100 | [diff] [blame] | 42 | |
| 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 Wharton | 3d656f7 | 2019-12-12 23:11:15 -0500 | [diff] [blame] | 66 | } |