Clément Béra | 7c503c0 | 2024-12-03 10:54:54 +0100 | [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 | |
| 5 | package backport; |
| 6 | |
| 7 | import com.android.tools.r8.TestParameters; |
| 8 | import com.android.tools.r8.TestRuntime.CfVm; |
| 9 | import com.android.tools.r8.desugar.backports.AbstractBackportTest; |
| 10 | import com.android.tools.r8.utils.AndroidApiLevel; |
| 11 | import java.util.Arrays; |
| 12 | import java.util.List; |
| 13 | import org.junit.runner.RunWith; |
| 14 | import org.junit.runners.Parameterized; |
| 15 | import org.junit.runners.Parameterized.Parameters; |
| 16 | |
| 17 | @RunWith(Parameterized.class) |
| 18 | public class ListBackportJava10Test extends AbstractBackportTest { |
| 19 | @Parameters(name = "{0}") |
| 20 | public static Iterable<?> data() { |
| 21 | return getTestParameters() |
| 22 | .withCfRuntimesStartingFromIncluding(CfVm.JDK10) |
| 23 | .withDexRuntimes() |
| 24 | .withAllApiLevelsAlsoForCf() |
| 25 | .build(); |
| 26 | } |
| 27 | |
| 28 | public ListBackportJava10Test(TestParameters parameters) { |
| 29 | super(parameters, List.class, ListBackportJava10Main.class); |
| 30 | // Note: None of the methods in this test exist in the latest android.jar. If/when they ship in |
| 31 | // an actual API level, migrate these tests to ListBackportTest. |
| 32 | |
| 33 | // Available since API 1 and used to test created lists. |
| 34 | ignoreInvokes("add"); |
| 35 | ignoreInvokes("get"); |
| 36 | ignoreInvokes("set"); |
| 37 | ignoreInvokes("size"); |
| 38 | |
| 39 | // List.copyOf added in API 31. |
| 40 | registerTarget(AndroidApiLevel.S, 3); |
| 41 | } |
| 42 | |
| 43 | public static class ListBackportJava10Main { |
| 44 | |
| 45 | public static void main(String[] args) { |
| 46 | testCopyOf(); |
| 47 | } |
| 48 | |
| 49 | private static void testCopyOf() { |
| 50 | Object anObject0 = new Object(); |
| 51 | Object anObject1 = new Object(); |
| 52 | List<Object> original = Arrays.asList(anObject0, anObject1); |
| 53 | List<Object> copy = List.copyOf(original); |
| 54 | assertEquals(2, copy.size()); |
| 55 | assertEquals(original, copy); |
| 56 | assertSame(anObject0, copy.get(0)); |
| 57 | assertSame(anObject1, copy.get(1)); |
| 58 | assertMutationNotAllowed(copy); |
| 59 | |
| 60 | // Mutate the original backing collection and ensure it's not reflected in copy. |
| 61 | original.set(0, new Object()); |
| 62 | assertSame(anObject0, copy.get(0)); |
| 63 | |
| 64 | try { |
| 65 | List.copyOf(null); |
| 66 | throw new AssertionError(); |
| 67 | } catch (NullPointerException expected) { |
| 68 | } |
| 69 | try { |
| 70 | List.copyOf(Arrays.asList(1, null, 2)); |
| 71 | throw new AssertionError(); |
| 72 | } catch (NullPointerException expected) { |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private static void assertMutationNotAllowed(List<Object> ofObject) { |
| 77 | try { |
| 78 | ofObject.add(new Object()); |
| 79 | throw new AssertionError(); |
| 80 | } catch (UnsupportedOperationException expected) { |
| 81 | } |
| 82 | try { |
| 83 | ofObject.set(0, new Object()); |
| 84 | throw new AssertionError(); |
| 85 | } catch (UnsupportedOperationException expected) { |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private static void assertSame(Object expected, Object actual) { |
| 90 | if (expected != actual) { |
| 91 | throw new AssertionError("Expected <" + expected + "> but was <" + actual + ">"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private static void assertEquals(Object expected, Object actual) { |
| 96 | if (expected != actual && !expected.equals(actual)) { |
| 97 | throw new AssertionError("Expected <" + expected + "> but was <" + actual + ">"); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |