Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Collection/BiggernYours.java
41149 views
1
/*
2
* Copyright (c) 2006, 2014, 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
/*
25
* @test
26
* @bug 6415641 6377302
27
* @summary Concurrent collections are permitted to lie about their size
28
* @author Martin Buchholz
29
*/
30
31
import java.util.Arrays;
32
import java.util.Collection;
33
import java.util.Map;
34
import java.util.NavigableMap;
35
import java.util.NavigableSet;
36
import java.util.Objects;
37
import java.util.Random;
38
import java.util.Set;
39
import java.util.TreeSet;
40
import java.util.concurrent.ArrayBlockingQueue;
41
import java.util.concurrent.ConcurrentHashMap;
42
import java.util.concurrent.ConcurrentLinkedDeque;
43
import java.util.concurrent.ConcurrentLinkedQueue;
44
import java.util.concurrent.ConcurrentSkipListMap;
45
import java.util.concurrent.ConcurrentSkipListSet;
46
import java.util.concurrent.CopyOnWriteArrayList;
47
import java.util.concurrent.CopyOnWriteArraySet;
48
import java.util.concurrent.LinkedBlockingDeque;
49
import java.util.concurrent.LinkedBlockingQueue;
50
import java.util.concurrent.LinkedTransferQueue;
51
import java.util.concurrent.PriorityBlockingQueue;
52
53
@SuppressWarnings("unchecked")
54
public class BiggernYours {
55
static final Random rnd = new Random(18675309);
56
57
static void compareCollections(Collection c1, Collection c2) {
58
Object[] c1Array = c1.toArray();
59
Object[] c2Array = c2.toArray();
60
61
check(c1Array.length == c2Array.length);
62
for (Object aC1 : c1Array) {
63
boolean found = false;
64
for (Object aC2 : c2Array) {
65
if (Objects.equals(aC1, aC2)) {
66
found = true;
67
break;
68
}
69
}
70
71
if (!found)
72
fail(aC1 + " not found in " + Arrays.toString(c2Array));
73
}
74
}
75
76
static void compareMaps(Map m1, Map m2) {
77
compareCollections(m1.keySet(),
78
m2.keySet());
79
compareCollections(m1.values(),
80
m2.values());
81
compareCollections(m1.entrySet(),
82
m2.entrySet());
83
}
84
85
static void compareNavigableMaps(NavigableMap m1, NavigableMap m2) {
86
compareMaps(m1, m2);
87
compareMaps(m1.descendingMap(),
88
m2.descendingMap());
89
compareMaps(m1.tailMap(Integer.MIN_VALUE),
90
m2.tailMap(Integer.MIN_VALUE));
91
compareMaps(m1.headMap(Integer.MAX_VALUE),
92
m2.headMap(Integer.MAX_VALUE));
93
}
94
95
static void compareNavigableSets(NavigableSet s1, NavigableSet s2) {
96
compareCollections(s1, s2);
97
compareCollections(s1.descendingSet(),
98
s2.descendingSet());
99
compareCollections(s1.tailSet(Integer.MIN_VALUE),
100
s2.tailSet(Integer.MIN_VALUE));
101
}
102
103
abstract static class MapFrobber { abstract void frob(Map m); }
104
abstract static class SetFrobber { abstract void frob(Set s); }
105
abstract static class ColFrobber { abstract void frob(Collection c); }
106
107
static ColFrobber adder(final int i) {
108
return new ColFrobber() {void frob(Collection c) { c.add(i); }};
109
}
110
111
static final ColFrobber[] adders =
112
{ adder(1), adder(3), adder(2) };
113
114
static MapFrobber putter(final int k, final int v) {
115
return new MapFrobber() {void frob(Map m) { m.put(k,v); }};
116
}
117
118
static final MapFrobber[] putters =
119
{ putter(1, -2), putter(3, -6), putter(2, -4) };
120
121
static void unexpected(Throwable t, Object suspect) {
122
System.out.println(suspect.getClass());
123
unexpected(t);
124
}
125
126
static void testCollections(Collection c1, Collection c2) {
127
try {
128
compareCollections(c1, c2);
129
for (ColFrobber adder : adders) {
130
for (Collection c : new Collection[]{c1, c2})
131
adder.frob(c);
132
compareCollections(c1, c2);
133
}
134
} catch (Throwable t) { unexpected(t, c1); }
135
}
136
137
static void testNavigableSets(NavigableSet s1, NavigableSet s2) {
138
try {
139
compareNavigableSets(s1, s2);
140
for (ColFrobber adder : adders) {
141
for (Set s : new Set[]{s1, s2})
142
adder.frob(s);
143
compareNavigableSets(s1, s2);
144
}
145
} catch (Throwable t) { unexpected(t, s1); }
146
}
147
148
static void testMaps(Map m1, Map m2) {
149
try {
150
compareMaps(m1, m2);
151
for (MapFrobber putter : putters) {
152
for (Map m : new Map[]{m1, m2})
153
putter.frob(m);
154
compareMaps(m1, m2);
155
}
156
} catch (Throwable t) { unexpected(t, m1); }
157
}
158
159
static void testNavigableMaps(NavigableMap m1, NavigableMap m2) {
160
try {
161
compareNavigableMaps(m1, m2);
162
for (MapFrobber putter : putters) {
163
for (Map m : new Map[]{m1, m2})
164
putter.frob(m);
165
compareNavigableMaps(m1, m2);
166
}
167
} catch (Throwable t) { unexpected(t, m1); }
168
}
169
170
static int randomize(int size) { return rnd.nextInt(size + 2); }
171
172
@SuppressWarnings("serial")
173
private static void realMain(String[] args) {
174
testNavigableMaps(
175
new ConcurrentSkipListMap(),
176
new ConcurrentSkipListMap() {
177
public int size() {return randomize(super.size());}});
178
179
testNavigableSets(
180
new ConcurrentSkipListSet(),
181
new ConcurrentSkipListSet() {
182
public int size() {return randomize(super.size());}});
183
184
testCollections(
185
new CopyOnWriteArraySet(),
186
new CopyOnWriteArraySet() {
187
public int size() {return randomize(super.size());}});
188
189
testCollections(
190
new CopyOnWriteArrayList(),
191
new CopyOnWriteArrayList() {
192
public int size() {return randomize(super.size());}});
193
194
testCollections(
195
new TreeSet(),
196
new TreeSet() {
197
public int size() {return randomize(super.size());}});
198
199
testMaps(
200
new ConcurrentHashMap(),
201
new ConcurrentHashMap() {
202
public int size() {return randomize(super.size());}});
203
204
testCollections(
205
new ConcurrentLinkedDeque(),
206
new ConcurrentLinkedDeque() {
207
public int size() {return randomize(super.size());}});
208
209
testCollections(
210
new ConcurrentLinkedQueue(),
211
new ConcurrentLinkedQueue() {
212
public int size() {return randomize(super.size());}});
213
214
testCollections(
215
new LinkedTransferQueue(),
216
new LinkedTransferQueue() {
217
public int size() {return randomize(super.size());}});
218
219
testCollections(
220
new LinkedBlockingQueue(),
221
new LinkedBlockingQueue() {
222
public int size() {return randomize(super.size());}});
223
224
testCollections(
225
new LinkedBlockingDeque(),
226
new LinkedBlockingDeque() {
227
public int size() {return randomize(super.size());}});
228
229
testCollections(
230
new ArrayBlockingQueue(5),
231
new ArrayBlockingQueue(5) {
232
public int size() {return randomize(super.size());}});
233
234
testCollections(
235
new PriorityBlockingQueue(5),
236
new PriorityBlockingQueue(5) {
237
public int size() {return randomize(super.size());}});
238
}
239
240
//--------------------- Infrastructure ---------------------------
241
static volatile int passed = 0, failed = 0;
242
static void pass() {passed++;}
243
static void fail() {failed++; Thread.dumpStack();}
244
static void fail(String msg) {System.out.println(msg); fail();}
245
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
246
static void check(boolean cond) {if (cond) pass(); else fail();}
247
static void equal(Object x, Object y) {
248
if (x == null ? y == null : x.equals(y)) pass();
249
else fail(x + " not equal to " + y);}
250
static void arrayEqual(Object[] x, Object[] y) {
251
if (x == null ? y == null : Arrays.equals(x, y)) pass();
252
else fail(Arrays.toString(x) + " not equal to " + Arrays.toString(y));}
253
public static void main(String[] args) {
254
try {realMain(args);} catch (Throwable t) {unexpected(t);}
255
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
256
if (failed > 0) throw new AssertionError("Some tests failed");}
257
private abstract static class CheckedThread extends Thread {
258
abstract void realRun() throws Throwable;
259
public void run() {
260
try {realRun();} catch (Throwable t) {unexpected(t);}}}
261
}
262
263