author | pli |
Tue, 16 Jul 2019 00:57:00 +0000 | |
changeset 55689 | 8c5c9d86e1d6 |
parent 48541 | 946e34c2dec9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
31 |
import java.util.Arrays; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
32 |
import java.util.Collection; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
33 |
import java.util.Map; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
34 |
import java.util.NavigableMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
35 |
import java.util.NavigableSet; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
36 |
import java.util.Objects; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
37 |
import java.util.Random; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
38 |
import java.util.Set; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
39 |
import java.util.TreeSet; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
40 |
import java.util.concurrent.ArrayBlockingQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
41 |
import java.util.concurrent.ConcurrentHashMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
42 |
import java.util.concurrent.ConcurrentLinkedDeque; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
43 |
import java.util.concurrent.ConcurrentLinkedQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
44 |
import java.util.concurrent.ConcurrentSkipListMap; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
45 |
import java.util.concurrent.ConcurrentSkipListSet; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
46 |
import java.util.concurrent.CopyOnWriteArrayList; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
47 |
import java.util.concurrent.CopyOnWriteArraySet; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
48 |
import java.util.concurrent.LinkedBlockingDeque; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
49 |
import java.util.concurrent.LinkedBlockingQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
50 |
import java.util.concurrent.LinkedTransferQueue; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
51 |
import java.util.concurrent.PriorityBlockingQueue; |
2 | 52 |
|
53 |
@SuppressWarnings("unchecked") |
|
54 |
public class BiggernYours { |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
55 |
static final Random rnd = new Random(18675309); |
2 | 56 |
|
57 |
static void compareCollections(Collection c1, Collection c2) { |
|
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
58 |
Object[] c1Array = c1.toArray(); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
59 |
Object[] c2Array = c2.toArray(); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
60 |
|
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
61 |
check(c1Array.length == c2Array.length); |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
32649
diff
changeset
|
62 |
for (Object aC1 : c1Array) { |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
63 |
boolean found = false; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
32649
diff
changeset
|
64 |
for (Object aC2 : c2Array) { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
32649
diff
changeset
|
65 |
if (Objects.equals(aC1, aC2)) { |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
66 |
found = true; |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
67 |
break; |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
68 |
} |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
69 |
} |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
70 |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
32649
diff
changeset
|
71 |
if (!found) |
12859
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
72 |
fail(aC1 + " not found in " + Arrays.toString(c2Array)); |
c44b88bb9b5e
7126277: Alternative String hashing implementation
mduigou
parents:
7668
diff
changeset
|
73 |
} |
2 | 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 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
24692
diff
changeset
|
103 |
abstract static class MapFrobber { abstract void frob(Map m); } |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
24692
diff
changeset
|
104 |
abstract static class SetFrobber { abstract void frob(Set s); } |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
24692
diff
changeset
|
105 |
abstract static class ColFrobber { abstract void frob(Collection c); } |
2 | 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") |
|
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
173 |
private static void realMain(String[] args) { |
2 | 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( |
|
6672 | 205 |
new ConcurrentLinkedDeque(), |
206 |
new ConcurrentLinkedDeque() { |
|
207 |
public int size() {return randomize(super.size());}}); |
|
208 |
||
209 |
testCollections( |
|
2 | 210 |
new ConcurrentLinkedQueue(), |
211 |
new ConcurrentLinkedQueue() { |
|
212 |
public int size() {return randomize(super.size());}}); |
|
213 |
||
4110 | 214 |
testCollections( |
215 |
new LinkedTransferQueue(), |
|
216 |
new LinkedTransferQueue() { |
|
217 |
public int size() {return randomize(super.size());}}); |
|
3708 | 218 |
|
2 | 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));} |
|
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
253 |
public static void main(String[] args) { |
2 | 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");} |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
24692
diff
changeset
|
257 |
private abstract static class CheckedThread extends Thread { |
2 | 258 |
abstract void realRun() throws Throwable; |
259 |
public void run() { |
|
260 |
try {realRun();} catch (Throwable t) {unexpected(t);}}} |
|
261 |
} |