author | mli |
Wed, 23 May 2018 14:21:14 +0800 | |
changeset 50230 | cae567ae015d |
parent 48541 | 946e34c2dec9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2007, 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 6359979 |
|
27 |
* @summary Compare List implementations for identical behavior |
|
28 |
* @author Martin Buchholz |
|
30046 | 29 |
* @key randomness |
2 | 30 |
*/ |
31 |
||
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
32 |
import java.io.ByteArrayInputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
33 |
import java.io.ByteArrayOutputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
34 |
import java.io.IOException; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
35 |
import java.io.InputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
36 |
import java.io.ObjectInputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
37 |
import java.io.ObjectOutputStream; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
38 |
import java.util.ArrayList; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
39 |
import java.util.Arrays; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
40 |
import java.util.Collection; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
41 |
import java.util.Collections; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
42 |
import java.util.ConcurrentModificationException; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
43 |
import java.util.Iterator; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
44 |
import java.util.LinkedList; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
45 |
import java.util.List; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
46 |
import java.util.ListIterator; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
47 |
import java.util.Random; |
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47216
diff
changeset
|
48 |
import java.util.Vector; |
2 | 49 |
|
50 |
@SuppressWarnings("unchecked") |
|
51 |
public class LockStep { |
|
52 |
final int DEFAULT_SIZE = 5; |
|
53 |
int size; // Running time is O(size**2) |
|
54 |
||
55 |
int intArg(String[] args, int i, int defaultValue) { |
|
56 |
return args.length > i ? Integer.parseInt(args[i]) : defaultValue; |
|
57 |
} |
|
58 |
||
59 |
boolean maybe(int n) { return rnd.nextInt(n) == 0; } |
|
60 |
||
61 |
void test(String[] args) { |
|
62 |
size = intArg(args, 0, DEFAULT_SIZE); |
|
63 |
||
64 |
lockSteps(new ArrayList(), |
|
65 |
new LinkedList(), |
|
66 |
new Vector()); |
|
67 |
} |
|
68 |
||
69 |
void equalLists(List... lists) { |
|
70 |
for (List list : lists) |
|
71 |
equalLists(list, lists[0]); |
|
72 |
} |
|
73 |
||
74 |
void equalLists(List x, List y) { |
|
75 |
equal(x, y); |
|
76 |
equal(y, x); |
|
77 |
equal(x.size(), y.size()); |
|
78 |
equal(x.isEmpty(), y.isEmpty()); |
|
79 |
equal(x.hashCode(), y.hashCode()); |
|
80 |
equal(x.toString(), y.toString()); |
|
81 |
equal(x.toArray(), y.toArray()); |
|
82 |
} |
|
83 |
||
84 |
void lockSteps(List... lists) { |
|
85 |
for (int i = 0; i < lists.length; i++) |
|
86 |
if (maybe(4)) lists[i] = serialClone(lists[i]); |
|
87 |
for (final List list : lists) |
|
88 |
testEmptyList(list); |
|
89 |
for (int i = 0; i < size; i++) { |
|
90 |
ListFrobber adder = randomAdder(); |
|
91 |
for (final List list : lists) { |
|
92 |
adder.frob(list); |
|
93 |
equal(list.size(), i+1); |
|
94 |
} |
|
95 |
equalLists(lists); |
|
96 |
} |
|
97 |
{ |
|
98 |
final ListFrobber adder = randomAdder(); |
|
99 |
final ListFrobber remover = randomRemover(); |
|
100 |
for (final List list : lists) { |
|
101 |
||
102 |
THROWS(ConcurrentModificationException.class, |
|
103 |
new F(){void f(){ |
|
104 |
Iterator it = list.iterator(); |
|
105 |
adder.frob(list); |
|
106 |
it.next();}}, |
|
107 |
new F(){void f(){ |
|
108 |
Iterator it = asSubList(list).iterator(); |
|
109 |
remover.frob(list); |
|
110 |
it.next();}}, |
|
111 |
new F(){void f(){ |
|
112 |
Iterator it = asSubList(asSubList(list)).iterator(); |
|
113 |
adder.frob(list); |
|
114 |
it.next();}}, |
|
115 |
new F(){void f(){ |
|
116 |
List subList = asSubList(list); |
|
117 |
remover.frob(list); |
|
118 |
subList.get(0);}}, |
|
119 |
new F(){void f(){ |
|
120 |
List sl = asSubList(list); |
|
121 |
List ssl = asSubList(sl); |
|
122 |
adder.frob(sl); |
|
123 |
ssl.get(0);}}, |
|
124 |
new F(){void f(){ |
|
125 |
List sl = asSubList(list); |
|
126 |
List ssl = asSubList(sl); |
|
127 |
remover.frob(sl); |
|
128 |
ssl.get(0);}}); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
for (final List l : lists) { |
|
133 |
final List sl = asSubList(l); |
|
134 |
final List ssl = asSubList(sl); |
|
135 |
ssl.add(0, 42); |
|
136 |
equal(ssl.get(0), 42); |
|
137 |
equal(sl.get(0), 42); |
|
138 |
equal(l.get(0), 42); |
|
139 |
final int s = l.size(); |
|
140 |
final int rndIndex = rnd.nextInt(l.size()); |
|
141 |
THROWS(IndexOutOfBoundsException.class, |
|
142 |
new F(){void f(){l.subList(rndIndex, rndIndex).get(0);}}, |
|
143 |
new F(){void f(){l.subList(s/2, s).get(s/2 + 1);}}, |
|
144 |
new F(){void f(){l.subList(s/2, s).get(-1);}} |
|
145 |
); |
|
146 |
THROWS(IllegalArgumentException.class, |
|
147 |
new F(){void f(){ l.subList(1, 0);}}, |
|
148 |
new F(){void f(){ sl.subList(1, 0);}}, |
|
149 |
new F(){void f(){ssl.subList(1, 0);}}); |
|
150 |
} |
|
151 |
||
152 |
equalLists(lists); |
|
153 |
||
154 |
for (final List list : lists) { |
|
155 |
equalLists(list, asSubList(list)); |
|
156 |
equalLists(list, asSubList(asSubList(list))); |
|
157 |
} |
|
158 |
for (final List list : lists) |
|
159 |
System.out.println(list); |
|
160 |
||
161 |
for (int i = lists[0].size(); i > 0; i--) { |
|
162 |
ListFrobber remover = randomRemover(); |
|
163 |
for (final List list : lists) |
|
164 |
remover.frob(list); |
|
165 |
equalLists(lists); |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
<T> List<T> asSubList(List<T> list) { |
|
170 |
return list.subList(0, list.size()); |
|
171 |
} |
|
172 |
||
173 |
void testEmptyCollection(Collection<?> c) { |
|
174 |
check(c.isEmpty()); |
|
175 |
equal(c.size(), 0); |
|
176 |
equal(c.toString(),"[]"); |
|
177 |
equal(c.toArray().length, 0); |
|
178 |
equal(c.toArray(new Object[0]).length, 0); |
|
179 |
||
180 |
Object[] a = new Object[1]; a[0] = Boolean.TRUE; |
|
181 |
equal(c.toArray(a), a); |
|
182 |
equal(a[0], null); |
|
183 |
} |
|
184 |
||
185 |
void testEmptyList(List list) { |
|
186 |
testEmptyCollection(list); |
|
187 |
equal(list.hashCode(), 1); |
|
188 |
equal(list, Collections.emptyList()); |
|
189 |
} |
|
190 |
||
191 |
final Random rnd = new Random(); |
|
192 |
||
193 |
abstract class ListFrobber { abstract void frob(List l); } |
|
194 |
||
195 |
ListFrobber randomAdder() { |
|
196 |
final Integer e = rnd.nextInt(1024); |
|
197 |
final int subListCount = rnd.nextInt(3); |
|
198 |
final boolean atBeginning = rnd.nextBoolean(); |
|
199 |
final boolean useIterator = rnd.nextBoolean(); |
|
200 |
final boolean simpleIterator = rnd.nextBoolean(); |
|
201 |
return new ListFrobber() {void frob(List l) { |
|
202 |
final int s = l.size(); |
|
203 |
List ll = l; |
|
204 |
for (int i = 0; i < subListCount; i++) |
|
205 |
ll = asSubList(ll); |
|
206 |
if (! useIterator) { |
|
207 |
if (atBeginning) { |
|
208 |
switch (rnd.nextInt(3)) { |
|
209 |
case 0: ll.add(0, e); break; |
|
210 |
case 1: ll.subList(0, rnd.nextInt(s+1)).add(0, e); break; |
|
211 |
case 2: ll.subList(0, rnd.nextInt(s+1)).subList(0,0).add(0,e); break; |
|
212 |
default: throw new Error(); |
|
213 |
} |
|
214 |
} else { |
|
215 |
switch (rnd.nextInt(3)) { |
|
216 |
case 0: check(ll.add(e)); break; |
|
217 |
case 1: ll.subList(s/2, s).add(s - s/2, e); break; |
|
218 |
case 2: ll.subList(s, s).subList(0, 0).add(0, e); break; |
|
219 |
default: throw new Error(); |
|
220 |
} |
|
221 |
} |
|
222 |
} else { |
|
223 |
if (atBeginning) { |
|
224 |
ListIterator it = ll.listIterator(); |
|
225 |
equal(it.nextIndex(), 0); |
|
226 |
check(! it.hasPrevious()); |
|
227 |
it.add(e); |
|
228 |
equal(it.previousIndex(), 0); |
|
229 |
equal(it.nextIndex(), 1); |
|
230 |
check(it.hasPrevious()); |
|
231 |
} else { |
|
232 |
final int siz = ll.size(); |
|
233 |
ListIterator it = ll.listIterator(siz); |
|
234 |
equal(it.previousIndex(), siz-1); |
|
235 |
check(! it.hasNext()); |
|
236 |
it.add(e); |
|
237 |
equal(it.previousIndex(), siz); |
|
238 |
equal(it.nextIndex(), siz+1); |
|
239 |
check(! it.hasNext()); |
|
240 |
check(it.hasPrevious()); |
|
241 |
} |
|
242 |
}}}; |
|
243 |
} |
|
244 |
||
245 |
ListFrobber randomRemover() { |
|
246 |
final int position = rnd.nextInt(3); |
|
247 |
final int subListCount = rnd.nextInt(3); |
|
248 |
return new ListFrobber() {void frob(List l) { |
|
249 |
final int s = l.size(); |
|
250 |
List ll = l; |
|
251 |
for (int i = 0; i < subListCount; i++) |
|
252 |
ll = asSubList(ll); |
|
253 |
switch (position) { |
|
254 |
case 0: // beginning |
|
255 |
switch (rnd.nextInt(3)) { |
|
256 |
case 0: ll.remove(0); break; |
|
257 |
case 1: { |
|
258 |
final Iterator it = ll.iterator(); |
|
259 |
check(it.hasNext()); |
|
260 |
THROWS(IllegalStateException.class, |
|
261 |
new F(){void f(){it.remove();}}); |
|
262 |
it.next(); |
|
263 |
it.remove(); |
|
264 |
THROWS(IllegalStateException.class, |
|
265 |
new F(){void f(){it.remove();}}); |
|
266 |
break;} |
|
267 |
case 2: { |
|
268 |
final ListIterator it = ll.listIterator(); |
|
269 |
check(it.hasNext()); |
|
270 |
THROWS(IllegalStateException.class, |
|
271 |
new F(){void f(){it.remove();}}); |
|
272 |
it.next(); |
|
273 |
it.remove(); |
|
274 |
THROWS(IllegalStateException.class, |
|
275 |
new F(){void f(){it.remove();}}); |
|
276 |
break;} |
|
277 |
default: throw new Error(); |
|
278 |
} |
|
279 |
break; |
|
280 |
case 1: // midpoint |
|
281 |
switch (rnd.nextInt(3)) { |
|
282 |
case 0: ll.remove(s/2); break; |
|
283 |
case 1: { |
|
284 |
final ListIterator it = ll.listIterator(s/2); |
|
285 |
it.next(); |
|
286 |
it.remove(); |
|
287 |
break; |
|
288 |
} |
|
289 |
case 2: { |
|
290 |
final ListIterator it = ll.listIterator(s/2+1); |
|
291 |
it.previous(); |
|
292 |
it.remove(); |
|
293 |
break; |
|
294 |
} |
|
295 |
default: throw new Error(); |
|
296 |
} |
|
297 |
break; |
|
298 |
case 2: // end |
|
299 |
switch (rnd.nextInt(3)) { |
|
300 |
case 0: ll.remove(s-1); break; |
|
301 |
case 1: ll.subList(s-1, s).clear(); break; |
|
302 |
case 2: |
|
303 |
final ListIterator it = ll.listIterator(s); |
|
304 |
check(! it.hasNext()); |
|
305 |
check(it.hasPrevious()); |
|
306 |
THROWS(IllegalStateException.class, |
|
307 |
new F(){void f(){it.remove();}}); |
|
308 |
it.previous(); |
|
309 |
equal(it.nextIndex(), s-1); |
|
310 |
check(it.hasNext()); |
|
311 |
it.remove(); |
|
312 |
equal(it.nextIndex(), s-1); |
|
313 |
check(! it.hasNext()); |
|
314 |
THROWS(IllegalStateException.class, |
|
315 |
new F(){void f(){it.remove();}}); |
|
316 |
break; |
|
317 |
default: throw new Error(); |
|
318 |
} |
|
319 |
break; |
|
320 |
default: throw new Error(); |
|
321 |
}}}; |
|
322 |
} |
|
323 |
||
324 |
//--------------------- Infrastructure --------------------------- |
|
325 |
volatile int passed = 0, failed = 0; |
|
326 |
void pass() {passed++;} |
|
327 |
void fail() {failed++; Thread.dumpStack();} |
|
328 |
void fail(String msg) {System.err.println(msg); fail();} |
|
329 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
330 |
void check(boolean cond) {if (cond) pass(); else fail();} |
|
331 |
void equal(Object x, Object y) { |
|
332 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
333 |
else fail(x + " not equal to " + y);} |
|
334 |
<T> void equal(T[] x, T[] y) {check(Arrays.equals(x,y));} |
|
335 |
public static void main(String[] args) throws Throwable { |
|
336 |
new LockStep().instanceMain(args);} |
|
337 |
void instanceMain(String[] args) throws Throwable { |
|
338 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
|
339 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
340 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
341 |
abstract class F {abstract void f() throws Throwable;} |
|
342 |
void THROWS(Class<? extends Throwable> k, F... fs) { |
|
343 |
for (F f : fs) |
|
344 |
try {f.f(); fail("Expected " + k.getName() + " not thrown");} |
|
345 |
catch (Throwable t) { |
|
346 |
if (k.isAssignableFrom(t.getClass())) pass(); |
|
347 |
else unexpected(t);}} |
|
348 |
static byte[] serializedForm(Object obj) { |
|
349 |
try { |
|
350 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
351 |
new ObjectOutputStream(baos).writeObject(obj); |
|
352 |
return baos.toByteArray(); |
|
353 |
} catch (IOException e) { throw new RuntimeException(e); }} |
|
354 |
static Object readObject(byte[] bytes) |
|
355 |
throws IOException, ClassNotFoundException { |
|
356 |
InputStream is = new ByteArrayInputStream(bytes); |
|
357 |
return new ObjectInputStream(is).readObject();} |
|
358 |
@SuppressWarnings("unchecked") |
|
359 |
static <T> T serialClone(T obj) { |
|
360 |
try { return (T) readObject(serializedForm(obj)); } |
|
361 |
catch (Exception e) { throw new RuntimeException(e); }} |
|
362 |
} |