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.*; |
6 |
|
import java.io.*; |
7 |
+ |
import java.util.*; |
8 |
|
|
9 |
|
/** |
10 |
|
* Interface-based Deque tester. This test currently makes three |
22 |
|
static int nextHead = -1; |
23 |
|
static int size() { return nextTail - nextHead - 1; } |
24 |
|
|
25 |
– |
|
25 |
|
static int random(int bound) { |
26 |
|
int x = seed; |
27 |
|
int t = (x % 127773) * 16807 - (x / 127773) * 2836; |
28 |
< |
seed = (t > 0)? t : t + 0x7fffffff; |
28 |
> |
seed = (t > 0) ? t : t + 0x7fffffff; |
29 |
|
return (t & 0x7fffffff) % bound; |
30 |
|
} |
31 |
|
|
32 |
|
static int random() { |
33 |
|
int x = seed; |
34 |
|
int t = (x % 127773) * 16807 - (x / 127773) * 2836; |
35 |
< |
seed = (t > 0)? t : t + 0x7fffffff; |
35 |
> |
seed = (t > 0) ? t : t + 0x7fffffff; |
36 |
|
return (t & 0x7fffffff); |
37 |
|
} |
38 |
|
|
39 |
< |
public static void main(String args[]) throws Exception { |
40 |
< |
Class cls = Class.forName(args[0]); |
39 |
> |
public static void main(String[] args) throws Exception { |
40 |
> |
Class<?> cls = Class.forName(args[0]); |
41 |
|
int n = 1000000; |
42 |
|
|
43 |
|
for (int j = 0; j < 3; ++j) { |
44 |
+ |
@SuppressWarnings("unchecked") |
45 |
|
Deque<Integer> deque = (Deque<Integer>) cls.newInstance(); |
46 |
|
nextTail = 0; |
47 |
|
nextHead = -1; |
48 |
< |
long start = System.currentTimeMillis(); |
48 |
> |
long start = System.nanoTime(); |
49 |
|
mainTest(deque, n); |
50 |
< |
long end = System.currentTimeMillis(); |
51 |
< |
System.out.println("Time: " + (end - start) + "ms"); |
50 |
> |
long end = System.nanoTime(); |
51 |
> |
long elapsedTimeMillis = (end - start) / (1000L * 1000L); |
52 |
> |
System.out.printf("Time: %d ms%n", elapsedTimeMillis); |
53 |
|
if (deque.isEmpty()) System.out.print(" "); |
54 |
|
} |
54 |
– |
|
55 |
|
} |
56 |
|
|
57 |
|
static void mainTest(Deque<Integer> deque, int n) throws Exception { |
71 |
|
|
72 |
|
// Test serialization and copying |
73 |
|
if ((i & 4095) == 0) { |
74 |
< |
testEqual(deque, deepCopy(deque)); |
75 |
< |
testEqual(deque, (Deque<Integer>) deque.getClass(). |
76 |
< |
getConstructor(Collection.class).newInstance(deque)); |
74 |
> |
testEqual(deque, serialClone(deque)); |
75 |
> |
testEqual(deque, copyConstructorClone(deque)); |
76 |
|
} |
77 |
|
|
78 |
|
// Test fancy removal stuff once in a blue moon |
79 |
|
if ((i & 8191) == 0) |
80 |
|
testRemove(deque); |
81 |
< |
|
83 |
< |
} |
81 |
> |
} |
82 |
|
|
83 |
|
// Stupid tests for clear, toString |
84 |
|
deque.clear(); |
151 |
|
deque.getLast() + " expecting " + (nextTail - 1)); |
152 |
|
} |
153 |
|
|
156 |
– |
|
154 |
|
static void randomOp(Deque<Integer> deque) throws Exception { |
155 |
|
|
156 |
|
// Perform a random operation |
157 |
< |
switch(random() & 3) { |
157 |
> |
switch (random() & 3) { |
158 |
|
case 0: |
159 |
< |
switch(random() & 3) { |
159 |
> |
switch (random() & 3) { |
160 |
|
case 0: deque.addLast(nextTail++); break; |
161 |
|
case 1: deque.offerLast(nextTail++); break; |
162 |
|
case 2: deque.offer(nextTail++); break; |
169 |
|
int result = 666; |
170 |
|
boolean threw = false; |
171 |
|
try { |
172 |
< |
switch(random(3)) { |
172 |
> |
switch (random(3)) { |
173 |
|
case 0: result = deque.removeFirst(); break; |
174 |
|
case 1: result = deque.remove(); break; |
175 |
|
case 2: result = deque.pop(); break; |
176 |
|
default: throw new Exception("How'd we get here"); |
177 |
|
} |
178 |
< |
} catch(NoSuchElementException e) { |
178 |
> |
} catch (NoSuchElementException e) { |
179 |
|
threw = true; |
180 |
|
} |
181 |
|
if (!threw) |
182 |
|
throw new Exception("Remove-no exception: " + result); |
183 |
|
} else { // deque nonempty |
184 |
|
int result = -1; |
185 |
< |
switch(random(5)) { |
185 |
> |
switch (random(5)) { |
186 |
|
case 0: result = deque.removeFirst(); break; |
187 |
|
case 1: result = deque.remove(); break; |
188 |
|
case 2: result = deque.pop(); break; |
196 |
|
} |
197 |
|
break; |
198 |
|
case 2: |
199 |
< |
switch(random(3)) { |
199 |
> |
switch (random(3)) { |
200 |
|
case 0: deque.addFirst(nextHead--); break; |
201 |
|
case 1: deque.offerFirst(nextHead--); break; |
202 |
|
case 2: deque.push(nextHead--); break; |
209 |
|
boolean threw = false; |
210 |
|
try { |
211 |
|
result = deque.removeLast(); |
212 |
< |
} catch(NoSuchElementException e) { |
212 |
> |
} catch (NoSuchElementException e) { |
213 |
|
threw = true; |
214 |
|
} |
215 |
|
if (!threw) |
216 |
|
throw new Exception("Remove-no exception: " + result); |
217 |
|
} else { // deque nonempty |
218 |
< |
int result = ((random() & 1) == 0? |
218 |
> |
int result = ((random() & 1) == 0 ? |
219 |
|
deque.removeLast() : deque.pollLast()); |
220 |
|
if (result != --nextTail) |
221 |
|
throw new Exception( |
222 |
< |
"Removed "+ result + " expecting "+(nextTail + 1)); |
222 |
> |
"Removed "+ result + " expecting "+(nextTail + 1)); |
223 |
|
} |
224 |
|
break; |
225 |
|
default: |
227 |
|
} |
228 |
|
} |
229 |
|
|
233 |
– |
|
230 |
|
private static void testEqual(Deque<Integer> d1, Deque<Integer> d2) |
231 |
|
throws Exception |
232 |
|
{ |
233 |
|
if (d1.size() != d2.size()) |
234 |
|
throw new Exception("Size " + d1.size() + " != " + d2.size()); |
235 |
|
Iterator<Integer> it = d2.iterator(); |
236 |
< |
for(int i : d1) { |
236 |
> |
for (int i : d1) { |
237 |
|
int j = it.next(); |
238 |
|
if (j != i) |
239 |
|
throw new Exception("Element " + j + " != " + i); |
240 |
|
} |
241 |
|
|
242 |
< |
for(int i : d1) |
242 |
> |
for (int i : d1) |
243 |
|
if (!d2.contains(i)) |
244 |
|
throw new Exception("d2 doesn't contain " + i); |
245 |
< |
for(int i : d2) |
245 |
> |
for (int i : d2) |
246 |
|
if (!d1.contains(i)) |
247 |
|
throw new Exception("d2 doesn't contain " + i); |
248 |
|
|
266 |
|
throw new Exception("d2 contains all of {Integer.MIN_VALUE }"); |
267 |
|
} |
268 |
|
|
269 |
< |
private static <T> T deepCopy(T o) { |
270 |
< |
try { |
271 |
< |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
272 |
< |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
273 |
< |
oos.writeObject(o); |
274 |
< |
oos.flush(); |
275 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream( |
276 |
< |
bos.toByteArray()); |
277 |
< |
ObjectInputStream ois = new ObjectInputStream(bin); |
269 |
> |
@SuppressWarnings("unchecked") |
270 |
> |
private static <T> T copyConstructorClone(T o) throws Exception { |
271 |
> |
return (T) o.getClass().getConstructor(Collection.class).newInstance(o); |
272 |
> |
} |
273 |
> |
|
274 |
> |
@SuppressWarnings("unchecked") |
275 |
> |
private static <T> T serialClone(T o) throws Exception { |
276 |
> |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
277 |
> |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
278 |
> |
oos.writeObject(o); |
279 |
> |
oos.flush(); |
280 |
> |
oos.close(); |
281 |
> |
ByteArrayInputStream bin = |
282 |
> |
new ByteArrayInputStream(bos.toByteArray()); |
283 |
> |
ObjectInputStream ois = new ObjectInputStream(bin); |
284 |
|
return (T) ois.readObject(); |
283 |
– |
} catch(Exception e) { |
284 |
– |
throw new IllegalArgumentException(e); |
285 |
– |
} |
285 |
|
} |
286 |
|
|
287 |
|
private static void testRemove(Deque<Integer> deque) throws Exception { |
288 |
< |
Deque<Integer> copy = null; |
289 |
< |
switch(random() & 1) { |
290 |
< |
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 |
< |
} |
288 |
> |
Deque<Integer> copy = ((random() & 1) == 0) ? |
289 |
> |
copyConstructorClone(deque) : |
290 |
> |
serialClone(deque); |
291 |
|
|
292 |
|
int numRemoved = 0; |
293 |
|
for (Iterator<Integer> it = copy.iterator(); it.hasNext(); ) { |
310 |
|
throw new Exception(e + " missing."); |
311 |
|
|
312 |
|
boolean removed = false; |
313 |
< |
switch(random(3)) { |
313 |
> |
switch (random(3)) { |
314 |
|
case 0: removed = copy.remove(e); break; |
315 |
|
case 1: removed = copy.removeFirstOccurrence(e); break; |
316 |
|
case 2: removed = copy.removeLastOccurrence(e); break; |
325 |
|
|
326 |
|
testEmpty(copy); |
327 |
|
|
328 |
< |
copy = (Deque<Integer>) deque.getClass(). |
339 |
< |
getConstructor(Collection.class).newInstance(deque); |
328 |
> |
copy = copyConstructorClone(deque); |
329 |
|
copy.retainAll(deque); |
330 |
|
testEqual(deque, copy); |
331 |
|
copy.removeAll(deque); |
357 |
|
boolean threw = false; |
358 |
|
int result = 666; |
359 |
|
try { |
360 |
< |
result = ((random() & 1) == 0? |
360 |
> |
result = ((random() & 1) == 0 ? |
361 |
|
deque.getFirst() : deque.element()); |
362 |
< |
} catch(NoSuchElementException e) { |
362 |
> |
} catch (NoSuchElementException e) { |
363 |
|
threw = true; |
364 |
|
} |
365 |
|
if (!threw) |
367 |
|
threw = false; |
368 |
|
try { |
369 |
|
result = deque.getLast(); |
370 |
< |
} catch(NoSuchElementException e) { |
370 |
> |
} catch (NoSuchElementException e) { |
371 |
|
threw = true; |
372 |
|
} |
373 |
|
if (!threw) |