--- jsr166/src/test/tck/ArrayBlockingQueueTest.java 2003/12/29 19:05:40 1.8 +++ jsr166/src/test/tck/ArrayBlockingQueueTest.java 2009/11/16 04:57:09 1.13 @@ -2,8 +2,8 @@ * 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. + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ @@ -14,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); @@ -27,14 +27,14 @@ public class ArrayBlockingQueueTest exte private ArrayBlockingQueue populatedQueue(int n) { ArrayBlockingQueue q = new ArrayBlockingQueue(n); assertTrue(q.isEmpty()); - for(int i = 0; i < n; i++) + for (int i = 0; i < n; i++) assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(0, q.remainingCapacity()); assertEquals(n, q.size()); return q; } - + /** * A new queue has the indicated capacity */ @@ -159,7 +159,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -170,7 +170,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(1); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -194,7 +194,7 @@ public class ArrayBlockingQueueTest exte assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); } catch (IllegalStateException success){ - } + } } /** @@ -289,9 +289,9 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); q.put(null); shouldThrow(); - } + } catch (NullPointerException success){ - } + } catch (InterruptedException ie) { unexpectedException(); } @@ -319,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; @@ -332,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(); } @@ -395,7 +395,7 @@ public class ArrayBlockingQueueTest exte } catch (InterruptedException success){} } }); - + try { t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -417,7 +417,7 @@ public class ArrayBlockingQueueTest exte } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -430,7 +430,7 @@ public class ArrayBlockingQueueTest exte try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success){ } } }); try { @@ -457,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(); } @@ -494,7 +494,7 @@ public class ArrayBlockingQueueTest exte assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -509,7 +509,7 @@ public class ArrayBlockingQueueTest exte assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -526,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(); } @@ -552,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 { @@ -564,7 +564,7 @@ public class ArrayBlockingQueueTest exte } catch (Exception e){ unexpectedException(); } - } + } /** @@ -609,7 +609,7 @@ public class ArrayBlockingQueueTest exte q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -626,7 +626,7 @@ public class ArrayBlockingQueueTest exte } assertTrue(q.isEmpty()); } - + /** * contains(x) reports true when elements added but not yet removed */ @@ -650,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()); } @@ -710,11 +711,11 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); Object[] o = q.toArray(); try { - for(int i = 0; i < o.length; i++) + for (int i = 0; i < o.length; i++) assertEquals(o[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -725,11 +726,11 @@ public class ArrayBlockingQueueTest exte Integer[] ints = new Integer[SIZE]; ints = (Integer[])q.toArray(ints); try { - for(int i = 0; i < ints.length; i++) + for (int i = 0; i < ints.length; i++) assertEquals(ints[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -740,7 +741,7 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); Object o[] = q.toArray(null); shouldThrow(); - } catch(NullPointerException success){} + } catch (NullPointerException success){} } /** @@ -751,10 +752,10 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); Object o[] = q.toArray(new String[10] ); shouldThrow(); - } catch(ArrayStoreException success){} + } catch (ArrayStoreException success){} } - + /** * iterator iterates through all elements */ @@ -762,12 +763,12 @@ public class ArrayBlockingQueueTest exte ArrayBlockingQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); try { - while(it.hasNext()){ + while (it.hasNext()){ assertEquals(it.next(), q.take()); } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -782,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); @@ -838,7 +839,7 @@ public class ArrayBlockingQueueTest exte for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -873,7 +874,7 @@ public class ArrayBlockingQueueTest exte } } }); - + joinPool(executor); } @@ -908,7 +909,7 @@ public class ArrayBlockingQueueTest exte } } }); - + joinPool(executor); } @@ -928,60 +929,71 @@ 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){ + } catch (Exception e){ unexpectedException(); } } /** * drainTo(null) throws NPE - */ + */ public void testDrainToNull() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(null); shouldThrow(); - } catch(NullPointerException success) { + } catch (NullPointerException success) { } } /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(q); shouldThrow(); - } catch(IllegalArgumentException success) { + } catch (IllegalArgumentException success) { } } /** * 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(); } } @@ -991,52 +1003,55 @@ 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); - } catch(Exception e){ + assertTrue(q.size() + l.size() >= SIZE); + } catch (Exception e){ unexpectedException(); } } /** * drainTo(null, n) throws NPE - */ + */ public void testDrainToNullN() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(null, 0); shouldThrow(); - } catch(NullPointerException success) { + } catch (NullPointerException success) { } } /** * drainTo(this, n) throws IAE - */ + */ public void testDrainToSelfN() { ArrayBlockingQueue q = populatedQueue(SIZE); try { q.drainTo(q, 0); shouldThrow(); - } catch(IllegalArgumentException success) { + } catch (IllegalArgumentException success) { } } /** * 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) ; } }