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.39 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.65 by jsr166, Sun Oct 16 20:44:18 2016 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 import junit.framework.*;
8 import java.util.*;
9 import java.util.concurrent.*;
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 < import java.io.*;
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  
# Line 20 | Line 32 | public class LinkedBlockingDequeTest ext
32  
33      public static class Bounded extends BlockingQueueTest {
34          protected BlockingQueue emptyCollection() {
35 <            return new LinkedBlockingDeque(20);
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() {
# Line 35 | Line 47 | public class LinkedBlockingDequeTest ext
47      }
48  
49      /**
50 <     * Create a deque of given size containing consecutive
51 <     * Integers 0 ... n.
50 >     * Returns a new deque of given size containing consecutive
51 >     * Integers 0 ... n - 1.
52       */
53      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
54          LinkedBlockingDeque<Integer> q =
# Line 47 | Line 59 | public class LinkedBlockingDequeTest ext
59          assertFalse(q.isEmpty());
60          assertEquals(0, q.remainingCapacity());
61          assertEquals(n, q.size());
62 +        assertEquals((Integer) 0, q.peekFirst());
63 +        assertEquals((Integer) (n - 1), q.peekLast());
64          return q;
65      }
66  
# Line 70 | Line 84 | public class LinkedBlockingDequeTest ext
84      public void testSize() {
85          LinkedBlockingDeque q = populatedDeque(SIZE);
86          for (int i = 0; i < SIZE; ++i) {
87 <            assertEquals(SIZE-i, q.size());
87 >            assertEquals(SIZE - i, q.size());
88              q.removeFirst();
89          }
90          for (int i = 0; i < SIZE; ++i) {
# Line 80 | Line 94 | public class LinkedBlockingDequeTest ext
94      }
95  
96      /**
97 <     * offer(null) throws NPE
97 >     * offerFirst(null) throws NullPointerException
98       */
99      public void testOfferFirstNull() {
100 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
101          try {
87            LinkedBlockingDeque q = new LinkedBlockingDeque();
102              q.offerFirst(null);
103              shouldThrow();
104          } catch (NullPointerException success) {}
105      }
106  
107      /**
108 +     * offerLast(null) throws NullPointerException
109 +     */
110 +    public void testOfferLastNull() {
111 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
112 +        try {
113 +            q.offerLast(null);
114 +            shouldThrow();
115 +        } catch (NullPointerException success) {}
116 +    }
117 +
118 +    /**
119       * OfferFirst succeeds
120       */
121      public void testOfferFirst() {
# Line 124 | Line 149 | public class LinkedBlockingDequeTest ext
149       */
150      public void testPollLast() {
151          LinkedBlockingDeque q = populatedDeque(SIZE);
152 <        for (int i = SIZE-1; i >= 0; --i) {
152 >        for (int i = SIZE - 1; i >= 0; --i) {
153              assertEquals(i, q.pollLast());
154          }
155          assertNull(q.pollLast());
# Line 163 | Line 188 | public class LinkedBlockingDequeTest ext
188       */
189      public void testPeekLast() {
190          LinkedBlockingDeque q = populatedDeque(SIZE);
191 <        for (int i = SIZE-1; i >= 0; --i) {
191 >        for (int i = SIZE - 1; i >= 0; --i) {
192              assertEquals(i, q.peekLast());
193              assertEquals(i, q.pollLast());
194              assertTrue(q.peekLast() == null ||
# Line 193 | Line 218 | public class LinkedBlockingDequeTest ext
218       */
219      public void testLastElement() {
220          LinkedBlockingDeque q = populatedDeque(SIZE);
221 <        for (int i = SIZE-1; i >= 0; --i) {
221 >        for (int i = SIZE - 1; i >= 0; --i) {
222              assertEquals(i, q.getLast());
223              assertEquals(i, q.pollLast());
224          }
# Line 253 | Line 278 | public class LinkedBlockingDequeTest ext
278       */
279      public void testRemoveFirstOccurrence() {
280          LinkedBlockingDeque q = populatedDeque(SIZE);
281 <        for (int i = 1; i < SIZE; i+=2) {
281 >        for (int i = 1; i < SIZE; i += 2) {
282              assertTrue(q.removeFirstOccurrence(new Integer(i)));
283          }
284 <        for (int i = 0; i < SIZE; i+=2) {
284 >        for (int i = 0; i < SIZE; i += 2) {
285              assertTrue(q.removeFirstOccurrence(new Integer(i)));
286 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
286 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
287          }
288          assertTrue(q.isEmpty());
289      }
# Line 268 | Line 293 | public class LinkedBlockingDequeTest ext
293       */
294      public void testRemoveLastOccurrence() {
295          LinkedBlockingDeque q = populatedDeque(SIZE);
296 <        for (int i = 1; i < SIZE; i+=2) {
296 >        for (int i = 1; i < SIZE; i += 2) {
297              assertTrue(q.removeLastOccurrence(new Integer(i)));
298          }
299 <        for (int i = 0; i < SIZE; i+=2) {
299 >        for (int i = 0; i < SIZE; i += 2) {
300              assertTrue(q.removeLastOccurrence(new Integer(i)));
301 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
301 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
302          }
303          assertTrue(q.isEmpty());
304      }
# Line 308 | Line 333 | public class LinkedBlockingDequeTest ext
333      }
334  
335      /**
336 <     * Constructor throws IAE if capacity argument nonpositive
336 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
337       */
338      public void testConstructor2() {
339          try {
340 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
340 >            new LinkedBlockingDeque(0);
341              shouldThrow();
342          } catch (IllegalArgumentException success) {}
343      }
344  
345      /**
346 <     * Initializing from null Collection throws NPE
346 >     * Initializing from null Collection throws NullPointerException
347       */
348      public void testConstructor3() {
349          try {
350 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
350 >            new LinkedBlockingDeque(null);
351              shouldThrow();
352          } catch (NullPointerException success) {}
353      }
354  
355      /**
356 <     * Initializing from Collection of null elements throws NPE
356 >     * Initializing from Collection of null elements throws NullPointerException
357       */
358      public void testConstructor4() {
359 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
360          try {
361 <            Integer[] ints = new Integer[SIZE];
336 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
361 >            new LinkedBlockingDeque(elements);
362              shouldThrow();
363          } catch (NullPointerException success) {}
364      }
365  
366      /**
367 <     * Initializing from Collection with some null elements throws NPE
367 >     * Initializing from Collection with some null elements throws
368 >     * NullPointerException
369       */
370      public void testConstructor5() {
371 +        Integer[] ints = new Integer[SIZE];
372 +        for (int i = 0; i < SIZE - 1; ++i)
373 +            ints[i] = i;
374 +        Collection<Integer> elements = Arrays.asList(ints);
375          try {
376 <            Integer[] ints = new Integer[SIZE];
347 <            for (int i = 0; i < SIZE-1; ++i)
348 <                ints[i] = new Integer(i);
349 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
376 >            new LinkedBlockingDeque(elements);
377              shouldThrow();
378          } catch (NullPointerException success) {}
379      }
# Line 357 | Line 384 | public class LinkedBlockingDequeTest ext
384      public void testConstructor6() {
385          Integer[] ints = new Integer[SIZE];
386          for (int i = 0; i < SIZE; ++i)
387 <            ints[i] = new Integer(i);
387 >            ints[i] = i;
388          LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
389          for (int i = 0; i < SIZE; ++i)
390              assertEquals(ints[i], q.poll());
# Line 382 | Line 409 | public class LinkedBlockingDequeTest ext
409       * remainingCapacity decreases on add, increases on remove
410       */
411      public void testRemainingCapacity() {
412 <        LinkedBlockingDeque q = populatedDeque(SIZE);
412 >        BlockingQueue q = populatedDeque(SIZE);
413          for (int i = 0; i < SIZE; ++i) {
414              assertEquals(i, q.remainingCapacity());
415 <            assertEquals(SIZE-i, q.size());
416 <            q.remove();
415 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
416 >            assertEquals(i, q.remove());
417          }
418          for (int i = 0; i < SIZE; ++i) {
419 <            assertEquals(SIZE-i, q.remainingCapacity());
420 <            assertEquals(i, q.size());
421 <            q.add(new Integer(i));
419 >            assertEquals(SIZE - i, q.remainingCapacity());
420 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
421 >            assertTrue(q.add(i));
422          }
423      }
424  
425      /**
399     * offer(null) throws NPE
400     */
401    public void testOfferNull() {
402        try {
403            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
404            q.offer(null);
405            shouldThrow();
406        } catch (NullPointerException success) {}
407    }
408
409    /**
410     * add(null) throws NPE
411     */
412    public void testAddNull() {
413        try {
414            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
415            q.add(null);
416            shouldThrow();
417        } catch (NullPointerException success) {}
418    }
419
420    /**
426       * push(null) throws NPE
427       */
428      public void testPushNull() {
429 +        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
430          try {
425            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
431              q.push(null);
432              shouldThrow();
433          } catch (NullPointerException success) {}
# Line 432 | Line 437 | public class LinkedBlockingDequeTest ext
437       * push succeeds if not full; throws ISE if full
438       */
439      public void testPush() {
440 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
441 +        for (int i = 0; i < SIZE; ++i) {
442 +            Integer x = new Integer(i);
443 +            q.push(x);
444 +            assertEquals(x, q.peek());
445 +        }
446 +        assertEquals(0, q.remainingCapacity());
447          try {
436            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
437            for (int i = 0; i < SIZE; ++i) {
438                Integer I = new Integer(i);
439                q.push(I);
440                assertEquals(I, q.peek());
441            }
442            assertEquals(0, q.remainingCapacity());
448              q.push(new Integer(SIZE));
449              shouldThrow();
450          } catch (IllegalStateException success) {}
# Line 482 | Line 487 | public class LinkedBlockingDequeTest ext
487       * add succeeds if not full; throws ISE if full
488       */
489      public void testAdd() {
490 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
491 +        for (int i = 0; i < SIZE; ++i)
492 +            assertTrue(q.add(new Integer(i)));
493 +        assertEquals(0, q.remainingCapacity());
494          try {
486            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
487            for (int i = 0; i < SIZE; ++i) {
488                assertTrue(q.add(new Integer(i)));
489            }
490            assertEquals(0, q.remainingCapacity());
495              q.add(new Integer(SIZE));
496              shouldThrow();
497          } catch (IllegalStateException success) {}
498      }
499  
500      /**
497     * addAll(null) throws NPE
498     */
499    public void testAddAll1() {
500        try {
501            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
502            q.addAll(null);
503            shouldThrow();
504        } catch (NullPointerException success) {}
505    }
506
507    /**
501       * addAll(this) throws IAE
502       */
503      public void testAddAllSelf() {
504 +        LinkedBlockingDeque q = populatedDeque(SIZE);
505          try {
512            LinkedBlockingDeque q = populatedDeque(SIZE);
506              q.addAll(q);
507              shouldThrow();
508          } catch (IllegalArgumentException success) {}
509      }
510  
511      /**
519     * addAll of a collection with null elements throws NPE
520     */
521    public void testAddAll2() {
522        try {
523            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
524            Integer[] ints = new Integer[SIZE];
525            q.addAll(Arrays.asList(ints));
526            shouldThrow();
527        } catch (NullPointerException success) {}
528    }
529
530    /**
512       * addAll of a collection with any null elements throws NPE after
513       * possibly adding some elements
514       */
515      public void testAddAll3() {
516 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
517 +        Integer[] ints = new Integer[SIZE];
518 +        for (int i = 0; i < SIZE - 1; ++i)
519 +            ints[i] = new Integer(i);
520 +        Collection<Integer> elements = Arrays.asList(ints);
521          try {
522 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
537 <            Integer[] ints = new Integer[SIZE];
538 <            for (int i = 0; i < SIZE-1; ++i)
539 <                ints[i] = new Integer(i);
540 <            q.addAll(Arrays.asList(ints));
522 >            q.addAll(elements);
523              shouldThrow();
524          } catch (NullPointerException success) {}
525      }
526  
527      /**
528 <     * addAll throws ISE if not enough room
528 >     * addAll throws IllegalStateException if not enough room
529       */
530      public void testAddAll4() {
531 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
532 +        Integer[] ints = new Integer[SIZE];
533 +        for (int i = 0; i < SIZE; ++i)
534 +            ints[i] = new Integer(i);
535 +        Collection<Integer> elements = Arrays.asList(ints);
536          try {
537 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
551 <            Integer[] ints = new Integer[SIZE];
552 <            for (int i = 0; i < SIZE; ++i)
553 <                ints[i] = new Integer(i);
554 <            q.addAll(Arrays.asList(ints));
537 >            q.addAll(elements);
538              shouldThrow();
539          } catch (IllegalStateException success) {}
540      }
# Line 572 | Line 555 | public class LinkedBlockingDequeTest ext
555      }
556  
557      /**
575     * put(null) throws NPE
576     */
577    public void testPutNull() throws InterruptedException {
578        try {
579            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
580            q.put(null);
581            shouldThrow();
582        } catch (NullPointerException success) {}
583    }
584
585    /**
558       * all elements successfully put are contained
559       */
560      public void testPut() throws InterruptedException {
561          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
562          for (int i = 0; i < SIZE; ++i) {
563 <            Integer I = new Integer(i);
564 <            q.put(I);
565 <            assertTrue(q.contains(I));
563 >            Integer x = new Integer(i);
564 >            q.put(x);
565 >            assertTrue(q.contains(x));
566          }
567          assertEquals(0, q.remainingCapacity());
568      }
# Line 655 | Line 627 | public class LinkedBlockingDequeTest ext
627              }});
628  
629          await(pleaseTake);
630 <        assertEquals(q.remainingCapacity(), 0);
630 >        assertEquals(0, q.remainingCapacity());
631          assertEquals(0, q.take());
632  
633          await(pleaseInterrupt);
634          assertThreadStaysAlive(t);
635          t.interrupt();
636          awaitTermination(t);
637 <        assertEquals(q.remainingCapacity(), 0);
637 >        assertEquals(0, q.remainingCapacity());
638      }
639  
640      /**
# Line 781 | Line 753 | public class LinkedBlockingDequeTest ext
753          final CountDownLatch aboutToWait = new CountDownLatch(1);
754          Thread t = newStartedThread(new CheckedRunnable() {
755              public void realRun() throws InterruptedException {
756 +                long startTime = System.nanoTime();
757                  for (int i = 0; i < SIZE; ++i) {
785                    long t0 = System.nanoTime();
758                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
787                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
759                  }
789                long t0 = System.nanoTime();
760                  aboutToWait.countDown();
761                  try {
762 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
762 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
763                      shouldThrow();
764                  } catch (InterruptedException success) {
765 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
765 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
766                  }
767              }});
768  
769          aboutToWait.await();
770 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
770 >        waitForThreadToEnterWaitState(t);
771          t.interrupt();
772 <        awaitTermination(t, MEDIUM_DELAY_MS);
772 >        awaitTermination(t);
773          checkEmpty(q);
774      }
775  
# Line 807 | Line 777 | public class LinkedBlockingDequeTest ext
777       * putFirst(null) throws NPE
778       */
779      public void testPutFirstNull() throws InterruptedException {
780 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
781          try {
811            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
782              q.putFirst(null);
783              shouldThrow();
784          } catch (NullPointerException success) {}
# Line 820 | Line 790 | public class LinkedBlockingDequeTest ext
790      public void testPutFirst() throws InterruptedException {
791          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792          for (int i = 0; i < SIZE; ++i) {
793 <            Integer I = new Integer(i);
794 <            q.putFirst(I);
795 <            assertTrue(q.contains(I));
793 >            Integer x = new Integer(i);
794 >            q.putFirst(x);
795 >            assertTrue(q.contains(x));
796          }
797          assertEquals(0, q.remainingCapacity());
798      }
# Line 887 | Line 857 | public class LinkedBlockingDequeTest ext
857              }});
858  
859          await(pleaseTake);
860 <        assertEquals(q.remainingCapacity(), 0);
860 >        assertEquals(0, q.remainingCapacity());
861          assertEquals(capacity - 1, q.take());
862  
863          await(pleaseInterrupt);
864          assertThreadStaysAlive(t);
865          t.interrupt();
866          awaitTermination(t);
867 <        assertEquals(q.remainingCapacity(), 0);
867 >        assertEquals(0, q.remainingCapacity());
868      }
869  
870      /**
# Line 1080 | Line 1050 | public class LinkedBlockingDequeTest ext
1050       * returning timeout status
1051       */
1052      public void testInterruptedTimedPollFirst() throws InterruptedException {
1053 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1054          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1055          Thread t = newStartedThread(new CheckedRunnable() {
1056              public void realRun() throws InterruptedException {
1057 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1057 >                long startTime = System.nanoTime();
1058                  for (int i = 0; i < SIZE; ++i) {
1059                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1060                  }
1061  
1062                  Thread.currentThread().interrupt();
1063                  try {
1064 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1064 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1065                      shouldThrow();
1066                  } catch (InterruptedException success) {}
1067                  assertFalse(Thread.interrupted());
1068  
1069                  pleaseInterrupt.countDown();
1070                  try {
1071 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1071 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1072                      shouldThrow();
1073                  } catch (InterruptedException success) {}
1074                  assertFalse(Thread.interrupted());
1075 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1076              }});
1077  
1078          await(pleaseInterrupt);
# Line 1154 | Line 1126 | public class LinkedBlockingDequeTest ext
1126       * putLast(null) throws NPE
1127       */
1128      public void testPutLastNull() throws InterruptedException {
1129 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1130          try {
1158            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1131              q.putLast(null);
1132              shouldThrow();
1133          } catch (NullPointerException success) {}
# Line 1167 | Line 1139 | public class LinkedBlockingDequeTest ext
1139      public void testPutLast() throws InterruptedException {
1140          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1141          for (int i = 0; i < SIZE; ++i) {
1142 <            Integer I = new Integer(i);
1143 <            q.putLast(I);
1144 <            assertTrue(q.contains(I));
1142 >            Integer x = new Integer(i);
1143 >            q.putLast(x);
1144 >            assertTrue(q.contains(x));
1145          }
1146          assertEquals(0, q.remainingCapacity());
1147      }
# Line 1234 | Line 1206 | public class LinkedBlockingDequeTest ext
1206              }});
1207  
1208          await(pleaseTake);
1209 <        assertEquals(q.remainingCapacity(), 0);
1209 >        assertEquals(0, q.remainingCapacity());
1210          assertEquals(0, q.take());
1211  
1212          await(pleaseInterrupt);
1213          assertThreadStaysAlive(t);
1214          t.interrupt();
1215          awaitTermination(t);
1216 <        assertEquals(q.remainingCapacity(), 0);
1216 >        assertEquals(0, q.remainingCapacity());
1217      }
1218  
1219      /**
# Line 1276 | Line 1248 | public class LinkedBlockingDequeTest ext
1248      public void testTakeLast() throws InterruptedException {
1249          LinkedBlockingDeque q = populatedDeque(SIZE);
1250          for (int i = 0; i < SIZE; ++i) {
1251 <            assertEquals(SIZE-i-1, q.takeLast());
1251 >            assertEquals(SIZE - i - 1, q.takeLast());
1252          }
1253      }
1254  
# Line 1289 | Line 1261 | public class LinkedBlockingDequeTest ext
1261          Thread t = newStartedThread(new CheckedRunnable() {
1262              public void realRun() throws InterruptedException {
1263                  for (int i = 0; i < SIZE; ++i) {
1264 <                    assertEquals(SIZE-i-1, q.takeLast());
1264 >                    assertEquals(SIZE - i - 1, q.takeLast());
1265                  }
1266  
1267                  Thread.currentThread().interrupt();
# Line 1319 | Line 1291 | public class LinkedBlockingDequeTest ext
1291      public void testTimedPollLast0() throws InterruptedException {
1292          LinkedBlockingDeque q = populatedDeque(SIZE);
1293          for (int i = 0; i < SIZE; ++i) {
1294 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1294 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1295          }
1296          assertNull(q.pollLast(0, MILLISECONDS));
1297      }
# Line 1331 | Line 1303 | public class LinkedBlockingDequeTest ext
1303          LinkedBlockingDeque q = populatedDeque(SIZE);
1304          for (int i = 0; i < SIZE; ++i) {
1305              long startTime = System.nanoTime();
1306 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1306 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1307              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1308          }
1309          long startTime = System.nanoTime();
# Line 1345 | Line 1317 | public class LinkedBlockingDequeTest ext
1317       * returning timeout status
1318       */
1319      public void testInterruptedTimedPollLast() throws InterruptedException {
1320 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1321          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1322          Thread t = newStartedThread(new CheckedRunnable() {
1323              public void realRun() throws InterruptedException {
1324 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1324 >                long startTime = System.nanoTime();
1325                  for (int i = 0; i < SIZE; ++i) {
1326 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1326 >                    assertEquals(SIZE - i - 1,
1327 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1328                  }
1329  
1330                  Thread.currentThread().interrupt();
# Line 1366 | Line 1340 | public class LinkedBlockingDequeTest ext
1340                      shouldThrow();
1341                  } catch (InterruptedException success) {}
1342                  assertFalse(Thread.interrupted());
1343 +
1344 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1345              }});
1346  
1347          await(pleaseInterrupt);
1348          assertThreadStaysAlive(t);
1349          t.interrupt();
1350          awaitTermination(t);
1351 +        checkEmpty(q);
1352      }
1353  
1354      /**
# Line 1404 | Line 1381 | public class LinkedBlockingDequeTest ext
1381                      shouldThrow();
1382                  } catch (InterruptedException success) {}
1383                  assertFalse(Thread.interrupted());
1384 +
1385 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1386              }});
1387  
1388          barrier.await();
# Line 1433 | Line 1412 | public class LinkedBlockingDequeTest ext
1412      }
1413  
1414      /**
1436     * remove(x) removes x and returns true if present
1437     */
1438    public void testRemoveElement() {
1439        LinkedBlockingDeque q = populatedDeque(SIZE);
1440        for (int i = 1; i < SIZE; i+=2) {
1441            assertTrue(q.contains(i));
1442            assertTrue(q.remove(i));
1443            assertFalse(q.contains(i));
1444            assertTrue(q.contains(i-1));
1445        }
1446        for (int i = 0; i < SIZE; i+=2) {
1447            assertTrue(q.contains(i));
1448            assertTrue(q.remove(i));
1449            assertFalse(q.contains(i));
1450            assertFalse(q.remove(i+1));
1451            assertFalse(q.contains(i+1));
1452        }
1453        assertTrue(q.isEmpty());
1454    }
1455
1456    /**
1415       * contains(x) reports true when elements added but not yet removed
1416       */
1417      public void testContains() {
# Line 1509 | Line 1467 | public class LinkedBlockingDequeTest ext
1467                  assertTrue(changed);
1468  
1469              assertTrue(q.containsAll(p));
1470 <            assertEquals(SIZE-i, q.size());
1470 >            assertEquals(SIZE - i, q.size());
1471              p.remove();
1472          }
1473      }
# Line 1522 | Line 1480 | public class LinkedBlockingDequeTest ext
1480              LinkedBlockingDeque q = populatedDeque(SIZE);
1481              LinkedBlockingDeque p = populatedDeque(i);
1482              assertTrue(q.removeAll(p));
1483 <            assertEquals(SIZE-i, q.size());
1483 >            assertEquals(SIZE - i, q.size());
1484              for (int j = 0; j < i; ++j) {
1485 <                Integer I = (Integer)(p.remove());
1486 <                assertFalse(q.contains(I));
1485 >                Integer x = (Integer)(p.remove());
1486 >                assertFalse(q.contains(x));
1487              }
1488          }
1489      }
# Line 1533 | Line 1491 | public class LinkedBlockingDequeTest ext
1491      /**
1492       * toArray contains all elements in FIFO order
1493       */
1494 <    public void testToArray() throws InterruptedException{
1494 >    public void testToArray() throws InterruptedException {
1495          LinkedBlockingDeque q = populatedDeque(SIZE);
1496          Object[] o = q.toArray();
1497          for (int i = 0; i < o.length; i++)
# Line 1553 | Line 1511 | public class LinkedBlockingDequeTest ext
1511      }
1512  
1513      /**
1556     * toArray(null) throws NullPointerException
1557     */
1558    public void testToArray_NullArg() {
1559        LinkedBlockingDeque q = populatedDeque(SIZE);
1560        try {
1561            q.toArray(null);
1562            shouldThrow();
1563        } catch (NullPointerException success) {}
1564    }
1565
1566    /**
1514       * toArray(incompatible array type) throws ArrayStoreException
1515       */
1516      public void testToArray1_BadArg() {
# Line 1580 | Line 1527 | public class LinkedBlockingDequeTest ext
1527      public void testIterator() throws InterruptedException {
1528          LinkedBlockingDeque q = populatedDeque(SIZE);
1529          Iterator it = q.iterator();
1530 <        while (it.hasNext()) {
1530 >        int i;
1531 >        for (i = 0; it.hasNext(); i++)
1532 >            assertTrue(q.contains(it.next()));
1533 >        assertEquals(i, SIZE);
1534 >        assertIteratorExhausted(it);
1535 >
1536 >        it = q.iterator();
1537 >        for (i = 0; it.hasNext(); i++)
1538              assertEquals(it.next(), q.take());
1539 <        }
1539 >        assertEquals(i, SIZE);
1540 >        assertIteratorExhausted(it);
1541 >    }
1542 >
1543 >    /**
1544 >     * iterator of empty collection has no elements
1545 >     */
1546 >    public void testEmptyIterator() {
1547 >        Deque c = new LinkedBlockingDeque();
1548 >        assertIteratorExhausted(c.iterator());
1549 >        assertIteratorExhausted(c.descendingIterator());
1550      }
1551  
1552      /**
# Line 1715 | Line 1679 | public class LinkedBlockingDequeTest ext
1679          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1680          q.add(one);
1681          q.add(two);
1718        ExecutorService executor = Executors.newFixedThreadPool(2);
1682          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1683 <        executor.execute(new CheckedRunnable() {
1684 <            public void realRun() throws InterruptedException {
1685 <                assertFalse(q.offer(three));
1686 <                threadsStarted.await();
1687 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1688 <                assertEquals(0, q.remainingCapacity());
1689 <            }});
1690 <
1691 <        executor.execute(new CheckedRunnable() {
1692 <            public void realRun() throws InterruptedException {
1693 <                threadsStarted.await();
1694 <                assertSame(one, q.take());
1695 <            }});
1696 <
1697 <        joinPool(executor);
1683 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1684 >        try (PoolCleaner cleaner = cleaner(executor)) {
1685 >            executor.execute(new CheckedRunnable() {
1686 >                public void realRun() throws InterruptedException {
1687 >                    assertFalse(q.offer(three));
1688 >                    threadsStarted.await();
1689 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1690 >                    assertEquals(0, q.remainingCapacity());
1691 >                }});
1692 >
1693 >            executor.execute(new CheckedRunnable() {
1694 >                public void realRun() throws InterruptedException {
1695 >                    threadsStarted.await();
1696 >                    assertSame(one, q.take());
1697 >                }});
1698 >        }
1699      }
1700  
1701      /**
# Line 1740 | Line 1704 | public class LinkedBlockingDequeTest ext
1704      public void testPollInExecutor() {
1705          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1706          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1707 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1708 <        executor.execute(new CheckedRunnable() {
1709 <            public void realRun() throws InterruptedException {
1710 <                assertNull(q.poll());
1711 <                threadsStarted.await();
1712 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1713 <                checkEmpty(q);
1714 <            }});
1715 <
1716 <        executor.execute(new CheckedRunnable() {
1717 <            public void realRun() throws InterruptedException {
1718 <                threadsStarted.await();
1719 <                q.put(one);
1720 <            }});
1721 <
1722 <        joinPool(executor);
1707 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1708 >        try (PoolCleaner cleaner = cleaner(executor)) {
1709 >            executor.execute(new CheckedRunnable() {
1710 >                public void realRun() throws InterruptedException {
1711 >                    assertNull(q.poll());
1712 >                    threadsStarted.await();
1713 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1714 >                    checkEmpty(q);
1715 >                }});
1716 >
1717 >            executor.execute(new CheckedRunnable() {
1718 >                public void realRun() throws InterruptedException {
1719 >                    threadsStarted.await();
1720 >                    q.put(one);
1721 >                }});
1722 >        }
1723      }
1724  
1725      /**
1726       * A deserialized serialized deque has same elements in same order
1727       */
1728      public void testSerialization() throws Exception {
1729 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1730 <
1767 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1768 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1769 <        out.writeObject(q);
1770 <        out.close();
1771 <
1772 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1773 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1774 <        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1775 <        assertEquals(q.size(), r.size());
1776 <        while (!q.isEmpty())
1777 <            assertEquals(q.remove(), r.remove());
1778 <    }
1729 >        Queue x = populatedDeque(SIZE);
1730 >        Queue y = serialClone(x);
1731  
1732 <    /**
1733 <     * drainTo(null) throws NPE
1734 <     */
1735 <    public void testDrainToNull() {
1736 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1737 <        try {
1738 <            q.drainTo(null);
1739 <            shouldThrow();
1740 <        } catch (NullPointerException success) {}
1789 <    }
1790 <
1791 <    /**
1792 <     * drainTo(this) throws IAE
1793 <     */
1794 <    public void testDrainToSelf() {
1795 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1796 <        try {
1797 <            q.drainTo(q);
1798 <            shouldThrow();
1799 <        } catch (IllegalArgumentException success) {}
1732 >        assertNotSame(y, x);
1733 >        assertEquals(x.size(), y.size());
1734 >        assertEquals(x.toString(), y.toString());
1735 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1736 >        while (!x.isEmpty()) {
1737 >            assertFalse(y.isEmpty());
1738 >            assertEquals(x.remove(), y.remove());
1739 >        }
1740 >        assertTrue(y.isEmpty());
1741      }
1742  
1743      /**
# Line 1806 | Line 1747 | public class LinkedBlockingDequeTest ext
1747          LinkedBlockingDeque q = populatedDeque(SIZE);
1748          ArrayList l = new ArrayList();
1749          q.drainTo(l);
1750 <        assertEquals(q.size(), 0);
1751 <        assertEquals(l.size(), SIZE);
1750 >        assertEquals(0, q.size());
1751 >        assertEquals(SIZE, l.size());
1752          for (int i = 0; i < SIZE; ++i)
1753              assertEquals(l.get(i), new Integer(i));
1754          q.add(zero);
# Line 1817 | Line 1758 | public class LinkedBlockingDequeTest ext
1758          assertTrue(q.contains(one));
1759          l.clear();
1760          q.drainTo(l);
1761 <        assertEquals(q.size(), 0);
1762 <        assertEquals(l.size(), 2);
1761 >        assertEquals(0, q.size());
1762 >        assertEquals(2, l.size());
1763          for (int i = 0; i < 2; ++i)
1764              assertEquals(l.get(i), new Integer(i));
1765      }
# Line 1830 | Line 1771 | public class LinkedBlockingDequeTest ext
1771          final LinkedBlockingDeque q = populatedDeque(SIZE);
1772          Thread t = new Thread(new CheckedRunnable() {
1773              public void realRun() throws InterruptedException {
1774 <                q.put(new Integer(SIZE+1));
1774 >                q.put(new Integer(SIZE + 1));
1775              }});
1776  
1777          t.start();
# Line 1844 | Line 1785 | public class LinkedBlockingDequeTest ext
1785      }
1786  
1787      /**
1847     * drainTo(null, n) throws NPE
1848     */
1849    public void testDrainToNullN() {
1850        LinkedBlockingDeque q = populatedDeque(SIZE);
1851        try {
1852            q.drainTo(null, 0);
1853            shouldThrow();
1854        } catch (NullPointerException success) {}
1855    }
1856
1857    /**
1858     * drainTo(this, n) throws IAE
1859     */
1860    public void testDrainToSelfN() {
1861        LinkedBlockingDeque q = populatedDeque(SIZE);
1862        try {
1863            q.drainTo(q, 0);
1864            shouldThrow();
1865        } catch (IllegalArgumentException success) {}
1866    }
1867
1868    /**
1788       * drainTo(c, n) empties first min(n, size) elements of queue into c
1789       */
1790      public void testDrainToN() {
# Line 1876 | Line 1795 | public class LinkedBlockingDequeTest ext
1795              ArrayList l = new ArrayList();
1796              q.drainTo(l, i);
1797              int k = (i < SIZE) ? i : SIZE;
1798 <            assertEquals(l.size(), k);
1799 <            assertEquals(q.size(), SIZE-k);
1798 >            assertEquals(k, l.size());
1799 >            assertEquals(SIZE - k, q.size());
1800              for (int j = 0; j < k; ++j)
1801                  assertEquals(l.get(j), new Integer(j));
1802 <            while (q.poll() != null) ;
1802 >            do {} while (q.poll() != null);
1803 >        }
1804 >    }
1805 >
1806 >    /**
1807 >     * remove(null), contains(null) always return false
1808 >     */
1809 >    public void testNeverContainsNull() {
1810 >        Deque<?>[] qs = {
1811 >            new LinkedBlockingDeque<Object>(),
1812 >            populatedDeque(2),
1813 >        };
1814 >
1815 >        for (Deque<?> q : qs) {
1816 >            assertFalse(q.contains(null));
1817 >            assertFalse(q.remove(null));
1818 >            assertFalse(q.removeFirstOccurrence(null));
1819 >            assertFalse(q.removeLastOccurrence(null));
1820          }
1821      }
1822  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines