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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines