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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines