--- jsr166/src/test/tck/LinkedBlockingDequeTest.java 2009/11/21 09:28:16 1.10 +++ jsr166/src/test/tck/LinkedBlockingDequeTest.java 2009/12/01 09:56:28 1.20 @@ -98,7 +98,7 @@ public class LinkedBlockingDequeTest ext public void testPollFirst() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst()).intValue()); + assertEquals(i, q.pollFirst()); } assertNull(q.pollFirst()); } @@ -109,7 +109,7 @@ public class LinkedBlockingDequeTest ext public void testPollLast() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = SIZE-1; i >= 0; --i) { - assertEquals(i, ((Integer)q.pollLast()).intValue()); + assertEquals(i, q.pollLast()); } assertNull(q.pollLast()); } @@ -120,10 +120,10 @@ public class LinkedBlockingDequeTest ext public void testPeekFirst() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.peekFirst()).intValue()); - q.pollFirst(); + assertEquals(i, q.peekFirst()); + assertEquals(i, q.pollFirst()); assertTrue(q.peekFirst() == null || - i != ((Integer)q.peekFirst()).intValue()); + !q.peekFirst().equals(i)); } assertNull(q.peekFirst()); } @@ -134,10 +134,10 @@ public class LinkedBlockingDequeTest ext public void testPeek() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.peek()).intValue()); - q.pollFirst(); + assertEquals(i, q.peek()); + assertEquals(i, q.pollFirst()); assertTrue(q.peek() == null || - i != ((Integer)q.peek()).intValue()); + !q.peek().equals(i)); } assertNull(q.peek()); } @@ -148,10 +148,10 @@ public class LinkedBlockingDequeTest ext public void testPeekLast() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = SIZE-1; i >= 0; --i) { - assertEquals(i, ((Integer)q.peekLast()).intValue()); - q.pollLast(); + assertEquals(i, q.peekLast()); + assertEquals(i, q.pollLast()); assertTrue(q.peekLast() == null || - i != ((Integer)q.peekLast()).intValue()); + !q.peekLast().equals(i)); } assertNull(q.peekLast()); } @@ -162,13 +162,14 @@ public class LinkedBlockingDequeTest ext public void testFirstElement() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.getFirst()).intValue()); - q.pollFirst(); + assertEquals(i, q.getFirst()); + assertEquals(i, q.pollFirst()); } try { q.getFirst(); shouldThrow(); } catch (NoSuchElementException success) {} + assertNull(q.peekFirst()); } /** @@ -177,8 +178,8 @@ public class LinkedBlockingDequeTest ext public void testLastElement() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = SIZE-1; i >= 0; --i) { - assertEquals(i, ((Integer)q.getLast()).intValue()); - q.pollLast(); + assertEquals(i, q.getLast()); + assertEquals(i, q.pollLast()); } try { q.getLast(); @@ -193,12 +194,28 @@ public class LinkedBlockingDequeTest ext public void testRemoveFirst() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.removeFirst()).intValue()); + assertEquals(i, q.removeFirst()); } try { q.removeFirst(); shouldThrow(); } catch (NoSuchElementException success) {} + assertNull(q.peekFirst()); + } + + /** + * removeLast removes last element, or throws NSEE if empty + */ + public void testRemoveLast() { + LinkedBlockingDeque q = populatedDeque(SIZE); + for (int i = SIZE - 1; i >= 0; --i) { + assertEquals(i, q.removeLast()); + } + try { + q.removeLast(); + shouldThrow(); + } catch (NoSuchElementException success) {} + assertNull(q.peekLast()); } /** @@ -207,7 +224,7 @@ public class LinkedBlockingDequeTest ext public void testRemove() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.remove()).intValue()); + assertEquals(i, q.remove()); } try { q.remove(); @@ -252,7 +269,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); q.addFirst(four); - assertEquals(four,q.peekFirst()); + assertSame(four, q.peekFirst()); } /** @@ -262,7 +279,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); q.addLast(four); - assertEquals(four,q.peekLast()); + assertSame(four, q.peekLast()); } @@ -276,7 +293,7 @@ public class LinkedBlockingDequeTest ext } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IAE if capacity argument nonpositive */ public void testConstructor2() { try { @@ -323,15 +340,12 @@ public class LinkedBlockingDequeTest ext * Deque contains all elements of collection used to initialize */ public void testConstructor6() { - try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints)); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } /** @@ -423,7 +437,7 @@ public class LinkedBlockingDequeTest ext LinkedBlockingDeque q = populatedDeque(3); q.pollLast(); q.push(four); - assertEquals(four,q.peekFirst()); + assertSame(four, q.peekFirst()); } @@ -433,7 +447,7 @@ public class LinkedBlockingDequeTest ext public void testPop() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pop()).intValue()); + assertEquals(i, q.pop()); } try { q.pop(); @@ -526,22 +540,20 @@ public class LinkedBlockingDequeTest ext shouldThrow(); } catch (IllegalStateException success) {} } + /** * Deque contains all elements, in traversal order, of successful addAll */ public void testAddAll5() { - try { - Integer[] empty = new Integer[0]; - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.poll()); - } - finally {} + Integer[] empty = new Integer[0]; + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(ints))); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.poll()); } @@ -573,56 +585,51 @@ public class LinkedBlockingDequeTest ext * put blocks interruptibly if full */ public void testBlockingPut() throws InterruptedException { + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.put(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.put(new Integer(i)); - ++added; - } - q.put(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertEquals(added, SIZE); - } + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} }}); t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * put blocks waiting for take when full */ public void testPutWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); Thread t = new Thread(new CheckedRunnable() { - public void realRun() { - int added = 0; + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.put(i); try { - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - q.put(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertTrue(added >= 2); - } + q.put(99); + shouldThrow(); + } catch (InterruptedException success) {} }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -630,13 +637,16 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOffer() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.put(new Object()); q.put(new Object()); - threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -650,7 +660,7 @@ public class LinkedBlockingDequeTest ext public void testTake() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } } @@ -674,14 +684,17 @@ public class LinkedBlockingDequeTest ext * Take removes existing elements until empty, then blocks interruptibly */ public void testBlockingTake() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final LinkedBlockingDeque q = populatedDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.take()).intValue()); + assertEquals(i, q.take()); } - q.take(); - }}; + try { + q.take(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -696,7 +709,7 @@ public class LinkedBlockingDequeTest ext public void testPoll() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll()).intValue()); + assertEquals(i, q.poll()); } assertNull(q.poll()); } @@ -707,7 +720,7 @@ public class LinkedBlockingDequeTest ext public void testTimedPoll0() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(0, MILLISECONDS)); } assertNull(q.poll(0, MILLISECONDS)); } @@ -718,7 +731,7 @@ public class LinkedBlockingDequeTest ext public void testTimedPoll() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); } assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); } @@ -728,14 +741,17 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPoll() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); } - q.poll(SMALL_DELAY_MS, MILLISECONDS); - }}; + try { + q.poll(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -749,12 +765,15 @@ public class LinkedBlockingDequeTest ext */ public void testTimedPollWithOffer() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); - q.poll(LONG_DELAY_MS, MILLISECONDS); - q.poll(LONG_DELAY_MS, MILLISECONDS); - }}; + assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS)); + try { + q.poll(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -792,57 +811,51 @@ public class LinkedBlockingDequeTest ext * putFirst blocks interruptibly if full */ public void testBlockingPutFirst() throws InterruptedException { - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.putFirst(new Integer(i)); - ++added; - } - q.putFirst(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertEquals(added, SIZE); - } - }}); + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.putFirst(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); + try { + q.putFirst(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * putFirst blocks waiting for take when full */ public void testPutFirstWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - q.putFirst(new Object()); - ++added; - q.putFirst(new Object()); - ++added; - q.putFirst(new Object()); - ++added; - q.putFirst(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertTrue(added >= 2); - } - } - }); + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.putFirst(i); + try { + q.putFirst(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(capacity - 1, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -850,13 +863,16 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOfferFirst() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.putFirst(new Object()); q.putFirst(new Object()); - threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -870,7 +886,7 @@ public class LinkedBlockingDequeTest ext public void testTakeFirst() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.takeFirst()).intValue()); + assertEquals(i, q.takeFirst()); } } @@ -894,14 +910,16 @@ public class LinkedBlockingDequeTest ext * TakeFirst removes existing elements until empty, then blocks interruptibly */ public void testBlockingTakeFirst() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final LinkedBlockingDeque q = populatedDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.takeFirst()).intValue()); - } - q.takeFirst(); - }}; + for (int i = 0; i < SIZE; ++i) + assertEquals(i, q.takeFirst()); + try { + q.takeFirst(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -916,7 +934,7 @@ public class LinkedBlockingDequeTest ext public void testTimedPollFirst0() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue()); + assertEquals(i, q.pollFirst(0, MILLISECONDS)); } assertNull(q.pollFirst(0, MILLISECONDS)); } @@ -927,7 +945,7 @@ public class LinkedBlockingDequeTest ext public void testTimedPollFirst() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); } assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); } @@ -937,14 +955,17 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPollFirst() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); } - q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); - }}; + try { + q.pollFirst(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -958,12 +979,15 @@ public class LinkedBlockingDequeTest ext */ public void testTimedPollFirstWithOfferFirst() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); - q.pollFirst(LONG_DELAY_MS, MILLISECONDS); - q.pollFirst(LONG_DELAY_MS, MILLISECONDS); - }}; + assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)); + assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS)); + try { + q.pollFirst(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -1000,56 +1024,51 @@ public class LinkedBlockingDequeTest ext * putLast blocks interruptibly if full */ public void testBlockingPutLast() throws InterruptedException { - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - q.putLast(new Integer(i)); - ++added; - } - q.putLast(new Integer(SIZE)); - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertEquals(added, SIZE); - } - }}); + final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < SIZE; ++i) + q.putLast(i); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); + try { + q.putLast(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); + t.start(); Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(SIZE, q.size()); + assertEquals(0, q.remainingCapacity()); } /** * putLast blocks waiting for take when full */ public void testPutLastWithTake() throws InterruptedException { - final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new Thread(new Runnable() { - public void run() { - int added = 0; - try { - q.putLast(new Object()); - ++added; - q.putLast(new Object()); - ++added; - q.putLast(new Object()); - ++added; - q.putLast(new Object()); - ++added; - threadShouldThrow(); - } catch (InterruptedException success) { - threadAssertTrue(added >= 2); - } - } - }); + final int capacity = 2; + final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity); + Thread t = new Thread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + for (int i = 0; i < capacity + 1; i++) + q.putLast(i); + try { + q.putLast(99); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); - q.take(); + assertEquals(q.remainingCapacity(), 0); + assertEquals(0, q.take()); + Thread.sleep(SHORT_DELAY_MS); t.interrupt(); t.join(); + assertEquals(q.remainingCapacity(), 0); } /** @@ -1057,13 +1076,16 @@ public class LinkedBlockingDequeTest ext */ public void testTimedOfferLast() throws InterruptedException { final LinkedBlockingDeque q = new LinkedBlockingDeque(2); - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { q.putLast(new Object()); q.putLast(new Object()); - threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); - q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); - }}; + assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS)); + try { + q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SMALL_DELAY_MS); @@ -1077,7 +1099,7 @@ public class LinkedBlockingDequeTest ext public void testTakeLast() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); + assertEquals(SIZE-i-1, q.takeLast()); } } @@ -1101,14 +1123,16 @@ public class LinkedBlockingDequeTest ext * TakeLast removes existing elements until empty, then blocks interruptibly */ public void testBlockingTakeLast() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + final LinkedBlockingDeque q = populatedDeque(SIZE); + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { - LinkedBlockingDeque q = populatedDeque(SIZE); - for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue()); - } - q.takeLast(); - }}; + for (int i = 0; i < SIZE; ++i) + assertEquals(SIZE - 1 - i, q.takeLast()); + try { + q.takeLast(); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -1116,14 +1140,13 @@ public class LinkedBlockingDequeTest ext t.join(); } - /** * timed pollLast with zero timeout succeeds when non-empty, else times out */ public void testTimedPollLast0() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue()); + assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS)); } assertNull(q.pollLast(0, MILLISECONDS)); } @@ -1134,7 +1157,7 @@ public class LinkedBlockingDequeTest ext public void testTimedPollLast() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); } assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); } @@ -1144,14 +1167,17 @@ public class LinkedBlockingDequeTest ext * returning timeout status */ public void testInterruptedTimedPollLast() throws InterruptedException { - Thread t = new ThreadShouldThrow(InterruptedException.class) { + Thread t = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue()); + assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS)); } - q.pollLast(SMALL_DELAY_MS, MILLISECONDS); - }}; + try { + q.pollLast(SMALL_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + }}); t.start(); Thread.sleep(SHORT_DELAY_MS); @@ -1189,7 +1215,7 @@ public class LinkedBlockingDequeTest ext public void testElement() { LinkedBlockingDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.element()).intValue()); + assertEquals(i, q.element()); q.poll(); } try { @@ -1315,8 +1341,8 @@ public class LinkedBlockingDequeTest ext * toArray(null) throws NPE */ public void testToArray_BadArg() { + LinkedBlockingDeque q = populatedDeque(SIZE); try { - LinkedBlockingDeque q = populatedDeque(SIZE); Object o[] = q.toArray(null); shouldThrow(); } catch (NullPointerException success) {} @@ -1326,9 +1352,9 @@ public class LinkedBlockingDequeTest ext * toArray with incompatible array type throws CCE */ public void testToArray1_BadArg() { + LinkedBlockingDeque q = populatedDeque(SIZE); try { - LinkedBlockingDeque q = populatedDeque(SIZE); - Object o[] = q.toArray(new String[10] ); + Object o[] = q.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} } @@ -1359,8 +1385,8 @@ public class LinkedBlockingDequeTest ext it.remove(); it = q.iterator(); - assertEquals(it.next(), one); - assertEquals(it.next(), three); + assertSame(it.next(), one); + assertSame(it.next(), three); assertFalse(it.hasNext()); } @@ -1376,8 +1402,7 @@ public class LinkedBlockingDequeTest ext assertEquals(0, q.remainingCapacity()); int k = 0; for (Iterator it = q.iterator(); it.hasNext();) { - int i = ((Integer)(it.next())).intValue(); - assertEquals(++k, i); + assertEquals(++k, it.next()); } assertEquals(3, k); } @@ -1428,8 +1453,7 @@ public class LinkedBlockingDequeTest ext q.add(new Integer(1)); int k = 0; for (Iterator it = q.descendingIterator(); it.hasNext();) { - int i = ((Integer)(it.next())).intValue(); - assertEquals(++k, i); + assertEquals(++k, it.next()); } assertEquals(3, k); @@ -1484,15 +1508,15 @@ public class LinkedBlockingDequeTest ext ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertFalse(q.offer(three)); - threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertEquals(0, q.remainingCapacity()); + assertFalse(q.offer(three)); + assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS)); + assertEquals(0, q.remainingCapacity()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); - threadAssertEquals(one, q.take()); + assertSame(one, q.take()); }}); joinPool(executor); @@ -1506,9 +1530,9 @@ public class LinkedBlockingDequeTest ext ExecutorService executor = Executors.newFixedThreadPool(2); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { - threadAssertNull(q.poll()); - threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); - threadAssertTrue(q.isEmpty()); + assertNull(q.poll()); + assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); + assertTrue(q.isEmpty()); }}); executor.execute(new CheckedRunnable() {