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.62 by jsr166, Tue Oct 6 00:03:55 2015 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
50 >     * Returns a new deque of given size containing consecutive
51       * Integers 0 ... n.
52       */
53      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
# Line 70 | Line 82 | public class LinkedBlockingDequeTest ext
82      public void testSize() {
83          LinkedBlockingDeque q = populatedDeque(SIZE);
84          for (int i = 0; i < SIZE; ++i) {
85 <            assertEquals(SIZE-i, q.size());
85 >            assertEquals(SIZE - i, q.size());
86              q.removeFirst();
87          }
88          for (int i = 0; i < SIZE; ++i) {
# Line 80 | Line 92 | public class LinkedBlockingDequeTest ext
92      }
93  
94      /**
95 <     * offer(null) throws NPE
95 >     * offerFirst(null) throws NullPointerException
96       */
97      public void testOfferFirstNull() {
98 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
99          try {
87            LinkedBlockingDeque q = new LinkedBlockingDeque();
100              q.offerFirst(null);
101              shouldThrow();
102          } catch (NullPointerException success) {}
103      }
104  
105      /**
106 +     * offerLast(null) throws NullPointerException
107 +     */
108 +    public void testOfferLastNull() {
109 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
110 +        try {
111 +            q.offerLast(null);
112 +            shouldThrow();
113 +        } catch (NullPointerException success) {}
114 +    }
115 +
116 +    /**
117       * OfferFirst succeeds
118       */
119      public void testOfferFirst() {
# Line 124 | Line 147 | public class LinkedBlockingDequeTest ext
147       */
148      public void testPollLast() {
149          LinkedBlockingDeque q = populatedDeque(SIZE);
150 <        for (int i = SIZE-1; i >= 0; --i) {
150 >        for (int i = SIZE - 1; i >= 0; --i) {
151              assertEquals(i, q.pollLast());
152          }
153          assertNull(q.pollLast());
# Line 163 | Line 186 | public class LinkedBlockingDequeTest ext
186       */
187      public void testPeekLast() {
188          LinkedBlockingDeque q = populatedDeque(SIZE);
189 <        for (int i = SIZE-1; i >= 0; --i) {
189 >        for (int i = SIZE - 1; i >= 0; --i) {
190              assertEquals(i, q.peekLast());
191              assertEquals(i, q.pollLast());
192              assertTrue(q.peekLast() == null ||
# Line 193 | Line 216 | public class LinkedBlockingDequeTest ext
216       */
217      public void testLastElement() {
218          LinkedBlockingDeque q = populatedDeque(SIZE);
219 <        for (int i = SIZE-1; i >= 0; --i) {
219 >        for (int i = SIZE - 1; i >= 0; --i) {
220              assertEquals(i, q.getLast());
221              assertEquals(i, q.pollLast());
222          }
# Line 253 | Line 276 | public class LinkedBlockingDequeTest ext
276       */
277      public void testRemoveFirstOccurrence() {
278          LinkedBlockingDeque q = populatedDeque(SIZE);
279 <        for (int i = 1; i < SIZE; i+=2) {
279 >        for (int i = 1; i < SIZE; i += 2) {
280              assertTrue(q.removeFirstOccurrence(new Integer(i)));
281          }
282 <        for (int i = 0; i < SIZE; i+=2) {
282 >        for (int i = 0; i < SIZE; i += 2) {
283              assertTrue(q.removeFirstOccurrence(new Integer(i)));
284 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
284 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
285          }
286          assertTrue(q.isEmpty());
287      }
# Line 268 | Line 291 | public class LinkedBlockingDequeTest ext
291       */
292      public void testRemoveLastOccurrence() {
293          LinkedBlockingDeque q = populatedDeque(SIZE);
294 <        for (int i = 1; i < SIZE; i+=2) {
294 >        for (int i = 1; i < SIZE; i += 2) {
295              assertTrue(q.removeLastOccurrence(new Integer(i)));
296          }
297 <        for (int i = 0; i < SIZE; i+=2) {
297 >        for (int i = 0; i < SIZE; i += 2) {
298              assertTrue(q.removeLastOccurrence(new Integer(i)));
299 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
299 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
300          }
301          assertTrue(q.isEmpty());
302      }
# Line 308 | Line 331 | public class LinkedBlockingDequeTest ext
331      }
332  
333      /**
334 <     * Constructor throws IAE if capacity argument nonpositive
334 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
335       */
336      public void testConstructor2() {
337          try {
338 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
338 >            new LinkedBlockingDeque(0);
339              shouldThrow();
340          } catch (IllegalArgumentException success) {}
341      }
342  
343      /**
344 <     * Initializing from null Collection throws NPE
344 >     * Initializing from null Collection throws NullPointerException
345       */
346      public void testConstructor3() {
347          try {
348 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
348 >            new LinkedBlockingDeque(null);
349              shouldThrow();
350          } catch (NullPointerException success) {}
351      }
352  
353      /**
354 <     * Initializing from Collection of null elements throws NPE
354 >     * Initializing from Collection of null elements throws NullPointerException
355       */
356      public void testConstructor4() {
357 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
358          try {
359 <            Integer[] ints = new Integer[SIZE];
336 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
359 >            new LinkedBlockingDeque(elements);
360              shouldThrow();
361          } catch (NullPointerException success) {}
362      }
363  
364      /**
365 <     * Initializing from Collection with some null elements throws NPE
365 >     * Initializing from Collection with some null elements throws
366 >     * NullPointerException
367       */
368      public void testConstructor5() {
369 +        Integer[] ints = new Integer[SIZE];
370 +        for (int i = 0; i < SIZE - 1; ++i)
371 +            ints[i] = i;
372 +        Collection<Integer> elements = Arrays.asList(ints);
373          try {
374 <            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));
374 >            new LinkedBlockingDeque(elements);
375              shouldThrow();
376          } catch (NullPointerException success) {}
377      }
# Line 357 | Line 382 | public class LinkedBlockingDequeTest ext
382      public void testConstructor6() {
383          Integer[] ints = new Integer[SIZE];
384          for (int i = 0; i < SIZE; ++i)
385 <            ints[i] = new Integer(i);
385 >            ints[i] = i;
386          LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
387          for (int i = 0; i < SIZE; ++i)
388              assertEquals(ints[i], q.poll());
# Line 382 | Line 407 | public class LinkedBlockingDequeTest ext
407       * remainingCapacity decreases on add, increases on remove
408       */
409      public void testRemainingCapacity() {
410 <        LinkedBlockingDeque q = populatedDeque(SIZE);
410 >        BlockingQueue q = populatedDeque(SIZE);
411          for (int i = 0; i < SIZE; ++i) {
412              assertEquals(i, q.remainingCapacity());
413 <            assertEquals(SIZE-i, q.size());
414 <            q.remove();
413 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
414 >            assertEquals(i, q.remove());
415          }
416          for (int i = 0; i < SIZE; ++i) {
417 <            assertEquals(SIZE-i, q.remainingCapacity());
418 <            assertEquals(i, q.size());
419 <            q.add(new Integer(i));
417 >            assertEquals(SIZE - i, q.remainingCapacity());
418 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
419 >            assertTrue(q.add(i));
420          }
421      }
422  
423      /**
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    /**
424       * push(null) throws NPE
425       */
426      public void testPushNull() {
427 +        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
428          try {
425            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
429              q.push(null);
430              shouldThrow();
431          } catch (NullPointerException success) {}
# Line 432 | Line 435 | public class LinkedBlockingDequeTest ext
435       * push succeeds if not full; throws ISE if full
436       */
437      public void testPush() {
438 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
439 +        for (int i = 0; i < SIZE; ++i) {
440 +            Integer x = new Integer(i);
441 +            q.push(x);
442 +            assertEquals(x, q.peek());
443 +        }
444 +        assertEquals(0, q.remainingCapacity());
445          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());
446              q.push(new Integer(SIZE));
447              shouldThrow();
448          } catch (IllegalStateException success) {}
# Line 482 | Line 485 | public class LinkedBlockingDequeTest ext
485       * add succeeds if not full; throws ISE if full
486       */
487      public void testAdd() {
488 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
489 +        for (int i = 0; i < SIZE; ++i)
490 +            assertTrue(q.add(new Integer(i)));
491 +        assertEquals(0, q.remainingCapacity());
492          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());
493              q.add(new Integer(SIZE));
494              shouldThrow();
495          } catch (IllegalStateException success) {}
496      }
497  
498      /**
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    /**
499       * addAll(this) throws IAE
500       */
501      public void testAddAllSelf() {
502 +        LinkedBlockingDeque q = populatedDeque(SIZE);
503          try {
512            LinkedBlockingDeque q = populatedDeque(SIZE);
504              q.addAll(q);
505              shouldThrow();
506          } catch (IllegalArgumentException success) {}
507      }
508  
509      /**
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    /**
510       * addAll of a collection with any null elements throws NPE after
511       * possibly adding some elements
512       */
513      public void testAddAll3() {
514 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
515 +        Integer[] ints = new Integer[SIZE];
516 +        for (int i = 0; i < SIZE - 1; ++i)
517 +            ints[i] = new Integer(i);
518 +        Collection<Integer> elements = Arrays.asList(ints);
519          try {
520 <            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));
520 >            q.addAll(elements);
521              shouldThrow();
522          } catch (NullPointerException success) {}
523      }
524  
525      /**
526 <     * addAll throws ISE if not enough room
526 >     * addAll throws IllegalStateException if not enough room
527       */
528      public void testAddAll4() {
529 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
530 +        Integer[] ints = new Integer[SIZE];
531 +        for (int i = 0; i < SIZE; ++i)
532 +            ints[i] = new Integer(i);
533 +        Collection<Integer> elements = Arrays.asList(ints);
534          try {
535 <            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));
535 >            q.addAll(elements);
536              shouldThrow();
537          } catch (IllegalStateException success) {}
538      }
# Line 572 | Line 553 | public class LinkedBlockingDequeTest ext
553      }
554  
555      /**
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    /**
556       * all elements successfully put are contained
557       */
558      public void testPut() throws InterruptedException {
559          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
560          for (int i = 0; i < SIZE; ++i) {
561 <            Integer I = new Integer(i);
562 <            q.put(I);
563 <            assertTrue(q.contains(I));
561 >            Integer x = new Integer(i);
562 >            q.put(x);
563 >            assertTrue(q.contains(x));
564          }
565          assertEquals(0, q.remainingCapacity());
566      }
# Line 655 | Line 625 | public class LinkedBlockingDequeTest ext
625              }});
626  
627          await(pleaseTake);
628 <        assertEquals(q.remainingCapacity(), 0);
628 >        assertEquals(0, q.remainingCapacity());
629          assertEquals(0, q.take());
630  
631          await(pleaseInterrupt);
632          assertThreadStaysAlive(t);
633          t.interrupt();
634          awaitTermination(t);
635 <        assertEquals(q.remainingCapacity(), 0);
635 >        assertEquals(0, q.remainingCapacity());
636      }
637  
638      /**
# Line 781 | Line 751 | public class LinkedBlockingDequeTest ext
751          final CountDownLatch aboutToWait = new CountDownLatch(1);
752          Thread t = newStartedThread(new CheckedRunnable() {
753              public void realRun() throws InterruptedException {
754 +                long startTime = System.nanoTime();
755                  for (int i = 0; i < SIZE; ++i) {
785                    long t0 = System.nanoTime();
756                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
787                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
757                  }
789                long t0 = System.nanoTime();
758                  aboutToWait.countDown();
759                  try {
760 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
760 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
761                      shouldThrow();
762                  } catch (InterruptedException success) {
763 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
763 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
764                  }
765              }});
766  
767          aboutToWait.await();
768 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
768 >        waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
769          t.interrupt();
770 <        awaitTermination(t, MEDIUM_DELAY_MS);
770 >        awaitTermination(t);
771          checkEmpty(q);
772      }
773  
# Line 807 | Line 775 | public class LinkedBlockingDequeTest ext
775       * putFirst(null) throws NPE
776       */
777      public void testPutFirstNull() throws InterruptedException {
778 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
779          try {
811            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
780              q.putFirst(null);
781              shouldThrow();
782          } catch (NullPointerException success) {}
# Line 820 | Line 788 | public class LinkedBlockingDequeTest ext
788      public void testPutFirst() throws InterruptedException {
789          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
790          for (int i = 0; i < SIZE; ++i) {
791 <            Integer I = new Integer(i);
792 <            q.putFirst(I);
793 <            assertTrue(q.contains(I));
791 >            Integer x = new Integer(i);
792 >            q.putFirst(x);
793 >            assertTrue(q.contains(x));
794          }
795          assertEquals(0, q.remainingCapacity());
796      }
# Line 887 | Line 855 | public class LinkedBlockingDequeTest ext
855              }});
856  
857          await(pleaseTake);
858 <        assertEquals(q.remainingCapacity(), 0);
858 >        assertEquals(0, q.remainingCapacity());
859          assertEquals(capacity - 1, q.take());
860  
861          await(pleaseInterrupt);
862          assertThreadStaysAlive(t);
863          t.interrupt();
864          awaitTermination(t);
865 <        assertEquals(q.remainingCapacity(), 0);
865 >        assertEquals(0, q.remainingCapacity());
866      }
867  
868      /**
# Line 1080 | Line 1048 | public class LinkedBlockingDequeTest ext
1048       * returning timeout status
1049       */
1050      public void testInterruptedTimedPollFirst() throws InterruptedException {
1051 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1052          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1053          Thread t = newStartedThread(new CheckedRunnable() {
1054              public void realRun() throws InterruptedException {
1055 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1055 >                long startTime = System.nanoTime();
1056                  for (int i = 0; i < SIZE; ++i) {
1057                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1058                  }
1059  
1060                  Thread.currentThread().interrupt();
1061                  try {
1062 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1062 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1063                      shouldThrow();
1064                  } catch (InterruptedException success) {}
1065                  assertFalse(Thread.interrupted());
1066  
1067                  pleaseInterrupt.countDown();
1068                  try {
1069 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1069 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1070                      shouldThrow();
1071                  } catch (InterruptedException success) {}
1072                  assertFalse(Thread.interrupted());
1073 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1074              }});
1075  
1076          await(pleaseInterrupt);
# Line 1154 | Line 1124 | public class LinkedBlockingDequeTest ext
1124       * putLast(null) throws NPE
1125       */
1126      public void testPutLastNull() throws InterruptedException {
1127 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1128          try {
1158            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1129              q.putLast(null);
1130              shouldThrow();
1131          } catch (NullPointerException success) {}
# Line 1167 | Line 1137 | public class LinkedBlockingDequeTest ext
1137      public void testPutLast() throws InterruptedException {
1138          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1139          for (int i = 0; i < SIZE; ++i) {
1140 <            Integer I = new Integer(i);
1141 <            q.putLast(I);
1142 <            assertTrue(q.contains(I));
1140 >            Integer x = new Integer(i);
1141 >            q.putLast(x);
1142 >            assertTrue(q.contains(x));
1143          }
1144          assertEquals(0, q.remainingCapacity());
1145      }
# Line 1234 | Line 1204 | public class LinkedBlockingDequeTest ext
1204              }});
1205  
1206          await(pleaseTake);
1207 <        assertEquals(q.remainingCapacity(), 0);
1207 >        assertEquals(0, q.remainingCapacity());
1208          assertEquals(0, q.take());
1209  
1210          await(pleaseInterrupt);
1211          assertThreadStaysAlive(t);
1212          t.interrupt();
1213          awaitTermination(t);
1214 <        assertEquals(q.remainingCapacity(), 0);
1214 >        assertEquals(0, q.remainingCapacity());
1215      }
1216  
1217      /**
# Line 1276 | Line 1246 | public class LinkedBlockingDequeTest ext
1246      public void testTakeLast() throws InterruptedException {
1247          LinkedBlockingDeque q = populatedDeque(SIZE);
1248          for (int i = 0; i < SIZE; ++i) {
1249 <            assertEquals(SIZE-i-1, q.takeLast());
1249 >            assertEquals(SIZE - i - 1, q.takeLast());
1250          }
1251      }
1252  
# Line 1289 | Line 1259 | public class LinkedBlockingDequeTest ext
1259          Thread t = newStartedThread(new CheckedRunnable() {
1260              public void realRun() throws InterruptedException {
1261                  for (int i = 0; i < SIZE; ++i) {
1262 <                    assertEquals(SIZE-i-1, q.takeLast());
1262 >                    assertEquals(SIZE - i - 1, q.takeLast());
1263                  }
1264  
1265                  Thread.currentThread().interrupt();
# Line 1319 | Line 1289 | public class LinkedBlockingDequeTest ext
1289      public void testTimedPollLast0() throws InterruptedException {
1290          LinkedBlockingDeque q = populatedDeque(SIZE);
1291          for (int i = 0; i < SIZE; ++i) {
1292 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1292 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1293          }
1294          assertNull(q.pollLast(0, MILLISECONDS));
1295      }
# Line 1331 | Line 1301 | public class LinkedBlockingDequeTest ext
1301          LinkedBlockingDeque q = populatedDeque(SIZE);
1302          for (int i = 0; i < SIZE; ++i) {
1303              long startTime = System.nanoTime();
1304 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1304 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1305              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1306          }
1307          long startTime = System.nanoTime();
# Line 1345 | Line 1315 | public class LinkedBlockingDequeTest ext
1315       * returning timeout status
1316       */
1317      public void testInterruptedTimedPollLast() throws InterruptedException {
1318 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1319          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1320          Thread t = newStartedThread(new CheckedRunnable() {
1321              public void realRun() throws InterruptedException {
1322 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1322 >                long startTime = System.nanoTime();
1323                  for (int i = 0; i < SIZE; ++i) {
1324 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1324 >                    assertEquals(SIZE - i - 1,
1325 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1326                  }
1327  
1328                  Thread.currentThread().interrupt();
# Line 1366 | Line 1338 | public class LinkedBlockingDequeTest ext
1338                      shouldThrow();
1339                  } catch (InterruptedException success) {}
1340                  assertFalse(Thread.interrupted());
1341 +
1342 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1343              }});
1344  
1345          await(pleaseInterrupt);
1346          assertThreadStaysAlive(t);
1347          t.interrupt();
1348          awaitTermination(t);
1349 +        checkEmpty(q);
1350      }
1351  
1352      /**
# Line 1433 | Line 1408 | public class LinkedBlockingDequeTest ext
1408      }
1409  
1410      /**
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    /**
1411       * contains(x) reports true when elements added but not yet removed
1412       */
1413      public void testContains() {
# Line 1509 | Line 1463 | public class LinkedBlockingDequeTest ext
1463                  assertTrue(changed);
1464  
1465              assertTrue(q.containsAll(p));
1466 <            assertEquals(SIZE-i, q.size());
1466 >            assertEquals(SIZE - i, q.size());
1467              p.remove();
1468          }
1469      }
# Line 1522 | Line 1476 | public class LinkedBlockingDequeTest ext
1476              LinkedBlockingDeque q = populatedDeque(SIZE);
1477              LinkedBlockingDeque p = populatedDeque(i);
1478              assertTrue(q.removeAll(p));
1479 <            assertEquals(SIZE-i, q.size());
1479 >            assertEquals(SIZE - i, q.size());
1480              for (int j = 0; j < i; ++j) {
1481 <                Integer I = (Integer)(p.remove());
1482 <                assertFalse(q.contains(I));
1481 >                Integer x = (Integer)(p.remove());
1482 >                assertFalse(q.contains(x));
1483              }
1484          }
1485      }
# Line 1533 | Line 1487 | public class LinkedBlockingDequeTest ext
1487      /**
1488       * toArray contains all elements in FIFO order
1489       */
1490 <    public void testToArray() throws InterruptedException{
1490 >    public void testToArray() throws InterruptedException {
1491          LinkedBlockingDeque q = populatedDeque(SIZE);
1492          Object[] o = q.toArray();
1493          for (int i = 0; i < o.length; i++)
# Line 1553 | Line 1507 | public class LinkedBlockingDequeTest ext
1507      }
1508  
1509      /**
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    /**
1510       * toArray(incompatible array type) throws ArrayStoreException
1511       */
1512      public void testToArray1_BadArg() {
# Line 1580 | Line 1523 | public class LinkedBlockingDequeTest ext
1523      public void testIterator() throws InterruptedException {
1524          LinkedBlockingDeque q = populatedDeque(SIZE);
1525          Iterator it = q.iterator();
1526 <        while (it.hasNext()) {
1526 >        int i;
1527 >        for (i = 0; it.hasNext(); i++)
1528 >            assertTrue(q.contains(it.next()));
1529 >        assertEquals(i, SIZE);
1530 >        assertIteratorExhausted(it);
1531 >
1532 >        it = q.iterator();
1533 >        for (i = 0; it.hasNext(); i++)
1534              assertEquals(it.next(), q.take());
1535 <        }
1535 >        assertEquals(i, SIZE);
1536 >        assertIteratorExhausted(it);
1537 >    }
1538 >
1539 >    /**
1540 >     * iterator of empty collection has no elements
1541 >     */
1542 >    public void testEmptyIterator() {
1543 >        Deque c = new LinkedBlockingDeque();
1544 >        assertIteratorExhausted(c.iterator());
1545 >        assertIteratorExhausted(c.descendingIterator());
1546      }
1547  
1548      /**
# Line 1715 | Line 1675 | public class LinkedBlockingDequeTest ext
1675          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1676          q.add(one);
1677          q.add(two);
1718        ExecutorService executor = Executors.newFixedThreadPool(2);
1678          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1679 <        executor.execute(new CheckedRunnable() {
1680 <            public void realRun() throws InterruptedException {
1681 <                assertFalse(q.offer(three));
1682 <                threadsStarted.await();
1683 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1684 <                assertEquals(0, q.remainingCapacity());
1685 <            }});
1686 <
1687 <        executor.execute(new CheckedRunnable() {
1688 <            public void realRun() throws InterruptedException {
1689 <                threadsStarted.await();
1690 <                assertSame(one, q.take());
1691 <            }});
1692 <
1693 <        joinPool(executor);
1679 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1680 >        try (PoolCleaner cleaner = cleaner(executor)) {
1681 >            executor.execute(new CheckedRunnable() {
1682 >                public void realRun() throws InterruptedException {
1683 >                    assertFalse(q.offer(three));
1684 >                    threadsStarted.await();
1685 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1686 >                    assertEquals(0, q.remainingCapacity());
1687 >                }});
1688 >
1689 >            executor.execute(new CheckedRunnable() {
1690 >                public void realRun() throws InterruptedException {
1691 >                    threadsStarted.await();
1692 >                    assertSame(one, q.take());
1693 >                }});
1694 >        }
1695      }
1696  
1697      /**
# Line 1740 | Line 1700 | public class LinkedBlockingDequeTest ext
1700      public void testPollInExecutor() {
1701          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1702          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1703 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1704 <        executor.execute(new CheckedRunnable() {
1705 <            public void realRun() throws InterruptedException {
1706 <                assertNull(q.poll());
1707 <                threadsStarted.await();
1708 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1709 <                checkEmpty(q);
1710 <            }});
1711 <
1712 <        executor.execute(new CheckedRunnable() {
1713 <            public void realRun() throws InterruptedException {
1714 <                threadsStarted.await();
1715 <                q.put(one);
1716 <            }});
1717 <
1718 <        joinPool(executor);
1703 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1704 >        try (PoolCleaner cleaner = cleaner(executor)) {
1705 >            executor.execute(new CheckedRunnable() {
1706 >                public void realRun() throws InterruptedException {
1707 >                    assertNull(q.poll());
1708 >                    threadsStarted.await();
1709 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1710 >                    checkEmpty(q);
1711 >                }});
1712 >
1713 >            executor.execute(new CheckedRunnable() {
1714 >                public void realRun() throws InterruptedException {
1715 >                    threadsStarted.await();
1716 >                    q.put(one);
1717 >                }});
1718 >        }
1719      }
1720  
1721      /**
1722       * A deserialized serialized deque has same elements in same order
1723       */
1724      public void testSerialization() throws Exception {
1725 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1726 <
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 <    }
1779 <
1780 <    /**
1781 <     * drainTo(null) throws NPE
1782 <     */
1783 <    public void testDrainToNull() {
1784 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1785 <        try {
1786 <            q.drainTo(null);
1787 <            shouldThrow();
1788 <        } catch (NullPointerException success) {}
1789 <    }
1725 >        Queue x = populatedDeque(SIZE);
1726 >        Queue y = serialClone(x);
1727  
1728 <    /**
1729 <     * drainTo(this) throws IAE
1730 <     */
1731 <    public void testDrainToSelf() {
1732 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1733 <        try {
1734 <            q.drainTo(q);
1735 <            shouldThrow();
1736 <        } catch (IllegalArgumentException success) {}
1728 >        assertNotSame(y, x);
1729 >        assertEquals(x.size(), y.size());
1730 >        assertEquals(x.toString(), y.toString());
1731 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1732 >        while (!x.isEmpty()) {
1733 >            assertFalse(y.isEmpty());
1734 >            assertEquals(x.remove(), y.remove());
1735 >        }
1736 >        assertTrue(y.isEmpty());
1737      }
1738  
1739      /**
# Line 1806 | Line 1743 | public class LinkedBlockingDequeTest ext
1743          LinkedBlockingDeque q = populatedDeque(SIZE);
1744          ArrayList l = new ArrayList();
1745          q.drainTo(l);
1746 <        assertEquals(q.size(), 0);
1747 <        assertEquals(l.size(), SIZE);
1746 >        assertEquals(0, q.size());
1747 >        assertEquals(SIZE, l.size());
1748          for (int i = 0; i < SIZE; ++i)
1749              assertEquals(l.get(i), new Integer(i));
1750          q.add(zero);
# Line 1817 | Line 1754 | public class LinkedBlockingDequeTest ext
1754          assertTrue(q.contains(one));
1755          l.clear();
1756          q.drainTo(l);
1757 <        assertEquals(q.size(), 0);
1758 <        assertEquals(l.size(), 2);
1757 >        assertEquals(0, q.size());
1758 >        assertEquals(2, l.size());
1759          for (int i = 0; i < 2; ++i)
1760              assertEquals(l.get(i), new Integer(i));
1761      }
# Line 1830 | Line 1767 | public class LinkedBlockingDequeTest ext
1767          final LinkedBlockingDeque q = populatedDeque(SIZE);
1768          Thread t = new Thread(new CheckedRunnable() {
1769              public void realRun() throws InterruptedException {
1770 <                q.put(new Integer(SIZE+1));
1770 >                q.put(new Integer(SIZE + 1));
1771              }});
1772  
1773          t.start();
# Line 1844 | Line 1781 | public class LinkedBlockingDequeTest ext
1781      }
1782  
1783      /**
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    /**
1784       * drainTo(c, n) empties first min(n, size) elements of queue into c
1785       */
1786      public void testDrainToN() {
# Line 1876 | Line 1791 | public class LinkedBlockingDequeTest ext
1791              ArrayList l = new ArrayList();
1792              q.drainTo(l, i);
1793              int k = (i < SIZE) ? i : SIZE;
1794 <            assertEquals(l.size(), k);
1795 <            assertEquals(q.size(), SIZE-k);
1794 >            assertEquals(k, l.size());
1795 >            assertEquals(SIZE - k, q.size());
1796              for (int j = 0; j < k; ++j)
1797                  assertEquals(l.get(j), new Integer(j));
1798 <            while (q.poll() != null) ;
1798 >            do {} while (q.poll() != null);
1799 >        }
1800 >    }
1801 >
1802 >    /**
1803 >     * remove(null), contains(null) always return false
1804 >     */
1805 >    public void testNeverContainsNull() {
1806 >        Deque<?>[] qs = {
1807 >            new LinkedBlockingDeque<Object>(),
1808 >            populatedDeque(2),
1809 >        };
1810 >
1811 >        for (Deque<?> q : qs) {
1812 >            assertFalse(q.contains(null));
1813 >            assertFalse(q.remove(null));
1814 >            assertFalse(q.removeFirstOccurrence(null));
1815 >            assertFalse(q.removeLastOccurrence(null));
1816          }
1817      }
1818  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines