--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2003/09/25 11:02:41 1.5 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2009/11/16 04:57:10 1.13 @@ -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. */ import junit.framework.*; @@ -13,7 +14,7 @@ import java.io.*; public class LinkedBlockingQueueTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run (suite()); } public static Test suite() { @@ -28,14 +29,14 @@ public class LinkedBlockingQueueTest ext private LinkedBlockingQueue populatedQueue(int n) { LinkedBlockingQueue q = new LinkedBlockingQueue(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, or Integer.MAX_VALUE if * none given @@ -148,7 +149,18 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = new LinkedBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } + } + + /** + * add(null) throws NPE + */ + public void testAddNull() { + try { + LinkedBlockingQueue q = new LinkedBlockingQueue(1); + q.add(null); + shouldThrow(); + } catch (NullPointerException success) { } } /** @@ -172,7 +184,7 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); } catch (IllegalStateException success){ - } + } } /** @@ -186,6 +198,19 @@ public class LinkedBlockingQueueTest ext } catch (NullPointerException success) {} } + + /** + * addAll(this) throws IAE + */ + public void testAddAllSelf() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + q.addAll(q); + shouldThrow(); + } + catch (IllegalArgumentException success) {} + } + /** * addAll of a collection with null elements throws NPE */ @@ -253,9 +278,9 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); q.put(null); shouldThrow(); - } + } catch (NullPointerException success){ - } + } catch (InterruptedException ie) { unexpectedException(); } @@ -296,11 +321,11 @@ public class LinkedBlockingQueueTest ext threadShouldThrow(); } catch (InterruptedException ie){ threadAssertEquals(added, SIZE); - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -359,7 +384,7 @@ public class LinkedBlockingQueueTest ext } catch (InterruptedException success){} } }); - + try { t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -381,7 +406,7 @@ public class LinkedBlockingQueueTest ext } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -394,7 +419,7 @@ public class LinkedBlockingQueueTest ext try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success){ } } }); try { @@ -421,11 +446,11 @@ public class LinkedBlockingQueueTest ext q.take(); threadShouldThrow(); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -458,7 +483,7 @@ public class LinkedBlockingQueueTest ext assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -473,7 +498,7 @@ public class LinkedBlockingQueueTest ext assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -490,11 +515,11 @@ public class LinkedBlockingQueueTest ext } threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException success){ - } + } }}); t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); + try { + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); } @@ -516,7 +541,7 @@ public class LinkedBlockingQueueTest ext q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); threadShouldThrow(); - } catch (InterruptedException success) { } + } catch (InterruptedException success) { } } }); try { @@ -528,7 +553,7 @@ public class LinkedBlockingQueueTest ext } catch (Exception e){ unexpectedException(); } - } + } /** * peek returns next element, or null if empty @@ -572,7 +597,7 @@ public class LinkedBlockingQueueTest ext q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -589,7 +614,24 @@ public class LinkedBlockingQueueTest ext } assertTrue(q.isEmpty()); } - + + /** + * An add following remove(x) succeeds + */ + public void testRemoveElementAndAdd() { + try { + LinkedBlockingQueue q = new LinkedBlockingQueue(); + assertTrue(q.add(new Integer(1))); + assertTrue(q.add(new Integer(2))); + assertTrue(q.remove(new Integer(1))); + assertTrue(q.remove(new Integer(2))); + assertTrue(q.add(new Integer(3))); + assertTrue(q.take() != null); + } catch (Exception e){ + unexpectedException(); + } + } + /** * contains(x) reports true when elements added but not yet removed */ @@ -613,6 +655,7 @@ public class LinkedBlockingQueueTest ext assertEquals(SIZE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); + assertTrue(q.contains(one)); q.clear(); assertTrue(q.isEmpty()); } @@ -673,11 +716,11 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue 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(); - } + } } /** @@ -688,13 +731,36 @@ public class LinkedBlockingQueueTest ext 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(); - } + } } - + + /** + * toArray(null) throws NPE + */ + public void testToArray_BadArg() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(null); + shouldThrow(); + } catch (NullPointerException success){} + } + + /** + * toArray with incompatible array type throws CCE + */ + public void testToArray1_BadArg() { + try { + LinkedBlockingQueue q = populatedQueue(SIZE); + Object o[] = q.toArray(new String[10] ); + shouldThrow(); + } catch (ArrayStoreException success){} + } + + /** * iterator iterates through all elements */ @@ -702,12 +768,12 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = populatedQueue(SIZE); Iterator it = q.iterator(); try { - while(it.hasNext()){ + while (it.hasNext()){ assertEquals(it.next(), q.take()); } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -722,7 +788,7 @@ public class LinkedBlockingQueueTest ext Iterator it = q.iterator(); it.next(); it.remove(); - + it = q.iterator(); assertEquals(it.next(), one); assertEquals(it.next(), three); @@ -777,7 +843,7 @@ public class LinkedBlockingQueueTest ext for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -812,7 +878,7 @@ public class LinkedBlockingQueueTest ext } } }); - + joinPool(executor); } @@ -846,7 +912,7 @@ public class LinkedBlockingQueueTest ext } } }); - + joinPool(executor); } @@ -866,11 +932,130 @@ public class LinkedBlockingQueueTest ext ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); LinkedBlockingQueue r = (LinkedBlockingQueue)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() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(null); + shouldThrow(); + } catch (NullPointerException success) { + } + } + + /** + * drainTo(this) throws IAE + */ + public void testDrainToSelf() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(q); + shouldThrow(); + } catch (IllegalArgumentException success) { + } + } + + /** + * drainTo(c) empties queue into another collection c + */ + public void testDrainTo() { + LinkedBlockingQueue 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) + 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 LinkedBlockingQueue q = populatedQueue(SIZE); + Thread t = new Thread(new Runnable() { + public void run() { + try { + q.put(new Integer(SIZE+1)); + } catch (InterruptedException ie){ + threadUnexpectedException(); + } + } + }); + try { + t.start(); + ArrayList l = new ArrayList(); + q.drainTo(l); + assertTrue(l.size() >= SIZE); + for (int i = 0; i < SIZE; ++i) + assertEquals(l.get(i), new Integer(i)); + t.join(); + assertTrue(q.size() + l.size() >= SIZE); + } catch (Exception e){ + unexpectedException(); + } + } + + /** + * drainTo(null, n) throws NPE + */ + public void testDrainToNullN() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(null, 0); + shouldThrow(); + } catch (NullPointerException success) { + } + } + + /** + * drainTo(this, n) throws IAE + */ + public void testDrainToSelfN() { + LinkedBlockingQueue q = populatedQueue(SIZE); + try { + q.drainTo(q, 0); + shouldThrow(); + } catch (IllegalArgumentException success) { + } + } + + /** + * drainTo(c, n) empties first max {n, size} elements of queue into c + */ + public void testDrainToN() { + LinkedBlockingQueue q = new LinkedBlockingQueue(); + for (int i = 0; i < SIZE + 2; ++i) { + 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(l.size(), k); + assertEquals(q.size(), SIZE-k); + for (int j = 0; j < k; ++j) + assertEquals(l.get(j), new Integer(j)); + while (q.poll() != null) ; + } + } + }