--- jsr166/src/test/tck/ArrayDequeTest.java 2016/10/22 05:45:56 1.50 +++ jsr166/src/test/tck/ArrayDequeTest.java 2017/08/04 03:30:21 1.56 @@ -8,17 +8,14 @@ import java.util.ArrayDeque; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Deque; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; import java.util.Random; -import java.util.Spliterator; import java.util.concurrent.ThreadLocalRandom; import junit.framework.Test; -import junit.framework.TestSuite; public class ArrayDequeTest extends JSR166TestCase { public static void main(String[] args) { @@ -43,7 +40,7 @@ public class ArrayDequeTest extends JSR1 */ private static ArrayDeque populatedDeque(int n) { // Randomize various aspects of memory layout, including - // filled-to-capacity and wraparound. + // capacity slop and wraparound. final ArrayDeque q; ThreadLocalRandom rnd = ThreadLocalRandom.current(); switch (rnd.nextInt(6)) { @@ -573,14 +570,17 @@ public class ArrayDequeTest extends JSR1 * removeFirstOccurrence(x) removes x and returns true if present */ public void testRemoveFirstOccurrence() { - ArrayDeque q = populatedDeque(SIZE); + Deque q = populatedDeque(SIZE); assertFalse(q.removeFirstOccurrence(null)); for (int i = 1; i < SIZE; i += 2) { - assertTrue(q.removeFirstOccurrence(new Integer(i))); + assertTrue(q.removeFirstOccurrence(i)); + assertFalse(q.contains(i)); } for (int i = 0; i < SIZE; i += 2) { - assertTrue(q.removeFirstOccurrence(new Integer(i))); - assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); + assertTrue(q.removeFirstOccurrence(i)); + assertFalse(q.removeFirstOccurrence(i + 1)); + assertFalse(q.contains(i)); + assertFalse(q.contains(i + 1)); } assertTrue(q.isEmpty()); assertFalse(q.removeFirstOccurrence(null)); @@ -594,14 +594,17 @@ public class ArrayDequeTest extends JSR1 * removeLastOccurrence(x) removes x and returns true if present */ public void testRemoveLastOccurrence() { - ArrayDeque q = populatedDeque(SIZE); + Deque q = populatedDeque(SIZE); assertFalse(q.removeLastOccurrence(null)); for (int i = 1; i < SIZE; i += 2) { - assertTrue(q.removeLastOccurrence(new Integer(i))); + assertTrue(q.removeLastOccurrence(i)); + assertFalse(q.contains(i)); } for (int i = 0; i < SIZE; i += 2) { - assertTrue(q.removeLastOccurrence(new Integer(i))); - assertFalse(q.removeLastOccurrence(new Integer(i + 1))); + assertTrue(q.removeLastOccurrence(i)); + assertFalse(q.removeLastOccurrence(i + 1)); + assertFalse(q.contains(i)); + assertFalse(q.contains(i + 1)); } assertTrue(q.isEmpty()); assertFalse(q.removeLastOccurrence(null)); @@ -681,89 +684,57 @@ public class ArrayDequeTest extends JSR1 } } - void checkToArray(ArrayDeque q) { + void checkToArray(ArrayDeque q) { int size = q.size(); - Object[] o = q.toArray(); - assertEquals(size, o.length); + Object[] a1 = q.toArray(); + assertEquals(size, a1.length); + Integer[] a2 = q.toArray(new Integer[0]); + assertEquals(size, a2.length); + Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]); + assertEquals(size, a3.length); + Integer[] a4 = new Integer[size]; + assertSame(a4, q.toArray(a4)); + Integer[] a5 = new Integer[size + 1]; + Arrays.fill(a5, 42); + assertSame(a5, q.toArray(a5)); + Integer[] a6 = new Integer[size + 2]; + Arrays.fill(a6, 42); + assertSame(a6, q.toArray(a6)); + Object[][] as = { a1, a2, a3, a4, a5, a6 }; + for (Object[] a : as) { + if (a.length > size) assertNull(a[size]); + if (a.length > size + 1) assertEquals(42, a[size + 1]); + } Iterator it = q.iterator(); + Integer s = q.peekFirst(); for (int i = 0; i < size; i++) { Integer x = (Integer) it.next(); - assertEquals((Integer)o[0] + i, (int) x); - assertSame(o[i], x); + assertEquals(s + i, (int) x); + for (Object[] a : as) + assertSame(a1[i], x); } } /** - * toArray() contains all elements in FIFO order + * toArray() and toArray(a) contain all elements in FIFO order */ public void testToArray() { - ArrayDeque q = new ArrayDeque(); - for (int i = 0; i < SIZE; i++) { + final int size = ThreadLocalRandom.current().nextInt(10); + ArrayDeque q = new ArrayDeque<>(size); + for (int i = 0; i < size; i++) { checkToArray(q); q.addLast(i); } // Provoke wraparound - for (int i = 0; i < SIZE; i++) { + int added = size * 2; + for (int i = 0; i < added; i++) { checkToArray(q); - assertEquals(i, q.poll()); - q.addLast(SIZE + i); + assertEquals((Integer) i, q.poll()); + q.addLast(size + i); } - for (int i = 0; i < SIZE; i++) { - checkToArray(q); - assertEquals(SIZE + i, q.poll()); - } - } - - void checkToArray2(ArrayDeque q) { - int size = q.size(); - Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; - Integer[] a2 = new Integer[size]; - Integer[] a3 = new Integer[size + 2]; - if (size > 0) Arrays.fill(a1, 42); - Arrays.fill(a2, 42); - Arrays.fill(a3, 42); - Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); - Integer[] b2 = (Integer[]) q.toArray(a2); - Integer[] b3 = (Integer[]) q.toArray(a3); - assertSame(a2, b2); - assertSame(a3, b3); - Iterator it = q.iterator(); for (int i = 0; i < size; i++) { - Integer x = (Integer) it.next(); - assertSame(b1[i], x); - assertEquals(b1[0] + i, (int) x); - assertSame(b2[i], x); - assertSame(b3[i], x); - } - assertNull(a3[size]); - assertEquals(42, (int) a3[size + 1]); - if (size > 0) { - assertNotSame(a1, b1); - assertEquals(size, b1.length); - for (int i = 0; i < a1.length; i++) { - assertEquals(42, (int) a1[i]); - } - } - } - - /** - * toArray(a) contains all elements in FIFO order - */ - public void testToArray2() { - ArrayDeque q = new ArrayDeque(); - for (int i = 0; i < SIZE; i++) { - checkToArray2(q); - q.addLast(i); - } - // Provoke wraparound - for (int i = 0; i < SIZE; i++) { - checkToArray2(q); - assertEquals(i, q.poll()); - q.addLast(SIZE + i); - } - for (int i = 0; i < SIZE; i++) { - checkToArray2(q); - assertEquals(SIZE + i, q.poll()); + checkToArray(q); + assertEquals((Integer) (added + i), q.poll()); } } @@ -782,13 +753,17 @@ public class ArrayDequeTest extends JSR1 /** * toArray(incompatible array type) throws ArrayStoreException */ - public void testToArray1_BadArg() { + public void testToArray_incompatibleArrayType() { ArrayDeque l = new ArrayDeque(); l.add(new Integer(5)); try { l.toArray(new String[10]); shouldThrow(); } catch (ArrayStoreException success) {} + try { + l.toArray(new String[0]); + shouldThrow(); + } catch (ArrayStoreException success) {} } /** @@ -937,7 +912,7 @@ public class ArrayDequeTest extends JSR1 } /** - * A deserialized serialized deque has same elements in same order + * A deserialized/reserialized deque has same elements in same order */ public void testSerialization() throws Exception { Queue x = populatedDeque(SIZE); @@ -946,6 +921,7 @@ public class ArrayDequeTest extends JSR1 assertNotSame(y, x); assertEquals(x.size(), y.size()); assertEquals(x.toString(), y.toString()); + assertEquals(Arrays.toString(x.toArray()), Arrays.toString(y.toArray())); assertTrue(Arrays.equals(x.toArray(), y.toArray())); while (!x.isEmpty()) { assertFalse(y.isEmpty()); @@ -989,79 +965,4 @@ public class ArrayDequeTest extends JSR1 } } - /** - * Spliterator characteristics are as advertised - */ - public void testSpliterator_characteristics() { - ArrayDeque q = new ArrayDeque(); - Spliterator s = q.spliterator(); - int characteristics = s.characteristics(); - int required = Spliterator.NONNULL - | Spliterator.ORDERED - | Spliterator.SIZED - | Spliterator.SUBSIZED; - assertEquals(required, characteristics & required); - assertTrue(s.hasCharacteristics(required)); - assertEquals(0, characteristics - & (Spliterator.CONCURRENT - | Spliterator.DISTINCT - | Spliterator.IMMUTABLE - | Spliterator.SORTED)); - } - - /** - * Spliterator.getComparator always throws IllegalStateException - */ - public void testSpliterator_getComparator() { - assertThrows(IllegalStateException.class, - () -> new ArrayDeque().spliterator().getComparator()); - } - - /** - * Handle capacities near Integer.MAX_VALUE. - * ant -Dvmoptions='-Xms28g -Xmx28g' -Djsr166.testImplementationDetails=true -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ArrayDequeTest -Djsr166.methodFilter=testHuge tck - */ - public void testHuge() { - if (! (testImplementationDetails - && expensiveTests - && Runtime.getRuntime().maxMemory() > 24L * (1 << 30))) - return; - - ArrayDeque q; - Integer e = 42; - final int maxSize = Integer.MAX_VALUE - 8; - - assertThrows(OutOfMemoryError.class, - () -> new ArrayDeque<>(Integer.MAX_VALUE)); - - { - q = new ArrayDeque<>(maxSize); - assertEquals(0, q.size()); - assertTrue(q.isEmpty()); - q = null; - } - - { - q = new ArrayDeque(); - assertTrue(q.addAll(Collections.nCopies(maxSize - 2, e))); - assertEquals(e, q.peekFirst()); - assertEquals(e, q.peekLast()); - assertEquals(maxSize - 2, q.size()); - q.addFirst((Integer) 0); - q.addLast((Integer) 1); - assertEquals((Integer) 0, q.peekFirst()); - assertEquals((Integer) 1, q.peekLast()); - assertEquals(maxSize, q.size()); - - ArrayDeque qq = q; - ArrayDeque smallish = new ArrayDeque( - Collections.nCopies(Integer.MAX_VALUE - maxSize + 1, e)); - assertThrows( - IllegalStateException.class, - () -> qq.addAll(qq), - () -> qq.addAll(smallish), - () -> smallish.addAll(qq)); - } - } - }