12 |
|
import java.util.NoSuchElementException; |
13 |
|
import java.util.Queue; |
14 |
|
import java.util.Random; |
15 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
|
17 |
|
import junit.framework.Test; |
18 |
|
import junit.framework.TestSuite; |
23 |
|
} |
24 |
|
|
25 |
|
public static Test suite() { |
26 |
< |
return new TestSuite(ArrayDequeTest.class); |
26 |
> |
class Implementation implements CollectionImplementation { |
27 |
> |
public Class<?> klazz() { return ArrayDeque.class; } |
28 |
> |
public Collection emptyCollection() { return new ArrayDeque(); } |
29 |
> |
public Object makeElement(int i) { return i; } |
30 |
> |
public boolean isConcurrent() { return false; } |
31 |
> |
public boolean permitsNulls() { return false; } |
32 |
> |
} |
33 |
> |
return newTestSuite(ArrayDequeTest.class, |
34 |
> |
CollectionTest.testSuite(new Implementation())); |
35 |
|
} |
36 |
|
|
37 |
|
/** |
38 |
|
* Returns a new deque of given size containing consecutive |
39 |
< |
* Integers 0 ... n. |
39 |
> |
* Integers 0 ... n - 1. |
40 |
|
*/ |
41 |
|
private ArrayDeque<Integer> populatedDeque(int n) { |
42 |
< |
ArrayDeque<Integer> q = new ArrayDeque<Integer>(); |
42 |
> |
// Randomize various aspects of memory layout, including |
43 |
> |
// filled-to-capacity and wraparound. |
44 |
> |
final ArrayDeque<Integer> q; |
45 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
46 |
> |
switch (rnd.nextInt(6)) { |
47 |
> |
case 0: q = new ArrayDeque<Integer>(); break; |
48 |
> |
case 1: q = new ArrayDeque<Integer>(0); break; |
49 |
> |
case 2: q = new ArrayDeque<Integer>(1); break; |
50 |
> |
case 3: q = new ArrayDeque<Integer>(n - 1); break; |
51 |
> |
case 4: q = new ArrayDeque<Integer>(n); break; |
52 |
> |
case 5: q = new ArrayDeque<Integer>(n + 1); break; |
53 |
> |
default: throw new AssertionError(); |
54 |
> |
} |
55 |
> |
switch (rnd.nextInt(3)) { |
56 |
> |
case 0: |
57 |
> |
q.addFirst(42); |
58 |
> |
assertEquals((Integer) 42, q.removeLast()); |
59 |
> |
break; |
60 |
> |
case 1: |
61 |
> |
q.addLast(42); |
62 |
> |
assertEquals((Integer) 42, q.removeLast()); |
63 |
> |
break; |
64 |
> |
case 2: /* do nothing */ break; |
65 |
> |
default: throw new AssertionError(); |
66 |
> |
} |
67 |
|
assertTrue(q.isEmpty()); |
68 |
< |
for (int i = 0; i < n; ++i) |
69 |
< |
assertTrue(q.offerLast(new Integer(i))); |
68 |
> |
if (rnd.nextBoolean()) |
69 |
> |
for (int i = 0; i < n; i++) |
70 |
> |
assertTrue(q.offerLast((Integer) i)); |
71 |
> |
else |
72 |
> |
for (int i = n; --i >= 0; ) |
73 |
> |
q.addFirst((Integer) i); |
74 |
|
assertFalse(q.isEmpty()); |
75 |
|
assertEquals(n, q.size()); |
76 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
77 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
78 |
|
return q; |
79 |
|
} |
80 |
|
|
569 |
|
*/ |
570 |
|
public void testRemoveFirstOccurrence() { |
571 |
|
ArrayDeque q = populatedDeque(SIZE); |
572 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
573 |
|
for (int i = 1; i < SIZE; i += 2) { |
574 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
575 |
|
} |
578 |
|
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
579 |
|
} |
580 |
|
assertTrue(q.isEmpty()); |
581 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
582 |
+ |
assertFalse(q.removeFirstOccurrence(42)); |
583 |
+ |
q = new ArrayDeque(); |
584 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
585 |
+ |
assertFalse(q.removeFirstOccurrence(42)); |
586 |
|
} |
587 |
|
|
588 |
|
/** |
590 |
|
*/ |
591 |
|
public void testRemoveLastOccurrence() { |
592 |
|
ArrayDeque q = populatedDeque(SIZE); |
593 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
594 |
|
for (int i = 1; i < SIZE; i += 2) { |
595 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
596 |
|
} |
599 |
|
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
600 |
|
} |
601 |
|
assertTrue(q.isEmpty()); |
602 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
603 |
+ |
assertFalse(q.removeLastOccurrence(42)); |
604 |
+ |
q = new ArrayDeque(); |
605 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
606 |
+ |
assertFalse(q.removeLastOccurrence(42)); |
607 |
|
} |
608 |
|
|
609 |
|
/** |