--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2003/10/05 23:00:39 1.6 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/02 20:28:31 1.12 @@ -1,8 +1,9 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ @@ -13,7 +14,7 @@ import java.io.*; public class ArrayBlockingQueueTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { return new TestSuite(ArrayBlockingQueueTest.class); @@ -33,7 +34,7 @@ public class ArrayBlockingQueueTest exte assertEquals(n, q.size()); return q; } - + /** * A new queue has the indicated capacity */ @@ -158,7 +159,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -169,7 +170,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -193,7 +194,7 @@ public class ArrayBlockingQueueTest exte assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); } catch (IllegalStateException success){ - } + } } /** @@ -288,9 +289,9 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); q.put(null); shouldThrow(); - } + } catch (NullPointerException success){ - } + } catch (InterruptedException ie) { unexpectedException(); } @@ -318,11 +319,11 @@ public class ArrayBlockingQueueTest exte * put blocks interruptibly if full */ public void testBlockingPut() { + final ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); Thread t = new Thread(new Runnable() { public void run() { int added = 0; try { - ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); for (int i = 0; i < SIZE; ++i) { q.put(new Integer(i)); ++added; @@ -331,11 +332,11 @@ public class ArrayBlockingQueueTest exte threadShouldThrow(); } catch (InterruptedException ie){ threadAssertEquals(added, SIZE); - } + } }}); - try { + try { t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(MEDIUM_DELAY_MS); t.interrupt(); t.join(); } @@ -394,7 +395,7 @@ public class ArrayBlockingQueueTest exte } catch (InterruptedException success){} } }); - + try { t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -416,7 +417,7 @@ public class ArrayBlockingQueueTest exte } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -429,7 +430,7 @@ public class ArrayBlockingQueueTest exte try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success){ } } }); try { @@ -456,11 +457,11 @@ public class ArrayBlockingQueueTest exte q.take(); threadShouldThrow(); } catch (InterruptedException success){ - } + } }}); - try { + try { t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -493,7 +494,7 @@ public class ArrayBlockingQueueTest exte assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -508,7 +509,7 @@ public class ArrayBlockingQueueTest exte assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -525,11 +526,11 @@ public class ArrayBlockingQueueTest exte } threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException success){ - } + } }}); - try { + try { t.start(); - Thread.sleep(SHORT_DELAY_MS); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -551,7 +552,7 @@ public class ArrayBlockingQueueTest exte q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); threadShouldThrow(); - } catch (InterruptedException success) { } + } catch (InterruptedException success) { } } }); try { @@ -563,7 +564,7 @@ public class ArrayBlockingQueueTest exte } catch (Exception e){ unexpectedException(); } - } + } /** @@ -608,7 +609,7 @@ public class ArrayBlockingQueueTest exte q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -625,7 +626,7 @@ public class ArrayBlockingQueueTest exte } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -649,6 +650,7 @@ public class ArrayBlockingQueueTest exte assertEquals(SIZE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertTrue(q.contains(one)); q.clear(); assertTrue(q.isEmpty()); } @@ -713,7 +715,7 @@ public class ArrayBlockingQueueTest exte assertEquals(o[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -728,7 +730,7 @@ public class ArrayBlockingQueueTest exte assertEquals(ints[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -743,7 +745,7 @@ public class ArrayBlockingQueueTest exte } /** - * toArray with incompatable array type throws CCE + * toArray with incompatible array type throws CCE */ public void testToArray1_BadArg() { try { @@ -753,7 +755,7 @@ public class ArrayBlockingQueueTest exte } catch(ArrayStoreException success){} } - + /** * iterator iterates through all elements */ @@ -766,7 +768,7 @@ public class ArrayBlockingQueueTest exte } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -781,7 +783,7 @@ public class ArrayBlockingQueueTest exte Iterator it = q.iterator(); it.next(); it.remove(); - + it = q.iterator(); assertEquals(it.next(), one); assertEquals(it.next(), three); @@ -837,7 +839,7 @@ public class ArrayBlockingQueueTest exte for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -872,7 +874,7 @@ public class ArrayBlockingQueueTest exte } } }); - + joinPool(executor); } @@ -907,7 +909,7 @@ public class ArrayBlockingQueueTest exte } } }); - + joinPool(executor); } @@ -927,7 +929,7 @@ public class ArrayBlockingQueueTest exte ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject(); assertEquals(q.size(), r.size()); - while (!q.isEmpty()) + while (!q.isEmpty()) assertEquals(q.remove(), r.remove()); } catch(Exception e){ unexpectedException(); @@ -936,7 +938,7 @@ public class ArrayBlockingQueueTest exte /** * drainTo(null) throws NPE - */ + */ public void testDrainToNull() { ArrayBlockingQueue q = populatedQueue(SIZE); try { @@ -948,7 +950,7 @@ public class ArrayBlockingQueueTest exte /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { ArrayBlockingQueue q = populatedQueue(SIZE); try { @@ -960,27 +962,38 @@ public class ArrayBlockingQueueTest exte /** * drainTo(c) empties queue into another collection c - */ + */ public void testDrainTo() { ArrayBlockingQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l); assertEquals(q.size(), 0); assertEquals(l.size(), SIZE); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + q.add(zero); + q.add(one); + assertFalse(q.isEmpty()); + assertTrue(q.contains(zero)); + assertTrue(q.contains(one)); + l.clear(); + q.drainTo(l); + assertEquals(q.size(), 0); + assertEquals(l.size(), 2); + for (int i = 0; i < 2; ++i) assertEquals(l.get(i), new Integer(i)); } /** * drainTo empties full queue, unblocking a waiting put. - */ + */ public void testDrainToWithActivePut() { final ArrayBlockingQueue q = populatedQueue(SIZE); Thread t = new Thread(new Runnable() { public void run() { try { q.put(new Integer(SIZE+1)); - } catch (InterruptedException ie){ + } catch (InterruptedException ie){ threadUnexpectedException(); } } @@ -990,10 +1003,10 @@ public class ArrayBlockingQueueTest exte ArrayList l = new ArrayList(); q.drainTo(l); assertTrue(l.size() >= SIZE); - for (int i = 0; i < SIZE; ++i) + for (int i = 0; i < SIZE; ++i) assertEquals(l.get(i), new Integer(i)); t.join(); - assertTrue(q.size() + l.size() == SIZE+1); + assertTrue(q.size() + l.size() >= SIZE); } catch(Exception e){ unexpectedException(); } @@ -1001,7 +1014,7 @@ public class ArrayBlockingQueueTest exte /** * drainTo(null, n) throws NPE - */ + */ public void testDrainToNullN() { ArrayBlockingQueue q = populatedQueue(SIZE); try { @@ -1013,7 +1026,7 @@ public class ArrayBlockingQueueTest exte /** * drainTo(this, n) throws IAE - */ + */ public void testDrainToSelfN() { ArrayBlockingQueue q = populatedQueue(SIZE); try { @@ -1025,17 +1038,20 @@ public class ArrayBlockingQueueTest exte /** * drainTo(c, n) empties first max {n, size} elements of queue into c - */ + */ public void testDrainToN() { + ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2); for (int i = 0; i < SIZE + 2; ++i) { - ArrayBlockingQueue q = populatedQueue(SIZE); + for(int j = 0; j < SIZE; j++) + assertTrue(q.offer(new Integer(j))); ArrayList l = new ArrayList(); q.drainTo(l, i); int k = (i < SIZE)? i : SIZE; - assertEquals(q.size(), SIZE-k); assertEquals(l.size(), k); - for (int j = 0; j < k; ++j) + assertEquals(q.size(), SIZE-k); + for (int j = 0; j < k; ++j) assertEquals(l.get(j), new Integer(j)); + while (q.poll() != null) ; } }