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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines