Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Collection/SetFactories.java
41149 views
1
/*
2
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
27
import java.io.ObjectInputStream;
28
import java.io.ObjectOutputStream;
29
import java.util.ArrayList;
30
import java.util.Arrays;
31
import java.util.Collections;
32
import java.util.Iterator;
33
import java.util.HashSet;
34
import java.util.List;
35
import java.util.Set;
36
import java.util.stream.Collectors;
37
import static org.testng.Assert.assertEquals;
38
39
import org.testng.annotations.DataProvider;
40
import org.testng.annotations.Test;
41
42
import static org.testng.Assert.assertFalse;
43
import static org.testng.Assert.assertNotEquals;
44
import static org.testng.Assert.assertNotSame;
45
import static org.testng.Assert.assertSame;
46
import static org.testng.Assert.assertTrue;
47
import static org.testng.Assert.fail;
48
49
/*
50
* @test
51
* @bug 8048330
52
* @summary Test convenience static factory methods on Set.
53
* @run testng SetFactories
54
*/
55
56
57
public class SetFactories {
58
59
static final int NUM_STRINGS = 20; // should be larger than the largest fixed-arg overload
60
static final String[] stringArray;
61
static {
62
String[] sa = new String[NUM_STRINGS];
63
for (int i = 0; i < NUM_STRINGS; i++) {
64
sa[i] = String.valueOf((char)('a' + i));
65
}
66
stringArray = sa;
67
}
68
69
static Object[] a(Set<String> act, Set<String> exp) {
70
return new Object[] { act, exp };
71
}
72
73
static Set<String> hashSetOf(String... args) {
74
return new HashSet<>(Arrays.asList(args));
75
}
76
77
@DataProvider(name="empty")
78
public Iterator<Object[]> empty() {
79
return Collections.singletonList(
80
// actual, expected
81
a(Set.of(), Collections.emptySet())
82
).iterator();
83
}
84
85
@DataProvider(name="nonempty")
86
public Iterator<Object[]> nonempty() {
87
return Arrays.asList(
88
// actual, expected
89
a( Set.of("a"),
90
hashSetOf("a")),
91
a( Set.of("a", "b"),
92
hashSetOf("a", "b")),
93
a( Set.of("a", "b", "c"),
94
hashSetOf("a", "b", "c")),
95
a( Set.of("a", "b", "c", "d"),
96
hashSetOf("a", "b", "c", "d")),
97
a( Set.of("a", "b", "c", "d", "e"),
98
hashSetOf("a", "b", "c", "d", "e")),
99
a( Set.of("a", "b", "c", "d", "e", "f"),
100
hashSetOf("a", "b", "c", "d", "e", "f")),
101
a( Set.of("a", "b", "c", "d", "e", "f", "g"),
102
hashSetOf("a", "b", "c", "d", "e", "f", "g")),
103
a( Set.of("a", "b", "c", "d", "e", "f", "g", "h"),
104
hashSetOf("a", "b", "c", "d", "e", "f", "g", "h")),
105
a( Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i"),
106
hashSetOf("a", "b", "c", "d", "e", "f", "g", "h", "i")),
107
a( Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
108
hashSetOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),
109
a( Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
110
Set.of("j", "i", "h", "g", "f", "e", "d", "c", "b", "a")),
111
a( Set.of(stringArray),
112
hashSetOf(stringArray))
113
).iterator();
114
}
115
116
@DataProvider(name="all")
117
public Iterator<Object[]> all() {
118
List<Object[]> all = new ArrayList<>();
119
empty().forEachRemaining(all::add);
120
nonempty().forEachRemaining(all::add);
121
return all.iterator();
122
}
123
124
@Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
125
public void cannotAdd(Set<String> act, Set<String> exp) {
126
act.add("x");
127
}
128
129
@Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
130
public void cannotRemove(Set<String> act, Set<String> exp) {
131
act.remove(act.iterator().next());
132
}
133
134
@Test(dataProvider="all")
135
public void contentsMatch(Set<String> act, Set<String> exp) {
136
assertEquals(act, exp);
137
}
138
139
@Test(expectedExceptions=IllegalArgumentException.class)
140
public void dupsDisallowed2() {
141
Set<String> set = Set.of("a", "a");
142
}
143
144
@Test(expectedExceptions=IllegalArgumentException.class)
145
public void dupsDisallowed3() {
146
Set<String> set = Set.of("a", "b", "a");
147
}
148
149
@Test(expectedExceptions=IllegalArgumentException.class)
150
public void dupsDisallowed4() {
151
Set<String> set = Set.of("a", "b", "c", "a");
152
}
153
154
@Test(expectedExceptions=IllegalArgumentException.class)
155
public void dupsDisallowed5() {
156
Set<String> set = Set.of("a", "b", "c", "d", "a");
157
}
158
159
@Test(expectedExceptions=IllegalArgumentException.class)
160
public void dupsDisallowed6() {
161
Set<String> set = Set.of("a", "b", "c", "d", "e", "a");
162
}
163
164
@Test(expectedExceptions=IllegalArgumentException.class)
165
public void dupsDisallowed7() {
166
Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "a");
167
}
168
169
@Test(expectedExceptions=IllegalArgumentException.class)
170
public void dupsDisallowed8() {
171
Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "a");
172
}
173
174
@Test(expectedExceptions=IllegalArgumentException.class)
175
public void dupsDisallowed9() {
176
Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "a");
177
}
178
179
@Test(expectedExceptions=IllegalArgumentException.class)
180
public void dupsDisallowed10() {
181
Set<String> set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "a");
182
}
183
184
@Test(expectedExceptions=IllegalArgumentException.class)
185
public void dupsDisallowedN() {
186
String[] array = stringArray.clone();
187
array[0] = array[1];
188
Set<String> set = Set.of(array);
189
}
190
191
@Test(dataProvider="all")
192
public void hashCodeEqual(Set<String> act, Set<String> exp) {
193
assertEquals(act.hashCode(), exp.hashCode());
194
}
195
196
@Test(dataProvider="all")
197
public void containsAll(Set<String> act, Set<String> exp) {
198
assertTrue(act.containsAll(exp));
199
assertTrue(exp.containsAll(act));
200
}
201
202
@Test(expectedExceptions=NullPointerException.class)
203
public void nullDisallowed1() {
204
Set.of((String)null); // force one-arg overload
205
}
206
207
@Test(expectedExceptions=NullPointerException.class)
208
public void nullDisallowed2a() {
209
Set.of("a", null);
210
}
211
212
@Test(expectedExceptions=NullPointerException.class)
213
public void nullDisallowed2b() {
214
Set.of(null, "b");
215
}
216
217
@Test(expectedExceptions=NullPointerException.class)
218
public void nullDisallowed3() {
219
Set.of("a", "b", null);
220
}
221
222
@Test(expectedExceptions=NullPointerException.class)
223
public void nullDisallowed4() {
224
Set.of("a", "b", "c", null);
225
}
226
227
@Test(expectedExceptions=NullPointerException.class)
228
public void nullDisallowed5() {
229
Set.of("a", "b", "c", "d", null);
230
}
231
232
@Test(expectedExceptions=NullPointerException.class)
233
public void nullDisallowed6() {
234
Set.of("a", "b", "c", "d", "e", null);
235
}
236
237
@Test(expectedExceptions=NullPointerException.class)
238
public void nullDisallowed7() {
239
Set.of("a", "b", "c", "d", "e", "f", null);
240
}
241
242
@Test(expectedExceptions=NullPointerException.class)
243
public void nullDisallowed8() {
244
Set.of("a", "b", "c", "d", "e", "f", "g", null);
245
}
246
247
@Test(expectedExceptions=NullPointerException.class)
248
public void nullDisallowed9() {
249
Set.of("a", "b", "c", "d", "e", "f", "g", "h", null);
250
}
251
252
@Test(expectedExceptions=NullPointerException.class)
253
public void nullDisallowed10() {
254
Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", null);
255
}
256
257
@Test(expectedExceptions=NullPointerException.class)
258
public void nullDisallowedN() {
259
String[] array = stringArray.clone();
260
array[0] = null;
261
Set.of(array);
262
}
263
264
@Test(expectedExceptions=NullPointerException.class)
265
public void nullArrayDisallowed() {
266
Set.of((Object[])null);
267
}
268
269
@Test(dataProvider="all", expectedExceptions=NullPointerException.class)
270
public void containsNullShouldThrowNPE(Set<String> act, Set<String> exp) {
271
act.contains(null);
272
}
273
274
@Test(dataProvider="all")
275
public void serialEquality(Set<String> act, Set<String> exp) {
276
// assume that act.equals(exp) tested elsewhere
277
Set<String> copy = serialClone(act);
278
assertEquals(act, copy);
279
assertEquals(copy, exp);
280
}
281
282
@SuppressWarnings("unchecked")
283
static <T> T serialClone(T obj) {
284
try {
285
ByteArrayOutputStream baos = new ByteArrayOutputStream();
286
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
287
oos.writeObject(obj);
288
}
289
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
290
ObjectInputStream ois = new ObjectInputStream(bais);
291
return (T) ois.readObject();
292
} catch (IOException | ClassNotFoundException e) {
293
throw new AssertionError(e);
294
}
295
}
296
297
Set<Integer> genSet() {
298
return new HashSet<>(Arrays.asList(1, 2, 3));
299
}
300
301
@Test
302
public void copyOfResultsEqual() {
303
Set<Integer> orig = genSet();
304
Set<Integer> copy = Set.copyOf(orig);
305
306
assertEquals(orig, copy);
307
assertEquals(copy, orig);
308
}
309
310
@Test
311
public void copyOfModifiedUnequal() {
312
Set<Integer> orig = genSet();
313
Set<Integer> copy = Set.copyOf(orig);
314
orig.add(4);
315
316
assertNotEquals(orig, copy);
317
assertNotEquals(copy, orig);
318
}
319
320
@Test
321
public void copyOfIdentity() {
322
Set<Integer> orig = genSet();
323
Set<Integer> copy1 = Set.copyOf(orig);
324
Set<Integer> copy2 = Set.copyOf(copy1);
325
326
assertNotSame(orig, copy1);
327
assertSame(copy1, copy2);
328
}
329
330
@Test(expectedExceptions=NullPointerException.class)
331
public void copyOfRejectsNullCollection() {
332
Set<Integer> set = Set.copyOf(null);
333
}
334
335
@Test(expectedExceptions=NullPointerException.class)
336
public void copyOfRejectsNullElements() {
337
Set<Integer> set = Set.copyOf(Arrays.asList(1, null, 3));
338
}
339
340
@Test
341
public void copyOfAcceptsDuplicates() {
342
Set<Integer> set = Set.copyOf(Arrays.asList(1, 1, 2, 3, 3, 3));
343
assertEquals(set, Set.of(1, 2, 3));
344
}
345
}
346
347