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

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 00:31:57 2003 UTC vs.
Revision 1.17 by jsr166, Sat Nov 21 02:33:20 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
13   import java.io.*;
14  
15   public class PriorityBlockingQueueTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());
18      }
17
19      public static Test suite() {
20 <        return new TestSuite(PriorityBlockingQueueTest.class);
20 >        return new TestSuite(PriorityBlockingQueueTest.class);
21      }
22  
23      private static final int NOCAP = Integer.MAX_VALUE;
24  
25      /** Sample Comparator */
26 <    static class MyReverseComparator implements Comparator {
26 >    static class MyReverseComparator implements Comparator {
27          public int compare(Object x, Object y) {
28              int i = ((Integer)x).intValue();
29              int j = ((Integer)y).intValue();
# Line 32 | Line 33 | public class PriorityBlockingQueueTest e
33          }
34      }
35  
35
36      /**
37       * Create a queue of given size containing consecutive
38       * Integers 0 ... n.
# Line 40 | Line 40 | public class PriorityBlockingQueueTest e
40      private PriorityBlockingQueue populatedQueue(int n) {
41          PriorityBlockingQueue q = new PriorityBlockingQueue(n);
42          assertTrue(q.isEmpty());
43 <        for(int i = n-1; i >= 0; i-=2)
44 <            assertTrue(q.offer(new Integer(i)));
45 <        for(int i = (n & 1); i < n; i+=2)
46 <            assertTrue(q.offer(new Integer(i)));
43 >        for (int i = n-1; i >= 0; i-=2)
44 >            assertTrue(q.offer(new Integer(i)));
45 >        for (int i = (n & 1); i < n; i+=2)
46 >            assertTrue(q.offer(new Integer(i)));
47          assertFalse(q.isEmpty());
48          assertEquals(NOCAP, q.remainingCapacity());
49 <        assertEquals(n, q.size());
49 >        assertEquals(n, q.size());
50          return q;
51      }
52 <
53 <    public void testConstructor1(){
52 >
53 >    /**
54 >     * A new queue has unbounded capacity
55 >     */
56 >    public void testConstructor1() {
57          assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity());
58      }
59  
60 <    public void testConstructor2(){
60 >    /**
61 >     * Constructor throws IAE if  capacity argument nonpositive
62 >     */
63 >    public void testConstructor2() {
64          try {
65              PriorityBlockingQueue q = new PriorityBlockingQueue(0);
66 <            fail("Cannot make zero-sized");
67 <        }
62 <        catch (IllegalArgumentException success) {}
66 >            shouldThrow();
67 >        } catch (IllegalArgumentException success) {}
68      }
69  
70 <    public void testConstructor3(){
71 <
70 >    /**
71 >     * Initializing from null Collection throws NPE
72 >     */
73 >    public void testConstructor3() {
74          try {
75              PriorityBlockingQueue q = new PriorityBlockingQueue(null);
76 <            fail("Cannot make from null collection");
77 <        }
71 <        catch (NullPointerException success) {}
76 >            shouldThrow();
77 >        } catch (NullPointerException success) {}
78      }
79  
80 <    public void testConstructor4(){
80 >    /**
81 >     * Initializing from Collection of null elements throws NPE
82 >     */
83 >    public void testConstructor4() {
84          try {
85              Integer[] ints = new Integer[SIZE];
86              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
87 <            fail("Cannot make with null elements");
88 <        }
80 <        catch (NullPointerException success) {}
87 >            shouldThrow();
88 >        } catch (NullPointerException success) {}
89      }
90  
91 <    public void testConstructor5(){
91 >    /**
92 >     * Initializing from Collection with some null elements throws NPE
93 >     */
94 >    public void testConstructor5() {
95          try {
96              Integer[] ints = new Integer[SIZE];
97              for (int i = 0; i < SIZE-1; ++i)
98                  ints[i] = new Integer(i);
99              PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
100 <            fail("Cannot make with null elements");
101 <        }
91 <        catch (NullPointerException success) {}
100 >            shouldThrow();
101 >        } catch (NullPointerException success) {}
102      }
103  
104 <    public void testConstructor6(){
105 <        try {
106 <            Integer[] ints = new Integer[SIZE];
107 <            for (int i = 0; i < SIZE; ++i)
108 <                ints[i] = new Integer(i);
109 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
110 <            for (int i = 0; i < SIZE; ++i)
111 <                assertEquals(ints[i], q.poll());
112 <        }
113 <        finally {}
104 >    /**
105 >     * Queue contains all elements of collection used to initialize
106 >     */
107 >    public void testConstructor6() {
108 >        Integer[] ints = new Integer[SIZE];
109 >        for (int i = 0; i < SIZE; ++i)
110 >            ints[i] = new Integer(i);
111 >        PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
112 >        for (int i = 0; i < SIZE; ++i)
113 >            assertEquals(ints[i], q.poll());
114      }
115  
116 <    public void testConstructor7(){
117 <        try {
118 <            MyReverseComparator cmp = new MyReverseComparator();
119 <            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
120 <            assertEquals(cmp, q.comparator());
121 <            Integer[] ints = new Integer[SIZE];
122 <            for (int i = 0; i < SIZE; ++i)
123 <                ints[i] = new Integer(i);
124 <            q.addAll(Arrays.asList(ints));
125 <            for (int i = SIZE-1; i >= 0; --i)
126 <                assertEquals(ints[i], q.poll());
127 <        }
128 <        finally {}
116 >    /**
117 >     * The comparator used in constructor is used
118 >     */
119 >    public void testConstructor7() {
120 >        MyReverseComparator cmp = new MyReverseComparator();
121 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
122 >        assertEquals(cmp, q.comparator());
123 >        Integer[] ints = new Integer[SIZE];
124 >        for (int i = 0; i < SIZE; ++i)
125 >            ints[i] = new Integer(i);
126 >        q.addAll(Arrays.asList(ints));
127 >        for (int i = SIZE-1; i >= 0; --i)
128 >            assertEquals(ints[i], q.poll());
129      }
130  
131 +    /**
132 +     * isEmpty is true before add, false after
133 +     */
134      public void testEmpty() {
135          PriorityBlockingQueue q = new PriorityBlockingQueue(2);
136          assertTrue(q.isEmpty());
137          assertEquals(NOCAP, q.remainingCapacity());
138 <        q.add(new Integer(1));
138 >        q.add(one);
139          assertFalse(q.isEmpty());
140 <        q.add(new Integer(2));
140 >        q.add(two);
141          q.remove();
142          q.remove();
143          assertTrue(q.isEmpty());
144      }
145  
146 <    public void testRemainingCapacity(){
146 >    /**
147 >     * remainingCapacity does not change when elements added or removed,
148 >     * but size does
149 >     */
150 >    public void testRemainingCapacity() {
151          PriorityBlockingQueue q = populatedQueue(SIZE);
152          for (int i = 0; i < SIZE; ++i) {
153              assertEquals(NOCAP, q.remainingCapacity());
# Line 144 | Line 161 | public class PriorityBlockingQueueTest e
161          }
162      }
163  
164 <    public void testOfferNull(){
165 <        try {
164 >    /**
165 >     * offer(null) throws NPE
166 >     */
167 >    public void testOfferNull() {
168 >        try {
169              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
170              q.offer(null);
171 <            fail("should throw NPE");
172 <        } catch (NullPointerException success) { }  
171 >            shouldThrow();
172 >        } catch (NullPointerException success) {}
173      }
174  
175 +    /**
176 +     * add(null) throws NPE
177 +     */
178 +    public void testAddNull() {
179 +        try {
180 +            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
181 +            q.add(null);
182 +            shouldThrow();
183 +        } catch (NullPointerException success) {}
184 +    }
185 +
186 +    /**
187 +     * Offer of comparable element succeeds
188 +     */
189      public void testOffer() {
190          PriorityBlockingQueue q = new PriorityBlockingQueue(1);
191 <        assertTrue(q.offer(new Integer(0)));
192 <        assertTrue(q.offer(new Integer(1)));
191 >        assertTrue(q.offer(zero));
192 >        assertTrue(q.offer(one));
193      }
194  
195 +    /**
196 +     * Offer of non-Comparable throws CCE
197 +     */
198      public void testOfferNonComparable() {
199          try {
200              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
201              q.offer(new Object());
202              q.offer(new Object());
203              q.offer(new Object());
204 <            fail("should throw CCE");
205 <        }
169 <        catch(ClassCastException success) {}
204 >            shouldThrow();
205 >        } catch (ClassCastException success) {}
206      }
207  
208 <    public void testAdd(){
208 >    /**
209 >     * add of comparable succeeds
210 >     */
211 >    public void testAdd() {
212          PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
213          for (int i = 0; i < SIZE; ++i) {
214              assertEquals(i, q.size());
# Line 177 | Line 216 | public class PriorityBlockingQueueTest e
216          }
217      }
218  
219 <    public void testAddAll1(){
219 >    /**
220 >     * addAll(null) throws NPE
221 >     */
222 >    public void testAddAll1() {
223          try {
224              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
225              q.addAll(null);
226 <            fail("Cannot add null collection");
227 <        }
186 <        catch (NullPointerException success) {}
226 >            shouldThrow();
227 >        } catch (NullPointerException success) {}
228      }
229 <    public void testAddAll2(){
229 >
230 >    /**
231 >     * addAll(this) throws IAE
232 >     */
233 >    public void testAddAllSelf() {
234 >        try {
235 >            PriorityBlockingQueue q = populatedQueue(SIZE);
236 >            q.addAll(q);
237 >            shouldThrow();
238 >        } catch (IllegalArgumentException success) {}
239 >    }
240 >
241 >    /**
242 >     * addAll of a collection with null elements throws NPE
243 >     */
244 >    public void testAddAll2() {
245          try {
246              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
247              Integer[] ints = new Integer[SIZE];
248              q.addAll(Arrays.asList(ints));
249 <            fail("Cannot add null elements");
250 <        }
195 <        catch (NullPointerException success) {}
249 >            shouldThrow();
250 >        } catch (NullPointerException success) {}
251      }
252 <    public void testAddAll3(){
252 >    /**
253 >     * addAll of a collection with any null elements throws NPE after
254 >     * possibly adding some elements
255 >     */
256 >    public void testAddAll3() {
257          try {
258              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
259              Integer[] ints = new Integer[SIZE];
260              for (int i = 0; i < SIZE-1; ++i)
261                  ints[i] = new Integer(i);
262              q.addAll(Arrays.asList(ints));
263 <            fail("Cannot add null elements");
264 <        }
206 <        catch (NullPointerException success) {}
263 >            shouldThrow();
264 >        } catch (NullPointerException success) {}
265      }
266  
267 <    public void testAddAll5(){
268 <        try {
269 <            Integer[] empty = new Integer[0];
270 <            Integer[] ints = new Integer[SIZE];
271 <            for (int i = SIZE-1; i >= 0; --i)
272 <                ints[i] = new Integer(i);
273 <            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
274 <            assertFalse(q.addAll(Arrays.asList(empty)));
275 <            assertTrue(q.addAll(Arrays.asList(ints)));
276 <            for (int i = 0; i < SIZE; ++i)
277 <                assertEquals(ints[i], q.poll());
278 <        }
279 <        finally {}
267 >    /**
268 >     * Queue contains all elements of successful addAll
269 >     */
270 >    public void testAddAll5() {
271 >        Integer[] empty = new Integer[0];
272 >        Integer[] ints = new Integer[SIZE];
273 >        for (int i = SIZE-1; i >= 0; --i)
274 >            ints[i] = new Integer(i);
275 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
276 >        assertFalse(q.addAll(Arrays.asList(empty)));
277 >        assertTrue(q.addAll(Arrays.asList(ints)));
278 >        for (int i = 0; i < SIZE; ++i)
279 >            assertEquals(ints[i], q.poll());
280      }
281  
282 +    /**
283 +     * put(null) throws NPE
284 +     */
285       public void testPutNull() {
286 <        try {
286 >        try {
287              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
288              q.put(null);
289 <            fail("put should throw NPE");
290 <        }
230 <        catch (NullPointerException success){
231 <        }  
289 >            shouldThrow();
290 >        } catch (NullPointerException success) {}
291       }
292  
293 +    /**
294 +     * all elements successfully put are contained
295 +     */
296       public void testPut() {
297 <         try {
298 <             PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
299 <             for (int i = 0; i < SIZE; ++i) {
300 <                 Integer I = new Integer(i);
301 <                 q.put(I);
240 <                 assertTrue(q.contains(I));
241 <             }
242 <             assertEquals(SIZE, q.size());
297 >         PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
298 >         for (int i = 0; i < SIZE; ++i) {
299 >             Integer I = new Integer(i);
300 >             q.put(I);
301 >             assertTrue(q.contains(I));
302           }
303 <         finally {
245 <        }
303 >         assertEquals(SIZE, q.size());
304      }
305  
306 <    public void testPutWithTake() {
306 >    /**
307 >     * put doesn't block waiting for take
308 >     */
309 >    public void testPutWithTake() throws InterruptedException {
310          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
311 <        Thread t = new Thread(new Runnable() {
312 <                public void run(){
313 <                    int added = 0;
314 <                    try {
315 <                        q.put(new Integer(0));
316 <                        ++added;
317 <                        q.put(new Integer(0));
318 <                        ++added;
319 <                        q.put(new Integer(0));
320 <                        ++added;
321 <                        q.put(new Integer(0));
322 <                        ++added;
323 <                        threadAssertTrue(added == 4);
263 <                    } finally {
264 <                    }
265 <                }
266 <            });
267 <        try {
268 <            t.start();
269 <            Thread.sleep(SHORT_DELAY_MS);
270 <            q.take();
271 <            t.interrupt();
272 <            t.join();
273 <        } catch (Exception e){
274 <            fail("Unexpected exception");
275 <        }
311 >        final int size = 4;
312 >        Thread t = new Thread(new CheckedRunnable() {
313 >            public void realRun() {
314 >                for (int i = 0; i < size; i++)
315 >                    q.put(new Integer(0));
316 >            }});
317 >
318 >        t.start();
319 >        Thread.sleep(SHORT_DELAY_MS);
320 >        assertEquals(q.size(), size);
321 >        q.take();
322 >        t.interrupt();
323 >        t.join();
324      }
325  
326 <    public void testTimedOffer() {
326 >    /**
327 >     * timed offer does not time out
328 >     */
329 >    public void testTimedOffer() throws InterruptedException {
330          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
331          Thread t = new Thread(new Runnable() {
332 <                public void run(){
332 >                public void run() {
333                      try {
334                          q.put(new Integer(0));
335                          q.put(new Integer(0));
336 <                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
337 <                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
336 >                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
337 >                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
338                      } finally { }
339                  }
340              });
341 <        
342 <        try {
343 <            t.start();
344 <            Thread.sleep(SMALL_DELAY_MS);
345 <            t.interrupt();
295 <            t.join();
296 <        } catch (Exception e){
297 <            fail("Unexpected exception");
298 <        }
341 >
342 >        t.start();
343 >        Thread.sleep(SMALL_DELAY_MS);
344 >        t.interrupt();
345 >        t.join();
346      }
347  
348 <    public void testTake(){
349 <        try {
350 <            PriorityBlockingQueue q = populatedQueue(SIZE);
351 <            for (int i = 0; i < SIZE; ++i) {
352 <                assertEquals(i, ((Integer)q.take()).intValue());
353 <            }
354 <        } catch (InterruptedException e){
355 <            fail("Unexpected exception");
309 <        }  
348 >    /**
349 >     * take retrieves elements in priority order
350 >     */
351 >    public void testTake() throws InterruptedException {
352 >        PriorityBlockingQueue q = populatedQueue(SIZE);
353 >        for (int i = 0; i < SIZE; ++i) {
354 >            assertEquals(i, ((Integer)q.take()).intValue());
355 >        }
356      }
357  
358 <    public void testTakeFromEmpty() {
358 >    /**
359 >     * take blocks interruptibly when empty
360 >     */
361 >    public void testTakeFromEmpty() throws InterruptedException {
362          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
363 <        Thread t = new Thread(new Runnable() {
364 <                public void run(){
365 <                    try {
366 <                        q.take();
367 <                        threadFail("Should block");
368 <                    } catch (InterruptedException success){ }                
369 <                }
370 <            });
371 <        try {
323 <            t.start();
324 <            Thread.sleep(SHORT_DELAY_MS);
325 <            t.interrupt();
326 <            t.join();
327 <        } catch (Exception e){
328 <            fail("Unexpected exception");
329 <        }
363 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
364 >            public void realRun() throws InterruptedException {
365 >                q.take();
366 >            }});
367 >
368 >        t.start();
369 >        Thread.sleep(SHORT_DELAY_MS);
370 >        t.interrupt();
371 >        t.join();
372      }
373  
374 <    public void testBlockingTake(){
375 <        Thread t = new Thread(new Runnable() {
376 <                public void run() {
377 <                    try {
378 <                        PriorityBlockingQueue q = populatedQueue(SIZE);
379 <                        for (int i = 0; i < SIZE; ++i) {
380 <                            threadAssertEquals(i, ((Integer)q.take()).intValue());
381 <                        }
382 <                        q.take();
383 <                        threadFail("take should block");
384 <                    } catch (InterruptedException success){
385 <                    }  
386 <                }});
374 >    /**
375 >     * Take removes existing elements until empty, then blocks interruptibly
376 >     */
377 >    public void testBlockingTake() throws InterruptedException {
378 >        Thread t = new Thread(new CheckedInterruptedRunnable() {
379 >            public void realRun() throws InterruptedException {
380 >                PriorityBlockingQueue q = populatedQueue(SIZE);
381 >                for (int i = 0; i < SIZE; ++i) {
382 >                    threadAssertEquals(i, ((Integer)q.take()).intValue());
383 >                }
384 >                q.take();
385 >            }});
386 >
387          t.start();
388 <        try {
389 <           Thread.sleep(SHORT_DELAY_MS);
390 <           t.interrupt();
349 <           t.join();
350 <        }
351 <        catch (InterruptedException ie) {
352 <            fail("Unexpected exception");
353 <        }
388 >        Thread.sleep(SHORT_DELAY_MS);
389 >        t.interrupt();
390 >        t.join();
391      }
392  
393  
394 <    public void testPoll(){
394 >    /**
395 >     * poll succeeds unless empty
396 >     */
397 >    public void testPoll() {
398          PriorityBlockingQueue q = populatedQueue(SIZE);
399          for (int i = 0; i < SIZE; ++i) {
400              assertEquals(i, ((Integer)q.poll()).intValue());
401          }
402 <        assertNull(q.poll());
402 >        assertNull(q.poll());
403      }
404  
405 <    public void testTimedPoll0() {
406 <        try {
407 <            PriorityBlockingQueue q = populatedQueue(SIZE);
408 <            for (int i = 0; i < SIZE; ++i) {
409 <                assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
410 <            }
411 <            assertNull(q.poll(0, TimeUnit.MILLISECONDS));
412 <        } catch (InterruptedException e){
413 <            fail("Unexpected exception");
374 <        }  
405 >    /**
406 >     * timed pool with zero timeout succeeds when non-empty, else times out
407 >     */
408 >    public void testTimedPoll0() throws InterruptedException {
409 >        PriorityBlockingQueue q = populatedQueue(SIZE);
410 >        for (int i = 0; i < SIZE; ++i) {
411 >            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
412 >        }
413 >        assertNull(q.poll(0, MILLISECONDS));
414      }
415  
416 <    public void testTimedPoll() {
417 <        try {
418 <            PriorityBlockingQueue q = populatedQueue(SIZE);
419 <            for (int i = 0; i < SIZE; ++i) {
420 <                assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
421 <            }
422 <            assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
423 <        } catch (InterruptedException e){
424 <            fail("Unexpected exception");
386 <        }  
416 >    /**
417 >     * timed pool with nonzero timeout succeeds when non-empty, else times out
418 >     */
419 >    public void testTimedPoll() throws InterruptedException {
420 >        PriorityBlockingQueue q = populatedQueue(SIZE);
421 >        for (int i = 0; i < SIZE; ++i) {
422 >            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
423 >        }
424 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
425      }
426  
427 <    public void testInterruptedTimedPoll(){
428 <        Thread t = new Thread(new Runnable() {
429 <                public void run() {
430 <                    try {
431 <                        PriorityBlockingQueue q = populatedQueue(SIZE);
432 <                        for (int i = 0; i < SIZE; ++i) {
433 <                            threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
434 <                        }
435 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
436 <                    } catch (InterruptedException success){
437 <                    }  
438 <                }});
427 >    /**
428 >     * Interrupted timed poll throws InterruptedException instead of
429 >     * returning timeout status
430 >     */
431 >    public void testInterruptedTimedPoll() throws InterruptedException {
432 >        Thread t = new Thread(new CheckedRunnable() {
433 >            public void realRun() throws InterruptedException {
434 >                PriorityBlockingQueue q = populatedQueue(SIZE);
435 >                for (int i = 0; i < SIZE; ++i) {
436 >                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
437 >                }
438 >                try {
439 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
440 >                    threadShouldThrow();
441 >                } catch (InterruptedException success) {}
442 >            }});
443 >
444          t.start();
445 <        try {
446 <           Thread.sleep(SHORT_DELAY_MS);
447 <           t.interrupt();
405 <           t.join();
406 <        }
407 <        catch (InterruptedException ie) {
408 <            fail("Unexpected exception");
409 <        }
445 >        Thread.sleep(SHORT_DELAY_MS);
446 >        t.interrupt();
447 >        t.join();
448      }
449  
450 <    public void testTimedPollWithOffer(){
450 >    /**
451 >     *  timed poll before a delayed offer fails; after offer succeeds;
452 >     *  on interruption throws
453 >     */
454 >    public void testTimedPollWithOffer() throws InterruptedException {
455          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
456 <        Thread t = new Thread(new Runnable() {
457 <                public void run(){
458 <                    try {
459 <                        threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
460 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
461 <                        q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
462 <                        threadFail("Should block");
463 <                    } catch (InterruptedException success) { }                
464 <                }
465 <            });
466 <        try {
467 <            t.start();
468 <            Thread.sleep(SMALL_DELAY_MS);
469 <            assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
470 <            t.interrupt();
471 <            t.join();
430 <        } catch (Exception e){
431 <            fail("Unexpected exception");
432 <        }
433 <    }  
456 >        Thread t = new Thread(new CheckedRunnable() {
457 >            public void realRun() throws InterruptedException {
458 >                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
459 >                threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
460 >                try {
461 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
462 >                    threadShouldThrow();
463 >                } catch (InterruptedException success) {}
464 >            }});
465 >
466 >        t.start();
467 >        Thread.sleep(SMALL_DELAY_MS);
468 >        assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
469 >        t.interrupt();
470 >        t.join();
471 >    }
472  
473  
474 <    public void testPeek(){
474 >    /**
475 >     * peek returns next element, or null if empty
476 >     */
477 >    public void testPeek() {
478          PriorityBlockingQueue q = populatedQueue(SIZE);
479          for (int i = 0; i < SIZE; ++i) {
480              assertEquals(i, ((Integer)q.peek()).intValue());
# Line 441 | Line 482 | public class PriorityBlockingQueueTest e
482              assertTrue(q.peek() == null ||
483                         i != ((Integer)q.peek()).intValue());
484          }
485 <        assertNull(q.peek());
485 >        assertNull(q.peek());
486      }
487  
488 <    public void testElement(){
488 >    /**
489 >     * element returns next element, or throws NSEE if empty
490 >     */
491 >    public void testElement() {
492          PriorityBlockingQueue q = populatedQueue(SIZE);
493          for (int i = 0; i < SIZE; ++i) {
494              assertEquals(i, ((Integer)q.element()).intValue());
# Line 452 | Line 496 | public class PriorityBlockingQueueTest e
496          }
497          try {
498              q.element();
499 <            fail("no such element");
500 <        }
457 <        catch (NoSuchElementException success) {}
499 >            shouldThrow();
500 >        } catch (NoSuchElementException success) {}
501      }
502  
503 <    public void testRemove(){
503 >    /**
504 >     * remove removes next element, or throws NSEE if empty
505 >     */
506 >    public void testRemove() {
507          PriorityBlockingQueue q = populatedQueue(SIZE);
508          for (int i = 0; i < SIZE; ++i) {
509              assertEquals(i, ((Integer)q.remove()).intValue());
510          }
511          try {
512              q.remove();
513 <            fail("remove should throw");
514 <        } catch (NoSuchElementException success){
469 <        }  
513 >            shouldThrow();
514 >        } catch (NoSuchElementException success) {}
515      }
516  
517 <    public void testRemoveElement(){
517 >    /**
518 >     * remove(x) removes x and returns true if present
519 >     */
520 >    public void testRemoveElement() {
521          PriorityBlockingQueue q = populatedQueue(SIZE);
522          for (int i = 1; i < SIZE; i+=2) {
523              assertTrue(q.remove(new Integer(i)));
# Line 480 | Line 528 | public class PriorityBlockingQueueTest e
528          }
529          assertTrue(q.isEmpty());
530      }
531 <        
532 <    public void testContains(){
531 >
532 >    /**
533 >     * contains(x) reports true when elements added but not yet removed
534 >     */
535 >    public void testContains() {
536          PriorityBlockingQueue q = populatedQueue(SIZE);
537          for (int i = 0; i < SIZE; ++i) {
538              assertTrue(q.contains(new Integer(i)));
# Line 490 | Line 541 | public class PriorityBlockingQueueTest e
541          }
542      }
543  
544 <    public void testClear(){
544 >    /**
545 >     * clear removes all elements
546 >     */
547 >    public void testClear() {
548          PriorityBlockingQueue q = populatedQueue(SIZE);
549          q.clear();
550          assertTrue(q.isEmpty());
551          assertEquals(0, q.size());
552 <        assertEquals(NOCAP, q.remainingCapacity());
499 <        q.add(new Integer(1));
552 >        q.add(one);
553          assertFalse(q.isEmpty());
554 +        assertTrue(q.contains(one));
555          q.clear();
556          assertTrue(q.isEmpty());
557      }
558  
559 <    public void testContainsAll(){
559 >    /**
560 >     * containsAll(c) is true when c contains a subset of elements
561 >     */
562 >    public void testContainsAll() {
563          PriorityBlockingQueue q = populatedQueue(SIZE);
564          PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE);
565          for (int i = 0; i < SIZE; ++i) {
# Line 513 | Line 570 | public class PriorityBlockingQueueTest e
570          assertTrue(p.containsAll(q));
571      }
572  
573 <    public void testRetainAll(){
573 >    /**
574 >     * retainAll(c) retains only those elements of c and reports true if changed
575 >     */
576 >    public void testRetainAll() {
577          PriorityBlockingQueue q = populatedQueue(SIZE);
578          PriorityBlockingQueue p = populatedQueue(SIZE);
579          for (int i = 0; i < SIZE; ++i) {
# Line 529 | Line 589 | public class PriorityBlockingQueueTest e
589          }
590      }
591  
592 <    public void testRemoveAll(){
592 >    /**
593 >     * removeAll(c) removes only those elements of c and reports true if changed
594 >     */
595 >    public void testRemoveAll() {
596          for (int i = 1; i < SIZE; ++i) {
597              PriorityBlockingQueue q = populatedQueue(SIZE);
598              PriorityBlockingQueue p = populatedQueue(i);
# Line 542 | Line 605 | public class PriorityBlockingQueueTest e
605          }
606      }
607  
608 <    public void testToArray(){
608 >    /**
609 >     *  toArray contains all elements
610 >     */
611 >    public void testToArray() throws InterruptedException {
612          PriorityBlockingQueue q = populatedQueue(SIZE);
613 <        Object[] o = q.toArray();
613 >        Object[] o = q.toArray();
614          Arrays.sort(o);
615 <        try {
616 <        for(int i = 0; i < o.length; i++)
551 <            assertEquals(o[i], q.take());
552 <        } catch (InterruptedException e){
553 <            fail("Unexpected exception");
554 <        }    
615 >        for (int i = 0; i < o.length; i++)
616 >            assertEquals(o[i], q.take());
617      }
618  
619 <    public void testToArray2(){
619 >    /**
620 >     * toArray(a) contains all elements
621 >     */
622 >    public void testToArray2() throws InterruptedException {
623          PriorityBlockingQueue q = populatedQueue(SIZE);
624 <        Integer[] ints = new Integer[SIZE];
625 <        ints = (Integer[])q.toArray(ints);
624 >        Integer[] ints = new Integer[SIZE];
625 >        ints = (Integer[])q.toArray(ints);
626          Arrays.sort(ints);
627 <        try {
628 <            for(int i = 0; i < ints.length; i++)
629 <                assertEquals(ints[i], q.take());
630 <        } catch (InterruptedException e){
631 <            fail("Unexpected exception");
632 <        }    
627 >        for (int i = 0; i < ints.length; i++)
628 >            assertEquals(ints[i], q.take());
629 >    }
630 >
631 >    /**
632 >     * toArray(null) throws NPE
633 >     */
634 >    public void testToArray_BadArg() {
635 >        try {
636 >            PriorityBlockingQueue q = populatedQueue(SIZE);
637 >            Object o[] = q.toArray(null);
638 >            shouldThrow();
639 >        } catch (NullPointerException success) {}
640 >    }
641 >
642 >    /**
643 >     * toArray with incompatible array type throws CCE
644 >     */
645 >    public void testToArray1_BadArg() {
646 >        try {
647 >            PriorityBlockingQueue q = populatedQueue(SIZE);
648 >            Object o[] = q.toArray(new String[10] );
649 >            shouldThrow();
650 >        } catch (ArrayStoreException  success) {}
651      }
652 <    
653 <    public void testIterator(){
652 >
653 >    /**
654 >     * iterator iterates through all elements
655 >     */
656 >    public void testIterator() {
657          PriorityBlockingQueue q = populatedQueue(SIZE);
658          int i = 0;
659 <        Iterator it = q.iterator();
660 <        while(it.hasNext()) {
659 >        Iterator it = q.iterator();
660 >        while (it.hasNext()) {
661              assertTrue(q.contains(it.next()));
662              ++i;
663          }
664          assertEquals(i, SIZE);
665      }
666  
667 +    /**
668 +     * iterator.remove removes current element
669 +     */
670      public void testIteratorRemove () {
582
671          final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
584
672          q.add(new Integer(2));
673          q.add(new Integer(1));
674          q.add(new Integer(3));
# Line 597 | Line 684 | public class PriorityBlockingQueueTest e
684      }
685  
686  
687 <    public void testToString(){
687 >    /**
688 >     * toString contains toStrings of elements
689 >     */
690 >    public void testToString() {
691          PriorityBlockingQueue q = populatedQueue(SIZE);
692          String s = q.toString();
693          for (int i = 0; i < SIZE; ++i) {
694              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
695          }
696 <    }        
696 >    }
697  
698 +    /**
699 +     * offer transfers elements across Executor tasks
700 +     */
701      public void testPollInExecutor() {
609
702          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
611
703          ExecutorService executor = Executors.newFixedThreadPool(2);
704 <
705 <        executor.execute(new Runnable() {
615 <            public void run() {
704 >        executor.execute(new CheckedRunnable() {
705 >            public void realRun() throws InterruptedException {
706                  threadAssertNull(q.poll());
707 <                try {
708 <                    threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
709 <                    threadAssertTrue(q.isEmpty());
710 <                }
711 <                catch (InterruptedException e) {
712 <                    threadFail("should not be interrupted");
713 <                }
714 <            }
715 <        });
707 >                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
708 >                threadAssertTrue(q.isEmpty());
709 >            }});
710 >
711 >        executor.execute(new CheckedRunnable() {
712 >            public void realRun() throws InterruptedException {
713 >                Thread.sleep(SMALL_DELAY_MS);
714 >                q.put(new Integer(1));
715 >            }});
716  
627        executor.execute(new Runnable() {
628            public void run() {
629                try {
630                    Thread.sleep(SMALL_DELAY_MS);
631                    q.put(new Integer(1));
632                }
633                catch (InterruptedException e) {
634                    threadFail("should not be interrupted");
635                }
636            }
637        });
638        
717          joinPool(executor);
718 +    }
719 +
720 +    /**
721 +     * A deserialized serialized queue has same elements
722 +     */
723 +    public void testSerialization() throws Exception {
724 +        PriorityBlockingQueue q = populatedQueue(SIZE);
725 +        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
726 +        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
727 +        out.writeObject(q);
728 +        out.close();
729 +
730 +        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
731 +        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
732 +        PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
733 +        assertEquals(q.size(), r.size());
734 +        while (!q.isEmpty())
735 +            assertEquals(q.remove(), r.remove());
736 +    }
737  
738 +    /**
739 +     * drainTo(null) throws NPE
740 +     */
741 +    public void testDrainToNull() {
742 +        PriorityBlockingQueue q = populatedQueue(SIZE);
743 +        try {
744 +            q.drainTo(null);
745 +            shouldThrow();
746 +        } catch (NullPointerException success) {}
747 +    }
748 +
749 +    /**
750 +     * drainTo(this) throws IAE
751 +     */
752 +    public void testDrainToSelf() {
753 +        PriorityBlockingQueue q = populatedQueue(SIZE);
754 +        try {
755 +            q.drainTo(q);
756 +            shouldThrow();
757 +        } catch (IllegalArgumentException success) {}
758 +    }
759 +
760 +    /**
761 +     * drainTo(c) empties queue into another collection c
762 +     */
763 +    public void testDrainTo() {
764 +        PriorityBlockingQueue q = populatedQueue(SIZE);
765 +        ArrayList l = new ArrayList();
766 +        q.drainTo(l);
767 +        assertEquals(q.size(), 0);
768 +        assertEquals(l.size(), SIZE);
769 +        for (int i = 0; i < SIZE; ++i)
770 +            assertEquals(l.get(i), new Integer(i));
771 +        q.add(zero);
772 +        q.add(one);
773 +        assertFalse(q.isEmpty());
774 +        assertTrue(q.contains(zero));
775 +        assertTrue(q.contains(one));
776 +        l.clear();
777 +        q.drainTo(l);
778 +        assertEquals(q.size(), 0);
779 +        assertEquals(l.size(), 2);
780 +        for (int i = 0; i < 2; ++i)
781 +            assertEquals(l.get(i), new Integer(i));
782 +    }
783 +
784 +    /**
785 +     * drainTo empties queue
786 +     */
787 +    public void testDrainToWithActivePut() throws InterruptedException {
788 +        final PriorityBlockingQueue q = populatedQueue(SIZE);
789 +        Thread t = new Thread(new CheckedRunnable() {
790 +            public void realRun() {
791 +                q.put(new Integer(SIZE+1));
792 +            }});
793 +
794 +        t.start();
795 +        ArrayList l = new ArrayList();
796 +        q.drainTo(l);
797 +        assertTrue(l.size() >= SIZE);
798 +        for (int i = 0; i < SIZE; ++i)
799 +            assertEquals(l.get(i), new Integer(i));
800 +        t.join();
801 +        assertTrue(q.size() + l.size() >= SIZE);
802      }
803  
804 <    public void testSerialization() {
804 >    /**
805 >     * drainTo(null, n) throws NPE
806 >     */
807 >    public void testDrainToNullN() {
808          PriorityBlockingQueue q = populatedQueue(SIZE);
809          try {
810 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
811 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
812 <            out.writeObject(q);
813 <            out.close();
814 <
815 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
816 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
817 <            PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
818 <            assertEquals(q.size(), r.size());
819 <            while (!q.isEmpty())
820 <                assertEquals(q.remove(), r.remove());
821 <        } catch(Exception e){
822 <            fail("unexpected exception");
810 >            q.drainTo(null, 0);
811 >            shouldThrow();
812 >        } catch (NullPointerException success) {}
813 >    }
814 >
815 >    /**
816 >     * drainTo(this, n) throws IAE
817 >     */
818 >    public void testDrainToSelfN() {
819 >        PriorityBlockingQueue q = populatedQueue(SIZE);
820 >        try {
821 >            q.drainTo(q, 0);
822 >            shouldThrow();
823 >        } catch (IllegalArgumentException success) {}
824 >    }
825 >
826 >    /**
827 >     * drainTo(c, n) empties first max {n, size} elements of queue into c
828 >     */
829 >    public void testDrainToN() {
830 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
831 >        for (int i = 0; i < SIZE + 2; ++i) {
832 >            for (int j = 0; j < SIZE; j++)
833 >                assertTrue(q.offer(new Integer(j)));
834 >            ArrayList l = new ArrayList();
835 >            q.drainTo(l, i);
836 >            int k = (i < SIZE)? i : SIZE;
837 >            assertEquals(l.size(), k);
838 >            assertEquals(q.size(), SIZE-k);
839 >            for (int j = 0; j < k; ++j)
840 >                assertEquals(l.get(j), new Integer(j));
841 >            while (q.poll() != null) ;
842          }
843      }
844  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines