diff -r fd16c54261b3 -r 90ce3da70b43 jdk/test/java/util/Deque/ChorusLine.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/util/Deque/ChorusLine.java Sat Dec 01 00:00:00 2007 +0000 @@ -0,0 +1,194 @@ +/* + * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test + * @bug 6324846 + * @summary Deque implementations must behave isomorphically + * @author Martin Buchholz + */ + +import java.util.*; +import java.util.concurrent.*; + +public class ChorusLine { + private interface Tweaker { + void run(Deque deq); + } + + private final static Tweaker[] tweakers = { + new Tweaker() { public void run(Deque deq) { + for (int i = 0; i < 7; i++) + deq.addLast(i); + deq.removeFirst(); + deq.removeFirst(); + deq.addLast(7); + deq.addLast(8); + Iterator it = deq.descendingIterator(); + equal(it.next(), 8); + it.remove(); + + try {it.remove();} + catch (IllegalStateException e) {pass();} + catch (Throwable t) {unexpected(t);} + + deq.addLast(9); + it = deq.descendingIterator(); + equal(it.next(), 9); + equal(it.next(), 7); + it.remove(); + + try {it.remove();} + catch (IllegalStateException e) {pass();} + catch (Throwable t) {unexpected(t);} + + equal(it.next(), 6); + + System.out.println(deq); + }}, + new Tweaker() { public void run(Deque deq) { + deq.clear(); + check(deq.isEmpty()); + check(deq.size() == 0); + check(! deq.iterator().hasNext()); + check(! deq.descendingIterator().hasNext()); + + try {deq.iterator().next(); fail();} + catch (NoSuchElementException e) {pass();} + catch (Throwable t) {unexpected(t);} + + try {deq.descendingIterator().next(); fail();} + catch (NoSuchElementException e) {pass();} + catch (Throwable t) {unexpected(t);} + }}, + new Tweaker() { public void run(Deque deq) { + for (int i = 0; i < 11; i++) + deq.add(i); + Iterator it = deq.iterator(); + equal(it.next(), 0); + equal(it.next(), 1); + it.remove(); + deq.addFirst(-1); + deq.addFirst(-2); + it = deq.iterator(); + equal(it.next(), -2); + equal(it.next(), -1); + equal(it.next(), 0); + it.remove(); + + it = deq.descendingIterator(); + + try {it.remove(); fail();} + catch (IllegalStateException e) {pass();} + catch (Throwable t) {unexpected(t);} + + equal(it.next(), 10); + it.remove(); + + try {it.remove(); fail();} + catch (IllegalStateException e) {pass();} + catch (Throwable t) {unexpected(t);} + + equal(it.next(), 9); + equal(it.next(), 8); + it.remove(); + System.out.println(deq); + }}, + new Tweaker() { public void run(Deque deq) { + while (deq.size() > 1) { + Iterator it = deq.iterator(); + it.next(); it.remove(); + it = deq.descendingIterator(); + it.next(); it.remove(); + } + System.out.println(deq); + }}}; + + private static void realMain(String[] args) throws Throwable { + Collection> deqs = new ArrayDeque>(3); + deqs.add(new ArrayDeque()); + deqs.add(new LinkedList()); + deqs.add(new LinkedBlockingDeque()); + + equal(deqs); + + for (Tweaker tweaker : tweakers) { + for (Deque deq : deqs) + tweaker.run(deq); + equal(deqs); + } + } + + private static void equal(Iterable> deqs) { + Deque prev = null; + for (Deque deq : deqs) { + if (prev != null) { + equal(prev.isEmpty(), deq.isEmpty()); + equal(prev.size(), deq.size()); + equal(prev.toString(), deq.toString()); + } + prev = deq; + } + + Deque> its = new ArrayDeque>(); + for (Deque deq : deqs) + its.addLast(deq.iterator()); + equal(its); + + Deque> dits = new ArrayDeque>(); + for (Deque deq : deqs) + dits.addLast(deq.descendingIterator()); + equal(dits); + } + + private static void equal(Deque> its) { + Iterator it0 = its.remove(); + while (it0.hasNext()) { + Integer i = it0.next(); + for (Iterator it : its) + equal(it.next(), i); + } + for (Iterator it : its) { + check(! it.hasNext()); + + try {it.next(); fail();} + catch (NoSuchElementException e) {pass();} + catch (Throwable t) {unexpected(t);} + } + } + + //--------------------- Infrastructure --------------------------- + static volatile int passed = 0, failed = 0; + static void pass() {passed++;} + static void fail() {failed++; Thread.dumpStack();} + static void fail(String msg) {System.out.println(msg); fail();} + static void unexpected(Throwable t) {failed++; t.printStackTrace();} + static void check(boolean cond) {if (cond) pass(); else fail();} + static void equal(Object x, Object y) { + if (x == null ? y == null : x.equals(y)) pass(); + else fail(x + " not equal to " + y);} + public static void main(String[] args) throws Throwable { + try {realMain(args);} catch (Throwable t) {unexpected(t);} + System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); + if (failed > 0) throw new AssertionError("Some tests failed");} +}