--- jsr166/src/test/tck/LinkedBlockingQueueTest.java 2004/01/07 01:13:50 1.9 +++ jsr166/src/test/tck/LinkedBlockingQueueTest.java 2009/11/02 20:28:31 1.12 @@ -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. */ import junit.framework.*; @@ -14,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() { @@ -36,7 +36,7 @@ public class LinkedBlockingQueueTest ext assertEquals(n, q.size()); return q; } - + /** * A new queue has the indicated capacity, or Integer.MAX_VALUE if * none given @@ -149,7 +149,7 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = new LinkedBlockingQueue(1); q.offer(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -160,7 +160,7 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = new LinkedBlockingQueue(1); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) { } } /** @@ -184,7 +184,7 @@ public class LinkedBlockingQueueTest ext assertEquals(0, q.remainingCapacity()); q.add(new Integer(SIZE)); } catch (IllegalStateException success){ - } + } } /** @@ -278,9 +278,9 @@ public class LinkedBlockingQueueTest ext LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE); q.put(null); shouldThrow(); - } + } catch (NullPointerException success){ - } + } catch (InterruptedException ie) { unexpectedException(); } @@ -321,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(); } @@ -384,7 +384,7 @@ public class LinkedBlockingQueueTest ext } catch (InterruptedException success){} } }); - + try { t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -406,7 +406,7 @@ public class LinkedBlockingQueueTest ext } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -419,7 +419,7 @@ public class LinkedBlockingQueueTest ext try { q.take(); threadShouldThrow(); - } catch (InterruptedException success){ } + } catch (InterruptedException success){ } } }); try { @@ -446,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(); } @@ -483,7 +483,7 @@ public class LinkedBlockingQueueTest ext assertNull(q.poll(0, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -498,7 +498,7 @@ public class LinkedBlockingQueueTest ext assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -515,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(); } @@ -541,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 { @@ -553,7 +553,7 @@ public class LinkedBlockingQueueTest ext } catch (Exception e){ unexpectedException(); } - } + } /** * peek returns next element, or null if empty @@ -597,7 +597,7 @@ public class LinkedBlockingQueueTest ext q.remove(); shouldThrow(); } catch (NoSuchElementException success){ - } + } } /** @@ -614,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 */ @@ -638,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()); } @@ -702,7 +720,7 @@ public class LinkedBlockingQueueTest ext assertEquals(o[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -717,7 +735,7 @@ public class LinkedBlockingQueueTest ext assertEquals(ints[i], q.take()); } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -742,7 +760,7 @@ public class LinkedBlockingQueueTest ext } catch(ArrayStoreException success){} } - + /** * iterator iterates through all elements */ @@ -755,7 +773,7 @@ public class LinkedBlockingQueueTest ext } } catch (InterruptedException e){ unexpectedException(); - } + } } /** @@ -770,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); @@ -825,7 +843,7 @@ public class LinkedBlockingQueueTest ext for (int i = 0; i < SIZE; ++i) { assertTrue(s.indexOf(String.valueOf(i)) >= 0); } - } + } /** @@ -860,7 +878,7 @@ public class LinkedBlockingQueueTest ext } } }); - + joinPool(executor); } @@ -894,7 +912,7 @@ public class LinkedBlockingQueueTest ext } } }); - + joinPool(executor); } @@ -914,7 +932,7 @@ 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){ unexpectedException(); @@ -923,7 +941,7 @@ public class LinkedBlockingQueueTest ext /** * drainTo(null) throws NPE - */ + */ public void testDrainToNull() { LinkedBlockingQueue q = populatedQueue(SIZE); try { @@ -935,7 +953,7 @@ public class LinkedBlockingQueueTest ext /** * drainTo(this) throws IAE - */ + */ public void testDrainToSelf() { LinkedBlockingQueue q = populatedQueue(SIZE); try { @@ -947,27 +965,38 @@ public class LinkedBlockingQueueTest ext /** * 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) + 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){ + } catch (InterruptedException ie){ threadUnexpectedException(); } } @@ -977,7 +1006,7 @@ public class LinkedBlockingQueueTest ext 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); @@ -988,7 +1017,7 @@ public class LinkedBlockingQueueTest ext /** * drainTo(null, n) throws NPE - */ + */ public void testDrainToNullN() { LinkedBlockingQueue q = populatedQueue(SIZE); try { @@ -1000,7 +1029,7 @@ public class LinkedBlockingQueueTest ext /** * drainTo(this, n) throws IAE - */ + */ public void testDrainToSelfN() { LinkedBlockingQueue q = populatedQueue(SIZE); try { @@ -1012,17 +1041,20 @@ public class LinkedBlockingQueueTest ext /** * 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) { - LinkedBlockingQueue 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) ; } }