1 |
|
/* |
2 |
< |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain, as explained at |
2 |
> |
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
> |
* members of JCP JSR-166 Expert Group and released to the public |
4 |
> |
* domain, as explained at |
5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
|
import java.util.ArrayDeque; |
9 |
|
import java.util.Arrays; |
10 |
|
import java.util.Collection; |
11 |
+ |
import java.util.Collections; |
12 |
|
import java.util.Deque; |
13 |
|
import java.util.Iterator; |
14 |
|
import java.util.NoSuchElementException; |
15 |
|
import java.util.Queue; |
16 |
|
import java.util.Random; |
17 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
18 |
|
|
19 |
|
import junit.framework.Test; |
20 |
|
import junit.framework.TestSuite; |
27 |
|
public static Test suite() { |
28 |
|
class Implementation implements CollectionImplementation { |
29 |
|
public Class<?> klazz() { return ArrayDeque.class; } |
30 |
< |
public Collection emptyCollection() { return new ArrayDeque(); } |
30 |
> |
public Collection emptyCollection() { return populatedDeque(0); } |
31 |
|
public Object makeElement(int i) { return i; } |
32 |
|
public boolean isConcurrent() { return false; } |
33 |
|
public boolean permitsNulls() { return false; } |
38 |
|
|
39 |
|
/** |
40 |
|
* Returns a new deque of given size containing consecutive |
41 |
< |
* Integers 0 ... n. |
41 |
> |
* Integers 0 ... n - 1. |
42 |
|
*/ |
43 |
< |
private ArrayDeque<Integer> populatedDeque(int n) { |
44 |
< |
ArrayDeque<Integer> q = new ArrayDeque<Integer>(); |
43 |
> |
private static ArrayDeque<Integer> populatedDeque(int n) { |
44 |
> |
// Randomize various aspects of memory layout, including |
45 |
> |
// filled-to-capacity and wraparound. |
46 |
> |
final ArrayDeque<Integer> q; |
47 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
48 |
> |
switch (rnd.nextInt(6)) { |
49 |
> |
case 0: q = new ArrayDeque<Integer>(); break; |
50 |
> |
case 1: q = new ArrayDeque<Integer>(0); break; |
51 |
> |
case 2: q = new ArrayDeque<Integer>(1); break; |
52 |
> |
case 3: q = new ArrayDeque<Integer>(Math.max(0, n - 1)); break; |
53 |
> |
case 4: q = new ArrayDeque<Integer>(n); break; |
54 |
> |
case 5: q = new ArrayDeque<Integer>(n + 1); break; |
55 |
> |
default: throw new AssertionError(); |
56 |
> |
} |
57 |
> |
switch (rnd.nextInt(3)) { |
58 |
> |
case 0: |
59 |
> |
q.addFirst(42); |
60 |
> |
assertEquals((Integer) 42, q.removeLast()); |
61 |
> |
break; |
62 |
> |
case 1: |
63 |
> |
q.addLast(42); |
64 |
> |
assertEquals((Integer) 42, q.removeFirst()); |
65 |
> |
break; |
66 |
> |
case 2: /* do nothing */ break; |
67 |
> |
default: throw new AssertionError(); |
68 |
> |
} |
69 |
|
assertTrue(q.isEmpty()); |
70 |
< |
for (int i = 0; i < n; ++i) |
71 |
< |
assertTrue(q.offerLast(new Integer(i))); |
72 |
< |
assertFalse(q.isEmpty()); |
70 |
> |
if (rnd.nextBoolean()) |
71 |
> |
for (int i = 0; i < n; i++) |
72 |
> |
assertTrue(q.offerLast((Integer) i)); |
73 |
> |
else |
74 |
> |
for (int i = n; --i >= 0; ) |
75 |
> |
q.addFirst((Integer) i); |
76 |
|
assertEquals(n, q.size()); |
77 |
+ |
if (n > 0) { |
78 |
+ |
assertFalse(q.isEmpty()); |
79 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
80 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
81 |
+ |
} |
82 |
|
return q; |
83 |
|
} |
84 |
|
|
944 |
|
|
945 |
|
assertNotSame(y, x); |
946 |
|
assertEquals(x.size(), y.size()); |
947 |
+ |
assertEquals(x.toString(), y.toString()); |
948 |
+ |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
949 |
+ |
while (!x.isEmpty()) { |
950 |
+ |
assertFalse(y.isEmpty()); |
951 |
+ |
assertEquals(x.remove(), y.remove()); |
952 |
+ |
} |
953 |
+ |
assertTrue(y.isEmpty()); |
954 |
+ |
} |
955 |
+ |
|
956 |
+ |
/** |
957 |
+ |
* A cloned deque has same elements in same order |
958 |
+ |
*/ |
959 |
+ |
public void testClone() throws Exception { |
960 |
+ |
ArrayDeque<Integer> x = populatedDeque(SIZE); |
961 |
+ |
ArrayDeque<Integer> y = x.clone(); |
962 |
+ |
|
963 |
+ |
assertNotSame(y, x); |
964 |
+ |
assertEquals(x.size(), y.size()); |
965 |
|
assertEquals(x.toString(), y.toString()); |
966 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
967 |
|
while (!x.isEmpty()) { |