blob: d824bd0ddc2ad2914cbcc4e3f39c59285cc4078e [file] [log] [blame]
Jake Whartona5f05062019-09-19 15:09:51 -04001// 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.Set;
8
9public class SetBackportJava9Main {
10
11 public static void main(String[] args) {
12 testOf0();
13 testOf1();
14 testOf2();
15 testOf10();
16 testOfVarargs();
17 }
18
19 private static void testOf0() {
20 Set<Object> ofObject = Set.of();
21 assertEquals(0, ofObject.size());
22 assertFalse(ofObject.contains(new Object()));
23 assertMutationNotAllowed(ofObject);
24
25 Set<Integer> ofInteger = Set.of();
26 assertEquals(0, ofInteger.size());
27 assertFalse(ofInteger.contains(0));
28 }
29
30 private static void testOf1() {
31 Object anObject = new Object();
32 Set<Object> ofObject = Set.of(anObject);
33 assertEquals(1, ofObject.size());
34 assertTrue(ofObject.contains(anObject));
35 assertFalse(ofObject.contains(new Object()));
36 assertMutationNotAllowed(ofObject);
37
38 Set<Integer> ofInteger = Set.of(1);
39 assertEquals(1, ofInteger.size());
40 assertTrue(ofInteger.contains(1));
41 assertFalse(ofInteger.contains(2));
42
43 try {
44 Set.of((Object) null);
45 throw new AssertionError();
46 } catch (NullPointerException expected) {
47 }
48 }
49
50 private static void testOf2() {
51 Object anObject0 = new Object();
52 Object anObject1 = new Object();
53 Set<Object> ofObject = Set.of(anObject0, anObject1);
54 assertEquals(2, ofObject.size());
55 assertTrue(ofObject.contains(anObject0));
56 assertTrue(ofObject.contains(anObject1));
57 assertFalse(ofObject.contains(new Object()));
58 assertMutationNotAllowed(ofObject);
59
60 Set<Integer> ofInteger = Set.of(1, 2);
61 assertEquals(2, ofInteger.size());
62 assertTrue(ofInteger.contains(1));
63 assertTrue(ofInteger.contains(2));
64 assertFalse(ofInteger.contains(3));
65
66 Set<Object> ofMixed = Set.of(anObject0, 1);
67 assertEquals(2, ofMixed.size());
68 assertTrue(ofMixed.contains(anObject0));
69 assertTrue(ofMixed.contains(1));
70 assertFalse(ofMixed.contains(2));
71 assertFalse(ofMixed.contains(anObject1));
72 assertMutationNotAllowed(ofMixed);
73
74 try {
75 Set.of(1, null);
76 throw new AssertionError();
77 } catch (NullPointerException expected) {
78 }
79
80 try {
81 Set.of(1, 1);
82 throw new AssertionError();
83 } catch (IllegalArgumentException expected) {
84 assertEquals("duplicate element: 1", expected.getMessage());
85 }
86 }
87
88 private static void testOf10() {
89 Object anObject0 = new Object();
90 Object anObject6 = new Object();
91 Object anObject9 = new Object();
92 Set<Object> ofObject =
93 Set.of(anObject0, new Object(), new Object(), new Object(), new Object(), new Object(),
94 anObject6, new Object(), new Object(), anObject9);
95 assertEquals(10, ofObject.size());
96 assertTrue(ofObject.contains(anObject0));
97 assertTrue(ofObject.contains(anObject6));
98 assertTrue(ofObject.contains(anObject9));
99 assertFalse(ofObject.contains(new Object()));
100 assertMutationNotAllowed(ofObject);
101
102 Set<Integer> ofInteger = Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
103 assertEquals(10, ofInteger.size());
104 assertTrue(ofInteger.contains(0));
105 assertTrue(ofInteger.contains(6));
106 assertTrue(ofInteger.contains(9));
107 assertFalse(ofInteger.contains(10));
108
109 Set<Object> ofMixed = Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, anObject9);
110 assertEquals(10, ofMixed.size());
111 assertTrue(ofMixed.contains(0));
112 assertTrue(ofMixed.contains(6));
113 assertTrue(ofMixed.contains(anObject9));
114 assertFalse(ofMixed.contains(anObject0));
115 assertMutationNotAllowed(ofMixed);
116
117 try {
118 Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, null);
119 throw new AssertionError();
120 } catch (NullPointerException expected) {
121 }
122
123 try {
124 Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 0);
125 throw new AssertionError();
126 } catch (IllegalArgumentException expected) {
127 assertEquals("duplicate element: 0", expected.getMessage());
128 }
129 }
130
131 private static void testOfVarargs() {
132 Object anObject0 = new Object();
133 Object anObject6 = new Object();
134 Object anObject10 = new Object();
135 Set<Object> ofObject =
136 Set.of(anObject0, new Object(), new Object(), new Object(), new Object(), new Object(),
137 anObject6, new Object(), new Object(), new Object(), anObject10);
138 assertEquals(11, ofObject.size());
139 assertTrue(ofObject.contains(anObject0));
140 assertTrue(ofObject.contains(anObject6));
141 assertTrue(ofObject.contains(anObject10));
142 assertFalse(ofObject.contains(new Object()));
143 assertMutationNotAllowed(ofObject);
144
145 Set<Integer> ofInteger = Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
146 assertEquals(11, ofInteger.size());
147 assertTrue(ofInteger.contains(0));
148 assertTrue(ofInteger.contains(6));
149 assertTrue(ofInteger.contains(10));
150 assertFalse(ofInteger.contains(11));
151
152 Set<Object> ofMixed = Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, anObject10);
153 assertEquals(11, ofMixed.size());
154 assertTrue(ofMixed.contains(0));
155 assertTrue(ofMixed.contains(6));
156 assertTrue(ofMixed.contains(anObject10));
157 assertFalse(ofMixed.contains(10));
158 assertFalse(ofMixed.contains(anObject0));
159 assertMutationNotAllowed(ofMixed);
160
161 // Ensure the supplied mutable array is not used directly since it is mutable.
162 Object[] mutableArray = { anObject0 };
163 Set<Object> ofMutableArray = Set.of(mutableArray);
164 mutableArray[0] = anObject10;
165 assertTrue(ofMutableArray.contains(anObject0));
166 assertFalse(ofMutableArray.contains(anObject10));
167
168 try {
169 Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, null);
170 throw new AssertionError();
171 } catch (NullPointerException expected) {
172 }
173
174 try {
175 Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
176 throw new AssertionError();
177 } catch (IllegalArgumentException expected) {
178 assertEquals("duplicate element: 0", expected.getMessage());
179 }
180 }
181
182 private static void assertMutationNotAllowed(Set<Object> ofObject) {
183 try {
184 ofObject.add(new Object());
185 throw new AssertionError();
186 } catch (UnsupportedOperationException expected) {
187 }
188 }
189
190 private static void assertTrue(boolean value) {
191 if (!value) {
192 throw new AssertionError("Expected <true> but was <false>");
193 }
194 }
195
196 private static void assertFalse(boolean value) {
197 if (value) {
198 throw new AssertionError("Expected <false> but was <true>");
199 }
200 }
201
202 private static void assertEquals(Object expected, Object actual) {
203 if (expected != actual && !expected.equals(actual)) {
204 throw new AssertionError("Expected <" + expected + "> but was <" + actual + ">");
205 }
206 }
207}