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.6 by dl, Thu Sep 25 11:02:41 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines