blob: 4c8ae88227c472971932687baf7b2c6b189e4b95 [file] [log] [blame]
clementbera5fcbfb22019-08-19 11:30:42 +02001// 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
5package backport;
6
7import java.util.List;
8
9public class ListBackportJava9Main {
10
Jake Wharton699e0ad2019-09-09 12:53:37 -040011 public static void main(String[] args) {
clementbera5fcbfb22019-08-19 11:30:42 +020012 testOf0();
13 testOf1();
14 testOf2();
15 testOf10();
Jake Wharton72fbd2f2019-09-09 13:31:56 -040016 testOfVarargs();
clementbera5fcbfb22019-08-19 11:30:42 +020017 }
18
19 private static void testOf0() {
20 List<Object> ofObject = List.of();
Jake Wharton699e0ad2019-09-09 12:53:37 -040021 assertEquals(0, ofObject.size());
22 assertMutationNotAllowed(ofObject);
23
clementbera5fcbfb22019-08-19 11:30:42 +020024 List<Integer> ofInteger = List.of();
Jake Wharton699e0ad2019-09-09 12:53:37 -040025 assertEquals(0, ofInteger.size());
26 assertMutationNotAllowed(ofObject);
clementbera5fcbfb22019-08-19 11:30:42 +020027 }
28
29 private static void testOf1() {
Jake Wharton699e0ad2019-09-09 12:53:37 -040030 Object anObject = new Object();
31 List<Object> ofObject = List.of(anObject);
32 assertEquals(1, ofObject.size());
33 assertSame(anObject, ofObject.get(0));
34 assertMutationNotAllowed(ofObject);
35
clementbera5fcbfb22019-08-19 11:30:42 +020036 List<Integer> ofInteger = List.of(1);
Jake Wharton699e0ad2019-09-09 12:53:37 -040037 assertEquals(1, ofInteger.size());
38 assertEquals(1, ofInteger.get(0));
39
40 try {
41 List.of((Object) null);
42 throw new AssertionError();
43 } catch (NullPointerException expected) {
44 }
clementbera5fcbfb22019-08-19 11:30:42 +020045 }
46
47 private static void testOf2() {
Jake Wharton699e0ad2019-09-09 12:53:37 -040048 Object anObject0 = new Object();
49 Object anObject1 = new Object();
50 List<Object> ofObject = List.of(anObject0, anObject1);
51 assertEquals(2, ofObject.size());
52 assertSame(anObject0, ofObject.get(0));
53 assertSame(anObject1, ofObject.get(1));
54 assertMutationNotAllowed(ofObject);
55
clementbera5fcbfb22019-08-19 11:30:42 +020056 List<Integer> ofInteger = List.of(1, 2);
Jake Wharton699e0ad2019-09-09 12:53:37 -040057 assertEquals(2, ofInteger.size());
58 assertEquals(1, ofInteger.get(0));
59 assertEquals(2, ofInteger.get(1));
60
61 List<Object> ofMixed = List.of(anObject0, 1);
62 assertEquals(2, ofMixed.size());
63 assertSame(anObject0, ofMixed.get(0));
64 assertEquals(1, ofMixed.get(1));
65 assertMutationNotAllowed(ofMixed);
66
67 try {
68 List.of(1, null);
69 throw new AssertionError();
70 } catch (NullPointerException expected) {
71 }
clementbera5fcbfb22019-08-19 11:30:42 +020072 }
73
74 private static void testOf10() {
Jake Wharton699e0ad2019-09-09 12:53:37 -040075 Object anObject0 = new Object();
76 Object anObject6 = new Object();
77 Object anObject9 = new Object();
78 List<Object> ofObject =
79 List.of(anObject0, new Object(), new Object(), new Object(), new Object(), new Object(),
80 anObject6, new Object(), new Object(), anObject9);
81 assertEquals(10, ofObject.size());
82 assertSame(anObject0, ofObject.get(0));
83 assertSame(anObject6, ofObject.get(6));
84 assertSame(anObject9, ofObject.get(9));
85 assertMutationNotAllowed(ofObject);
86
clementbera5fcbfb22019-08-19 11:30:42 +020087 List<Integer> ofInteger = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
Jake Wharton699e0ad2019-09-09 12:53:37 -040088 assertEquals(10, ofInteger.size());
89 assertEquals(0, ofInteger.get(0));
90 assertEquals(6, ofInteger.get(6));
91 assertEquals(9, ofInteger.get(9));
92
93 List<Object> ofMixed = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, anObject9);
94 assertEquals(10, ofMixed.size());
95 assertEquals(0, ofMixed.get(0));
96 assertEquals(6, ofMixed.get(6));
97 assertSame(anObject9, ofMixed.get(9));
98 assertMutationNotAllowed(ofMixed);
99
100 try {
101 List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, null);
102 throw new AssertionError();
103 } catch (NullPointerException expected) {
104 }
clementbera5fcbfb22019-08-19 11:30:42 +0200105 }
106
Jake Wharton72fbd2f2019-09-09 13:31:56 -0400107 private static void testOfVarargs() {
108 Object anObject0 = new Object();
109 Object anObject6 = new Object();
110 Object anObject10 = new Object();
111 List<Object> ofObject =
112 List.of(anObject0, new Object(), new Object(), new Object(), new Object(), new Object(),
113 anObject6, new Object(), new Object(), new Object(), anObject10);
114 assertEquals(11, ofObject.size());
115 assertSame(anObject0, ofObject.get(0));
116 assertSame(anObject6, ofObject.get(6));
117 assertSame(anObject10, ofObject.get(10));
118 assertMutationNotAllowed(ofObject);
119
120 List<Integer> ofInteger = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
121 assertEquals(11, ofInteger.size());
122 assertEquals(0, ofInteger.get(0));
123 assertEquals(6, ofInteger.get(6));
124 assertEquals(10, ofInteger.get(10));
125
126 List<Object> ofMixed = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, anObject10);
127 assertEquals(11, ofMixed.size());
128 assertEquals(0, ofMixed.get(0));
129 assertEquals(6, ofMixed.get(6));
130 assertSame(anObject10, ofMixed.get(10));
131 assertMutationNotAllowed(ofMixed);
132
133 // Ensure the supplied mutable array is not used directly since it is mutable.
134 Object[] mutableArray = { anObject0 };
135 List<Object> ofMutableArray = List.of(mutableArray);
136 mutableArray[0] = anObject10;
137 assertSame(anObject0, ofMutableArray.get(0));
138
139 try {
140 List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, null);
141 throw new AssertionError();
142 } catch (NullPointerException expected) {
143 }
144 }
145
Jake Wharton699e0ad2019-09-09 12:53:37 -0400146 private static void assertMutationNotAllowed(List<Object> ofObject) {
147 try {
148 ofObject.add(new Object());
149 throw new AssertionError();
150 } catch (UnsupportedOperationException expected) {
151 }
152 try {
153 ofObject.set(0, new Object());
154 throw new AssertionError();
155 } catch (UnsupportedOperationException expected) {
156 }
157 }
158
159 private static void assertSame(Object expected, Object actual) {
160 if (expected != actual) {
161 throw new AssertionError("Expected <" + expected + "> but was <" + actual + ">");
162 }
163 }
164
165 private static void assertEquals(Object expected, Object actual) {
166 if (expected != actual && !expected.equals(actual)) {
167 throw new AssertionError("Expected <" + expected + "> but was <" + actual + ">");
clementbera5fcbfb22019-08-19 11:30:42 +0200168 }
169 }
170}