1 |
|
/* |
2 |
|
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
< |
* as explained at http://creativecommons.org/licenses/publicdomain. |
3 |
> |
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
4 |
|
*/ |
5 |
|
|
6 |
|
import java.util.*; |
38 |
|
} |
39 |
|
|
40 |
|
public static void main(String args[]) throws Exception { |
41 |
< |
Class cls = Class.forName(args[0]); |
41 |
> |
Class<?> cls = Class.forName(args[0]); |
42 |
|
int n = 1000000; |
43 |
|
|
44 |
|
for (int j = 0; j < 3; ++j) { |
45 |
+ |
@SuppressWarnings("unchecked") |
46 |
|
Deque<Integer> deque = (Deque<Integer>) cls.newInstance(); |
47 |
|
nextTail = 0; |
48 |
|
nextHead = -1; |
49 |
< |
long start = System.currentTimeMillis(); |
49 |
> |
long start = System.nanoTime(); |
50 |
|
mainTest(deque, n); |
51 |
< |
long end = System.currentTimeMillis(); |
52 |
< |
System.out.println("Time: " + (end - start) + "ms"); |
51 |
> |
long end = System.nanoTime(); |
52 |
> |
long elapsedTimeMillis = (end - start) / (1000L * 1000L); |
53 |
> |
System.out.printf("Time: %d ms%n", elapsedTimeMillis); |
54 |
|
if (deque.isEmpty()) System.out.print(" "); |
55 |
|
} |
56 |
|
|
73 |
|
|
74 |
|
// Test serialization and copying |
75 |
|
if ((i & 4095) == 0) { |
76 |
< |
testEqual(deque, deepCopy(deque)); |
77 |
< |
testEqual(deque, (Deque<Integer>) deque.getClass(). |
76 |
< |
getConstructor(Collection.class).newInstance(deque)); |
76 |
> |
testEqual(deque, serialClone(deque)); |
77 |
> |
testEqual(deque, copyConstructorClone(deque)); |
78 |
|
} |
79 |
|
|
80 |
|
// Test fancy removal stuff once in a blue moon |
81 |
|
if ((i & 8191) == 0) |
82 |
|
testRemove(deque); |
83 |
|
|
84 |
< |
} |
84 |
> |
} |
85 |
|
|
86 |
|
// Stupid tests for clear, toString |
87 |
|
deque.clear(); |
271 |
|
throw new Exception("d2 contains all of {Integer.MIN_VALUE }"); |
272 |
|
} |
273 |
|
|
274 |
< |
private static <T> T deepCopy(T o) { |
275 |
< |
try { |
276 |
< |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
277 |
< |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
278 |
< |
oos.writeObject(o); |
279 |
< |
oos.flush(); |
280 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream( |
281 |
< |
bos.toByteArray()); |
282 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
274 |
> |
@SuppressWarnings("unchecked") |
275 |
> |
private static <T> T copyConstructorClone(T o) throws Exception { |
276 |
> |
return (T) o.getClass().getConstructor(Collection.class).newInstance(o); |
277 |
> |
} |
278 |
> |
|
279 |
> |
@SuppressWarnings("unchecked") |
280 |
> |
private static <T> T serialClone(T o) throws Exception { |
281 |
> |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
282 |
> |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
283 |
> |
oos.writeObject(o); |
284 |
> |
oos.flush(); |
285 |
> |
oos.close(); |
286 |
> |
ByteArrayInputStream bin = |
287 |
> |
new ByteArrayInputStream(bos.toByteArray()); |
288 |
> |
ObjectInputStream ois = new ObjectInputStream(bin); |
289 |
|
return (T) ois.readObject(); |
283 |
– |
} catch (Exception e) { |
284 |
– |
throw new IllegalArgumentException(e); |
285 |
– |
} |
290 |
|
} |
291 |
|
|
292 |
|
private static void testRemove(Deque<Integer> deque) throws Exception { |
293 |
< |
Deque<Integer> copy = null; |
294 |
< |
switch (random() & 1) { |
295 |
< |
case 0: |
292 |
< |
copy = (Deque<Integer>) deque.getClass(). |
293 |
< |
getConstructor(Collection.class).newInstance(deque); |
294 |
< |
break; |
295 |
< |
case 1: |
296 |
< |
copy = deepCopy(deque); |
297 |
< |
break; |
298 |
< |
default: |
299 |
< |
throw new Exception("How'd we get here"); |
300 |
< |
} |
293 |
> |
Deque<Integer> copy = ((random() & 1) == 0) ? |
294 |
> |
copyConstructorClone(deque) : |
295 |
> |
serialClone(deque); |
296 |
|
|
297 |
|
int numRemoved = 0; |
298 |
|
for (Iterator<Integer> it = copy.iterator(); it.hasNext(); ) { |
330 |
|
|
331 |
|
testEmpty(copy); |
332 |
|
|
333 |
< |
copy = (Deque<Integer>) deque.getClass(). |
339 |
< |
getConstructor(Collection.class).newInstance(deque); |
333 |
> |
copy = copyConstructorClone(deque); |
334 |
|
copy.retainAll(deque); |
335 |
|
testEqual(deque, copy); |
336 |
|
copy.removeAll(deque); |