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.19 by jsr166, Tue Dec 1 06:03:49 2009 UTC vs.
Revision 1.32 by jsr166, Fri Nov 5 00:17:22 2010 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import java.io.*;
12  
13   public class LinkedBlockingDequeTest extends JSR166TestCase {
14 +
15 +    public static class Unbounded extends BlockingQueueTest {
16 +        protected BlockingQueue emptyCollection() {
17 +            return new LinkedBlockingDeque();
18 +        }
19 +    }
20 +
21 +    public static class Bounded extends BlockingQueueTest {
22 +        protected BlockingQueue emptyCollection() {
23 +            return new LinkedBlockingDeque(20);
24 +        }
25 +    }
26 +
27      public static void main(String[] args) {
28 <        junit.textui.TestRunner.run (suite());
28 >        junit.textui.TestRunner.run(suite());
29      }
30  
31      public static Test suite() {
32 <        return new TestSuite(LinkedBlockingDequeTest.class);
32 >        return newTestSuite(LinkedBlockingDequeTest.class,
33 >                            new Unbounded().testSuite(),
34 >                            new Bounded().testSuite());
35      }
36  
37      /**
38       * Create a deque of given size containing consecutive
39       * Integers 0 ... n.
40       */
41 <    private LinkedBlockingDeque populatedDeque(int n) {
42 <        LinkedBlockingDeque q = new LinkedBlockingDeque(n);
41 >    private LinkedBlockingDeque<Integer> populatedDeque(int n) {
42 >        LinkedBlockingDeque<Integer> q =
43 >            new LinkedBlockingDeque<Integer>(n);
44          assertTrue(q.isEmpty());
45          for (int i = 0; i < n; i++)
46              assertTrue(q.offer(new Integer(i)));
# Line 93 | Line 109 | public class LinkedBlockingDequeTest ext
109      }
110  
111      /**
112 <     *  pollFirst succeeds unless empty
112 >     * pollFirst succeeds unless empty
113       */
114      public void testPollFirst() {
115          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 104 | Line 120 | public class LinkedBlockingDequeTest ext
120      }
121  
122      /**
123 <     *  pollLast succeeds unless empty
123 >     * pollLast succeeds unless empty
124       */
125      public void testPollLast() {
126          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 115 | Line 131 | public class LinkedBlockingDequeTest ext
131      }
132  
133      /**
134 <     *  peekFirst returns next element, or null if empty
134 >     * peekFirst returns next element, or null if empty
135       */
136      public void testPeekFirst() {
137          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 129 | Line 145 | public class LinkedBlockingDequeTest ext
145      }
146  
147      /**
148 <     *  peek returns next element, or null if empty
148 >     * peek returns next element, or null if empty
149       */
150      public void testPeek() {
151          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 143 | Line 159 | public class LinkedBlockingDequeTest ext
159      }
160  
161      /**
162 <     *  peekLast returns next element, or null if empty
162 >     * peekLast returns next element, or null if empty
163       */
164      public void testPeekLast() {
165          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 157 | Line 173 | public class LinkedBlockingDequeTest ext
173      }
174  
175      /**
176 <     * getFirst returns next getFirst, or throws NSEE if empty
176 >     * getFirst() returns first element, or throws NSEE if empty
177       */
178      public void testFirstElement() {
179          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 173 | Line 189 | public class LinkedBlockingDequeTest ext
189      }
190  
191      /**
192 <     *  getLast returns next element, or throws NSEE if empty
192 >     * getLast() returns last element, or throws NSEE if empty
193       */
194      public void testLastElement() {
195          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 189 | Line 205 | public class LinkedBlockingDequeTest ext
205      }
206  
207      /**
208 <     *  removeFirst removes next element, or throws NSEE if empty
208 >     * removeFirst() removes first element, or throws NSEE if empty
209       */
210      public void testRemoveFirst() {
211          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 204 | Line 220 | public class LinkedBlockingDequeTest ext
220      }
221  
222      /**
223 <     *  removeLast removes last element, or throws NSEE if empty
223 >     * removeLast() removes last element, or throws NSEE if empty
224       */
225      public void testRemoveLast() {
226          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 219 | Line 235 | public class LinkedBlockingDequeTest ext
235      }
236  
237      /**
238 <     *  remove removes next element, or throws NSEE if empty
238 >     * remove removes next element, or throws NSEE if empty
239       */
240      public void testRemove() {
241          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 269 | Line 285 | public class LinkedBlockingDequeTest ext
285          LinkedBlockingDeque q = populatedDeque(3);
286          q.pollLast();
287          q.addFirst(four);
288 <        assertEquals(four,q.peekFirst());
288 >        assertSame(four, q.peekFirst());
289      }
290  
291      /**
# Line 279 | Line 295 | public class LinkedBlockingDequeTest ext
295          LinkedBlockingDeque q = populatedDeque(3);
296          q.pollLast();
297          q.addLast(four);
298 <        assertEquals(four,q.peekLast());
298 >        assertSame(four, q.peekLast());
299      }
300  
301  
# Line 437 | Line 453 | public class LinkedBlockingDequeTest ext
453          LinkedBlockingDeque q = populatedDeque(3);
454          q.pollLast();
455          q.push(four);
456 <        assertEquals(four,q.peekFirst());
456 >        assertSame(four, q.peekFirst());
457      }
458  
459  
460      /**
461 <     *  pop removes next element, or throws NSEE if empty
461 >     * pop removes next element, or throws NSEE if empty
462       */
463      public void testPop() {
464          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 513 | Line 529 | public class LinkedBlockingDequeTest ext
529              shouldThrow();
530          } catch (NullPointerException success) {}
531      }
532 +
533      /**
534       * addAll of a collection with any null elements throws NPE after
535       * possibly adding some elements
# Line 527 | Line 544 | public class LinkedBlockingDequeTest ext
544              shouldThrow();
545          } catch (NullPointerException success) {}
546      }
547 +
548      /**
549       * addAll throws ISE if not enough room
550       */
# Line 665 | Line 683 | public class LinkedBlockingDequeTest ext
683      }
684  
685      /**
668     * take blocks interruptibly when empty
669     */
670    public void testTakeFromEmpty() throws InterruptedException {
671        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
672        Thread t = new ThreadShouldThrow(InterruptedException.class) {
673            public void realRun() throws InterruptedException {
674                q.take();
675            }};
676
677        t.start();
678        Thread.sleep(SHORT_DELAY_MS);
679        t.interrupt();
680        t.join();
681    }
682
683    /**
686       * Take removes existing elements until empty, then blocks interruptibly
687       */
688      public void testBlockingTake() throws InterruptedException {
# Line 760 | Line 762 | public class LinkedBlockingDequeTest ext
762      }
763  
764      /**
763     *  timed poll before a delayed offer fails; after offer succeeds;
764     *  on interruption throws
765     */
766    public void testTimedPollWithOffer() throws InterruptedException {
767        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
768        Thread t = new Thread(new CheckedRunnable() {
769            public void realRun() throws InterruptedException {
770                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
771                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
772                try {
773                    q.poll(LONG_DELAY_MS, MILLISECONDS);
774                    shouldThrow();
775                } catch (InterruptedException success) {}
776            }});
777
778        t.start();
779        Thread.sleep(SMALL_DELAY_MS);
780        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
781        t.interrupt();
782        t.join();
783    }
784
785
786    /**
765       * putFirst(null) throws NPE
766       */
767       public void testPutFirstNull() throws InterruptedException {
# Line 974 | Line 952 | public class LinkedBlockingDequeTest ext
952      }
953  
954      /**
955 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
956 <     *  on interruption throws
955 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
956 >     * on interruption throws
957       */
958      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
959          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
# Line 1186 | Line 1164 | public class LinkedBlockingDequeTest ext
1164      }
1165  
1166      /**
1167 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1168 <     *  on interruption throws
1167 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1168 >     * on interruption throws
1169       */
1170      public void testTimedPollWithOfferLast() throws InterruptedException {
1171          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
# Line 1317 | Line 1295 | public class LinkedBlockingDequeTest ext
1295      }
1296  
1297      /**
1298 <     * toArray contains all elements
1298 >     * toArray contains all elements in FIFO order
1299       */
1300      public void testToArray() throws InterruptedException{
1301          LinkedBlockingDeque q = populatedDeque(SIZE);
1302          Object[] o = q.toArray();
1303          for (int i = 0; i < o.length; i++)
1304 <            assertEquals(o[i], q.take());
1304 >            assertSame(o[i], q.poll());
1305      }
1306  
1307      /**
1308 <     * toArray(a) contains all elements
1308 >     * toArray(a) contains all elements in FIFO order
1309       */
1310 <    public void testToArray2() throws InterruptedException {
1311 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1310 >    public void testToArray2() {
1311 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1312          Integer[] ints = new Integer[SIZE];
1313 <        ints = (Integer[])q.toArray(ints);
1313 >        Integer[] array = q.toArray(ints);
1314 >        assertSame(ints, array);
1315          for (int i = 0; i < ints.length; i++)
1316 <            assertEquals(ints[i], q.take());
1316 >            assertSame(ints[i], q.remove());
1317      }
1318  
1319      /**
1320 <     * toArray(null) throws NPE
1320 >     * toArray(null) throws NullPointerException
1321       */
1322 <    public void testToArray_BadArg() {
1322 >    public void testToArray_NullArg() {
1323          LinkedBlockingDeque q = populatedDeque(SIZE);
1324          try {
1325 <            Object o[] = q.toArray(null);
1325 >            q.toArray(null);
1326              shouldThrow();
1327          } catch (NullPointerException success) {}
1328      }
1329  
1330      /**
1331 <     * toArray with incompatible array type throws CCE
1331 >     * toArray(incompatible array type) throws ArrayStoreException
1332       */
1333      public void testToArray1_BadArg() {
1334          LinkedBlockingDeque q = populatedDeque(SIZE);
1335          try {
1336 <            Object o[] = q.toArray(new String[10]);
1336 >            q.toArray(new String[10]);
1337              shouldThrow();
1338          } catch (ArrayStoreException success) {}
1339      }
# Line 1374 | Line 1353 | public class LinkedBlockingDequeTest ext
1353      /**
1354       * iterator.remove removes current element
1355       */
1356 <    public void testIteratorRemove () {
1356 >    public void testIteratorRemove() {
1357          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1358          q.add(two);
1359          q.add(one);
# Line 1385 | Line 1364 | public class LinkedBlockingDequeTest ext
1364          it.remove();
1365  
1366          it = q.iterator();
1367 <        assertEquals(it.next(), one);
1368 <        assertEquals(it.next(), three);
1367 >        assertSame(it.next(), one);
1368 >        assertSame(it.next(), three);
1369          assertFalse(it.hasNext());
1370      }
1371  
# Line 1410 | Line 1389 | public class LinkedBlockingDequeTest ext
1389      /**
1390       * Modifications do not cause iterators to fail
1391       */
1392 <    public void testWeaklyConsistentIteration () {
1392 >    public void testWeaklyConsistentIteration() {
1393          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1394          q.add(one);
1395          q.add(two);
# Line 1424 | Line 1403 | public class LinkedBlockingDequeTest ext
1403  
1404  
1405      /**
1406 <     *  Descending iterator iterates through all elements
1406 >     * Descending iterator iterates through all elements
1407       */
1408      public void testDescendingIterator() {
1409          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 1443 | Line 1422 | public class LinkedBlockingDequeTest ext
1422      }
1423  
1424      /**
1425 <     *  Descending iterator ordering is reverse FIFO
1425 >     * Descending iterator ordering is reverse FIFO
1426       */
1427      public void testDescendingIteratorOrdering() {
1428          final LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1466 | Line 1445 | public class LinkedBlockingDequeTest ext
1445      /**
1446       * descendingIterator.remove removes current element
1447       */
1448 <    public void testDescendingIteratorRemove () {
1448 >    public void testDescendingIteratorRemove() {
1449          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1450          for (int iters = 0; iters < 100; ++iters) {
1451              q.add(new Integer(3));
# Line 1652 | Line 1631 | public class LinkedBlockingDequeTest ext
1631      }
1632  
1633      /**
1634 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1634 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1635       */
1636      public void testDrainToN() {
1637          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1661 | Line 1640 | public class LinkedBlockingDequeTest ext
1640                  assertTrue(q.offer(new Integer(j)));
1641              ArrayList l = new ArrayList();
1642              q.drainTo(l, i);
1643 <            int k = (i < SIZE)? i : SIZE;
1643 >            int k = (i < SIZE) ? i : SIZE;
1644              assertEquals(l.size(), k);
1645              assertEquals(q.size(), SIZE-k);
1646              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines