Rico Wind | 5a3f774 | 2024-04-10 11:03:25 +0200 | [diff] [blame] | 1 | // Copyright (c) 2024, 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 | |
| 5 | package backport; |
| 6 | |
| 7 | import com.android.tools.r8.TestBase; |
| 8 | import com.android.tools.r8.TestParameters; |
| 9 | import com.android.tools.r8.TestRuntime.CfVm; |
| 10 | import java.util.Objects; |
| 11 | import org.junit.Test; |
| 12 | import org.junit.runner.RunWith; |
| 13 | import org.junit.runners.Parameterized; |
| 14 | import org.junit.runners.Parameterized.Parameter; |
| 15 | import org.junit.runners.Parameterized.Parameters; |
| 16 | |
| 17 | @RunWith(Parameterized.class) |
| 18 | public class ObjectsBackportJava17Test extends TestBase { |
| 19 | |
| 20 | @Parameter(0) |
| 21 | public TestParameters parameters; |
| 22 | |
| 23 | @Parameters(name = "{0}") |
| 24 | public static Iterable<?> data() { |
| 25 | return getTestParameters() |
| 26 | .withDexRuntimes() |
| 27 | .withCfRuntimesStartingFromIncluding(CfVm.JDK17) |
| 28 | .withAllApiLevelsAlsoForCf() |
| 29 | .build(); |
| 30 | } |
| 31 | |
| 32 | @Test |
| 33 | public void test() throws Exception { |
| 34 | testForDesugaring(parameters) |
| 35 | .addInnerClassesAndStrippedOuter(getClass()) |
| 36 | .run(parameters.getRuntime(), TestClass.class) |
| 37 | .assertSuccess(); |
| 38 | } |
| 39 | |
| 40 | public static class TestClass { |
| 41 | |
| 42 | public static void main(String[] args) { |
| 43 | // The methods are actually from Java 16, but we can test them from Java 17. |
| 44 | testCheckIndex(); |
| 45 | testCheckFromToIndex(); |
| 46 | testCheckFromIndexSize(); |
| 47 | } |
| 48 | |
| 49 | private static void testCheckIndex() { |
| 50 | for (long i = 0L; i < 10L; i++) { |
| 51 | assertEquals(i, Objects.checkIndex(i, 10L)); |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | throw new AssertionError(Objects.checkIndex(-1L, 10L)); |
| 56 | } catch (IndexOutOfBoundsException expected) { |
| 57 | } |
| 58 | try { |
| 59 | throw new AssertionError(Objects.checkIndex(10L, 0L)); |
| 60 | } catch (IndexOutOfBoundsException expected) { |
| 61 | } |
| 62 | try { |
| 63 | throw new AssertionError(Objects.checkIndex(0L, 0L)); |
| 64 | } catch (IndexOutOfBoundsException expected) { |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private static void testCheckFromToIndex() { |
| 69 | for (long i = 0L; i <= 10L; i++) { |
| 70 | for (long j = i; j <= 10L; j++) { |
| 71 | assertEquals(i, Objects.checkFromToIndex(i, j, 10L)); |
| 72 | } |
| 73 | } |
| 74 | assertEquals(0L, Objects.checkFromToIndex(0L, 0L, 0L)); |
| 75 | |
| 76 | try { |
| 77 | throw new AssertionError(Objects.checkFromToIndex(4L, 2L, 10L)); |
| 78 | } catch (IndexOutOfBoundsException expected) { |
| 79 | } |
| 80 | try { |
| 81 | throw new AssertionError(Objects.checkFromToIndex(-1L, 5L, 10L)); |
| 82 | } catch (IndexOutOfBoundsException expected) { |
| 83 | } |
| 84 | try { |
| 85 | throw new AssertionError(Objects.checkFromToIndex(0L, -1L, 10L)); |
| 86 | } catch (IndexOutOfBoundsException expected) { |
| 87 | } |
| 88 | try { |
| 89 | throw new AssertionError(Objects.checkFromToIndex(11L, 11L, 10L)); |
| 90 | } catch (IndexOutOfBoundsException expected) { |
| 91 | } |
| 92 | try { |
| 93 | throw new AssertionError(Objects.checkFromToIndex(0L, 1L, 0L)); |
| 94 | } catch (IndexOutOfBoundsException expected) { |
| 95 | } |
| 96 | try { |
| 97 | throw new AssertionError(Objects.checkFromToIndex(1L, 1L, 0L)); |
| 98 | } catch (IndexOutOfBoundsException expected) { |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private static void testCheckFromIndexSize() { |
| 103 | for (long i = 0L; i <= 10L; i++) { |
| 104 | for (long j = 10L - i; j >= 0L; j--) { |
| 105 | assertEquals(i, Objects.checkFromIndexSize(i, j, 10L)); |
| 106 | } |
| 107 | } |
| 108 | assertEquals(0, Objects.checkFromIndexSize(0L, 0L, 0L)); |
| 109 | |
| 110 | try { |
| 111 | throw new AssertionError(Objects.checkFromIndexSize(8L, 4L, 10L)); |
| 112 | } catch (IndexOutOfBoundsException expected) { |
| 113 | } |
| 114 | try { |
| 115 | throw new AssertionError(Objects.checkFromIndexSize(-1L, 5L, 10L)); |
| 116 | } catch (IndexOutOfBoundsException expected) { |
| 117 | } |
| 118 | try { |
| 119 | throw new AssertionError(Objects.checkFromIndexSize(11L, 0L, 10L)); |
| 120 | } catch (IndexOutOfBoundsException expected) { |
| 121 | } |
| 122 | try { |
| 123 | throw new AssertionError(Objects.checkFromIndexSize(0L, 1L, 0L)); |
| 124 | } catch (IndexOutOfBoundsException expected) { |
| 125 | } |
| 126 | try { |
| 127 | throw new AssertionError(Objects.checkFromIndexSize(1L, 1L, 0L)); |
| 128 | } catch (IndexOutOfBoundsException expected) { |
| 129 | } |
| 130 | |
| 131 | // Check for cases where overflow might occur producing incorrect results. |
| 132 | try { |
| 133 | throw new AssertionError(Objects.checkFromIndexSize(Long.MAX_VALUE, 1L, Long.MAX_VALUE)); |
| 134 | } catch (IndexOutOfBoundsException expected) { |
| 135 | } |
| 136 | try { |
| 137 | throw new AssertionError(Objects.checkFromIndexSize(0L, 1L, Long.MIN_VALUE)); |
| 138 | } catch (IndexOutOfBoundsException expected) { |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private static void assertEquals(long expected, long actual) { |
| 143 | if (expected != actual) { |
| 144 | throw new AssertionError("Expected <" + expected + "> but was <" + actual + '>'); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |