--- jsr166/src/test/tck/ConcurrentLinkedDequeTest.java 2012/02/21 02:04:17 1.11 +++ jsr166/src/test/tck/ConcurrentLinkedDequeTest.java 2015/05/23 00:53:08 1.21 @@ -6,19 +6,22 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; import java.util.Arrays; import java.util.Collection; +import java.util.Deque; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; import java.util.Random; import java.util.concurrent.ConcurrentLinkedDeque; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentLinkedDequeTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -52,7 +55,7 @@ public class ConcurrentLinkedDequeTest e */ public void testConstructor3() { try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null); + new ConcurrentLinkedDeque((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -62,8 +65,7 @@ public class ConcurrentLinkedDequeTest e */ public void testConstructor4() { try { - Integer[] ints = new Integer[SIZE]; - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); + new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -72,11 +74,11 @@ public class ConcurrentLinkedDequeTest e * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); + new ConcurrentLinkedDeque(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -113,7 +115,7 @@ public class ConcurrentLinkedDequeTest e public void testSize() { ConcurrentLinkedDeque q = populatedDeque(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); q.remove(); } for (int i = 0; i < SIZE; ++i) { @@ -126,8 +128,8 @@ public class ConcurrentLinkedDequeTest e * push(null) throws NPE */ public void testPushNull() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); q.push(null); shouldThrow(); } catch (NullPointerException success) {} @@ -161,8 +163,8 @@ public class ConcurrentLinkedDequeTest e * offer(null) throws NPE */ public void testOfferNull() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); q.offer(null); shouldThrow(); } catch (NullPointerException success) {} @@ -172,8 +174,8 @@ public class ConcurrentLinkedDequeTest e * offerFirst(null) throws NPE */ public void testOfferFirstNull() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); q.offerFirst(null); shouldThrow(); } catch (NullPointerException success) {} @@ -183,8 +185,8 @@ public class ConcurrentLinkedDequeTest e * offerLast(null) throws NPE */ public void testOfferLastNull() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); q.offerLast(null); shouldThrow(); } catch (NullPointerException success) {} @@ -227,8 +229,8 @@ public class ConcurrentLinkedDequeTest e * add(null) throws NPE */ public void testAddNull() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -238,8 +240,8 @@ public class ConcurrentLinkedDequeTest e * addFirst(null) throws NPE */ public void testAddFirstNull() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); q.addFirst(null); shouldThrow(); } catch (NullPointerException success) {} @@ -249,8 +251,8 @@ public class ConcurrentLinkedDequeTest e * addLast(null) throws NPE */ public void testAddLastNull() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); q.addLast(null); shouldThrow(); } catch (NullPointerException success) {} @@ -293,8 +295,8 @@ public class ConcurrentLinkedDequeTest e * addAll(null) throws NPE */ public void testAddAll1() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -304,8 +306,8 @@ public class ConcurrentLinkedDequeTest e * addAll(this) throws IAE */ public void testAddAllSelf() { + ConcurrentLinkedDeque q = populatedDeque(SIZE); try { - ConcurrentLinkedDeque q = populatedDeque(SIZE); q.addAll(q); shouldThrow(); } catch (IllegalArgumentException success) {} @@ -315,10 +317,9 @@ public class ConcurrentLinkedDequeTest e * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); + q.addAll(Arrays.asList(new Integer[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -328,11 +329,11 @@ public class ConcurrentLinkedDequeTest e * possibly adding some elements */ public void testAddAll3() { + ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i); try { - ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -369,7 +370,7 @@ public class ConcurrentLinkedDequeTest e */ public void testPollLast() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = SIZE-1; i >= 0; --i) { + for (int i = SIZE - 1; i >= 0; --i) { assertEquals(i, q.pollLast()); } assertNull(q.pollLast()); @@ -434,13 +435,13 @@ public class ConcurrentLinkedDequeTest e */ public void testRemoveElement() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); assertTrue(q.contains(i-1)); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); @@ -469,7 +470,7 @@ public class ConcurrentLinkedDequeTest e */ public void testPeekLast() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = SIZE-1; i >= 0; --i) { + for (int i = SIZE - 1; i >= 0; --i) { assertEquals(i, q.peekLast()); assertEquals(i, q.pollLast()); assertTrue(q.peekLast() == null || @@ -498,7 +499,7 @@ public class ConcurrentLinkedDequeTest e */ public void testLastElement() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = SIZE-1; i >= 0; --i) { + for (int i = SIZE - 1; i >= 0; --i) { assertEquals(i, q.getLast()); assertEquals(i, q.pollLast()); } @@ -544,10 +545,10 @@ public class ConcurrentLinkedDequeTest e */ public void testRemoveFirstOccurrence() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeFirstOccurrence(new Integer(i))); assertFalse(q.removeFirstOccurrence(new Integer(i+1))); } @@ -559,10 +560,10 @@ public class ConcurrentLinkedDequeTest e */ public void testRemoveLastOccurrence() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2) { assertTrue(q.removeLastOccurrence(new Integer(i))); assertFalse(q.removeLastOccurrence(new Integer(i+1))); } @@ -623,7 +624,7 @@ public class ConcurrentLinkedDequeTest e assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -636,10 +637,10 @@ public class ConcurrentLinkedDequeTest e ConcurrentLinkedDeque q = populatedDeque(SIZE); ConcurrentLinkedDeque p = populatedDeque(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.remove()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.remove()); + assertFalse(q.contains(x)); } } } @@ -693,13 +694,21 @@ public class ConcurrentLinkedDequeTest e */ public void testIterator() { ConcurrentLinkedDeque q = populatedDeque(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + Deque c = new ConcurrentLinkedDeque(); + assertIteratorExhausted(c.iterator()); + assertIteratorExhausted(c.descendingIterator()); } /** @@ -850,7 +859,7 @@ public class ConcurrentLinkedDequeTest e Queue x = populatedDeque(SIZE); Queue y = serialClone(x); - assertTrue(x != y); + assertNotSame(x, y); assertEquals(x.size(), y.size()); assertEquals(x.toString(), y.toString()); assertTrue(Arrays.equals(x.toArray(), y.toArray())); @@ -861,4 +870,30 @@ public class ConcurrentLinkedDequeTest e assertTrue(y.isEmpty()); } + /** + * contains(null) always return false. + * remove(null) always throws NullPointerException. + */ + public void testNeverContainsNull() { + Deque[] qs = { + new ConcurrentLinkedDeque(), + populatedDeque(2), + }; + + for (Deque q : qs) { + assertFalse(q.contains(null)); + try { + assertFalse(q.remove(null)); + shouldThrow(); + } catch (NullPointerException success) {} + try { + assertFalse(q.removeFirstOccurrence(null)); + shouldThrow(); + } catch (NullPointerException success) {} + try { + assertFalse(q.removeLastOccurrence(null)); + shouldThrow(); + } catch (NullPointerException success) {} + } + } }