ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingDequeTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedBlockingDequeTest.java (file contents):
Revision 1.64 by jsr166, Sat Aug 6 17:02:49 2016 UTC vs.
Revision 1.88 by dl, Tue Jan 26 13:33:06 2021 UTC

# Line 41 | Line 41 | public class LinkedBlockingDequeTest ext
41      }
42  
43      public static Test suite() {
44 +        class Implementation implements CollectionImplementation {
45 +            public Class<?> klazz() { return LinkedBlockingDeque.class; }
46 +            public Collection emptyCollection() { return new LinkedBlockingDeque(); }
47 +            public Object makeElement(int i) { return JSR166TestCase.itemFor(i); }
48 +            public boolean isConcurrent() { return true; }
49 +            public boolean permitsNulls() { return false; }
50 +        }
51          return newTestSuite(LinkedBlockingDequeTest.class,
52                              new Unbounded().testSuite(),
53 <                            new Bounded().testSuite());
53 >                            new Bounded().testSuite(),
54 >                            CollectionTest.testSuite(new Implementation()));
55      }
56  
57      /**
58       * Returns a new deque of given size containing consecutive
59 <     * Integers 0 ... n.
59 >     * Items 0 ... n - 1.
60       */
61 <    private LinkedBlockingDeque<Integer> populatedDeque(int n) {
62 <        LinkedBlockingDeque<Integer> q =
63 <            new LinkedBlockingDeque<Integer>(n);
61 >    private static LinkedBlockingDeque<Item> populatedDeque(int n) {
62 >        LinkedBlockingDeque<Item> q =
63 >            new LinkedBlockingDeque<Item>(n);
64          assertTrue(q.isEmpty());
65          for (int i = 0; i < n; i++)
66 <            assertTrue(q.offer(new Integer(i)));
66 >            mustOffer(q, i);
67          assertFalse(q.isEmpty());
68 <        assertEquals(0, q.remainingCapacity());
69 <        assertEquals(n, q.size());
68 >        mustEqual(0, q.remainingCapacity());
69 >        mustEqual(n, q.size());
70 >        mustEqual(0, q.peekFirst());
71 >        mustEqual((n - 1), q.peekLast());
72          return q;
73      }
74  
# Line 66 | Line 76 | public class LinkedBlockingDequeTest ext
76       * isEmpty is true before add, false after
77       */
78      public void testEmpty() {
79 <        LinkedBlockingDeque q = new LinkedBlockingDeque();
79 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
80          assertTrue(q.isEmpty());
81 <        q.add(new Integer(1));
81 >        q.add(one);
82          assertFalse(q.isEmpty());
83 <        q.add(new Integer(2));
83 >        q.add(two);
84          q.removeFirst();
85          q.removeFirst();
86          assertTrue(q.isEmpty());
# Line 80 | Line 90 | public class LinkedBlockingDequeTest ext
90       * size changes when elements added and removed
91       */
92      public void testSize() {
93 <        LinkedBlockingDeque q = populatedDeque(SIZE);
93 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
94          for (int i = 0; i < SIZE; ++i) {
95 <            assertEquals(SIZE - i, q.size());
95 >            mustEqual(SIZE - i, q.size());
96              q.removeFirst();
97          }
98          for (int i = 0; i < SIZE; ++i) {
99 <            assertEquals(i, q.size());
100 <            q.add(new Integer(i));
99 >            mustEqual(i, q.size());
100 >            mustAdd(q, one);
101          }
102      }
103  
# Line 95 | Line 105 | public class LinkedBlockingDequeTest ext
105       * offerFirst(null) throws NullPointerException
106       */
107      public void testOfferFirstNull() {
108 <        LinkedBlockingDeque q = new LinkedBlockingDeque();
108 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
109          try {
110              q.offerFirst(null);
111              shouldThrow();
# Line 106 | Line 116 | public class LinkedBlockingDequeTest ext
116       * offerLast(null) throws NullPointerException
117       */
118      public void testOfferLastNull() {
119 <        LinkedBlockingDeque q = new LinkedBlockingDeque();
119 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
120          try {
121              q.offerLast(null);
122              shouldThrow();
# Line 117 | Line 127 | public class LinkedBlockingDequeTest ext
127       * OfferFirst succeeds
128       */
129      public void testOfferFirst() {
130 <        LinkedBlockingDeque q = new LinkedBlockingDeque();
131 <        assertTrue(q.offerFirst(new Integer(0)));
132 <        assertTrue(q.offerFirst(new Integer(1)));
130 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
131 >        assertTrue(q.offerFirst(zero));
132 >        assertTrue(q.offerFirst(two));
133      }
134  
135      /**
136       * OfferLast succeeds
137       */
138      public void testOfferLast() {
139 <        LinkedBlockingDeque q = new LinkedBlockingDeque();
140 <        assertTrue(q.offerLast(new Integer(0)));
141 <        assertTrue(q.offerLast(new Integer(1)));
139 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
140 >        assertTrue(q.offerLast(zero));
141 >        assertTrue(q.offerLast(one));
142      }
143  
144      /**
145       * pollFirst succeeds unless empty
146       */
147      public void testPollFirst() {
148 <        LinkedBlockingDeque q = populatedDeque(SIZE);
148 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
149          for (int i = 0; i < SIZE; ++i) {
150 <            assertEquals(i, q.pollFirst());
150 >            mustEqual(i, q.pollFirst());
151          }
152          assertNull(q.pollFirst());
153      }
# Line 146 | Line 156 | public class LinkedBlockingDequeTest ext
156       * pollLast succeeds unless empty
157       */
158      public void testPollLast() {
159 <        LinkedBlockingDeque q = populatedDeque(SIZE);
159 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
160          for (int i = SIZE - 1; i >= 0; --i) {
161 <            assertEquals(i, q.pollLast());
161 >            mustEqual(i, q.pollLast());
162          }
163          assertNull(q.pollLast());
164      }
# Line 157 | Line 167 | public class LinkedBlockingDequeTest ext
167       * peekFirst returns next element, or null if empty
168       */
169      public void testPeekFirst() {
170 <        LinkedBlockingDeque q = populatedDeque(SIZE);
170 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
171          for (int i = 0; i < SIZE; ++i) {
172 <            assertEquals(i, q.peekFirst());
173 <            assertEquals(i, q.pollFirst());
172 >            mustEqual(i, q.peekFirst());
173 >            mustEqual(i, q.pollFirst());
174              assertTrue(q.peekFirst() == null ||
175                         !q.peekFirst().equals(i));
176          }
# Line 171 | Line 181 | public class LinkedBlockingDequeTest ext
181       * peek returns next element, or null if empty
182       */
183      public void testPeek() {
184 <        LinkedBlockingDeque q = populatedDeque(SIZE);
184 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
185          for (int i = 0; i < SIZE; ++i) {
186 <            assertEquals(i, q.peek());
187 <            assertEquals(i, q.pollFirst());
186 >            mustEqual(i, q.peek());
187 >            mustEqual(i, q.pollFirst());
188              assertTrue(q.peek() == null ||
189                         !q.peek().equals(i));
190          }
# Line 185 | Line 195 | public class LinkedBlockingDequeTest ext
195       * peekLast returns next element, or null if empty
196       */
197      public void testPeekLast() {
198 <        LinkedBlockingDeque q = populatedDeque(SIZE);
198 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
199          for (int i = SIZE - 1; i >= 0; --i) {
200 <            assertEquals(i, q.peekLast());
201 <            assertEquals(i, q.pollLast());
200 >            mustEqual(i, q.peekLast());
201 >            mustEqual(i, q.pollLast());
202              assertTrue(q.peekLast() == null ||
203                         !q.peekLast().equals(i));
204          }
# Line 199 | Line 209 | public class LinkedBlockingDequeTest ext
209       * getFirst() returns first element, or throws NSEE if empty
210       */
211      public void testFirstElement() {
212 <        LinkedBlockingDeque q = populatedDeque(SIZE);
212 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
213          for (int i = 0; i < SIZE; ++i) {
214 <            assertEquals(i, q.getFirst());
215 <            assertEquals(i, q.pollFirst());
214 >            mustEqual(i, q.getFirst());
215 >            mustEqual(i, q.pollFirst());
216          }
217          try {
218              q.getFirst();
# Line 215 | Line 225 | public class LinkedBlockingDequeTest ext
225       * getLast() returns last element, or throws NSEE if empty
226       */
227      public void testLastElement() {
228 <        LinkedBlockingDeque q = populatedDeque(SIZE);
228 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
229          for (int i = SIZE - 1; i >= 0; --i) {
230 <            assertEquals(i, q.getLast());
231 <            assertEquals(i, q.pollLast());
230 >            mustEqual(i, q.getLast());
231 >            mustEqual(i, q.pollLast());
232          }
233          try {
234              q.getLast();
# Line 231 | Line 241 | public class LinkedBlockingDequeTest ext
241       * removeFirst() removes first element, or throws NSEE if empty
242       */
243      public void testRemoveFirst() {
244 <        LinkedBlockingDeque q = populatedDeque(SIZE);
244 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
245          for (int i = 0; i < SIZE; ++i) {
246 <            assertEquals(i, q.removeFirst());
246 >            mustEqual(i, q.removeFirst());
247          }
248          try {
249              q.removeFirst();
# Line 246 | Line 256 | public class LinkedBlockingDequeTest ext
256       * removeLast() removes last element, or throws NSEE if empty
257       */
258      public void testRemoveLast() {
259 <        LinkedBlockingDeque q = populatedDeque(SIZE);
259 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
260          for (int i = SIZE - 1; i >= 0; --i) {
261 <            assertEquals(i, q.removeLast());
261 >            mustEqual(i, q.removeLast());
262          }
263          try {
264              q.removeLast();
# Line 261 | Line 271 | public class LinkedBlockingDequeTest ext
271       * remove removes next element, or throws NSEE if empty
272       */
273      public void testRemove() {
274 <        LinkedBlockingDeque q = populatedDeque(SIZE);
274 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
275          for (int i = 0; i < SIZE; ++i) {
276 <            assertEquals(i, q.remove());
276 >            mustEqual(i, q.remove());
277          }
278          try {
279              q.remove();
# Line 275 | Line 285 | public class LinkedBlockingDequeTest ext
285       * removeFirstOccurrence(x) removes x and returns true if present
286       */
287      public void testRemoveFirstOccurrence() {
288 <        LinkedBlockingDeque q = populatedDeque(SIZE);
288 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
289          for (int i = 1; i < SIZE; i += 2) {
290 <            assertTrue(q.removeFirstOccurrence(new Integer(i)));
290 >            assertTrue(q.removeFirstOccurrence(itemFor(i)));
291          }
292          for (int i = 0; i < SIZE; i += 2) {
293 <            assertTrue(q.removeFirstOccurrence(new Integer(i)));
294 <            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
293 >            assertTrue(q.removeFirstOccurrence(itemFor(i)));
294 >            assertFalse(q.removeFirstOccurrence(itemFor(i + 1)));
295          }
296          assertTrue(q.isEmpty());
297      }
# Line 290 | Line 300 | public class LinkedBlockingDequeTest ext
300       * removeLastOccurrence(x) removes x and returns true if present
301       */
302      public void testRemoveLastOccurrence() {
303 <        LinkedBlockingDeque q = populatedDeque(SIZE);
303 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
304          for (int i = 1; i < SIZE; i += 2) {
305 <            assertTrue(q.removeLastOccurrence(new Integer(i)));
305 >            assertTrue(q.removeLastOccurrence(itemFor(i)));
306          }
307          for (int i = 0; i < SIZE; i += 2) {
308 <            assertTrue(q.removeLastOccurrence(new Integer(i)));
309 <            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
308 >            assertTrue(q.removeLastOccurrence(itemFor(i)));
309 >            assertFalse(q.removeLastOccurrence(itemFor(i + 1)));
310          }
311          assertTrue(q.isEmpty());
312      }
# Line 305 | Line 315 | public class LinkedBlockingDequeTest ext
315       * peekFirst returns element inserted with addFirst
316       */
317      public void testAddFirst() {
318 <        LinkedBlockingDeque q = populatedDeque(3);
318 >        LinkedBlockingDeque<Item> q = populatedDeque(3);
319          q.pollLast();
320          q.addFirst(four);
321          assertSame(four, q.peekFirst());
# Line 315 | Line 325 | public class LinkedBlockingDequeTest ext
325       * peekLast returns element inserted with addLast
326       */
327      public void testAddLast() {
328 <        LinkedBlockingDeque q = populatedDeque(3);
328 >        LinkedBlockingDeque<Item> q = populatedDeque(3);
329          q.pollLast();
330          q.addLast(four);
331          assertSame(four, q.peekLast());
# Line 326 | Line 336 | public class LinkedBlockingDequeTest ext
336       * none given
337       */
338      public void testConstructor1() {
339 <        assertEquals(SIZE, new LinkedBlockingDeque(SIZE).remainingCapacity());
340 <        assertEquals(Integer.MAX_VALUE, new LinkedBlockingDeque().remainingCapacity());
339 >        mustEqual(SIZE, new LinkedBlockingDeque<Item>(SIZE).remainingCapacity());
340 >        mustEqual(Integer.MAX_VALUE, new LinkedBlockingDeque<Item>().remainingCapacity());
341      }
342  
343      /**
# Line 335 | Line 345 | public class LinkedBlockingDequeTest ext
345       */
346      public void testConstructor2() {
347          try {
348 <            new LinkedBlockingDeque(0);
348 >            new LinkedBlockingDeque<Item>(0);
349              shouldThrow();
350          } catch (IllegalArgumentException success) {}
351      }
# Line 345 | Line 355 | public class LinkedBlockingDequeTest ext
355       */
356      public void testConstructor3() {
357          try {
358 <            new LinkedBlockingDeque(null);
358 >            new LinkedBlockingDeque<Item>(null);
359              shouldThrow();
360          } catch (NullPointerException success) {}
361      }
# Line 354 | Line 364 | public class LinkedBlockingDequeTest ext
364       * Initializing from Collection of null elements throws NullPointerException
365       */
366      public void testConstructor4() {
367 <        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
367 >        Collection<Item> elements = Arrays.asList(new Item[SIZE]);
368          try {
369 <            new LinkedBlockingDeque(elements);
369 >            new LinkedBlockingDeque<Item>(elements);
370              shouldThrow();
371          } catch (NullPointerException success) {}
372      }
# Line 366 | Line 376 | public class LinkedBlockingDequeTest ext
376       * NullPointerException
377       */
378      public void testConstructor5() {
379 <        Integer[] ints = new Integer[SIZE];
380 <        for (int i = 0; i < SIZE - 1; ++i)
381 <            ints[i] = i;
372 <        Collection<Integer> elements = Arrays.asList(ints);
379 >        Item[] items = new Item[2];
380 >        items[0] = zero;
381 >        Collection<Item> elements = Arrays.asList(items);
382          try {
383 <            new LinkedBlockingDeque(elements);
383 >            new LinkedBlockingDeque<Item>(elements);
384              shouldThrow();
385          } catch (NullPointerException success) {}
386      }
# Line 380 | Line 389 | public class LinkedBlockingDequeTest ext
389       * Deque contains all elements of collection used to initialize
390       */
391      public void testConstructor6() {
392 <        Integer[] ints = new Integer[SIZE];
392 >        Item[] items = defaultItems;
393 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(Arrays.asList(items));
394          for (int i = 0; i < SIZE; ++i)
395 <            ints[i] = i;
386 <        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
387 <        for (int i = 0; i < SIZE; ++i)
388 <            assertEquals(ints[i], q.poll());
395 >            mustEqual(items[i], q.poll());
396      }
397  
398      /**
399       * Deque transitions from empty to full when elements added
400       */
401      public void testEmptyFull() {
402 <        LinkedBlockingDeque q = new LinkedBlockingDeque(2);
402 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(2);
403          assertTrue(q.isEmpty());
404 <        assertEquals("should have room for 2", 2, q.remainingCapacity());
404 >        mustEqual(2, q.remainingCapacity());
405          q.add(one);
406          assertFalse(q.isEmpty());
407          q.add(two);
408          assertFalse(q.isEmpty());
409 <        assertEquals(0, q.remainingCapacity());
409 >        mustEqual(0, q.remainingCapacity());
410          assertFalse(q.offer(three));
411      }
412  
# Line 407 | Line 414 | public class LinkedBlockingDequeTest ext
414       * remainingCapacity decreases on add, increases on remove
415       */
416      public void testRemainingCapacity() {
417 <        BlockingQueue q = populatedDeque(SIZE);
417 >        BlockingQueue<Item> q = populatedDeque(SIZE);
418          for (int i = 0; i < SIZE; ++i) {
419 <            assertEquals(i, q.remainingCapacity());
420 <            assertEquals(SIZE, q.size() + q.remainingCapacity());
421 <            assertEquals(i, q.remove());
419 >            mustEqual(i, q.remainingCapacity());
420 >            mustEqual(SIZE, q.size() + q.remainingCapacity());
421 >            mustEqual(i, q.remove());
422          }
423          for (int i = 0; i < SIZE; ++i) {
424 <            assertEquals(SIZE - i, q.remainingCapacity());
425 <            assertEquals(SIZE, q.size() + q.remainingCapacity());
426 <            assertTrue(q.add(i));
424 >            mustEqual(SIZE - i, q.remainingCapacity());
425 >            mustEqual(SIZE, q.size() + q.remainingCapacity());
426 >            mustAdd(q, i);
427          }
428      }
429  
# Line 424 | Line 431 | public class LinkedBlockingDequeTest ext
431       * push(null) throws NPE
432       */
433      public void testPushNull() {
434 <        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
434 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(1);
435          try {
436              q.push(null);
437              shouldThrow();
# Line 432 | Line 439 | public class LinkedBlockingDequeTest ext
439      }
440  
441      /**
442 <     * push succeeds if not full; throws ISE if full
442 >     * push succeeds if not full; throws IllegalStateException if full
443       */
444      public void testPush() {
445 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
445 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
446          for (int i = 0; i < SIZE; ++i) {
447 <            Integer x = new Integer(i);
447 >            Item x = itemFor(i);
448              q.push(x);
449 <            assertEquals(x, q.peek());
449 >            mustEqual(x, q.peek());
450          }
451 <        assertEquals(0, q.remainingCapacity());
451 >        mustEqual(0, q.remainingCapacity());
452          try {
453 <            q.push(new Integer(SIZE));
453 >            q.push(itemFor(SIZE));
454              shouldThrow();
455          } catch (IllegalStateException success) {}
456      }
# Line 452 | Line 459 | public class LinkedBlockingDequeTest ext
459       * peekFirst returns element inserted with push
460       */
461      public void testPushWithPeek() {
462 <        LinkedBlockingDeque q = populatedDeque(3);
462 >        LinkedBlockingDeque<Item> q = populatedDeque(3);
463          q.pollLast();
464          q.push(four);
465          assertSame(four, q.peekFirst());
# Line 462 | Line 469 | public class LinkedBlockingDequeTest ext
469       * pop removes next element, or throws NSEE if empty
470       */
471      public void testPop() {
472 <        LinkedBlockingDeque q = populatedDeque(SIZE);
472 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
473          for (int i = 0; i < SIZE; ++i) {
474 <            assertEquals(i, q.pop());
474 >            mustEqual(i, q.pop());
475          }
476          try {
477              q.pop();
# Line 476 | Line 483 | public class LinkedBlockingDequeTest ext
483       * Offer succeeds if not full; fails if full
484       */
485      public void testOffer() {
486 <        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
486 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(1);
487          assertTrue(q.offer(zero));
488          assertFalse(q.offer(one));
489      }
490  
491      /**
492 <     * add succeeds if not full; throws ISE if full
492 >     * add succeeds if not full; throws IllegalStateException if full
493       */
494      public void testAdd() {
495 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
495 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
496          for (int i = 0; i < SIZE; ++i)
497 <            assertTrue(q.add(new Integer(i)));
498 <        assertEquals(0, q.remainingCapacity());
497 >            mustAdd(q, i);
498 >        mustEqual(0, q.remainingCapacity());
499          try {
500 <            q.add(new Integer(SIZE));
500 >            q.add(itemFor(SIZE));
501              shouldThrow();
502          } catch (IllegalStateException success) {}
503      }
504  
505      /**
506 <     * addAll(this) throws IAE
506 >     * addAll(this) throws IllegalArgumentException
507       */
508      public void testAddAllSelf() {
509 <        LinkedBlockingDeque q = populatedDeque(SIZE);
509 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
510          try {
511              q.addAll(q);
512              shouldThrow();
# Line 511 | Line 518 | public class LinkedBlockingDequeTest ext
518       * possibly adding some elements
519       */
520      public void testAddAll3() {
521 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
522 <        Integer[] ints = new Integer[SIZE];
523 <        for (int i = 0; i < SIZE - 1; ++i)
517 <            ints[i] = new Integer(i);
518 <        Collection<Integer> elements = Arrays.asList(ints);
521 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
522 >        Item[] items = new Item[2]; items[0] = zero;
523 >        Collection<Item> elements = Arrays.asList(items);
524          try {
525              q.addAll(elements);
526              shouldThrow();
# Line 526 | Line 531 | public class LinkedBlockingDequeTest ext
531       * addAll throws IllegalStateException if not enough room
532       */
533      public void testAddAll4() {
534 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
535 <        Integer[] ints = new Integer[SIZE];
536 <        for (int i = 0; i < SIZE; ++i)
532 <            ints[i] = new Integer(i);
533 <        Collection<Integer> elements = Arrays.asList(ints);
534 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE - 1);
535 >        Item[] items = defaultItems;
536 >        Collection<Item> elements = Arrays.asList(items);
537          try {
538              q.addAll(elements);
539              shouldThrow();
# Line 541 | Line 544 | public class LinkedBlockingDequeTest ext
544       * Deque contains all elements, in traversal order, of successful addAll
545       */
546      public void testAddAll5() {
547 <        Integer[] empty = new Integer[0];
548 <        Integer[] ints = new Integer[SIZE];
549 <        for (int i = 0; i < SIZE; ++i)
547 <            ints[i] = new Integer(i);
548 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
547 >        Item[] empty = new Item[0];
548 >        Item[] items = defaultItems;
549 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
550          assertFalse(q.addAll(Arrays.asList(empty)));
551 <        assertTrue(q.addAll(Arrays.asList(ints)));
551 >        assertTrue(q.addAll(Arrays.asList(items)));
552          for (int i = 0; i < SIZE; ++i)
553 <            assertEquals(ints[i], q.poll());
553 >            mustEqual(items[i], q.poll());
554      }
555  
556      /**
557       * all elements successfully put are contained
558       */
559      public void testPut() throws InterruptedException {
560 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
560 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
561          for (int i = 0; i < SIZE; ++i) {
562 <            Integer x = new Integer(i);
562 >            Item x = itemFor(i);
563              q.put(x);
564 <            assertTrue(q.contains(x));
564 >            mustContain(q, x);
565          }
566 <        assertEquals(0, q.remainingCapacity());
566 >        mustEqual(0, q.remainingCapacity());
567      }
568  
569      /**
570       * put blocks interruptibly if full
571       */
572      public void testBlockingPut() throws InterruptedException {
573 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
573 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
574          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
575          Thread t = newStartedThread(new CheckedRunnable() {
576              public void realRun() throws InterruptedException {
577                  for (int i = 0; i < SIZE; ++i)
578 <                    q.put(i);
579 <                assertEquals(SIZE, q.size());
580 <                assertEquals(0, q.remainingCapacity());
578 >                    q.put(itemFor(i));
579 >                mustEqual(SIZE, q.size());
580 >                mustEqual(0, q.remainingCapacity());
581  
582                  Thread.currentThread().interrupt();
583                  try {
584 <                    q.put(99);
584 >                    q.put(ninetynine);
585                      shouldThrow();
586                  } catch (InterruptedException success) {}
587                  assertFalse(Thread.interrupted());
588  
589                  pleaseInterrupt.countDown();
590                  try {
591 <                    q.put(99);
591 >                    q.put(ninetynine);
592                      shouldThrow();
593                  } catch (InterruptedException success) {}
594                  assertFalse(Thread.interrupted());
595              }});
596  
597          await(pleaseInterrupt);
598 <        assertThreadStaysAlive(t);
598 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
599          t.interrupt();
600          awaitTermination(t);
601 <        assertEquals(SIZE, q.size());
602 <        assertEquals(0, q.remainingCapacity());
601 >        mustEqual(SIZE, q.size());
602 >        mustEqual(0, q.remainingCapacity());
603      }
604  
605      /**
# Line 606 | Line 607 | public class LinkedBlockingDequeTest ext
607       */
608      public void testPutWithTake() throws InterruptedException {
609          final int capacity = 2;
610 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
610 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(capacity);
611          final CountDownLatch pleaseTake = new CountDownLatch(1);
612          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
613          Thread t = newStartedThread(new CheckedRunnable() {
614              public void realRun() throws InterruptedException {
615                  for (int i = 0; i < capacity; i++)
616 <                    q.put(i);
616 >                    q.put(itemFor(i));
617                  pleaseTake.countDown();
618 <                q.put(86);
618 >                q.put(eightysix);
619 >
620 >                Thread.currentThread().interrupt();
621 >                try {
622 >                    q.put(ninetynine);
623 >                    shouldThrow();
624 >                } catch (InterruptedException success) {}
625 >                assertFalse(Thread.interrupted());
626  
627                  pleaseInterrupt.countDown();
628                  try {
629 <                    q.put(99);
629 >                    q.put(ninetynine);
630                      shouldThrow();
631                  } catch (InterruptedException success) {}
632                  assertFalse(Thread.interrupted());
633              }});
634  
635          await(pleaseTake);
636 <        assertEquals(0, q.remainingCapacity());
637 <        assertEquals(0, q.take());
636 >        mustEqual(0, q.remainingCapacity());
637 >        mustEqual(0, q.take());
638  
639          await(pleaseInterrupt);
640 <        assertThreadStaysAlive(t);
640 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
641          t.interrupt();
642          awaitTermination(t);
643 <        assertEquals(0, q.remainingCapacity());
643 >        mustEqual(0, q.remainingCapacity());
644      }
645  
646      /**
647       * timed offer times out if full and elements not taken
648       */
649 <    public void testTimedOffer() throws InterruptedException {
650 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
649 >    public void testTimedOffer() {
650 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(2);
651          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
652          Thread t = newStartedThread(new CheckedRunnable() {
653              public void realRun() throws InterruptedException {
654 <                q.put(new Object());
655 <                q.put(new Object());
654 >                q.put(zero);
655 >                q.put(one);
656                  long startTime = System.nanoTime();
657 <                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
657 >
658 >                assertFalse(q.offer(two, timeoutMillis(), MILLISECONDS));
659                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
660 +
661 +                Thread.currentThread().interrupt();
662 +                try {
663 +                    q.offer(three, randomTimeout(), randomTimeUnit());
664 +                    shouldThrow();
665 +                } catch (InterruptedException success) {}
666 +                assertFalse(Thread.interrupted());
667 +
668                  pleaseInterrupt.countDown();
669                  try {
670 <                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
670 >                    q.offer(four, LONGER_DELAY_MS, MILLISECONDS);
671                      shouldThrow();
672                  } catch (InterruptedException success) {}
673 +                assertFalse(Thread.interrupted());
674              }});
675  
676          await(pleaseInterrupt);
677 <        assertThreadStaysAlive(t);
677 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
678          t.interrupt();
679          awaitTermination(t);
680      }
# Line 665 | Line 683 | public class LinkedBlockingDequeTest ext
683       * take retrieves elements in FIFO order
684       */
685      public void testTake() throws InterruptedException {
686 <        LinkedBlockingDeque q = populatedDeque(SIZE);
686 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
687          for (int i = 0; i < SIZE; ++i) {
688 <            assertEquals(i, q.take());
688 >            mustEqual(i, q.take());
689          }
690      }
691  
# Line 675 | Line 693 | public class LinkedBlockingDequeTest ext
693       * take removes existing elements until empty, then blocks interruptibly
694       */
695      public void testBlockingTake() throws InterruptedException {
696 <        final LinkedBlockingDeque q = populatedDeque(SIZE);
696 >        final LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
697          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
698          Thread t = newStartedThread(new CheckedRunnable() {
699              public void realRun() throws InterruptedException {
700 <                for (int i = 0; i < SIZE; ++i) {
683 <                    assertEquals(i, q.take());
684 <                }
700 >                for (int i = 0; i < SIZE; i++) mustEqual(i, q.take());
701  
702                  Thread.currentThread().interrupt();
703                  try {
# Line 699 | Line 715 | public class LinkedBlockingDequeTest ext
715              }});
716  
717          await(pleaseInterrupt);
718 <        assertThreadStaysAlive(t);
718 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
719          t.interrupt();
720          awaitTermination(t);
721      }
# Line 708 | Line 724 | public class LinkedBlockingDequeTest ext
724       * poll succeeds unless empty
725       */
726      public void testPoll() {
727 <        LinkedBlockingDeque q = populatedDeque(SIZE);
727 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
728          for (int i = 0; i < SIZE; ++i) {
729 <            assertEquals(i, q.poll());
729 >            mustEqual(i, q.poll());
730          }
731          assertNull(q.poll());
732      }
# Line 719 | Line 735 | public class LinkedBlockingDequeTest ext
735       * timed poll with zero timeout succeeds when non-empty, else times out
736       */
737      public void testTimedPoll0() throws InterruptedException {
738 <        LinkedBlockingDeque q = populatedDeque(SIZE);
738 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
739          for (int i = 0; i < SIZE; ++i) {
740 <            assertEquals(i, q.poll(0, MILLISECONDS));
740 >            mustEqual(i, q.poll(0, MILLISECONDS));
741          }
742          assertNull(q.poll(0, MILLISECONDS));
743      }
# Line 730 | Line 746 | public class LinkedBlockingDequeTest ext
746       * timed poll with nonzero timeout succeeds when non-empty, else times out
747       */
748      public void testTimedPoll() throws InterruptedException {
749 <        LinkedBlockingDeque q = populatedDeque(SIZE);
749 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
750          for (int i = 0; i < SIZE; ++i) {
751              long startTime = System.nanoTime();
752 <            assertEquals(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
752 >            mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
753              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
754          }
755          long startTime = System.nanoTime();
# Line 747 | Line 763 | public class LinkedBlockingDequeTest ext
763       * returning timeout status
764       */
765      public void testInterruptedTimedPoll() throws InterruptedException {
766 <        final BlockingQueue<Integer> q = populatedDeque(SIZE);
767 <        final CountDownLatch aboutToWait = new CountDownLatch(1);
766 >        final BlockingQueue<Item> q = populatedDeque(SIZE);
767 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
768          Thread t = newStartedThread(new CheckedRunnable() {
769              public void realRun() throws InterruptedException {
770 <                long startTime = System.nanoTime();
771 <                for (int i = 0; i < SIZE; ++i) {
772 <                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
773 <                }
758 <                aboutToWait.countDown();
770 >                for (int i = 0; i < SIZE; i++)
771 >                    mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
772 >
773 >                Thread.currentThread().interrupt();
774                  try {
775 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
775 >                    q.poll(randomTimeout(), randomTimeUnit());
776 >                    shouldThrow();
777 >                } catch (InterruptedException success) {}
778 >                assertFalse(Thread.interrupted());
779 >
780 >                pleaseInterrupt.countDown();
781 >                try {
782 >                    q.poll(LONGER_DELAY_MS, MILLISECONDS);
783                      shouldThrow();
784 <                } catch (InterruptedException success) {
785 <                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
764 <                }
784 >                } catch (InterruptedException success) {}
785 >                assertFalse(Thread.interrupted());
786              }});
787  
788 <        aboutToWait.await();
789 <        waitForThreadToEnterWaitState(t);
788 >        await(pleaseInterrupt);
789 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
790          t.interrupt();
791          awaitTermination(t);
792          checkEmpty(q);
# Line 775 | Line 796 | public class LinkedBlockingDequeTest ext
796       * putFirst(null) throws NPE
797       */
798      public void testPutFirstNull() throws InterruptedException {
799 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
799 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
800          try {
801              q.putFirst(null);
802              shouldThrow();
# Line 786 | Line 807 | public class LinkedBlockingDequeTest ext
807       * all elements successfully putFirst are contained
808       */
809      public void testPutFirst() throws InterruptedException {
810 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
810 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
811          for (int i = 0; i < SIZE; ++i) {
812 <            Integer x = new Integer(i);
812 >            Item x = itemFor(i);
813              q.putFirst(x);
814 <            assertTrue(q.contains(x));
814 >            mustContain(q, x);
815          }
816 <        assertEquals(0, q.remainingCapacity());
816 >        mustEqual(0, q.remainingCapacity());
817      }
818  
819      /**
820       * putFirst blocks interruptibly if full
821       */
822      public void testBlockingPutFirst() throws InterruptedException {
823 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
823 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
824          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
825          Thread t = newStartedThread(new CheckedRunnable() {
826              public void realRun() throws InterruptedException {
827                  for (int i = 0; i < SIZE; ++i)
828 <                    q.putFirst(i);
829 <                assertEquals(SIZE, q.size());
830 <                assertEquals(0, q.remainingCapacity());
828 >                    q.putFirst(itemFor(i));
829 >                mustEqual(SIZE, q.size());
830 >                mustEqual(0, q.remainingCapacity());
831  
832                  Thread.currentThread().interrupt();
833                  try {
834 <                    q.putFirst(99);
834 >                    q.putFirst(ninetynine);
835                      shouldThrow();
836                  } catch (InterruptedException success) {}
837                  assertFalse(Thread.interrupted());
838  
839                  pleaseInterrupt.countDown();
840                  try {
841 <                    q.putFirst(99);
841 >                    q.putFirst(ninetynine);
842                      shouldThrow();
843                  } catch (InterruptedException success) {}
844                  assertFalse(Thread.interrupted());
845              }});
846  
847          await(pleaseInterrupt);
848 <        assertThreadStaysAlive(t);
848 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
849          t.interrupt();
850          awaitTermination(t);
851 <        assertEquals(SIZE, q.size());
852 <        assertEquals(0, q.remainingCapacity());
851 >        mustEqual(SIZE, q.size());
852 >        mustEqual(0, q.remainingCapacity());
853      }
854  
855      /**
# Line 836 | Line 857 | public class LinkedBlockingDequeTest ext
857       */
858      public void testPutFirstWithTake() throws InterruptedException {
859          final int capacity = 2;
860 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
860 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(capacity);
861          final CountDownLatch pleaseTake = new CountDownLatch(1);
862          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
863          Thread t = newStartedThread(new CheckedRunnable() {
864              public void realRun() throws InterruptedException {
865                  for (int i = 0; i < capacity; i++)
866 <                    q.putFirst(i);
866 >                    q.putFirst(itemFor(i));
867                  pleaseTake.countDown();
868 <                q.putFirst(86);
868 >                q.putFirst(eightysix);
869  
870                  pleaseInterrupt.countDown();
871                  try {
872 <                    q.putFirst(99);
872 >                    q.putFirst(ninetynine);
873                      shouldThrow();
874                  } catch (InterruptedException success) {}
875                  assertFalse(Thread.interrupted());
876              }});
877  
878          await(pleaseTake);
879 <        assertEquals(0, q.remainingCapacity());
880 <        assertEquals(capacity - 1, q.take());
879 >        mustEqual(0, q.remainingCapacity());
880 >        mustEqual(capacity - 1, q.take());
881  
882          await(pleaseInterrupt);
883 <        assertThreadStaysAlive(t);
883 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
884          t.interrupt();
885          awaitTermination(t);
886 <        assertEquals(0, q.remainingCapacity());
886 >        mustEqual(0, q.remainingCapacity());
887      }
888  
889      /**
890       * timed offerFirst times out if full and elements not taken
891       */
892 <    public void testTimedOfferFirst() throws InterruptedException {
893 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
892 >    public void testTimedOfferFirst() {
893 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(2);
894          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
895          Thread t = newStartedThread(new CheckedRunnable() {
896              public void realRun() throws InterruptedException {
897 <                q.putFirst(new Object());
898 <                q.putFirst(new Object());
897 >                q.putFirst(zero);
898 >                q.putFirst(one);
899                  long startTime = System.nanoTime();
900 <                assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
900 >
901 >                assertFalse(q.offerFirst(two, timeoutMillis(), MILLISECONDS));
902                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
903 +
904 +                Thread.currentThread().interrupt();
905 +                try {
906 +                    q.offerFirst(three, randomTimeout(), randomTimeUnit());
907 +                    shouldThrow();
908 +                } catch (InterruptedException success) {}
909 +                assertFalse(Thread.interrupted());
910 +
911                  pleaseInterrupt.countDown();
912                  try {
913 <                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
913 >                    q.offerFirst(four, LONGER_DELAY_MS, MILLISECONDS);
914                      shouldThrow();
915                  } catch (InterruptedException success) {}
916 +                assertFalse(Thread.interrupted());
917              }});
918  
919          await(pleaseInterrupt);
920 <        assertThreadStaysAlive(t);
920 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
921          t.interrupt();
922          awaitTermination(t);
923      }
# Line 895 | Line 926 | public class LinkedBlockingDequeTest ext
926       * take retrieves elements in FIFO order
927       */
928      public void testTakeFirst() throws InterruptedException {
929 <        LinkedBlockingDeque q = populatedDeque(SIZE);
929 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
930          for (int i = 0; i < SIZE; ++i) {
931 <            assertEquals(i, q.takeFirst());
931 >            mustEqual(i, q.takeFirst());
932          }
933      }
934  
# Line 905 | Line 936 | public class LinkedBlockingDequeTest ext
936       * takeFirst() blocks interruptibly when empty
937       */
938      public void testTakeFirstFromEmptyBlocksInterruptibly() {
939 <        final BlockingDeque q = new LinkedBlockingDeque();
939 >        final BlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
940          final CountDownLatch threadStarted = new CountDownLatch(1);
941          Thread t = newStartedThread(new CheckedRunnable() {
942              public void realRun() {
# Line 918 | Line 949 | public class LinkedBlockingDequeTest ext
949              }});
950  
951          await(threadStarted);
952 <        assertThreadStaysAlive(t);
952 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
953          t.interrupt();
954          awaitTermination(t);
955      }
# Line 928 | Line 959 | public class LinkedBlockingDequeTest ext
959       * before waiting
960       */
961      public void testTakeFirstFromEmptyAfterInterrupt() {
962 <        final BlockingDeque q = new LinkedBlockingDeque();
962 >        final BlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
963          Thread t = newStartedThread(new CheckedRunnable() {
964              public void realRun() {
965                  Thread.currentThread().interrupt();
# Line 946 | Line 977 | public class LinkedBlockingDequeTest ext
977       * takeLast() blocks interruptibly when empty
978       */
979      public void testTakeLastFromEmptyBlocksInterruptibly() {
980 <        final BlockingDeque q = new LinkedBlockingDeque();
980 >        final BlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
981          final CountDownLatch threadStarted = new CountDownLatch(1);
982          Thread t = newStartedThread(new CheckedRunnable() {
983              public void realRun() {
# Line 959 | Line 990 | public class LinkedBlockingDequeTest ext
990              }});
991  
992          await(threadStarted);
993 <        assertThreadStaysAlive(t);
993 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
994          t.interrupt();
995          awaitTermination(t);
996      }
# Line 969 | Line 1000 | public class LinkedBlockingDequeTest ext
1000       * before waiting
1001       */
1002      public void testTakeLastFromEmptyAfterInterrupt() {
1003 <        final BlockingDeque q = new LinkedBlockingDeque();
1003 >        final BlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
1004          Thread t = newStartedThread(new CheckedRunnable() {
1005              public void realRun() {
1006                  Thread.currentThread().interrupt();
# Line 987 | Line 1018 | public class LinkedBlockingDequeTest ext
1018       * takeFirst removes existing elements until empty, then blocks interruptibly
1019       */
1020      public void testBlockingTakeFirst() throws InterruptedException {
1021 <        final LinkedBlockingDeque q = populatedDeque(SIZE);
1021 >        final LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1022          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1023          Thread t = newStartedThread(new CheckedRunnable() {
1024              public void realRun() throws InterruptedException {
1025 <                for (int i = 0; i < SIZE; ++i) {
995 <                    assertEquals(i, q.takeFirst());
996 <                }
1025 >                for (int i = 0; i < SIZE; i++) mustEqual(i, q.takeFirst());
1026  
1027                  Thread.currentThread().interrupt();
1028                  try {
# Line 1011 | Line 1040 | public class LinkedBlockingDequeTest ext
1040              }});
1041  
1042          await(pleaseInterrupt);
1043 <        assertThreadStaysAlive(t);
1043 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1044          t.interrupt();
1045          awaitTermination(t);
1046      }
# Line 1020 | Line 1049 | public class LinkedBlockingDequeTest ext
1049       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1050       */
1051      public void testTimedPollFirst0() throws InterruptedException {
1052 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1052 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1053          for (int i = 0; i < SIZE; ++i) {
1054 <            assertEquals(i, q.pollFirst(0, MILLISECONDS));
1054 >            mustEqual(i, q.pollFirst(0, MILLISECONDS));
1055          }
1056          assertNull(q.pollFirst(0, MILLISECONDS));
1057      }
# Line 1031 | Line 1060 | public class LinkedBlockingDequeTest ext
1060       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
1061       */
1062      public void testTimedPollFirst() throws InterruptedException {
1063 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1063 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1064          for (int i = 0; i < SIZE; ++i) {
1065              long startTime = System.nanoTime();
1066 <            assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1066 >            mustEqual(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1067              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1068          }
1069          long startTime = System.nanoTime();
# Line 1048 | Line 1077 | public class LinkedBlockingDequeTest ext
1077       * returning timeout status
1078       */
1079      public void testInterruptedTimedPollFirst() throws InterruptedException {
1080 <        final LinkedBlockingDeque q = populatedDeque(SIZE);
1080 >        final LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1081          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1082          Thread t = newStartedThread(new CheckedRunnable() {
1083              public void realRun() throws InterruptedException {
1084 <                long startTime = System.nanoTime();
1085 <                for (int i = 0; i < SIZE; ++i) {
1057 <                    assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1058 <                }
1084 >                for (int i = 0; i < SIZE; i++)
1085 >                    mustEqual(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1086  
1087                  Thread.currentThread().interrupt();
1088                  try {
1089 <                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1089 >                    q.pollFirst(randomTimeout(), randomTimeUnit());
1090                      shouldThrow();
1091                  } catch (InterruptedException success) {}
1092                  assertFalse(Thread.interrupted());
1093  
1094                  pleaseInterrupt.countDown();
1095                  try {
1096 <                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1096 >                    q.pollFirst(LONGER_DELAY_MS, MILLISECONDS);
1097                      shouldThrow();
1098                  } catch (InterruptedException success) {}
1099                  assertFalse(Thread.interrupted());
1073                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1100              }});
1101  
1102          await(pleaseInterrupt);
1103 <        assertThreadStaysAlive(t);
1103 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1104          t.interrupt();
1105          awaitTermination(t);
1106      }
# Line 1084 | Line 1110 | public class LinkedBlockingDequeTest ext
1110       * on interruption throws
1111       */
1112      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
1113 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1113 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(2);
1114          final CheckedBarrier barrier = new CheckedBarrier(2);
1115          Thread t = newStartedThread(new CheckedRunnable() {
1116              public void realRun() throws InterruptedException {
# Line 1098 | Line 1124 | public class LinkedBlockingDequeTest ext
1124  
1125                  Thread.currentThread().interrupt();
1126                  try {
1127 <                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1127 >                    q.pollFirst(randomTimeout(), randomTimeUnit());
1128                      shouldThrow();
1129                  } catch (InterruptedException success) {}
1130  
# Line 1107 | Line 1133 | public class LinkedBlockingDequeTest ext
1133                      q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1134                      shouldThrow();
1135                  } catch (InterruptedException success) {}
1136 +                assertFalse(Thread.interrupted());
1137 +
1138                  assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1139              }});
1140  
# Line 1115 | Line 1143 | public class LinkedBlockingDequeTest ext
1143          assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1144          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1145          barrier.await();
1146 <        assertThreadStaysAlive(t);
1146 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1147          t.interrupt();
1148          awaitTermination(t);
1149      }
# Line 1124 | Line 1152 | public class LinkedBlockingDequeTest ext
1152       * putLast(null) throws NPE
1153       */
1154      public void testPutLastNull() throws InterruptedException {
1155 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1155 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
1156          try {
1157              q.putLast(null);
1158              shouldThrow();
# Line 1135 | Line 1163 | public class LinkedBlockingDequeTest ext
1163       * all elements successfully putLast are contained
1164       */
1165      public void testPutLast() throws InterruptedException {
1166 <        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1166 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
1167          for (int i = 0; i < SIZE; ++i) {
1168 <            Integer x = new Integer(i);
1168 >            Item x = itemFor(i);
1169              q.putLast(x);
1170 <            assertTrue(q.contains(x));
1170 >            mustContain(q, x);
1171          }
1172 <        assertEquals(0, q.remainingCapacity());
1172 >        mustEqual(0, q.remainingCapacity());
1173      }
1174  
1175      /**
1176       * putLast blocks interruptibly if full
1177       */
1178      public void testBlockingPutLast() throws InterruptedException {
1179 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1179 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
1180          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1181          Thread t = newStartedThread(new CheckedRunnable() {
1182              public void realRun() throws InterruptedException {
1183                  for (int i = 0; i < SIZE; ++i)
1184 <                    q.putLast(i);
1185 <                assertEquals(SIZE, q.size());
1186 <                assertEquals(0, q.remainingCapacity());
1184 >                    q.putLast(itemFor(i));
1185 >                mustEqual(SIZE, q.size());
1186 >                mustEqual(0, q.remainingCapacity());
1187  
1188                  Thread.currentThread().interrupt();
1189                  try {
1190 <                    q.putLast(99);
1190 >                    q.putLast(ninetynine);
1191                      shouldThrow();
1192                  } catch (InterruptedException success) {}
1193                  assertFalse(Thread.interrupted());
1194  
1195                  pleaseInterrupt.countDown();
1196                  try {
1197 <                    q.putLast(99);
1197 >                    q.putLast(ninetynine);
1198                      shouldThrow();
1199                  } catch (InterruptedException success) {}
1200                  assertFalse(Thread.interrupted());
1201              }});
1202  
1203          await(pleaseInterrupt);
1204 <        assertThreadStaysAlive(t);
1204 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1205          t.interrupt();
1206          awaitTermination(t);
1207 <        assertEquals(SIZE, q.size());
1208 <        assertEquals(0, q.remainingCapacity());
1207 >        mustEqual(SIZE, q.size());
1208 >        mustEqual(0, q.remainingCapacity());
1209      }
1210  
1211      /**
# Line 1185 | Line 1213 | public class LinkedBlockingDequeTest ext
1213       */
1214      public void testPutLastWithTake() throws InterruptedException {
1215          final int capacity = 2;
1216 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1216 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(capacity);
1217          final CountDownLatch pleaseTake = new CountDownLatch(1);
1218          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1219          Thread t = newStartedThread(new CheckedRunnable() {
1220              public void realRun() throws InterruptedException {
1221                  for (int i = 0; i < capacity; i++)
1222 <                    q.putLast(i);
1222 >                    q.putLast(itemFor(i));
1223                  pleaseTake.countDown();
1224 <                q.putLast(86);
1224 >                q.putLast(eightysix);
1225 >
1226 >                Thread.currentThread().interrupt();
1227 >                try {
1228 >                    q.putLast(ninetynine);
1229 >                    shouldThrow();
1230 >                } catch (InterruptedException success) {}
1231 >                assertFalse(Thread.interrupted());
1232  
1233                  pleaseInterrupt.countDown();
1234                  try {
1235 <                    q.putLast(99);
1235 >                    q.putLast(ninetynine);
1236                      shouldThrow();
1237                  } catch (InterruptedException success) {}
1238                  assertFalse(Thread.interrupted());
1239              }});
1240  
1241          await(pleaseTake);
1242 <        assertEquals(0, q.remainingCapacity());
1243 <        assertEquals(0, q.take());
1242 >        mustEqual(0, q.remainingCapacity());
1243 >        mustEqual(0, q.take());
1244  
1245          await(pleaseInterrupt);
1246 <        assertThreadStaysAlive(t);
1246 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1247          t.interrupt();
1248          awaitTermination(t);
1249 <        assertEquals(0, q.remainingCapacity());
1249 >        mustEqual(0, q.remainingCapacity());
1250      }
1251  
1252      /**
1253       * timed offerLast times out if full and elements not taken
1254       */
1255 <    public void testTimedOfferLast() throws InterruptedException {
1256 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1255 >    public void testTimedOfferLast() {
1256 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(2);
1257          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1258          Thread t = newStartedThread(new CheckedRunnable() {
1259              public void realRun() throws InterruptedException {
1260 <                q.putLast(new Object());
1261 <                q.putLast(new Object());
1260 >                q.putLast(zero);
1261 >                q.putLast(one);
1262                  long startTime = System.nanoTime();
1263 <                assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1263 >
1264 >                assertFalse(q.offerLast(two, timeoutMillis(), MILLISECONDS));
1265                  assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1266 +
1267 +                Thread.currentThread().interrupt();
1268 +                try {
1269 +                    q.offerLast(three, randomTimeout(), randomTimeUnit());
1270 +                    shouldThrow();
1271 +                } catch (InterruptedException success) {}
1272 +
1273                  pleaseInterrupt.countDown();
1274                  try {
1275 <                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1275 >                    q.offerLast(four, LONGER_DELAY_MS, MILLISECONDS);
1276                      shouldThrow();
1277                  } catch (InterruptedException success) {}
1278              }});
1279  
1280          await(pleaseInterrupt);
1281 <        assertThreadStaysAlive(t);
1281 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1282          t.interrupt();
1283          awaitTermination(t);
1284      }
# Line 1244 | Line 1287 | public class LinkedBlockingDequeTest ext
1287       * takeLast retrieves elements in FIFO order
1288       */
1289      public void testTakeLast() throws InterruptedException {
1290 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1290 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1291          for (int i = 0; i < SIZE; ++i) {
1292 <            assertEquals(SIZE - i - 1, q.takeLast());
1292 >            mustEqual(SIZE - i - 1, q.takeLast());
1293          }
1294      }
1295  
# Line 1254 | Line 1297 | public class LinkedBlockingDequeTest ext
1297       * takeLast removes existing elements until empty, then blocks interruptibly
1298       */
1299      public void testBlockingTakeLast() throws InterruptedException {
1300 <        final LinkedBlockingDeque q = populatedDeque(SIZE);
1300 >        final LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1301          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1302          Thread t = newStartedThread(new CheckedRunnable() {
1303              public void realRun() throws InterruptedException {
1304 <                for (int i = 0; i < SIZE; ++i) {
1305 <                    assertEquals(SIZE - i - 1, q.takeLast());
1263 <                }
1304 >                for (int i = 0; i < SIZE; i++)
1305 >                    mustEqual(SIZE - i - 1, q.takeLast());
1306  
1307                  Thread.currentThread().interrupt();
1308                  try {
# Line 1278 | Line 1320 | public class LinkedBlockingDequeTest ext
1320              }});
1321  
1322          await(pleaseInterrupt);
1323 <        assertThreadStaysAlive(t);
1323 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1324          t.interrupt();
1325          awaitTermination(t);
1326      }
# Line 1287 | Line 1329 | public class LinkedBlockingDequeTest ext
1329       * timed pollLast with zero timeout succeeds when non-empty, else times out
1330       */
1331      public void testTimedPollLast0() throws InterruptedException {
1332 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1332 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1333          for (int i = 0; i < SIZE; ++i) {
1334 <            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1334 >            mustEqual(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1335          }
1336          assertNull(q.pollLast(0, MILLISECONDS));
1337      }
# Line 1298 | Line 1340 | public class LinkedBlockingDequeTest ext
1340       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1341       */
1342      public void testTimedPollLast() throws InterruptedException {
1343 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1343 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1344          for (int i = 0; i < SIZE; ++i) {
1345              long startTime = System.nanoTime();
1346 <            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1346 >            mustEqual(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1347              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1348          }
1349          long startTime = System.nanoTime();
# Line 1315 | Line 1357 | public class LinkedBlockingDequeTest ext
1357       * returning timeout status
1358       */
1359      public void testInterruptedTimedPollLast() throws InterruptedException {
1360 <        final LinkedBlockingDeque q = populatedDeque(SIZE);
1360 >        final LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1361          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1362          Thread t = newStartedThread(new CheckedRunnable() {
1363              public void realRun() throws InterruptedException {
1364 <                long startTime = System.nanoTime();
1365 <                for (int i = 0; i < SIZE; ++i) {
1324 <                    assertEquals(SIZE - i - 1,
1364 >                for (int i = 0; i < SIZE; i++)
1365 >                    mustEqual(SIZE - i - 1,
1366                                   q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1326                }
1367  
1368                  Thread.currentThread().interrupt();
1369                  try {
1370 <                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1370 >                    q.pollLast(randomTimeout(), randomTimeUnit());
1371                      shouldThrow();
1372                  } catch (InterruptedException success) {}
1373                  assertFalse(Thread.interrupted());
1374  
1375                  pleaseInterrupt.countDown();
1376                  try {
1377 <                    q.pollLast(LONG_DELAY_MS, MILLISECONDS);
1377 >                    q.pollLast(LONGER_DELAY_MS, MILLISECONDS);
1378                      shouldThrow();
1379                  } catch (InterruptedException success) {}
1380                  assertFalse(Thread.interrupted());
1341
1342                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1381              }});
1382  
1383          await(pleaseInterrupt);
1384 <        assertThreadStaysAlive(t);
1384 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1385          t.interrupt();
1386          awaitTermination(t);
1387          checkEmpty(q);
# Line 1354 | Line 1392 | public class LinkedBlockingDequeTest ext
1392       * on interruption throws
1393       */
1394      public void testTimedPollWithOfferLast() throws InterruptedException {
1395 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1395 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(2);
1396          final CheckedBarrier barrier = new CheckedBarrier(2);
1397          Thread t = newStartedThread(new CheckedRunnable() {
1398              public void realRun() throws InterruptedException {
# Line 1368 | Line 1406 | public class LinkedBlockingDequeTest ext
1406  
1407                  Thread.currentThread().interrupt();
1408                  try {
1409 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1409 >                    q.poll(randomTimeout(), randomTimeUnit());
1410                      shouldThrow();
1411                  } catch (InterruptedException success) {}
1412                  assertFalse(Thread.interrupted());
# Line 1389 | Line 1427 | public class LinkedBlockingDequeTest ext
1427          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1428  
1429          barrier.await();
1430 <        assertThreadStaysAlive(t);
1430 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1431          t.interrupt();
1432          awaitTermination(t);
1433      }
# Line 1398 | Line 1436 | public class LinkedBlockingDequeTest ext
1436       * element returns next element, or throws NSEE if empty
1437       */
1438      public void testElement() {
1439 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1439 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1440          for (int i = 0; i < SIZE; ++i) {
1441 <            assertEquals(i, q.element());
1441 >            mustEqual(i, q.element());
1442              q.poll();
1443          }
1444          try {
# Line 1413 | Line 1451 | public class LinkedBlockingDequeTest ext
1451       * contains(x) reports true when elements added but not yet removed
1452       */
1453      public void testContains() {
1454 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1454 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1455          for (int i = 0; i < SIZE; ++i) {
1456 <            assertTrue(q.contains(new Integer(i)));
1456 >            mustContain(q, i);
1457              q.poll();
1458 <            assertFalse(q.contains(new Integer(i)));
1458 >            mustNotContain(q, i);
1459          }
1460      }
1461  
# Line 1425 | Line 1463 | public class LinkedBlockingDequeTest ext
1463       * clear removes all elements
1464       */
1465      public void testClear() {
1466 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1466 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1467          q.clear();
1468          assertTrue(q.isEmpty());
1469 <        assertEquals(0, q.size());
1470 <        assertEquals(SIZE, q.remainingCapacity());
1469 >        mustEqual(0, q.size());
1470 >        mustEqual(SIZE, q.remainingCapacity());
1471          q.add(one);
1472          assertFalse(q.isEmpty());
1473 <        assertTrue(q.contains(one));
1473 >        mustContain(q, one);
1474          q.clear();
1475          assertTrue(q.isEmpty());
1476      }
# Line 1441 | Line 1479 | public class LinkedBlockingDequeTest ext
1479       * containsAll(c) is true when c contains a subset of elements
1480       */
1481      public void testContainsAll() {
1482 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1483 <        LinkedBlockingDeque p = new LinkedBlockingDeque(SIZE);
1482 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1483 >        LinkedBlockingDeque<Item> p = new LinkedBlockingDeque<Item>(SIZE);
1484          for (int i = 0; i < SIZE; ++i) {
1485              assertTrue(q.containsAll(p));
1486              assertFalse(p.containsAll(q));
1487 <            p.add(new Integer(i));
1487 >            mustAdd(p, i);
1488          }
1489          assertTrue(p.containsAll(q));
1490      }
# Line 1455 | Line 1493 | public class LinkedBlockingDequeTest ext
1493       * retainAll(c) retains only those elements of c and reports true if changed
1494       */
1495      public void testRetainAll() {
1496 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1497 <        LinkedBlockingDeque p = populatedDeque(SIZE);
1496 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1497 >        LinkedBlockingDeque<Item> p = populatedDeque(SIZE);
1498          for (int i = 0; i < SIZE; ++i) {
1499              boolean changed = q.retainAll(p);
1500              if (i == 0)
# Line 1465 | Line 1503 | public class LinkedBlockingDequeTest ext
1503                  assertTrue(changed);
1504  
1505              assertTrue(q.containsAll(p));
1506 <            assertEquals(SIZE - i, q.size());
1506 >            mustEqual(SIZE - i, q.size());
1507              p.remove();
1508          }
1509      }
# Line 1475 | Line 1513 | public class LinkedBlockingDequeTest ext
1513       */
1514      public void testRemoveAll() {
1515          for (int i = 1; i < SIZE; ++i) {
1516 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1517 <            LinkedBlockingDeque p = populatedDeque(i);
1516 >            LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1517 >            LinkedBlockingDeque<Item> p = populatedDeque(i);
1518              assertTrue(q.removeAll(p));
1519 <            assertEquals(SIZE - i, q.size());
1519 >            mustEqual(SIZE - i, q.size());
1520              for (int j = 0; j < i; ++j) {
1521 <                Integer x = (Integer)(p.remove());
1484 <                assertFalse(q.contains(x));
1521 >                mustNotContain(q, p.remove());
1522              }
1523          }
1524      }
# Line 1490 | Line 1527 | public class LinkedBlockingDequeTest ext
1527       * toArray contains all elements in FIFO order
1528       */
1529      public void testToArray() throws InterruptedException {
1530 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1531 <        Object[] o = q.toArray();
1532 <        for (int i = 0; i < o.length; i++)
1533 <            assertSame(o[i], q.poll());
1530 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1531 >        Object[] a = q.toArray();
1532 >        assertSame(Object[].class, a.getClass());
1533 >        for (Object o : a)
1534 >            assertSame(o, q.poll());
1535 >        assertTrue(q.isEmpty());
1536      }
1537  
1538      /**
1539       * toArray(a) contains all elements in FIFO order
1540       */
1541      public void testToArray2() {
1542 <        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1543 <        Integer[] ints = new Integer[SIZE];
1544 <        Integer[] array = q.toArray(ints);
1545 <        assertSame(ints, array);
1546 <        for (int i = 0; i < ints.length; i++)
1547 <            assertSame(ints[i], q.remove());
1542 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1543 >        Item[] items = new Item[SIZE];
1544 >        Item[] array = q.toArray(items);
1545 >        assertSame(items, array);
1546 >        for (Item o : items)
1547 >            assertSame(o, q.remove());
1548 >        assertTrue(q.isEmpty());
1549      }
1550  
1551      /**
1552       * toArray(incompatible array type) throws ArrayStoreException
1553       */
1554      public void testToArray1_BadArg() {
1555 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1555 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1556          try {
1557              q.toArray(new String[10]);
1558              shouldThrow();
# Line 1523 | Line 1563 | public class LinkedBlockingDequeTest ext
1563       * iterator iterates through all elements
1564       */
1565      public void testIterator() throws InterruptedException {
1566 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1567 <        Iterator it = q.iterator();
1566 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1567 >        Iterator<? extends Item> it = q.iterator();
1568          int i;
1569          for (i = 0; it.hasNext(); i++)
1570 <            assertTrue(q.contains(it.next()));
1571 <        assertEquals(i, SIZE);
1570 >            mustContain(q, it.next());
1571 >        mustEqual(i, SIZE);
1572          assertIteratorExhausted(it);
1573  
1574          it = q.iterator();
1575          for (i = 0; it.hasNext(); i++)
1576 <            assertEquals(it.next(), q.take());
1577 <        assertEquals(i, SIZE);
1576 >            mustEqual(it.next(), q.take());
1577 >        mustEqual(i, SIZE);
1578          assertIteratorExhausted(it);
1579      }
1580  
# Line 1542 | Line 1582 | public class LinkedBlockingDequeTest ext
1582       * iterator of empty collection has no elements
1583       */
1584      public void testEmptyIterator() {
1585 <        Deque c = new LinkedBlockingDeque();
1585 >        Deque<Item> c = new LinkedBlockingDeque<Item>();
1586          assertIteratorExhausted(c.iterator());
1587          assertIteratorExhausted(c.descendingIterator());
1588      }
# Line 1551 | Line 1591 | public class LinkedBlockingDequeTest ext
1591       * iterator.remove removes current element
1592       */
1593      public void testIteratorRemove() {
1594 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1594 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(3);
1595          q.add(two);
1596          q.add(one);
1597          q.add(three);
1598  
1599 <        Iterator it = q.iterator();
1599 >        Iterator<? extends Item> it = q.iterator();
1600          it.next();
1601          it.remove();
1602  
# Line 1570 | Line 1610 | public class LinkedBlockingDequeTest ext
1610       * iterator ordering is FIFO
1611       */
1612      public void testIteratorOrdering() {
1613 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1613 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(3);
1614          q.add(one);
1615          q.add(two);
1616          q.add(three);
1617 <        assertEquals(0, q.remainingCapacity());
1617 >        mustEqual(0, q.remainingCapacity());
1618          int k = 0;
1619 <        for (Iterator it = q.iterator(); it.hasNext();) {
1620 <            assertEquals(++k, it.next());
1619 >        for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
1620 >            mustEqual(++k, it.next());
1621          }
1622 <        assertEquals(3, k);
1622 >        mustEqual(3, k);
1623      }
1624  
1625      /**
1626       * Modifications do not cause iterators to fail
1627       */
1628      public void testWeaklyConsistentIteration() {
1629 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1629 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(3);
1630          q.add(one);
1631          q.add(two);
1632          q.add(three);
1633 <        for (Iterator it = q.iterator(); it.hasNext();) {
1633 >        for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
1634              q.remove();
1635              it.next();
1636          }
1637 <        assertEquals(0, q.size());
1637 >        mustEqual(0, q.size());
1638      }
1639  
1640      /**
1641       * Descending iterator iterates through all elements
1642       */
1643      public void testDescendingIterator() {
1644 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1644 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1645          int i = 0;
1646 <        Iterator it = q.descendingIterator();
1646 >        Iterator<? extends Item> it = q.descendingIterator();
1647          while (it.hasNext()) {
1648 <            assertTrue(q.contains(it.next()));
1648 >            mustContain(q, it.next());
1649              ++i;
1650          }
1651 <        assertEquals(i, SIZE);
1651 >        mustEqual(i, SIZE);
1652          assertFalse(it.hasNext());
1653          try {
1654              it.next();
# Line 1620 | Line 1660 | public class LinkedBlockingDequeTest ext
1660       * Descending iterator ordering is reverse FIFO
1661       */
1662      public void testDescendingIteratorOrdering() {
1663 <        final LinkedBlockingDeque q = new LinkedBlockingDeque();
1663 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
1664          for (int iters = 0; iters < 100; ++iters) {
1665 <            q.add(new Integer(3));
1666 <            q.add(new Integer(2));
1667 <            q.add(new Integer(1));
1665 >            mustAdd(q, three);
1666 >            mustAdd(q, two);
1667 >            mustAdd(q, one);
1668 >
1669              int k = 0;
1670 <            for (Iterator it = q.descendingIterator(); it.hasNext();) {
1671 <                assertEquals(++k, it.next());
1670 >            for (Iterator<? extends Item> it = q.descendingIterator(); it.hasNext();) {
1671 >                mustEqual(++k, it.next());
1672              }
1673  
1674 <            assertEquals(3, k);
1674 >            mustEqual(3, k);
1675              q.remove();
1676              q.remove();
1677              q.remove();
# Line 1641 | Line 1682 | public class LinkedBlockingDequeTest ext
1682       * descendingIterator.remove removes current element
1683       */
1684      public void testDescendingIteratorRemove() {
1685 <        final LinkedBlockingDeque q = new LinkedBlockingDeque();
1685 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
1686          for (int iters = 0; iters < 100; ++iters) {
1687 <            q.add(new Integer(3));
1688 <            q.add(new Integer(2));
1689 <            q.add(new Integer(1));
1690 <            Iterator it = q.descendingIterator();
1691 <            assertEquals(it.next(), new Integer(1));
1687 >            mustAdd(q, three);
1688 >            mustAdd(q, two);
1689 >            mustAdd(q, one);
1690 >            Iterator<? extends Item> it = q.descendingIterator();
1691 >            mustEqual(it.next(), one);
1692              it.remove();
1693 <            assertEquals(it.next(), new Integer(2));
1693 >            mustEqual(it.next(), two);
1694              it = q.descendingIterator();
1695 <            assertEquals(it.next(), new Integer(2));
1696 <            assertEquals(it.next(), new Integer(3));
1695 >            mustEqual(it.next(), two);
1696 >            mustEqual(it.next(), three);
1697              it.remove();
1698              assertFalse(it.hasNext());
1699              q.remove();
# Line 1663 | Line 1704 | public class LinkedBlockingDequeTest ext
1704       * toString contains toStrings of elements
1705       */
1706      public void testToString() {
1707 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1707 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1708          String s = q.toString();
1709          for (int i = 0; i < SIZE; ++i) {
1710              assertTrue(s.contains(String.valueOf(i)));
# Line 1674 | Line 1715 | public class LinkedBlockingDequeTest ext
1715       * offer transfers elements across Executor tasks
1716       */
1717      public void testOfferInExecutor() {
1718 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1718 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(2);
1719          q.add(one);
1720          q.add(two);
1721          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
# Line 1685 | Line 1726 | public class LinkedBlockingDequeTest ext
1726                      assertFalse(q.offer(three));
1727                      threadsStarted.await();
1728                      assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1729 <                    assertEquals(0, q.remainingCapacity());
1729 >                    mustEqual(0, q.remainingCapacity());
1730                  }});
1731  
1732              executor.execute(new CheckedRunnable() {
# Line 1700 | Line 1741 | public class LinkedBlockingDequeTest ext
1741       * timed poll retrieves elements across Executor threads
1742       */
1743      public void testPollInExecutor() {
1744 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1744 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(2);
1745          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1746          final ExecutorService executor = Executors.newFixedThreadPool(2);
1747          try (PoolCleaner cleaner = cleaner(executor)) {
# Line 1721 | Line 1762 | public class LinkedBlockingDequeTest ext
1762      }
1763  
1764      /**
1765 <     * A deserialized serialized deque has same elements in same order
1765 >     * A deserialized/reserialized deque has same elements in same order
1766       */
1767      public void testSerialization() throws Exception {
1768 <        Queue x = populatedDeque(SIZE);
1769 <        Queue y = serialClone(x);
1768 >        Queue<Item> x = populatedDeque(SIZE);
1769 >        Queue<Item> y = serialClone(x);
1770  
1771          assertNotSame(y, x);
1772 <        assertEquals(x.size(), y.size());
1773 <        assertEquals(x.toString(), y.toString());
1772 >        mustEqual(x.size(), y.size());
1773 >        mustEqual(x.toString(), y.toString());
1774          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1775          while (!x.isEmpty()) {
1776              assertFalse(y.isEmpty());
1777 <            assertEquals(x.remove(), y.remove());
1777 >            mustEqual(x.remove(), y.remove());
1778          }
1779          assertTrue(y.isEmpty());
1780      }
# Line 1742 | Line 1783 | public class LinkedBlockingDequeTest ext
1783       * drainTo(c) empties deque into another collection c
1784       */
1785      public void testDrainTo() {
1786 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1787 <        ArrayList l = new ArrayList();
1786 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1787 >        ArrayList<Item> l = new ArrayList<Item>();
1788          q.drainTo(l);
1789 <        assertEquals(0, q.size());
1790 <        assertEquals(SIZE, l.size());
1789 >        mustEqual(0, q.size());
1790 >        mustEqual(SIZE, l.size());
1791          for (int i = 0; i < SIZE; ++i)
1792 <            assertEquals(l.get(i), new Integer(i));
1792 >            mustEqual(l.get(i), i);
1793          q.add(zero);
1794          q.add(one);
1795          assertFalse(q.isEmpty());
1796 <        assertTrue(q.contains(zero));
1797 <        assertTrue(q.contains(one));
1796 >        mustContain(q, zero);
1797 >        mustContain(q, one);
1798          l.clear();
1799          q.drainTo(l);
1800 <        assertEquals(0, q.size());
1801 <        assertEquals(2, l.size());
1800 >        mustEqual(0, q.size());
1801 >        mustEqual(2, l.size());
1802          for (int i = 0; i < 2; ++i)
1803 <            assertEquals(l.get(i), new Integer(i));
1803 >            mustEqual(l.get(i), i);
1804      }
1805  
1806      /**
1807       * drainTo empties full deque, unblocking a waiting put.
1808       */
1809      public void testDrainToWithActivePut() throws InterruptedException {
1810 <        final LinkedBlockingDeque q = populatedDeque(SIZE);
1810 >        final LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1811          Thread t = new Thread(new CheckedRunnable() {
1812              public void realRun() throws InterruptedException {
1813 <                q.put(new Integer(SIZE + 1));
1813 >                q.put(new Item(SIZE + 1));
1814              }});
1815  
1816          t.start();
1817 <        ArrayList l = new ArrayList();
1817 >        ArrayList<Item> l = new ArrayList<Item>();
1818          q.drainTo(l);
1819          assertTrue(l.size() >= SIZE);
1820          for (int i = 0; i < SIZE; ++i)
1821 <            assertEquals(l.get(i), new Integer(i));
1821 >            mustEqual(l.get(i), i);
1822          t.join();
1823          assertTrue(q.size() + l.size() >= SIZE);
1824      }
# Line 1786 | Line 1827 | public class LinkedBlockingDequeTest ext
1827       * drainTo(c, n) empties first min(n, size) elements of queue into c
1828       */
1829      public void testDrainToN() {
1830 <        LinkedBlockingDeque q = new LinkedBlockingDeque();
1830 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
1831          for (int i = 0; i < SIZE + 2; ++i) {
1832              for (int j = 0; j < SIZE; j++)
1833 <                assertTrue(q.offer(new Integer(j)));
1834 <            ArrayList l = new ArrayList();
1833 >                mustOffer(q, j);
1834 >            ArrayList<Item> l = new ArrayList<Item>();
1835              q.drainTo(l, i);
1836              int k = (i < SIZE) ? i : SIZE;
1837 <            assertEquals(k, l.size());
1838 <            assertEquals(SIZE - k, q.size());
1837 >            mustEqual(k, l.size());
1838 >            mustEqual(SIZE - k, q.size());
1839              for (int j = 0; j < k; ++j)
1840 <                assertEquals(l.get(j), new Integer(j));
1840 >                mustEqual(l.get(j), j);
1841              do {} while (q.poll() != null);
1842          }
1843      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines