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.5 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.88 by dl, Tue Jan 26 13:33:06 2021 UTC

# Line 1 | Line 1
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
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
10 < import java.io.*;
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 >
9 > import java.util.ArrayList;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Deque;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.Queue;
16 > import java.util.concurrent.BlockingDeque;
17 > import java.util.concurrent.BlockingQueue;
18 > import java.util.concurrent.CountDownLatch;
19 > import java.util.concurrent.Executors;
20 > import java.util.concurrent.ExecutorService;
21 > import java.util.concurrent.LinkedBlockingDeque;
22 >
23 > import junit.framework.Test;
24  
25   public class LinkedBlockingDequeTest extends JSR166TestCase {
26 +
27 +    public static class Unbounded extends BlockingQueueTest {
28 +        protected BlockingQueue emptyCollection() {
29 +            return new LinkedBlockingDeque();
30 +        }
31 +    }
32 +
33 +    public static class Bounded extends BlockingQueueTest {
34 +        protected BlockingQueue emptyCollection() {
35 +            return new LinkedBlockingDeque(SIZE);
36 +        }
37 +    }
38 +
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run (suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
44 <        return new TestSuite(LinkedBlockingDequeTest.class);
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(),
54 >                            CollectionTest.testSuite(new Implementation()));
55      }
56  
57      /**
58 <     * Create a deque of given size containing consecutive
59 <     * Integers 0 ... n.
60 <     */
61 <    private LinkedBlockingDeque populatedDeque(int n) {
62 <        LinkedBlockingDeque q = new LinkedBlockingDeque(n);
58 >     * Returns a new deque of given size containing consecutive
59 >     * Items 0 ... n - 1.
60 >     */
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)));
65 >        for (int i = 0; i < n; 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 37 | 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 51 | 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  
104      /**
105 <     * offer(null) throws NPE
105 >     * offerFirst(null) throws NullPointerException
106       */
107      public void testOfferFirstNull() {
108 <        try {
109 <            LinkedBlockingDeque q = new LinkedBlockingDeque();
108 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
109 >        try {
110              q.offerFirst(null);
111              shouldThrow();
112 <        } catch (NullPointerException success) {
113 <        }
112 >        } catch (NullPointerException success) {}
113 >    }
114 >
115 >    /**
116 >     * offerLast(null) throws NullPointerException
117 >     */
118 >    public void testOfferLastNull() {
119 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
120 >        try {
121 >            q.offerLast(null);
122 >            shouldThrow();
123 >        } catch (NullPointerException success) {}
124      }
125  
126      /**
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
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, ((Integer)q.pollFirst()).intValue());
150 >            mustEqual(i, q.pollFirst());
151          }
152 <        assertNull(q.pollFirst());
152 >        assertNull(q.pollFirst());
153      }
154  
155      /**
156 <     *  pollLast succeeds unless empty
156 >     * pollLast succeeds unless empty
157       */
158      public void testPollLast() {
159 <        LinkedBlockingDeque q = populatedDeque(SIZE);
160 <        for (int i = SIZE-1; i >= 0; --i) {
161 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
159 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
160 >        for (int i = SIZE - 1; i >= 0; --i) {
161 >            mustEqual(i, q.pollLast());
162          }
163 <        assertNull(q.pollLast());
163 >        assertNull(q.pollLast());
164      }
165  
166      /**
167 <     *  peekFirst returns next element, or null if empty
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, ((Integer)q.peekFirst()).intValue());
173 <            q.pollFirst();
172 >            mustEqual(i, q.peekFirst());
173 >            mustEqual(i, q.pollFirst());
174              assertTrue(q.peekFirst() == null ||
175 <                       i != ((Integer)q.peekFirst()).intValue());
175 >                       !q.peekFirst().equals(i));
176          }
177 <        assertNull(q.peekFirst());
177 >        assertNull(q.peekFirst());
178      }
179  
180      /**
181 <     *  peek returns next element, or null if empty
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, ((Integer)q.peek()).intValue());
187 <            q.pollFirst();
186 >            mustEqual(i, q.peek());
187 >            mustEqual(i, q.pollFirst());
188              assertTrue(q.peek() == null ||
189 <                       i != ((Integer)q.peek()).intValue());
189 >                       !q.peek().equals(i));
190          }
191 <        assertNull(q.peek());
191 >        assertNull(q.peek());
192      }
193  
194      /**
195 <     *  peekLast returns next element, or null if empty
195 >     * peekLast returns next element, or null if empty
196       */
197      public void testPeekLast() {
198 <        LinkedBlockingDeque q = populatedDeque(SIZE);
199 <        for (int i = SIZE-1; i >= 0; --i) {
200 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
201 <            q.pollLast();
198 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
199 >        for (int i = SIZE - 1; i >= 0; --i) {
200 >            mustEqual(i, q.peekLast());
201 >            mustEqual(i, q.pollLast());
202              assertTrue(q.peekLast() == null ||
203 <                       i != ((Integer)q.peekLast()).intValue());
203 >                       !q.peekLast().equals(i));
204          }
205 <        assertNull(q.peekLast());
205 >        assertNull(q.peekLast());
206      }
207  
208      /**
209 <     * getFirst returns next getFirst, or throws NSEE if empty
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, ((Integer)q.getFirst()).intValue());
215 <            q.pollFirst();
214 >            mustEqual(i, q.getFirst());
215 >            mustEqual(i, q.pollFirst());
216          }
217          try {
218              q.getFirst();
219              shouldThrow();
220 <        }
221 <        catch (NoSuchElementException success) {}
220 >        } catch (NoSuchElementException success) {}
221 >        assertNull(q.peekFirst());
222      }
223  
224      /**
225 <     *  getLast returns next element, or throws NSEE if empty
225 >     * getLast() returns last element, or throws NSEE if empty
226       */
227      public void testLastElement() {
228 <        LinkedBlockingDeque q = populatedDeque(SIZE);
229 <        for (int i = SIZE-1; i >= 0; --i) {
230 <            assertEquals(i, ((Integer)q.getLast()).intValue());
231 <            q.pollLast();
228 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
229 >        for (int i = SIZE - 1; i >= 0; --i) {
230 >            mustEqual(i, q.getLast());
231 >            mustEqual(i, q.pollLast());
232          }
233          try {
234              q.getLast();
235              shouldThrow();
236 <        }
237 <        catch (NoSuchElementException success) {}
189 <        assertNull(q.peekLast());
236 >        } catch (NoSuchElementException success) {}
237 >        assertNull(q.peekLast());
238      }
239  
240      /**
241 <     *  removeFirst removes next element, or throws NSEE if empty
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, ((Integer)q.removeFirst()).intValue());
246 >            mustEqual(i, q.removeFirst());
247          }
248          try {
249              q.removeFirst();
250              shouldThrow();
251 <        } catch (NoSuchElementException success){
252 <        }
251 >        } catch (NoSuchElementException success) {}
252 >        assertNull(q.peekFirst());
253 >    }
254 >
255 >    /**
256 >     * removeLast() removes last element, or throws NSEE if empty
257 >     */
258 >    public void testRemoveLast() {
259 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
260 >        for (int i = SIZE - 1; i >= 0; --i) {
261 >            mustEqual(i, q.removeLast());
262 >        }
263 >        try {
264 >            q.removeLast();
265 >            shouldThrow();
266 >        } catch (NoSuchElementException success) {}
267 >        assertNull(q.peekLast());
268      }
269  
270      /**
271 <     *  remove removes next element, or throws NSEE if empty
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, ((Integer)q.remove()).intValue());
276 >            mustEqual(i, q.remove());
277          }
278          try {
279              q.remove();
280              shouldThrow();
281 <        } catch (NoSuchElementException success){
219 <        }
281 >        } catch (NoSuchElementException success) {}
282      }
283  
284      /**
285       * removeFirstOccurrence(x) removes x and returns true if present
286       */
287      public void testRemoveFirstOccurrence() {
288 <        LinkedBlockingDeque q = populatedDeque(SIZE);
289 <        for (int i = 1; i < SIZE; i+=2) {
290 <            assertTrue(q.removeFirstOccurrence(new Integer(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)));
288 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
289 >        for (int i = 1; i < SIZE; i += 2) {
290 >            assertTrue(q.removeFirstOccurrence(itemFor(i)));
291 >        }
292 >        for (int i = 0; i < SIZE; i += 2) {
293 >            assertTrue(q.removeFirstOccurrence(itemFor(i)));
294 >            assertFalse(q.removeFirstOccurrence(itemFor(i + 1)));
295          }
296          assertTrue(q.isEmpty());
297      }
# Line 238 | 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);
304 <        for (int i = 1; i < SIZE; i+=2) {
305 <            assertTrue(q.removeLastOccurrence(new Integer(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)));
303 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
304 >        for (int i = 1; i < SIZE; i += 2) {
305 >            assertTrue(q.removeLastOccurrence(itemFor(i)));
306 >        }
307 >        for (int i = 0; i < SIZE; i += 2) {
308 >            assertTrue(q.removeLastOccurrence(itemFor(i)));
309 >            assertFalse(q.removeLastOccurrence(itemFor(i + 1)));
310          }
311          assertTrue(q.isEmpty());
312      }
# Line 253 | 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 <        assertEquals(four,q.peekFirst());
320 >        q.addFirst(four);
321 >        assertSame(four, q.peekFirst());
322      }
323  
324      /**
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 <        assertEquals(four,q.peekLast());
330 >        q.addLast(four);
331 >        assertSame(four, q.peekLast());
332      }
333  
272
334      /**
335       * A new deque has the indicated capacity, or Integer.MAX_VALUE if
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      /**
344 <     * Constructor throws IAE if  capacity argument nonpositive
344 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
345       */
346      public void testConstructor2() {
347          try {
348 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
348 >            new LinkedBlockingDeque<Item>(0);
349              shouldThrow();
350 <        }
290 <        catch (IllegalArgumentException success) {}
350 >        } catch (IllegalArgumentException success) {}
351      }
352  
353      /**
354 <     * Initializing from null Collection throws NPE
354 >     * Initializing from null Collection throws NullPointerException
355       */
356      public void testConstructor3() {
357          try {
358 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
358 >            new LinkedBlockingDeque<Item>(null);
359              shouldThrow();
360 <        }
301 <        catch (NullPointerException success) {}
360 >        } catch (NullPointerException success) {}
361      }
362  
363      /**
364 <     * Initializing from Collection of null elements throws NPE
364 >     * Initializing from Collection of null elements throws NullPointerException
365       */
366      public void testConstructor4() {
367 +        Collection<Item> elements = Arrays.asList(new Item[SIZE]);
368          try {
369 <            Integer[] ints = new Integer[SIZE];
310 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
369 >            new LinkedBlockingDeque<Item>(elements);
370              shouldThrow();
371 <        }
313 <        catch (NullPointerException success) {}
371 >        } catch (NullPointerException success) {}
372      }
373  
374      /**
375 <     * Initializing from Collection with some null elements throws NPE
375 >     * Initializing from Collection with some null elements throws
376 >     * NullPointerException
377       */
378      public void testConstructor5() {
379 +        Item[] items = new Item[2];
380 +        items[0] = zero;
381 +        Collection<Item> elements = Arrays.asList(items);
382          try {
383 <            Integer[] ints = new Integer[SIZE];
322 <            for (int i = 0; i < SIZE-1; ++i)
323 <                ints[i] = new Integer(i);
324 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
383 >            new LinkedBlockingDeque<Item>(elements);
384              shouldThrow();
385 <        }
327 <        catch (NullPointerException success) {}
385 >        } catch (NullPointerException success) {}
386      }
387  
388      /**
389       * Deque contains all elements of collection used to initialize
390       */
391      public void testConstructor6() {
392 <        try {
393 <            Integer[] ints = new Integer[SIZE];
394 <            for (int i = 0; i < SIZE; ++i)
395 <                ints[i] = new Integer(i);
338 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
339 <            for (int i = 0; i < SIZE; ++i)
340 <                assertEquals(ints[i], q.poll());
341 <        }
342 <        finally {}
392 >        Item[] items = defaultItems;
393 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(Arrays.asList(items));
394 >        for (int i = 0; i < SIZE; ++i)
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 361 | Line 414 | public class LinkedBlockingDequeTest ext
414       * remainingCapacity decreases on add, increases on remove
415       */
416      public void testRemainingCapacity() {
417 <        LinkedBlockingDeque 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-i, q.size());
421 <            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(i, q.size());
426 <            q.add(new Integer(i));
424 >            mustEqual(SIZE - i, q.remainingCapacity());
425 >            mustEqual(SIZE, q.size() + q.remainingCapacity());
426 >            mustAdd(q, i);
427          }
428      }
429  
430      /**
378     * offer(null) throws NPE
379     */
380    public void testOfferNull() {
381        try {
382            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
383            q.offer(null);
384            shouldThrow();
385        } catch (NullPointerException success) { }
386    }
387
388    /**
389     * add(null) throws NPE
390     */
391    public void testAddNull() {
392        try {
393            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
394            q.add(null);
395            shouldThrow();
396        } catch (NullPointerException success) { }
397    }
398
399    /**
431       * push(null) throws NPE
432       */
433      public void testPushNull() {
434 <        try {
435 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
434 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(1);
435 >        try {
436              q.push(null);
437              shouldThrow();
438 <        } catch (NullPointerException success) { }
438 >        } catch (NullPointerException success) {}
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 <        try {
446 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
447 <            for (int i = 0; i < SIZE; ++i) {
448 <                Integer I = new Integer(i);
449 <                q.push(I);
450 <                assertEquals(I, q.peek());
451 <            }
452 <            assertEquals(0, q.remainingCapacity());
453 <            q.push(new Integer(SIZE));
454 <        } catch (IllegalStateException success){
455 <        }
445 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
446 >        for (int i = 0; i < SIZE; ++i) {
447 >            Item x = itemFor(i);
448 >            q.push(x);
449 >            mustEqual(x, q.peek());
450 >        }
451 >        mustEqual(0, q.remainingCapacity());
452 >        try {
453 >            q.push(itemFor(SIZE));
454 >            shouldThrow();
455 >        } catch (IllegalStateException success) {}
456      }
457  
458      /**
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 <        assertEquals(four,q.peekFirst());
464 >        q.push(four);
465 >        assertSame(four, q.peekFirst());
466      }
467  
437
468      /**
469 <     *  pop removes next element, or throws NSEE if empty
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, ((Integer)q.pop()).intValue());
474 >            mustEqual(i, q.pop());
475          }
476          try {
477              q.pop();
478              shouldThrow();
479 <        } catch (NoSuchElementException success){
450 <        }
479 >        } catch (NoSuchElementException success) {}
480      }
481  
453
482      /**
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 <        try {
496 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
497 <            for (int i = 0; i < SIZE; ++i) {
498 <                assertTrue(q.add(new Integer(i)));
471 <            }
472 <            assertEquals(0, q.remainingCapacity());
473 <            q.add(new Integer(SIZE));
474 <        } catch (IllegalStateException success){
475 <        }
476 <    }
477 <
478 <    /**
479 <     * addAll(null) throws NPE
480 <     */
481 <    public void testAddAll1() {
495 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
496 >        for (int i = 0; i < SIZE; ++i)
497 >            mustAdd(q, i);
498 >        mustEqual(0, q.remainingCapacity());
499          try {
500 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
484 <            q.addAll(null);
500 >            q.add(itemFor(SIZE));
501              shouldThrow();
502 <        }
487 <        catch (NullPointerException success) {}
502 >        } catch (IllegalStateException success) {}
503      }
504  
505      /**
506 <     * addAll(this) throws IAE
506 >     * addAll(this) throws IllegalArgumentException
507       */
508      public void testAddAllSelf() {
509 +        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
510          try {
495            LinkedBlockingDeque q = populatedDeque(SIZE);
511              q.addAll(q);
512              shouldThrow();
513 <        }
499 <        catch (IllegalArgumentException success) {}
513 >        } catch (IllegalArgumentException success) {}
514      }
515  
516      /**
503     * addAll of a collection with null elements throws NPE
504     */
505    public void testAddAll2() {
506        try {
507            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
508            Integer[] ints = new Integer[SIZE];
509            q.addAll(Arrays.asList(ints));
510            shouldThrow();
511        }
512        catch (NullPointerException success) {}
513    }
514    /**
517       * addAll of a collection with any null elements throws NPE after
518       * possibly adding some elements
519       */
520      public void testAddAll3() {
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 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
521 <            Integer[] ints = new Integer[SIZE];
522 <            for (int i = 0; i < SIZE-1; ++i)
523 <                ints[i] = new Integer(i);
524 <            q.addAll(Arrays.asList(ints));
525 >            q.addAll(elements);
526              shouldThrow();
527 <        }
527 <        catch (NullPointerException success) {}
527 >        } catch (NullPointerException success) {}
528      }
529 +
530      /**
531 <     * addAll throws ISE if not enough room
531 >     * addAll throws IllegalStateException if not enough room
532       */
533      public void testAddAll4() {
534 +        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE - 1);
535 +        Item[] items = defaultItems;
536 +        Collection<Item> elements = Arrays.asList(items);
537          try {
538 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
535 <            Integer[] ints = new Integer[SIZE];
536 <            for (int i = 0; i < SIZE; ++i)
537 <                ints[i] = new Integer(i);
538 <            q.addAll(Arrays.asList(ints));
538 >            q.addAll(elements);
539              shouldThrow();
540 <        }
541 <        catch (IllegalStateException success) {}
540 >        } catch (IllegalStateException success) {}
541      }
542 +
543      /**
544       * Deque contains all elements, in traversal order, of successful addAll
545       */
546      public void testAddAll5() {
547 <        try {
548 <            Integer[] empty = new Integer[0];
549 <            Integer[] ints = new Integer[SIZE];
550 <            for (int i = 0; i < SIZE; ++i)
551 <                ints[i] = new Integer(i);
552 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
553 <            assertFalse(q.addAll(Arrays.asList(empty)));
554 <            assertTrue(q.addAll(Arrays.asList(ints)));
555 <            for (int i = 0; i < SIZE; ++i)
556 <                assertEquals(ints[i], q.poll());
557 <        }
558 <        finally {}
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(items)));
552 >        for (int i = 0; i < SIZE; ++i)
553 >            mustEqual(items[i], q.poll());
554      }
555  
561
562    /**
563     * put(null) throws NPE
564     */
565     public void testPutNull() {
566        try {
567            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
568            q.put(null);
569            shouldThrow();
570        }
571        catch (NullPointerException success){
572        }
573        catch (InterruptedException ie) {
574            unexpectedException();
575        }
576     }
577
556      /**
557       * all elements successfully put are contained
558       */
559 <     public void testPut() {
560 <         try {
561 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
562 <             for (int i = 0; i < SIZE; ++i) {
563 <                 Integer I = new Integer(i);
564 <                 q.put(I);
587 <                 assertTrue(q.contains(I));
588 <             }
589 <             assertEquals(0, q.remainingCapacity());
590 <         }
591 <        catch (InterruptedException ie) {
592 <            unexpectedException();
559 >    public void testPut() throws InterruptedException {
560 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
561 >        for (int i = 0; i < SIZE; ++i) {
562 >            Item x = itemFor(i);
563 >            q.put(x);
564 >            mustContain(q, x);
565          }
566 +        mustEqual(0, q.remainingCapacity());
567      }
568  
569      /**
570       * put blocks interruptibly if full
571       */
572 <    public void testBlockingPut() {
573 <        Thread t = new Thread(new Runnable() {
574 <                public void run() {
575 <                    int added = 0;
576 <                    try {
577 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
578 <                        for (int i = 0; i < SIZE; ++i) {
579 <                            q.put(new Integer(i));
580 <                            ++added;
581 <                        }
582 <                        q.put(new Integer(SIZE));
583 <                        threadShouldThrow();
584 <                    } catch (InterruptedException ie){
585 <                        threadAssertEquals(added, SIZE);
586 <                    }
587 <                }});
588 <        t.start();
589 <        try {
590 <           Thread.sleep(SHORT_DELAY_MS);
591 <           t.interrupt();
592 <           t.join();
593 <        }
594 <        catch (InterruptedException ie) {
595 <            unexpectedException();
596 <        }
572 >    public void testBlockingPut() throws InterruptedException {
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(itemFor(i));
579 >                mustEqual(SIZE, q.size());
580 >                mustEqual(0, q.remainingCapacity());
581 >
582 >                Thread.currentThread().interrupt();
583 >                try {
584 >                    q.put(ninetynine);
585 >                    shouldThrow();
586 >                } catch (InterruptedException success) {}
587 >                assertFalse(Thread.interrupted());
588 >
589 >                pleaseInterrupt.countDown();
590 >                try {
591 >                    q.put(ninetynine);
592 >                    shouldThrow();
593 >                } catch (InterruptedException success) {}
594 >                assertFalse(Thread.interrupted());
595 >            }});
596 >
597 >        await(pleaseInterrupt);
598 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
599 >        t.interrupt();
600 >        awaitTermination(t);
601 >        mustEqual(SIZE, q.size());
602 >        mustEqual(0, q.remainingCapacity());
603      }
604  
605      /**
606 <     * put blocks waiting for take when full
607 <     */
608 <    public void testPutWithTake() {
609 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
610 <        Thread t = new Thread(new Runnable() {
611 <                public void run() {
612 <                    int added = 0;
613 <                    try {
614 <                        q.put(new Object());
615 <                        ++added;
616 <                        q.put(new Object());
617 <                        ++added;
618 <                        q.put(new Object());
619 <                        ++added;
620 <                        q.put(new Object());
621 <                        ++added;
622 <                        threadShouldThrow();
623 <                    } catch (InterruptedException e){
624 <                        threadAssertTrue(added >= 2);
625 <                    }
626 <                }
627 <            });
628 <        try {
629 <            t.start();
630 <            Thread.sleep(SHORT_DELAY_MS);
631 <            q.take();
632 <            t.interrupt();
633 <            t.join();
634 <        } catch (Exception e){
635 <            unexpectedException();
636 <        }
606 >     * put blocks interruptibly waiting for take when full
607 >     */
608 >    public void testPutWithTake() throws InterruptedException {
609 >        final int capacity = 2;
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(itemFor(i));
617 >                pleaseTake.countDown();
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(ninetynine);
630 >                    shouldThrow();
631 >                } catch (InterruptedException success) {}
632 >                assertFalse(Thread.interrupted());
633 >            }});
634 >
635 >        await(pleaseTake);
636 >        mustEqual(0, q.remainingCapacity());
637 >        mustEqual(0, q.take());
638 >
639 >        await(pleaseInterrupt);
640 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
641 >        t.interrupt();
642 >        awaitTermination(t);
643 >        mustEqual(0, q.remainingCapacity());
644      }
645  
646      /**
647       * timed offer times out if full and elements not taken
648       */
649      public void testTimedOffer() {
650 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
651 <        Thread t = new Thread(new Runnable() {
652 <                public void run() {
653 <                    try {
654 <                        q.put(new Object());
655 <                        q.put(new Object());
656 <                        threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
657 <                        q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
658 <                        threadShouldThrow();
659 <                    } catch (InterruptedException success){}
660 <                }
661 <            });
662 <
663 <        try {
664 <            t.start();
665 <            Thread.sleep(SMALL_DELAY_MS);
666 <            t.interrupt();
667 <            t.join();
668 <        } catch (Exception e){
669 <            unexpectedException();
670 <        }
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(zero);
655 >                q.put(one);
656 >                long startTime = System.nanoTime();
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(four, LONGER_DELAY_MS, MILLISECONDS);
671 >                    shouldThrow();
672 >                } catch (InterruptedException success) {}
673 >                assertFalse(Thread.interrupted());
674 >            }});
675 >
676 >        await(pleaseInterrupt);
677 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
678 >        t.interrupt();
679 >        awaitTermination(t);
680      }
681  
682      /**
683       * take retrieves elements in FIFO order
684       */
685 <    public void testTake() {
686 <        try {
687 <            LinkedBlockingDeque q = populatedDeque(SIZE);
688 <            for (int i = 0; i < SIZE; ++i) {
694 <                assertEquals(i, ((Integer)q.take()).intValue());
695 <            }
696 <        } catch (InterruptedException e){
697 <            unexpectedException();
698 <        }
699 <    }
700 <
701 <    /**
702 <     * take blocks interruptibly when empty
703 <     */
704 <    public void testTakeFromEmpty() {
705 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
706 <        Thread t = new Thread(new Runnable() {
707 <                public void run() {
708 <                    try {
709 <                        q.take();
710 <                        threadShouldThrow();
711 <                    } catch (InterruptedException success){ }
712 <                }
713 <            });
714 <        try {
715 <            t.start();
716 <            Thread.sleep(SHORT_DELAY_MS);
717 <            t.interrupt();
718 <            t.join();
719 <        } catch (Exception e){
720 <            unexpectedException();
685 >    public void testTake() throws InterruptedException {
686 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
687 >        for (int i = 0; i < SIZE; ++i) {
688 >            mustEqual(i, q.take());
689          }
690      }
691  
692      /**
693 <     * Take removes existing elements until empty, then blocks interruptibly
694 <     */
695 <    public void testBlockingTake() {
696 <        Thread t = new Thread(new Runnable() {
697 <                public void run() {
698 <                    try {
699 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
700 <                        for (int i = 0; i < SIZE; ++i) {
701 <                            assertEquals(i, ((Integer)q.take()).intValue());
702 <                        }
703 <                        q.take();
704 <                        threadShouldThrow();
705 <                    } catch (InterruptedException success){
706 <                    }
707 <                }});
740 <        t.start();
741 <        try {
742 <           Thread.sleep(SHORT_DELAY_MS);
743 <           t.interrupt();
744 <           t.join();
745 <        }
746 <        catch (InterruptedException ie) {
747 <            unexpectedException();
748 <        }
749 <    }
693 >     * take removes existing elements until empty, then blocks interruptibly
694 >     */
695 >    public void testBlockingTake() throws InterruptedException {
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++) mustEqual(i, q.take());
701 >
702 >                Thread.currentThread().interrupt();
703 >                try {
704 >                    q.take();
705 >                    shouldThrow();
706 >                } catch (InterruptedException success) {}
707 >                assertFalse(Thread.interrupted());
708  
709 +                pleaseInterrupt.countDown();
710 +                try {
711 +                    q.take();
712 +                    shouldThrow();
713 +                } catch (InterruptedException success) {}
714 +                assertFalse(Thread.interrupted());
715 +            }});
716 +
717 +        await(pleaseInterrupt);
718 +        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
719 +        t.interrupt();
720 +        awaitTermination(t);
721 +    }
722  
723      /**
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, ((Integer)q.poll()).intValue());
729 >            mustEqual(i, q.poll());
730          }
731 <        assertNull(q.poll());
731 >        assertNull(q.poll());
732      }
733  
734      /**
735       * timed poll with zero timeout succeeds when non-empty, else times out
736       */
737 <    public void testTimedPoll0() {
738 <        try {
739 <            LinkedBlockingDeque q = populatedDeque(SIZE);
740 <            for (int i = 0; i < SIZE; ++i) {
741 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
742 <            }
772 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
773 <        } catch (InterruptedException e){
774 <            unexpectedException();
775 <        }
737 >    public void testTimedPoll0() throws InterruptedException {
738 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
739 >        for (int i = 0; i < SIZE; ++i) {
740 >            mustEqual(i, q.poll(0, MILLISECONDS));
741 >        }
742 >        assertNull(q.poll(0, MILLISECONDS));
743      }
744  
745      /**
746       * timed poll with nonzero timeout succeeds when non-empty, else times out
747       */
748 <    public void testTimedPoll() {
749 <        try {
750 <            LinkedBlockingDeque q = populatedDeque(SIZE);
751 <            for (int i = 0; i < SIZE; ++i) {
752 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
753 <            }
754 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
755 <        } catch (InterruptedException e){
756 <            unexpectedException();
757 <        }
748 >    public void testTimedPoll() throws InterruptedException {
749 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
750 >        for (int i = 0; i < SIZE; ++i) {
751 >            long startTime = System.nanoTime();
752 >            mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
753 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
754 >        }
755 >        long startTime = System.nanoTime();
756 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
757 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
758 >        checkEmpty(q);
759      }
760  
761      /**
762       * Interrupted timed poll throws InterruptedException instead of
763       * returning timeout status
764       */
765 <    public void testInterruptedTimedPoll() {
766 <        Thread t = new Thread(new Runnable() {
767 <                public void run() {
768 <                    try {
769 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
770 <                        for (int i = 0; i < SIZE; ++i) {
771 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
804 <                        }
805 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
806 <                    } catch (InterruptedException success){
807 <                    }
808 <                }});
809 <        t.start();
810 <        try {
811 <           Thread.sleep(SHORT_DELAY_MS);
812 <           t.interrupt();
813 <           t.join();
814 <        }
815 <        catch (InterruptedException ie) {
816 <            unexpectedException();
817 <        }
818 <    }
765 >    public void testInterruptedTimedPoll() throws InterruptedException {
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 >                for (int i = 0; i < SIZE; i++)
771 >                    mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
772  
773 <    /**
774 <     *  timed poll before a delayed offer fails; after offer succeeds;
775 <     *  on interruption throws
776 <     */
777 <    public void testTimedPollWithOffer() {
778 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
826 <        Thread t = new Thread(new Runnable() {
827 <                public void run() {
828 <                    try {
829 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
830 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
831 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
832 <                        threadShouldThrow();
833 <                    } catch (InterruptedException success) { }
834 <                }
835 <            });
836 <        try {
837 <            t.start();
838 <            Thread.sleep(SMALL_DELAY_MS);
839 <            assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
840 <            t.interrupt();
841 <            t.join();
842 <        } catch (Exception e){
843 <            unexpectedException();
844 <        }
845 <    }
773 >                Thread.currentThread().interrupt();
774 >                try {
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 +                assertFalse(Thread.interrupted());
786 +            }});
787 +
788 +        await(pleaseInterrupt);
789 +        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
790 +        t.interrupt();
791 +        awaitTermination(t);
792 +        checkEmpty(q);
793 +    }
794  
795      /**
796       * putFirst(null) throws NPE
797       */
798 <     public void testPutFirstNull() {
799 <        try {
800 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
798 >    public void testPutFirstNull() throws InterruptedException {
799 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
800 >        try {
801              q.putFirst(null);
802              shouldThrow();
803 <        }
804 <        catch (NullPointerException success){
858 <        }
859 <        catch (InterruptedException ie) {
860 <            unexpectedException();
861 <        }
862 <     }
803 >        } catch (NullPointerException success) {}
804 >    }
805  
806      /**
807       * all elements successfully putFirst are contained
808       */
809 <     public void testPutFirst() {
810 <         try {
811 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
812 <             for (int i = 0; i < SIZE; ++i) {
813 <                 Integer I = new Integer(i);
814 <                 q.putFirst(I);
873 <                 assertTrue(q.contains(I));
874 <             }
875 <             assertEquals(0, q.remainingCapacity());
876 <         }
877 <        catch (InterruptedException ie) {
878 <            unexpectedException();
809 >    public void testPutFirst() throws InterruptedException {
810 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
811 >        for (int i = 0; i < SIZE; ++i) {
812 >            Item x = itemFor(i);
813 >            q.putFirst(x);
814 >            mustContain(q, x);
815          }
816 +        mustEqual(0, q.remainingCapacity());
817      }
818  
819      /**
820       * putFirst blocks interruptibly if full
821       */
822 <    public void testBlockingPutFirst() {
823 <        Thread t = new Thread(new Runnable() {
824 <                public void run() {
825 <                    int added = 0;
826 <                    try {
827 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
828 <                        for (int i = 0; i < SIZE; ++i) {
829 <                            q.putFirst(new Integer(i));
830 <                            ++added;
831 <                        }
832 <                        q.putFirst(new Integer(SIZE));
833 <                        threadShouldThrow();
834 <                    } catch (InterruptedException ie){
835 <                        threadAssertEquals(added, SIZE);
836 <                    }
837 <                }});
838 <        t.start();
839 <        try {
840 <           Thread.sleep(SHORT_DELAY_MS);
841 <           t.interrupt();
842 <           t.join();
843 <        }
844 <        catch (InterruptedException ie) {
845 <            unexpectedException();
846 <        }
822 >    public void testBlockingPutFirst() throws InterruptedException {
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(itemFor(i));
829 >                mustEqual(SIZE, q.size());
830 >                mustEqual(0, q.remainingCapacity());
831 >
832 >                Thread.currentThread().interrupt();
833 >                try {
834 >                    q.putFirst(ninetynine);
835 >                    shouldThrow();
836 >                } catch (InterruptedException success) {}
837 >                assertFalse(Thread.interrupted());
838 >
839 >                pleaseInterrupt.countDown();
840 >                try {
841 >                    q.putFirst(ninetynine);
842 >                    shouldThrow();
843 >                } catch (InterruptedException success) {}
844 >                assertFalse(Thread.interrupted());
845 >            }});
846 >
847 >        await(pleaseInterrupt);
848 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
849 >        t.interrupt();
850 >        awaitTermination(t);
851 >        mustEqual(SIZE, q.size());
852 >        mustEqual(0, q.remainingCapacity());
853      }
854  
855      /**
856 <     * putFirst blocks waiting for take when full
857 <     */
858 <    public void testPutFirstWithTake() {
859 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
860 <        Thread t = new Thread(new Runnable() {
861 <                public void run() {
862 <                    int added = 0;
863 <                    try {
864 <                        q.putFirst(new Object());
865 <                        ++added;
866 <                        q.putFirst(new Object());
867 <                        ++added;
868 <                        q.putFirst(new Object());
869 <                        ++added;
870 <                        q.putFirst(new Object());
871 <                        ++added;
872 <                        threadShouldThrow();
873 <                    } catch (InterruptedException e){
874 <                        threadAssertTrue(added >= 2);
875 <                    }
876 <                }
877 <            });
878 <        try {
879 <            t.start();
880 <            Thread.sleep(SHORT_DELAY_MS);
881 <            q.take();
882 <            t.interrupt();
883 <            t.join();
884 <        } catch (Exception e){
885 <            unexpectedException();
886 <        }
856 >     * putFirst blocks interruptibly waiting for take when full
857 >     */
858 >    public void testPutFirstWithTake() throws InterruptedException {
859 >        final int capacity = 2;
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(itemFor(i));
867 >                pleaseTake.countDown();
868 >                q.putFirst(eightysix);
869 >
870 >                pleaseInterrupt.countDown();
871 >                try {
872 >                    q.putFirst(ninetynine);
873 >                    shouldThrow();
874 >                } catch (InterruptedException success) {}
875 >                assertFalse(Thread.interrupted());
876 >            }});
877 >
878 >        await(pleaseTake);
879 >        mustEqual(0, q.remainingCapacity());
880 >        mustEqual(capacity - 1, q.take());
881 >
882 >        await(pleaseInterrupt);
883 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
884 >        t.interrupt();
885 >        awaitTermination(t);
886 >        mustEqual(0, q.remainingCapacity());
887      }
888  
889      /**
890       * timed offerFirst times out if full and elements not taken
891       */
892      public void testTimedOfferFirst() {
893 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
894 <        Thread t = new Thread(new Runnable() {
895 <                public void run() {
896 <                    try {
897 <                        q.putFirst(new Object());
898 <                        q.putFirst(new Object());
899 <                        threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
900 <                        q.offerFirst(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
901 <                        threadShouldThrow();
902 <                    } catch (InterruptedException success){}
903 <                }
904 <            });
905 <
906 <        try {
907 <            t.start();
908 <            Thread.sleep(SMALL_DELAY_MS);
909 <            t.interrupt();
910 <            t.join();
911 <        } catch (Exception e){
912 <            unexpectedException();
913 <        }
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(zero);
898 >                q.putFirst(one);
899 >                long startTime = System.nanoTime();
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(four, LONGER_DELAY_MS, MILLISECONDS);
914 >                    shouldThrow();
915 >                } catch (InterruptedException success) {}
916 >                assertFalse(Thread.interrupted());
917 >            }});
918 >
919 >        await(pleaseInterrupt);
920 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
921 >        t.interrupt();
922 >        awaitTermination(t);
923      }
924  
925      /**
926       * take retrieves elements in FIFO order
927       */
928 <    public void testTakeFirst() {
929 <        try {
930 <            LinkedBlockingDeque q = populatedDeque(SIZE);
931 <            for (int i = 0; i < SIZE; ++i) {
932 <                assertEquals(i, ((Integer)q.takeFirst()).intValue());
981 <            }
982 <        } catch (InterruptedException e){
983 <            unexpectedException();
984 <        }
928 >    public void testTakeFirst() throws InterruptedException {
929 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
930 >        for (int i = 0; i < SIZE; ++i) {
931 >            mustEqual(i, q.takeFirst());
932 >        }
933      }
934  
935      /**
936 <     * takeFirst blocks interruptibly when empty
937 <     */
938 <    public void testTakeFirstFromEmpty() {
939 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
940 <        Thread t = new Thread(new Runnable() {
941 <                public void run() {
942 <                    try {
943 <                        q.takeFirst();
944 <                        threadShouldThrow();
945 <                    } catch (InterruptedException success){ }
946 <                }
947 <            });
948 <        try {
949 <            t.start();
950 <            Thread.sleep(SHORT_DELAY_MS);
951 <            t.interrupt();
952 <            t.join();
953 <        } catch (Exception e){
954 <            unexpectedException();
1007 <        }
936 >     * takeFirst() blocks interruptibly when empty
937 >     */
938 >    public void testTakeFirstFromEmptyBlocksInterruptibly() {
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() {
943 >                threadStarted.countDown();
944 >                try {
945 >                    q.takeFirst();
946 >                    shouldThrow();
947 >                } catch (InterruptedException success) {}
948 >                assertFalse(Thread.interrupted());
949 >            }});
950 >
951 >        await(threadStarted);
952 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
953 >        t.interrupt();
954 >        awaitTermination(t);
955      }
956  
957      /**
958 <     * TakeFirst removes existing elements until empty, then blocks interruptibly
959 <     */
960 <    public void testBlockingTakeFirst() {
961 <        Thread t = new Thread(new Runnable() {
962 <                public void run() {
963 <                    try {
964 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
965 <                        for (int i = 0; i < SIZE; ++i) {
966 <                            assertEquals(i, ((Integer)q.takeFirst()).intValue());
967 <                        }
968 <                        q.takeFirst();
969 <                        threadShouldThrow();
970 <                    } catch (InterruptedException success){
971 <                    }
972 <                }});
973 <        t.start();
1027 <        try {
1028 <           Thread.sleep(SHORT_DELAY_MS);
1029 <           t.interrupt();
1030 <           t.join();
1031 <        }
1032 <        catch (InterruptedException ie) {
1033 <            unexpectedException();
1034 <        }
958 >     * takeFirst() throws InterruptedException immediately if interrupted
959 >     * before waiting
960 >     */
961 >    public void testTakeFirstFromEmptyAfterInterrupt() {
962 >        final BlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
963 >        Thread t = newStartedThread(new CheckedRunnable() {
964 >            public void realRun() {
965 >                Thread.currentThread().interrupt();
966 >                try {
967 >                    q.takeFirst();
968 >                    shouldThrow();
969 >                } catch (InterruptedException success) {}
970 >                assertFalse(Thread.interrupted());
971 >            }});
972 >
973 >        awaitTermination(t);
974      }
975  
976 +    /**
977 +     * takeLast() blocks interruptibly when empty
978 +     */
979 +    public void testTakeLastFromEmptyBlocksInterruptibly() {
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() {
984 +                threadStarted.countDown();
985 +                try {
986 +                    q.takeLast();
987 +                    shouldThrow();
988 +                } catch (InterruptedException success) {}
989 +                assertFalse(Thread.interrupted());
990 +            }});
991 +
992 +        await(threadStarted);
993 +        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
994 +        t.interrupt();
995 +        awaitTermination(t);
996 +    }
997 +
998 +    /**
999 +     * takeLast() throws InterruptedException immediately if interrupted
1000 +     * before waiting
1001 +     */
1002 +    public void testTakeLastFromEmptyAfterInterrupt() {
1003 +        final BlockingDeque<Item> q = new LinkedBlockingDeque<Item>();
1004 +        Thread t = newStartedThread(new CheckedRunnable() {
1005 +            public void realRun() {
1006 +                Thread.currentThread().interrupt();
1007 +                try {
1008 +                    q.takeLast();
1009 +                    shouldThrow();
1010 +                } catch (InterruptedException success) {}
1011 +                assertFalse(Thread.interrupted());
1012 +            }});
1013 +
1014 +        awaitTermination(t);
1015 +    }
1016 +
1017 +    /**
1018 +     * takeFirst removes existing elements until empty, then blocks interruptibly
1019 +     */
1020 +    public void testBlockingTakeFirst() throws InterruptedException {
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++) mustEqual(i, q.takeFirst());
1026 +
1027 +                Thread.currentThread().interrupt();
1028 +                try {
1029 +                    q.takeFirst();
1030 +                    shouldThrow();
1031 +                } catch (InterruptedException success) {}
1032 +                assertFalse(Thread.interrupted());
1033 +
1034 +                pleaseInterrupt.countDown();
1035 +                try {
1036 +                    q.takeFirst();
1037 +                    shouldThrow();
1038 +                } catch (InterruptedException success) {}
1039 +                assertFalse(Thread.interrupted());
1040 +            }});
1041 +
1042 +        await(pleaseInterrupt);
1043 +        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1044 +        t.interrupt();
1045 +        awaitTermination(t);
1046 +    }
1047  
1048      /**
1049       * timed pollFirst with zero timeout succeeds when non-empty, else times out
1050       */
1051 <    public void testTimedPollFirst0() {
1052 <        try {
1053 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1054 <            for (int i = 0; i < SIZE; ++i) {
1055 <                assertEquals(i, ((Integer)q.pollFirst(0, TimeUnit.MILLISECONDS)).intValue());
1056 <            }
1047 <            assertNull(q.pollFirst(0, TimeUnit.MILLISECONDS));
1048 <        } catch (InterruptedException e){
1049 <            unexpectedException();
1050 <        }
1051 >    public void testTimedPollFirst0() throws InterruptedException {
1052 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1053 >        for (int i = 0; i < SIZE; ++i) {
1054 >            mustEqual(i, q.pollFirst(0, MILLISECONDS));
1055 >        }
1056 >        assertNull(q.pollFirst(0, MILLISECONDS));
1057      }
1058  
1059      /**
1060       * timed pollFirst with nonzero timeout succeeds when non-empty, else times out
1061       */
1062 <    public void testTimedPollFirst() {
1063 <        try {
1064 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1065 <            for (int i = 0; i < SIZE; ++i) {
1066 <                assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1067 <            }
1068 <            assertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1069 <        } catch (InterruptedException e){
1070 <            unexpectedException();
1071 <        }
1062 >    public void testTimedPollFirst() throws InterruptedException {
1063 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1064 >        for (int i = 0; i < SIZE; ++i) {
1065 >            long startTime = System.nanoTime();
1066 >            mustEqual(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1067 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1068 >        }
1069 >        long startTime = System.nanoTime();
1070 >        assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1071 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1072 >        checkEmpty(q);
1073      }
1074  
1075      /**
1076       * Interrupted timed pollFirst throws InterruptedException instead of
1077       * returning timeout status
1078       */
1079 <    public void testInterruptedTimedPollFirst() {
1080 <        Thread t = new Thread(new Runnable() {
1081 <                public void run() {
1082 <                    try {
1083 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1084 <                        for (int i = 0; i < SIZE; ++i) {
1085 <                            threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1086 <                        }
1087 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1088 <                    } catch (InterruptedException success){
1089 <                    }
1090 <                }});
1091 <        t.start();
1092 <        try {
1093 <           Thread.sleep(SHORT_DELAY_MS);
1094 <           t.interrupt();
1095 <           t.join();
1096 <        }
1097 <        catch (InterruptedException ie) {
1098 <            unexpectedException();
1099 <        }
1079 >    public void testInterruptedTimedPollFirst() throws InterruptedException {
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 >                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(randomTimeout(), randomTimeUnit());
1090 >                    shouldThrow();
1091 >                } catch (InterruptedException success) {}
1092 >                assertFalse(Thread.interrupted());
1093 >
1094 >                pleaseInterrupt.countDown();
1095 >                try {
1096 >                    q.pollFirst(LONGER_DELAY_MS, MILLISECONDS);
1097 >                    shouldThrow();
1098 >                } catch (InterruptedException success) {}
1099 >                assertFalse(Thread.interrupted());
1100 >            }});
1101 >
1102 >        await(pleaseInterrupt);
1103 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1104 >        t.interrupt();
1105 >        awaitTermination(t);
1106      }
1107  
1108      /**
1109 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1110 <     *  on interruption throws
1111 <     */
1112 <    public void testTimedPollFirstWithOfferFirst() {
1113 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1114 <        Thread t = new Thread(new Runnable() {
1115 <                public void run() {
1116 <                    try {
1117 <                        threadAssertNull(q.pollFirst(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1118 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1119 <                        q.pollFirst(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1120 <                        threadShouldThrow();
1121 <                    } catch (InterruptedException success) { }
1122 <                }
1123 <            });
1124 <        try {
1125 <            t.start();
1126 <            Thread.sleep(SMALL_DELAY_MS);
1127 <            assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1128 <            t.interrupt();
1129 <            t.join();
1130 <        } catch (Exception e){
1131 <            unexpectedException();
1132 <        }
1109 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
1110 >     * on interruption throws
1111 >     */
1112 >    public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
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 {
1117 >                long startTime = System.nanoTime();
1118 >                assertNull(q.pollFirst(timeoutMillis(), MILLISECONDS));
1119 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1120 >
1121 >                barrier.await();
1122 >
1123 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1124 >
1125 >                Thread.currentThread().interrupt();
1126 >                try {
1127 >                    q.pollFirst(randomTimeout(), randomTimeUnit());
1128 >                    shouldThrow();
1129 >                } catch (InterruptedException success) {}
1130 >
1131 >                barrier.await();
1132 >                try {
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 >
1141 >        barrier.await();
1142 >        long startTime = System.nanoTime();
1143 >        assertTrue(q.offerFirst(zero, LONG_DELAY_MS, MILLISECONDS));
1144 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1145 >        barrier.await();
1146 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1147 >        t.interrupt();
1148 >        awaitTermination(t);
1149      }
1150  
1151      /**
1152       * putLast(null) throws NPE
1153       */
1154 <     public void testPutLastNull() {
1155 <        try {
1156 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1154 >    public void testPutLastNull() throws InterruptedException {
1155 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
1156 >        try {
1157              q.putLast(null);
1158              shouldThrow();
1159 <        }
1160 <        catch (NullPointerException success){
1132 <        }
1133 <        catch (InterruptedException ie) {
1134 <            unexpectedException();
1135 <        }
1136 <     }
1159 >        } catch (NullPointerException success) {}
1160 >    }
1161  
1162      /**
1163       * all elements successfully putLast are contained
1164       */
1165 <     public void testPutLast() {
1166 <         try {
1167 <             LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1168 <             for (int i = 0; i < SIZE; ++i) {
1169 <                 Integer I = new Integer(i);
1170 <                 q.putLast(I);
1147 <                 assertTrue(q.contains(I));
1148 <             }
1149 <             assertEquals(0, q.remainingCapacity());
1150 <         }
1151 <        catch (InterruptedException ie) {
1152 <            unexpectedException();
1165 >    public void testPutLast() throws InterruptedException {
1166 >        LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(SIZE);
1167 >        for (int i = 0; i < SIZE; ++i) {
1168 >            Item x = itemFor(i);
1169 >            q.putLast(x);
1170 >            mustContain(q, x);
1171          }
1172 +        mustEqual(0, q.remainingCapacity());
1173      }
1174  
1175      /**
1176       * putLast blocks interruptibly if full
1177       */
1178 <    public void testBlockingPutLast() {
1179 <        Thread t = new Thread(new Runnable() {
1180 <                public void run() {
1181 <                    int added = 0;
1182 <                    try {
1183 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1184 <                        for (int i = 0; i < SIZE; ++i) {
1185 <                            q.putLast(new Integer(i));
1186 <                            ++added;
1187 <                        }
1188 <                        q.putLast(new Integer(SIZE));
1189 <                        threadShouldThrow();
1190 <                    } catch (InterruptedException ie){
1191 <                        threadAssertEquals(added, SIZE);
1192 <                    }
1193 <                }});
1194 <        t.start();
1195 <        try {
1196 <           Thread.sleep(SHORT_DELAY_MS);
1197 <           t.interrupt();
1198 <           t.join();
1199 <        }
1200 <        catch (InterruptedException ie) {
1201 <            unexpectedException();
1202 <        }
1178 >    public void testBlockingPutLast() throws InterruptedException {
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(itemFor(i));
1185 >                mustEqual(SIZE, q.size());
1186 >                mustEqual(0, q.remainingCapacity());
1187 >
1188 >                Thread.currentThread().interrupt();
1189 >                try {
1190 >                    q.putLast(ninetynine);
1191 >                    shouldThrow();
1192 >                } catch (InterruptedException success) {}
1193 >                assertFalse(Thread.interrupted());
1194 >
1195 >                pleaseInterrupt.countDown();
1196 >                try {
1197 >                    q.putLast(ninetynine);
1198 >                    shouldThrow();
1199 >                } catch (InterruptedException success) {}
1200 >                assertFalse(Thread.interrupted());
1201 >            }});
1202 >
1203 >        await(pleaseInterrupt);
1204 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1205 >        t.interrupt();
1206 >        awaitTermination(t);
1207 >        mustEqual(SIZE, q.size());
1208 >        mustEqual(0, q.remainingCapacity());
1209      }
1210  
1211      /**
1212 <     * putLast blocks waiting for take when full
1213 <     */
1214 <    public void testPutLastWithTake() {
1215 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1216 <        Thread t = new Thread(new Runnable() {
1217 <                public void run() {
1218 <                    int added = 0;
1219 <                    try {
1220 <                        q.putLast(new Object());
1221 <                        ++added;
1222 <                        q.putLast(new Object());
1223 <                        ++added;
1224 <                        q.putLast(new Object());
1225 <                        ++added;
1226 <                        q.putLast(new Object());
1227 <                        ++added;
1228 <                        threadShouldThrow();
1229 <                    } catch (InterruptedException e){
1230 <                        threadAssertTrue(added >= 2);
1231 <                    }
1232 <                }
1233 <            });
1234 <        try {
1235 <            t.start();
1236 <            Thread.sleep(SHORT_DELAY_MS);
1237 <            q.take();
1238 <            t.interrupt();
1239 <            t.join();
1240 <        } catch (Exception e){
1241 <            unexpectedException();
1242 <        }
1212 >     * putLast blocks interruptibly waiting for take when full
1213 >     */
1214 >    public void testPutLastWithTake() throws InterruptedException {
1215 >        final int capacity = 2;
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(itemFor(i));
1223 >                pleaseTake.countDown();
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(ninetynine);
1236 >                    shouldThrow();
1237 >                } catch (InterruptedException success) {}
1238 >                assertFalse(Thread.interrupted());
1239 >            }});
1240 >
1241 >        await(pleaseTake);
1242 >        mustEqual(0, q.remainingCapacity());
1243 >        mustEqual(0, q.take());
1244 >
1245 >        await(pleaseInterrupt);
1246 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1247 >        t.interrupt();
1248 >        awaitTermination(t);
1249 >        mustEqual(0, q.remainingCapacity());
1250      }
1251  
1252      /**
1253       * timed offerLast times out if full and elements not taken
1254       */
1255      public void testTimedOfferLast() {
1256 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1257 <        Thread t = new Thread(new Runnable() {
1258 <                public void run() {
1259 <                    try {
1260 <                        q.putLast(new Object());
1261 <                        q.putLast(new Object());
1262 <                        threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1263 <                        q.offerLast(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1264 <                        threadShouldThrow();
1265 <                    } catch (InterruptedException success){}
1266 <                }
1267 <            });
1268 <
1269 <        try {
1270 <            t.start();
1271 <            Thread.sleep(SMALL_DELAY_MS);
1272 <            t.interrupt();
1273 <            t.join();
1274 <        } catch (Exception e){
1275 <            unexpectedException();
1276 <        }
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(zero);
1261 >                q.putLast(one);
1262 >                long startTime = System.nanoTime();
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(four, LONGER_DELAY_MS, MILLISECONDS);
1276 >                    shouldThrow();
1277 >                } catch (InterruptedException success) {}
1278 >            }});
1279 >
1280 >        await(pleaseInterrupt);
1281 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1282 >        t.interrupt();
1283 >        awaitTermination(t);
1284      }
1285  
1286      /**
1287       * takeLast retrieves elements in FIFO order
1288       */
1289 <    public void testTakeLast() {
1290 <        try {
1291 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1292 <            for (int i = 0; i < SIZE; ++i) {
1254 <                assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1255 <            }
1256 <        } catch (InterruptedException e){
1257 <            unexpectedException();
1258 <        }
1259 <    }
1260 <
1261 <    /**
1262 <     * takeLast blocks interruptibly when empty
1263 <     */
1264 <    public void testTakeLastFromEmpty() {
1265 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1266 <        Thread t = new Thread(new Runnable() {
1267 <                public void run() {
1268 <                    try {
1269 <                        q.takeLast();
1270 <                        threadShouldThrow();
1271 <                    } catch (InterruptedException success){ }
1272 <                }
1273 <            });
1274 <        try {
1275 <            t.start();
1276 <            Thread.sleep(SHORT_DELAY_MS);
1277 <            t.interrupt();
1278 <            t.join();
1279 <        } catch (Exception e){
1280 <            unexpectedException();
1289 >    public void testTakeLast() throws InterruptedException {
1290 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1291 >        for (int i = 0; i < SIZE; ++i) {
1292 >            mustEqual(SIZE - i - 1, q.takeLast());
1293          }
1294      }
1295  
1296      /**
1297 <     * TakeLast removes existing elements until empty, then blocks interruptibly
1298 <     */
1299 <    public void testBlockingTakeLast() {
1300 <        Thread t = new Thread(new Runnable() {
1301 <                public void run() {
1302 <                    try {
1303 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1304 <                        for (int i = 0; i < SIZE; ++i) {
1305 <                            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1294 <                        }
1295 <                        q.takeLast();
1296 <                        threadShouldThrow();
1297 <                    } catch (InterruptedException success){
1298 <                    }
1299 <                }});
1300 <        t.start();
1301 <        try {
1302 <           Thread.sleep(SHORT_DELAY_MS);
1303 <           t.interrupt();
1304 <           t.join();
1305 <        }
1306 <        catch (InterruptedException ie) {
1307 <            unexpectedException();
1308 <        }
1309 <    }
1297 >     * takeLast removes existing elements until empty, then blocks interruptibly
1298 >     */
1299 >    public void testBlockingTakeLast() throws InterruptedException {
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 >                    mustEqual(SIZE - i - 1, q.takeLast());
1306  
1307 +                Thread.currentThread().interrupt();
1308 +                try {
1309 +                    q.takeLast();
1310 +                    shouldThrow();
1311 +                } catch (InterruptedException success) {}
1312 +                assertFalse(Thread.interrupted());
1313 +
1314 +                pleaseInterrupt.countDown();
1315 +                try {
1316 +                    q.takeLast();
1317 +                    shouldThrow();
1318 +                } catch (InterruptedException success) {}
1319 +                assertFalse(Thread.interrupted());
1320 +            }});
1321 +
1322 +        await(pleaseInterrupt);
1323 +        if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
1324 +        t.interrupt();
1325 +        awaitTermination(t);
1326 +    }
1327  
1328      /**
1329       * timed pollLast with zero timeout succeeds when non-empty, else times out
1330       */
1331 <    public void testTimedPollLast0() {
1332 <        try {
1333 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1334 <            for (int i = 0; i < SIZE; ++i) {
1335 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, TimeUnit.MILLISECONDS)).intValue());
1336 <            }
1321 <            assertNull(q.pollLast(0, TimeUnit.MILLISECONDS));
1322 <        } catch (InterruptedException e){
1323 <            unexpectedException();
1324 <        }
1331 >    public void testTimedPollLast0() throws InterruptedException {
1332 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1333 >        for (int i = 0; i < SIZE; ++i) {
1334 >            mustEqual(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1335 >        }
1336 >        assertNull(q.pollLast(0, MILLISECONDS));
1337      }
1338  
1339      /**
1340       * timed pollLast with nonzero timeout succeeds when non-empty, else times out
1341       */
1342 <    public void testTimedPollLast() {
1343 <        try {
1344 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1345 <            for (int i = 0; i < SIZE; ++i) {
1346 <                assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1347 <            }
1348 <            assertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1349 <        } catch (InterruptedException e){
1350 <            unexpectedException();
1351 <        }
1342 >    public void testTimedPollLast() throws InterruptedException {
1343 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1344 >        for (int i = 0; i < SIZE; ++i) {
1345 >            long startTime = System.nanoTime();
1346 >            mustEqual(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1347 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1348 >        }
1349 >        long startTime = System.nanoTime();
1350 >        assertNull(q.pollLast(timeoutMillis(), MILLISECONDS));
1351 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1352 >        checkEmpty(q);
1353      }
1354  
1355      /**
1356       * Interrupted timed pollLast throws InterruptedException instead of
1357       * returning timeout status
1358       */
1359 <    public void testInterruptedTimedPollLast() {
1360 <        Thread t = new Thread(new Runnable() {
1361 <                public void run() {
1362 <                    try {
1363 <                        LinkedBlockingDeque q = populatedDeque(SIZE);
1364 <                        for (int i = 0; i < SIZE; ++i) {
1365 <                            threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
1366 <                        }
1367 <                        threadAssertNull(q.pollLast(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1368 <                    } catch (InterruptedException success){
1369 <                    }
1370 <                }});
1371 <        t.start();
1372 <        try {
1373 <           Thread.sleep(SHORT_DELAY_MS);
1374 <           t.interrupt();
1375 <           t.join();
1376 <        }
1377 <        catch (InterruptedException ie) {
1378 <            unexpectedException();
1379 <        }
1359 >    public void testInterruptedTimedPollLast() throws InterruptedException {
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 >                for (int i = 0; i < SIZE; i++)
1365 >                    mustEqual(SIZE - i - 1,
1366 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1367 >
1368 >                Thread.currentThread().interrupt();
1369 >                try {
1370 >                    q.pollLast(randomTimeout(), randomTimeUnit());
1371 >                    shouldThrow();
1372 >                } catch (InterruptedException success) {}
1373 >                assertFalse(Thread.interrupted());
1374 >
1375 >                pleaseInterrupt.countDown();
1376 >                try {
1377 >                    q.pollLast(LONGER_DELAY_MS, MILLISECONDS);
1378 >                    shouldThrow();
1379 >                } catch (InterruptedException success) {}
1380 >                assertFalse(Thread.interrupted());
1381 >            }});
1382 >
1383 >        await(pleaseInterrupt);
1384 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1385 >        t.interrupt();
1386 >        awaitTermination(t);
1387 >        checkEmpty(q);
1388      }
1389  
1390      /**
1391 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1392 <     *  on interruption throws
1393 <     */
1394 <    public void testTimedPollWithOfferLast() {
1395 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1396 <        Thread t = new Thread(new Runnable() {
1397 <                public void run() {
1398 <                    try {
1399 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1400 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1401 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
1381 <                        threadShouldThrow();
1382 <                    } catch (InterruptedException success) { }
1383 <                }
1384 <            });
1385 <        try {
1386 <            t.start();
1387 <            Thread.sleep(SMALL_DELAY_MS);
1388 <            assertTrue(q.offerLast(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
1389 <            t.interrupt();
1390 <            t.join();
1391 <        } catch (Exception e){
1392 <            unexpectedException();
1393 <        }
1394 <    }
1391 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1392 >     * on interruption throws
1393 >     */
1394 >    public void testTimedPollWithOfferLast() throws InterruptedException {
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 {
1399 >                long startTime = System.nanoTime();
1400 >                assertNull(q.poll(timeoutMillis(), MILLISECONDS));
1401 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1402  
1403 +                barrier.await();
1404 +
1405 +                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
1406 +
1407 +                Thread.currentThread().interrupt();
1408 +                try {
1409 +                    q.poll(randomTimeout(), randomTimeUnit());
1410 +                    shouldThrow();
1411 +                } catch (InterruptedException success) {}
1412 +                assertFalse(Thread.interrupted());
1413 +
1414 +                barrier.await();
1415 +                try {
1416 +                    q.poll(LONG_DELAY_MS, MILLISECONDS);
1417 +                    shouldThrow();
1418 +                } catch (InterruptedException success) {}
1419 +                assertFalse(Thread.interrupted());
1420 +
1421 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1422 +            }});
1423 +
1424 +        barrier.await();
1425 +        long startTime = System.nanoTime();
1426 +        assertTrue(q.offerLast(zero, LONG_DELAY_MS, MILLISECONDS));
1427 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1428 +
1429 +        barrier.await();
1430 +        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
1431 +        t.interrupt();
1432 +        awaitTermination(t);
1433 +    }
1434  
1435      /**
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, ((Integer)q.element()).intValue());
1441 >            mustEqual(i, q.element());
1442              q.poll();
1443          }
1444          try {
1445              q.element();
1446              shouldThrow();
1447 <        }
1410 <        catch (NoSuchElementException success) {}
1411 <    }
1412 <
1413 <    /**
1414 <     * remove(x) removes x and returns true if present
1415 <     */
1416 <    public void testRemoveElement() {
1417 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1418 <        for (int i = 1; i < SIZE; i+=2) {
1419 <            assertTrue(q.remove(new Integer(i)));
1420 <        }
1421 <        for (int i = 0; i < SIZE; i+=2) {
1422 <            assertTrue(q.remove(new Integer(i)));
1423 <            assertFalse(q.remove(new Integer(i+1)));
1424 <        }
1425 <        assertTrue(q.isEmpty());
1447 >        } catch (NoSuchElementException success) {}
1448      }
1449  
1450      /**
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 1441 | 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 1457 | 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 1471 | 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 1481 | 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 1491 | 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 I = (Integer)(p.remove());
1500 <                assertFalse(q.contains(I));
1521 >                mustNotContain(q, p.remove());
1522              }
1523          }
1524      }
1525  
1526      /**
1527 <     * toArray contains all elements
1527 >     * toArray contains all elements in FIFO order
1528       */
1529 <    public void testToArray() {
1530 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1531 <        Object[] o = q.toArray();
1532 <        try {
1533 <        for (int i = 0; i < o.length; i++)
1534 <            assertEquals(o[i], q.take());
1535 <        } catch (InterruptedException e){
1515 <            unexpectedException();
1516 <        }
1529 >    public void testToArray() throws InterruptedException {
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
1539 >     * toArray(a) contains all elements in FIFO order
1540       */
1541      public void testToArray2() {
1542 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1543 <        Integer[] ints = new Integer[SIZE];
1544 <        ints = (Integer[])q.toArray(ints);
1545 <        try {
1546 <            for (int i = 0; i < ints.length; i++)
1547 <                assertEquals(ints[i], q.take());
1548 <        } catch (InterruptedException e){
1530 <            unexpectedException();
1531 <        }
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(null) throws NPE
1552 >     * toArray(incompatible array type) throws ArrayStoreException
1553       */
1554 <    public void testToArray_BadArg() {
1555 <        try {
1556 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1557 <            Object o[] = q.toArray(null);
1558 <            shouldThrow();
1559 <        } catch (NullPointerException success){}
1554 >    public void testToArray1_BadArg() {
1555 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1556 >        try {
1557 >            q.toArray(new String[10]);
1558 >            shouldThrow();
1559 >        } catch (ArrayStoreException success) {}
1560      }
1561  
1562      /**
1563 <     * toArray with incompatible array type throws CCE
1563 >     * iterator iterates through all elements
1564       */
1565 <    public void testToArray1_BadArg() {
1566 <        try {
1567 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1568 <            Object o[] = q.toArray(new String[10] );
1569 <            shouldThrow();
1570 <        } catch (ArrayStoreException  success){}
1571 <    }
1565 >    public void testIterator() throws InterruptedException {
1566 >        LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1567 >        Iterator<? extends Item> it = q.iterator();
1568 >        int i;
1569 >        for (i = 0; it.hasNext(); i++)
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 +            mustEqual(it.next(), q.take());
1577 +        mustEqual(i, SIZE);
1578 +        assertIteratorExhausted(it);
1579 +    }
1580  
1581      /**
1582 <     * iterator iterates through all elements
1582 >     * iterator of empty collection has no elements
1583       */
1584 <    public void testIterator() {
1585 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1586 <        Iterator it = q.iterator();
1587 <        try {
1564 <            while (it.hasNext()){
1565 <                assertEquals(it.next(), q.take());
1566 <            }
1567 <        } catch (InterruptedException e){
1568 <            unexpectedException();
1569 <        }
1584 >    public void testEmptyIterator() {
1585 >        Deque<Item> c = new LinkedBlockingDeque<Item>();
1586 >        assertIteratorExhausted(c.iterator());
1587 >        assertIteratorExhausted(c.descendingIterator());
1588      }
1589  
1590      /**
1591       * iterator.remove removes current element
1592       */
1593 <    public void testIteratorRemove () {
1594 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1593 >    public void testIteratorRemove() {
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  
1603          it = q.iterator();
1604 <        assertEquals(it.next(), one);
1605 <        assertEquals(it.next(), three);
1604 >        assertSame(it.next(), one);
1605 >        assertSame(it.next(), three);
1606          assertFalse(it.hasNext());
1607      }
1608  
1591
1609      /**
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 <            int i = ((Integer)(it.next())).intValue();
1604 <            assertEquals(++k, i);
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);
1628 >    public void testWeaklyConsistentIteration() {
1629 >        final LinkedBlockingDeque<Item> q = new LinkedBlockingDeque<Item>(3);
1630          q.add(one);
1631          q.add(two);
1632          q.add(three);
1633 <        try {
1634 <            for (Iterator it = q.iterator(); it.hasNext();) {
1635 <                q.remove();
1620 <                it.next();
1621 <            }
1622 <        }
1623 <        catch (ConcurrentModificationException e) {
1624 <            unexpectedException();
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  
1629
1640      /**
1641 <     *  Descending iterator iterates through all elements
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();
1655 <        } catch (NoSuchElementException success) {
1656 <        }
1655 >            shouldThrow();
1656 >        } catch (NoSuchElementException success) {}
1657      }
1658  
1659      /**
1660 <     *  Descending iterator ordering is reverse FIFO
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 <                int i = ((Integer)(it.next())).intValue();
1661 <                assertEquals(++k, i);
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 1671 | Line 1681 | public class LinkedBlockingDequeTest ext
1681      /**
1682       * descendingIterator.remove removes current element
1683       */
1684 <    public void testDescendingIteratorRemove () {
1685 <        final LinkedBlockingDeque q = new LinkedBlockingDeque();
1684 >    public void testDescendingIteratorRemove() {
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();
1700          }
1701      }
1702  
1693
1703      /**
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.indexOf(String.valueOf(i)) >= 0);
1710 >            assertTrue(s.contains(String.valueOf(i)));
1711          }
1712      }
1713  
1705
1714      /**
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 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1722 <        executor.execute(new Runnable() {
1723 <            public void run() {
1724 <                threadAssertFalse(q.offer(three));
1725 <                try {
1726 <                    threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1727 <                    threadAssertEquals(0, q.remainingCapacity());
1728 <                }
1729 <                catch (InterruptedException e) {
1730 <                    threadUnexpectedException();
1723 <                }
1724 <            }
1725 <        });
1726 <
1727 <        executor.execute(new Runnable() {
1728 <            public void run() {
1729 <                try {
1730 <                    Thread.sleep(SMALL_DELAY_MS);
1731 <                    threadAssertEquals(one, q.take());
1732 <                }
1733 <                catch (InterruptedException e) {
1734 <                    threadUnexpectedException();
1735 <                }
1736 <            }
1737 <        });
1721 >        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1722 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1723 >        try (PoolCleaner cleaner = cleaner(executor)) {
1724 >            executor.execute(new CheckedRunnable() {
1725 >                public void realRun() throws InterruptedException {
1726 >                    assertFalse(q.offer(three));
1727 >                    threadsStarted.await();
1728 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1729 >                    mustEqual(0, q.remainingCapacity());
1730 >                }});
1731  
1732 <        joinPool(executor);
1732 >            executor.execute(new CheckedRunnable() {
1733 >                public void realRun() throws InterruptedException {
1734 >                    threadsStarted.await();
1735 >                    assertSame(one, q.take());
1736 >                }});
1737 >        }
1738      }
1739  
1740      /**
1741 <     * poll retrieves elements across Executor threads
1741 >     * timed poll retrieves elements across Executor threads
1742       */
1743      public void testPollInExecutor() {
1744 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1745 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1746 <        executor.execute(new Runnable() {
1747 <            public void run() {
1748 <                threadAssertNull(q.poll());
1749 <                try {
1750 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
1751 <                    threadAssertTrue(q.isEmpty());
1752 <                }
1753 <                catch (InterruptedException e) {
1754 <                    threadUnexpectedException();
1757 <                }
1758 <            }
1759 <        });
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)) {
1748 >            executor.execute(new CheckedRunnable() {
1749 >                public void realRun() throws InterruptedException {
1750 >                    assertNull(q.poll());
1751 >                    threadsStarted.await();
1752 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1753 >                    checkEmpty(q);
1754 >                }});
1755  
1756 <        executor.execute(new Runnable() {
1757 <            public void run() {
1758 <                try {
1764 <                    Thread.sleep(SMALL_DELAY_MS);
1756 >            executor.execute(new CheckedRunnable() {
1757 >                public void realRun() throws InterruptedException {
1758 >                    threadsStarted.await();
1759                      q.put(one);
1760 <                }
1767 <                catch (InterruptedException e) {
1768 <                    threadUnexpectedException();
1769 <                }
1770 <            }
1771 <        });
1772 <
1773 <        joinPool(executor);
1774 <    }
1775 <
1776 <    /**
1777 <     * A deserialized serialized deque has same elements in same order
1778 <     */
1779 <    public void testSerialization() {
1780 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1781 <
1782 <        try {
1783 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1784 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1785 <            out.writeObject(q);
1786 <            out.close();
1787 <
1788 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1789 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1790 <            LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1791 <            assertEquals(q.size(), r.size());
1792 <            while (!q.isEmpty())
1793 <                assertEquals(q.remove(), r.remove());
1794 <        } catch (Exception e){
1795 <            unexpectedException();
1796 <        }
1797 <    }
1798 <
1799 <    /**
1800 <     * drainTo(null) throws NPE
1801 <     */
1802 <    public void testDrainToNull() {
1803 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1804 <        try {
1805 <            q.drainTo(null);
1806 <            shouldThrow();
1807 <        } catch (NullPointerException success) {
1760 >                }});
1761          }
1762      }
1763  
1764      /**
1765 <     * drainTo(this) throws IAE
1765 >     * A deserialized/reserialized deque has same elements in same order
1766       */
1767 <    public void testDrainToSelf() {
1768 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1769 <        try {
1770 <            q.drainTo(q);
1771 <            shouldThrow();
1772 <        } catch (IllegalArgumentException success) {
1767 >    public void testSerialization() throws Exception {
1768 >        Queue<Item> x = populatedDeque(SIZE);
1769 >        Queue<Item> y = serialClone(x);
1770 >
1771 >        assertNotSame(y, x);
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 >            mustEqual(x.remove(), y.remove());
1778          }
1779 +        assertTrue(y.isEmpty());
1780      }
1781  
1782      /**
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(q.size(), 0);
1790 <        assertEquals(l.size(), 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(q.size(), 0);
1801 <        assertEquals(l.size(), 2);
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() {
1810 <        final LinkedBlockingDeque q = populatedDeque(SIZE);
1811 <        Thread t = new Thread(new Runnable() {
1812 <                public void run() {
1813 <                    try {
1814 <                        q.put(new Integer(SIZE+1));
1856 <                    } catch (InterruptedException ie){
1857 <                        threadUnexpectedException();
1858 <                    }
1859 <                }
1860 <            });
1861 <        try {
1862 <            t.start();
1863 <            ArrayList l = new ArrayList();
1864 <            q.drainTo(l);
1865 <            assertTrue(l.size() >= SIZE);
1866 <            for (int i = 0; i < SIZE; ++i)
1867 <                assertEquals(l.get(i), new Integer(i));
1868 <            t.join();
1869 <            assertTrue(q.size() + l.size() >= SIZE);
1870 <        } catch (Exception e){
1871 <            unexpectedException();
1872 <        }
1873 <    }
1809 >    public void testDrainToWithActivePut() throws InterruptedException {
1810 >        final LinkedBlockingDeque<Item> q = populatedDeque(SIZE);
1811 >        Thread t = new Thread(new CheckedRunnable() {
1812 >            public void realRun() throws InterruptedException {
1813 >                q.put(new Item(SIZE + 1));
1814 >            }});
1815  
1816 <    /**
1817 <     * drainTo(null, n) throws NPE
1818 <     */
1819 <    public void testDrainToNullN() {
1820 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1821 <        try {
1822 <            q.drainTo(null, 0);
1823 <            shouldThrow();
1883 <        } catch (NullPointerException success) {
1884 <        }
1885 <    }
1886 <
1887 <    /**
1888 <     * drainTo(this, n) throws IAE
1889 <     */
1890 <    public void testDrainToSelfN() {
1891 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1892 <        try {
1893 <            q.drainTo(q, 0);
1894 <            shouldThrow();
1895 <        } catch (IllegalArgumentException success) {
1896 <        }
1816 >        t.start();
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 >            mustEqual(l.get(i), i);
1822 >        t.join();
1823 >        assertTrue(q.size() + l.size() >= SIZE);
1824      }
1825  
1826      /**
1827 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
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(l.size(), k);
1838 <            assertEquals(q.size(), SIZE-k);
1836 >            int k = (i < SIZE) ? i : 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));
1841 <            while (q.poll() != null) ;
1840 >                mustEqual(l.get(j), j);
1841 >            do {} while (q.poll() != null);
1842 >        }
1843 >    }
1844 >
1845 >    /**
1846 >     * remove(null), contains(null) always return false
1847 >     */
1848 >    public void testNeverContainsNull() {
1849 >        Deque<?>[] qs = {
1850 >            new LinkedBlockingDeque<Object>(),
1851 >            populatedDeque(2),
1852 >        };
1853 >
1854 >        for (Deque<?> q : qs) {
1855 >            assertFalse(q.contains(null));
1856 >            assertFalse(q.remove(null));
1857 >            assertFalse(q.removeFirstOccurrence(null));
1858 >            assertFalse(q.removeLastOccurrence(null));
1859          }
1860      }
1861  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines