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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.7 by dl, Sat Dec 27 19:26:42 2003 UTC vs.
Revision 1.15 by jsr166, Sat Nov 21 02:07:26 2009 UTC

# Line 2 | Line 2
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.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9  
# Line 14 | Line 14 | import java.io.*;
14  
15   public class ArrayBlockingQueueTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(ArrayBlockingQueueTest.class);
20 >        return new TestSuite(ArrayBlockingQueueTest.class);
21      }
22  
23      /**
# Line 27 | Line 27 | public class ArrayBlockingQueueTest exte
27      private ArrayBlockingQueue populatedQueue(int n) {
28          ArrayBlockingQueue q = new ArrayBlockingQueue(n);
29          assertTrue(q.isEmpty());
30 <        for(int i = 0; i < n; i++)
31 <            assertTrue(q.offer(new Integer(i)));
30 >        for (int i = 0; i < n; i++)
31 >            assertTrue(q.offer(new Integer(i)));
32          assertFalse(q.isEmpty());
33          assertEquals(0, q.remainingCapacity());
34 <        assertEquals(n, q.size());
34 >        assertEquals(n, q.size());
35          return q;
36      }
37 <
37 >
38      /**
39       * A new queue has the indicated capacity
40       */
# Line 155 | Line 155 | public class ArrayBlockingQueueTest exte
155       *  offer(null) throws NPE
156       */
157      public void testOfferNull() {
158 <        try {
158 >        try {
159              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
160              q.offer(null);
161              shouldThrow();
162 <        } catch (NullPointerException success) { }  
162 >        } catch (NullPointerException success) { }
163      }
164  
165      /**
166       *  add(null) throws NPE
167       */
168      public void testAddNull() {
169 <        try {
169 >        try {
170              ArrayBlockingQueue q = new ArrayBlockingQueue(1);
171              q.add(null);
172              shouldThrow();
173 <        } catch (NullPointerException success) { }  
173 >        } catch (NullPointerException success) { }
174      }
175  
176      /**
# Line 186 | Line 186 | public class ArrayBlockingQueueTest exte
186       * add succeeds if not full; throws ISE if full
187       */
188      public void testAdd() {
189 <        try {
189 >        try {
190              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
191              for (int i = 0; i < SIZE; ++i) {
192                  assertTrue(q.add(new Integer(i)));
193              }
194              assertEquals(0, q.remainingCapacity());
195              q.add(new Integer(SIZE));
196 <        } catch (IllegalStateException success){
197 <        }  
196 >        } catch (IllegalStateException success) {
197 >        }
198      }
199  
200      /**
# Line 285 | Line 285 | public class ArrayBlockingQueueTest exte
285       *  put(null) throws NPE
286       */
287       public void testPutNull() {
288 <        try {
288 >        try {
289              ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
290              q.put(null);
291              shouldThrow();
292 <        }
293 <        catch (NullPointerException success){
294 <        }  
292 >        }
293 >        catch (NullPointerException success) {
294 >        }
295          catch (InterruptedException ie) {
296 <            unexpectedException();
296 >            unexpectedException();
297          }
298       }
299  
# Line 311 | Line 311 | public class ArrayBlockingQueueTest exte
311               assertEquals(0, q.remainingCapacity());
312           }
313          catch (InterruptedException ie) {
314 <            unexpectedException();
314 >            unexpectedException();
315          }
316      }
317  
# Line 319 | Line 319 | public class ArrayBlockingQueueTest exte
319       * put blocks interruptibly if full
320       */
321      public void testBlockingPut() {
322 +        final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
323          Thread t = new Thread(new Runnable() {
324                  public void run() {
325                      int added = 0;
326                      try {
326                        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
327                          for (int i = 0; i < SIZE; ++i) {
328                              q.put(new Integer(i));
329                              ++added;
330                          }
331                          q.put(new Integer(SIZE));
332                          threadShouldThrow();
333 <                    } catch (InterruptedException ie){
333 >                    } catch (InterruptedException ie) {
334                          threadAssertEquals(added, SIZE);
335 <                    }  
335 >                    }
336                  }});
337 <        try {
337 >        try {
338              t.start();
339 <           Thread.sleep(SHORT_DELAY_MS);
339 >           Thread.sleep(MEDIUM_DELAY_MS);
340             t.interrupt();
341             t.join();
342          }
343          catch (InterruptedException ie) {
344 <            unexpectedException();
344 >            unexpectedException();
345          }
346      }
347  
# Line 362 | Line 362 | public class ArrayBlockingQueueTest exte
362                          ++added;
363                          q.put(new Object());
364                          ++added;
365 <                        threadShouldThrow();
366 <                    } catch (InterruptedException e){
365 >                        threadShouldThrow();
366 >                    } catch (InterruptedException e) {
367                          threadAssertTrue(added >= 2);
368                      }
369                  }
# Line 374 | Line 374 | public class ArrayBlockingQueueTest exte
374              q.take();
375              t.interrupt();
376              t.join();
377 <        } catch (Exception e){
377 >        } catch (Exception e) {
378              unexpectedException();
379          }
380      }
# Line 391 | Line 391 | public class ArrayBlockingQueueTest exte
391                          q.put(new Object());
392                          threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
393                          q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
394 <                        threadShouldThrow();
395 <                    } catch (InterruptedException success){}
394 >                        threadShouldThrow();
395 >                    } catch (InterruptedException success) {}
396                  }
397              });
398 <        
398 >
399          try {
400              t.start();
401              Thread.sleep(SHORT_DELAY_MS);
402              t.interrupt();
403              t.join();
404 <        } catch (Exception e){
404 >        } catch (Exception e) {
405              unexpectedException();
406          }
407      }
# Line 410 | Line 410 | public class ArrayBlockingQueueTest exte
410       * take retrieves elements in FIFO order
411       */
412      public void testTake() {
413 <        try {
413 >        try {
414              ArrayBlockingQueue q = populatedQueue(SIZE);
415              for (int i = 0; i < SIZE; ++i) {
416                  assertEquals(i, ((Integer)q.take()).intValue());
417              }
418 <        } catch (InterruptedException e){
419 <            unexpectedException();
420 <        }  
418 >        } catch (InterruptedException e) {
419 >            unexpectedException();
420 >        }
421      }
422  
423      /**
# Line 429 | Line 429 | public class ArrayBlockingQueueTest exte
429                  public void run() {
430                      try {
431                          q.take();
432 <                        threadShouldThrow();
433 <                    } catch (InterruptedException success){ }                
432 >                        threadShouldThrow();
433 >                    } catch (InterruptedException success) { }
434                  }
435              });
436          try {
# Line 438 | Line 438 | public class ArrayBlockingQueueTest exte
438              Thread.sleep(SHORT_DELAY_MS);
439              t.interrupt();
440              t.join();
441 <        } catch (Exception e){
441 >        } catch (Exception e) {
442              unexpectedException();
443          }
444      }
# Line 456 | Line 456 | public class ArrayBlockingQueueTest exte
456                          }
457                          q.take();
458                          threadShouldThrow();
459 <                    } catch (InterruptedException success){
460 <                    }  
459 >                    } catch (InterruptedException success) {
460 >                    }
461                  }});
462 <        try {
462 >        try {
463              t.start();
464 <            Thread.sleep(SHORT_DELAY_MS);
464 >            Thread.sleep(SHORT_DELAY_MS);
465              t.interrupt();
466              t.join();
467          }
468          catch (InterruptedException ie) {
469 <            unexpectedException();
469 >            unexpectedException();
470          }
471      }
472  
# Line 479 | Line 479 | public class ArrayBlockingQueueTest exte
479          for (int i = 0; i < SIZE; ++i) {
480              assertEquals(i, ((Integer)q.poll()).intValue());
481          }
482 <        assertNull(q.poll());
482 >        assertNull(q.poll());
483      }
484  
485      /**
# Line 492 | Line 492 | public class ArrayBlockingQueueTest exte
492                  assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
493              }
494              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
495 <        } catch (InterruptedException e){
496 <            unexpectedException();
497 <        }  
495 >        } catch (InterruptedException e) {
496 >            unexpectedException();
497 >        }
498      }
499  
500      /**
# Line 507 | Line 507 | public class ArrayBlockingQueueTest exte
507                  assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
508              }
509              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
510 <        } catch (InterruptedException e){
511 <            unexpectedException();
512 <        }  
510 >        } catch (InterruptedException e) {
511 >            unexpectedException();
512 >        }
513      }
514  
515      /**
# Line 525 | Line 525 | public class ArrayBlockingQueueTest exte
525                              threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
526                          }
527                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
528 <                    } catch (InterruptedException success){
529 <                    }  
528 >                    } catch (InterruptedException success) {
529 >                    }
530                  }});
531 <        try {
531 >        try {
532              t.start();
533 <            Thread.sleep(SHORT_DELAY_MS);
533 >            Thread.sleep(SHORT_DELAY_MS);
534              t.interrupt();
535              t.join();
536          }
537          catch (InterruptedException ie) {
538 <            unexpectedException();
538 >            unexpectedException();
539          }
540      }
541  
# Line 551 | Line 551 | public class ArrayBlockingQueueTest exte
551                          threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
552                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
553                          q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
554 <                        threadShouldThrow();
555 <                    } catch (InterruptedException success) { }                
554 >                        threadShouldThrow();
555 >                    } catch (InterruptedException success) { }
556                  }
557              });
558          try {
# Line 561 | Line 561 | public class ArrayBlockingQueueTest exte
561              assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
562              t.interrupt();
563              t.join();
564 <        } catch (Exception e){
564 >        } catch (Exception e) {
565              unexpectedException();
566          }
567 <    }  
567 >    }
568  
569  
570      /**
# Line 578 | Line 578 | public class ArrayBlockingQueueTest exte
578              assertTrue(q.peek() == null ||
579                         i != ((Integer)q.peek()).intValue());
580          }
581 <        assertNull(q.peek());
581 >        assertNull(q.peek());
582      }
583  
584      /**
# Line 608 | Line 608 | public class ArrayBlockingQueueTest exte
608          try {
609              q.remove();
610              shouldThrow();
611 <        } catch (NoSuchElementException success){
612 <        }  
611 >        } catch (NoSuchElementException success) {
612 >        }
613      }
614  
615      /**
# Line 626 | Line 626 | public class ArrayBlockingQueueTest exte
626          }
627          assertTrue(q.isEmpty());
628      }
629 <        
629 >
630      /**
631       * contains(x) reports true when elements added but not yet removed
632       */
# Line 650 | Line 650 | public class ArrayBlockingQueueTest exte
650          assertEquals(SIZE, q.remainingCapacity());
651          q.add(one);
652          assertFalse(q.isEmpty());
653 +        assertTrue(q.contains(one));
654          q.clear();
655          assertTrue(q.isEmpty());
656      }
# Line 708 | Line 709 | public class ArrayBlockingQueueTest exte
709       */
710      public void testToArray() {
711          ArrayBlockingQueue q = populatedQueue(SIZE);
712 <        Object[] o = q.toArray();
713 <        try {
714 <        for(int i = 0; i < o.length; i++)
715 <            assertEquals(o[i], q.take());
716 <        } catch (InterruptedException e){
717 <            unexpectedException();
718 <        }    
712 >        Object[] o = q.toArray();
713 >        try {
714 >        for (int i = 0; i < o.length; i++)
715 >            assertEquals(o[i], q.take());
716 >        } catch (InterruptedException e) {
717 >            unexpectedException();
718 >        }
719      }
720  
721      /**
# Line 722 | Line 723 | public class ArrayBlockingQueueTest exte
723       */
724      public void testToArray2() {
725          ArrayBlockingQueue q = populatedQueue(SIZE);
726 <        Integer[] ints = new Integer[SIZE];
727 <        ints = (Integer[])q.toArray(ints);
728 <        try {
729 <            for(int i = 0; i < ints.length; i++)
730 <                assertEquals(ints[i], q.take());
731 <        } catch (InterruptedException e){
732 <            unexpectedException();
733 <        }    
726 >        Integer[] ints = new Integer[SIZE];
727 >        ints = (Integer[])q.toArray(ints);
728 >        try {
729 >            for (int i = 0; i < ints.length; i++)
730 >                assertEquals(ints[i], q.take());
731 >        } catch (InterruptedException e) {
732 >            unexpectedException();
733 >        }
734      }
735  
736      /**
737       * toArray(null) throws NPE
738       */
739      public void testToArray_BadArg() {
740 <        try {
740 >        try {
741              ArrayBlockingQueue q = populatedQueue(SIZE);
742 <            Object o[] = q.toArray(null);
743 <            shouldThrow();
744 <        } catch(NullPointerException success){}
742 >            Object o[] = q.toArray(null);
743 >            shouldThrow();
744 >        } catch (NullPointerException success) {}
745      }
746  
747      /**
748 <     * toArray with incompatable array type throws CCE
748 >     * toArray with incompatible array type throws CCE
749       */
750      public void testToArray1_BadArg() {
751 <        try {
751 >        try {
752              ArrayBlockingQueue q = populatedQueue(SIZE);
753 <            Object o[] = q.toArray(new String[10] );
754 <            shouldThrow();
755 <        } catch(ArrayStoreException  success){}
753 >            Object o[] = q.toArray(new String[10] );
754 >            shouldThrow();
755 >        } catch (ArrayStoreException  success) {}
756      }
757  
758 <    
758 >
759      /**
760       * iterator iterates through all elements
761       */
762      public void testIterator() {
763          ArrayBlockingQueue q = populatedQueue(SIZE);
764 <        Iterator it = q.iterator();
765 <        try {
766 <            while(it.hasNext()){
767 <                assertEquals(it.next(), q.take());
768 <            }
769 <        } catch (InterruptedException e){
770 <            unexpectedException();
771 <        }    
764 >        Iterator it = q.iterator();
765 >        try {
766 >            while (it.hasNext()) {
767 >                assertEquals(it.next(), q.take());
768 >            }
769 >        } catch (InterruptedException e) {
770 >            unexpectedException();
771 >        }
772      }
773  
774      /**
# Line 782 | Line 783 | public class ArrayBlockingQueueTest exte
783          Iterator it = q.iterator();
784          it.next();
785          it.remove();
786 <        
786 >
787          it = q.iterator();
788          assertEquals(it.next(), one);
789          assertEquals(it.next(), three);
# Line 838 | Line 839 | public class ArrayBlockingQueueTest exte
839          for (int i = 0; i < SIZE; ++i) {
840              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
841          }
842 <    }        
842 >    }
843  
844  
845      /**
# Line 873 | Line 874 | public class ArrayBlockingQueueTest exte
874                  }
875              }
876          });
877 <        
877 >
878          joinPool(executor);
879  
880      }
# Line 908 | Line 909 | public class ArrayBlockingQueueTest exte
909                  }
910              }
911          });
912 <        
912 >
913          joinPool(executor);
914      }
915  
# Line 928 | Line 929 | public class ArrayBlockingQueueTest exte
929              ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
930              ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
931              assertEquals(q.size(), r.size());
932 <            while (!q.isEmpty())
932 >            while (!q.isEmpty())
933                  assertEquals(q.remove(), r.remove());
934 <        } catch(Exception e){
934 >        } catch (Exception e) {
935              unexpectedException();
936          }
937      }
938  
939      /**
940       * drainTo(null) throws NPE
941 <     */
941 >     */
942      public void testDrainToNull() {
943          ArrayBlockingQueue q = populatedQueue(SIZE);
944          try {
945              q.drainTo(null);
946              shouldThrow();
947 <        } catch(NullPointerException success) {
947 >        } catch (NullPointerException success) {
948          }
949      }
950  
951      /**
952       * drainTo(this) throws IAE
953 <     */
953 >     */
954      public void testDrainToSelf() {
955          ArrayBlockingQueue q = populatedQueue(SIZE);
956          try {
957              q.drainTo(q);
958              shouldThrow();
959 <        } catch(IllegalArgumentException success) {
959 >        } catch (IllegalArgumentException success) {
960          }
961      }
962  
963      /**
964       * drainTo(c) empties queue into another collection c
965 <     */
965 >     */
966      public void testDrainTo() {
967          ArrayBlockingQueue q = populatedQueue(SIZE);
968          ArrayList l = new ArrayList();
969          q.drainTo(l);
970          assertEquals(q.size(), 0);
971          assertEquals(l.size(), SIZE);
972 <        for (int i = 0; i < SIZE; ++i)
972 >        for (int i = 0; i < SIZE; ++i)
973 >            assertEquals(l.get(i), new Integer(i));
974 >        q.add(zero);
975 >        q.add(one);
976 >        assertFalse(q.isEmpty());
977 >        assertTrue(q.contains(zero));
978 >        assertTrue(q.contains(one));
979 >        l.clear();
980 >        q.drainTo(l);
981 >        assertEquals(q.size(), 0);
982 >        assertEquals(l.size(), 2);
983 >        for (int i = 0; i < 2; ++i)
984              assertEquals(l.get(i), new Integer(i));
985      }
986  
987      /**
988       * drainTo empties full queue, unblocking a waiting put.
989 <     */
989 >     */
990      public void testDrainToWithActivePut() {
991          final ArrayBlockingQueue q = populatedQueue(SIZE);
992          Thread t = new Thread(new Runnable() {
993                  public void run() {
994                      try {
995                          q.put(new Integer(SIZE+1));
996 <                    } catch (InterruptedException ie){
996 >                    } catch (InterruptedException ie) {
997                          threadUnexpectedException();
998                      }
999                  }
# Line 991 | Line 1003 | public class ArrayBlockingQueueTest exte
1003              ArrayList l = new ArrayList();
1004              q.drainTo(l);
1005              assertTrue(l.size() >= SIZE);
1006 <            for (int i = 0; i < SIZE; ++i)
1006 >            for (int i = 0; i < SIZE; ++i)
1007                  assertEquals(l.get(i), new Integer(i));
1008              t.join();
1009 <            assertTrue(q.size() + l.size() == SIZE+1);
1010 <        } catch(Exception e){
1009 >            assertTrue(q.size() + l.size() >= SIZE);
1010 >        } catch (Exception e) {
1011              unexpectedException();
1012          }
1013      }
1014  
1015      /**
1016       * drainTo(null, n) throws NPE
1017 <     */
1017 >     */
1018      public void testDrainToNullN() {
1019          ArrayBlockingQueue q = populatedQueue(SIZE);
1020          try {
1021              q.drainTo(null, 0);
1022              shouldThrow();
1023 <        } catch(NullPointerException success) {
1023 >        } catch (NullPointerException success) {
1024          }
1025      }
1026  
1027      /**
1028       * drainTo(this, n) throws IAE
1029 <     */
1029 >     */
1030      public void testDrainToSelfN() {
1031          ArrayBlockingQueue q = populatedQueue(SIZE);
1032          try {
1033              q.drainTo(q, 0);
1034              shouldThrow();
1035 <        } catch(IllegalArgumentException success) {
1035 >        } catch (IllegalArgumentException success) {
1036          }
1037      }
1038  
1039      /**
1040       * drainTo(c, n) empties first max {n, size} elements of queue into c
1041 <     */
1041 >     */
1042      public void testDrainToN() {
1043 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
1044          for (int i = 0; i < SIZE + 2; ++i) {
1045 <            ArrayBlockingQueue q = populatedQueue(SIZE);
1045 >            for (int j = 0; j < SIZE; j++)
1046 >                assertTrue(q.offer(new Integer(j)));
1047              ArrayList l = new ArrayList();
1048              q.drainTo(l, i);
1049              int k = (i < SIZE)? i : SIZE;
1036            assertEquals(q.size(), SIZE-k);
1050              assertEquals(l.size(), k);
1051 <            for (int j = 0; j < k; ++j)
1051 >            assertEquals(q.size(), SIZE-k);
1052 >            for (int j = 0; j < k; ++j)
1053                  assertEquals(l.get(j), new Integer(j));
1054 +            while (q.poll() != null) ;
1055          }
1056      }
1057  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines