Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Deque/ChorusLine.java
47192 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 632484626* @summary Deque implementations must behave isomorphically27* @author Martin Buchholz28*/2930import java.util.*;31import java.util.concurrent.*;3233public class ChorusLine {34private interface Tweaker {35void run(Deque<Integer> deq);36}3738private final static Tweaker[] tweakers = {39new Tweaker() { public void run(Deque<Integer> deq) {40for (int i = 0; i < 7; i++)41deq.addLast(i);42deq.removeFirst();43deq.removeFirst();44deq.addLast(7);45deq.addLast(8);46Iterator<Integer> it = deq.descendingIterator();47equal(it.next(), 8);48it.remove();4950try {it.remove();}51catch (IllegalStateException e) {pass();}52catch (Throwable t) {unexpected(t);}5354deq.addLast(9);55it = deq.descendingIterator();56equal(it.next(), 9);57equal(it.next(), 7);58it.remove();5960try {it.remove();}61catch (IllegalStateException e) {pass();}62catch (Throwable t) {unexpected(t);}6364equal(it.next(), 6);6566System.out.println(deq);67}},68new Tweaker() { public void run(Deque<Integer> deq) {69deq.clear();70check(deq.isEmpty());71check(deq.size() == 0);72check(! deq.iterator().hasNext());73check(! deq.descendingIterator().hasNext());7475try {deq.iterator().next(); fail();}76catch (NoSuchElementException e) {pass();}77catch (Throwable t) {unexpected(t);}7879try {deq.descendingIterator().next(); fail();}80catch (NoSuchElementException e) {pass();}81catch (Throwable t) {unexpected(t);}82}},83new Tweaker() { public void run(Deque<Integer> deq) {84for (int i = 0; i < 11; i++)85deq.add(i);86Iterator<Integer> it = deq.iterator();87equal(it.next(), 0);88equal(it.next(), 1);89it.remove();90deq.addFirst(-1);91deq.addFirst(-2);92it = deq.iterator();93equal(it.next(), -2);94equal(it.next(), -1);95equal(it.next(), 0);96it.remove();9798it = deq.descendingIterator();99100try {it.remove(); fail();}101catch (IllegalStateException e) {pass();}102catch (Throwable t) {unexpected(t);}103104equal(it.next(), 10);105it.remove();106107try {it.remove(); fail();}108catch (IllegalStateException e) {pass();}109catch (Throwable t) {unexpected(t);}110111equal(it.next(), 9);112equal(it.next(), 8);113it.remove();114System.out.println(deq);115}},116new Tweaker() { public void run(Deque<Integer> deq) {117while (deq.size() > 1) {118Iterator<Integer> it = deq.iterator();119it.next(); it.remove();120it = deq.descendingIterator();121it.next(); it.remove();122}123System.out.println(deq);124}}};125126private static void realMain(String[] args) throws Throwable {127Collection<Deque<Integer>> deqs = new ArrayDeque<Deque<Integer>>(3);128deqs.add(new ArrayDeque<Integer>());129deqs.add(new LinkedList<Integer>());130deqs.add(new LinkedBlockingDeque<Integer>());131deqs.add(new ConcurrentLinkedDeque<Integer>());132133equal(deqs);134135for (Tweaker tweaker : tweakers) {136for (Deque<Integer> deq : deqs)137tweaker.run(deq);138equal(deqs);139}140}141142private static void equal(Iterable<Deque<Integer>> deqs) {143Deque<Integer> prev = null;144for (Deque<Integer> deq : deqs) {145if (prev != null) {146equal(prev.isEmpty(), deq.isEmpty());147equal(prev.size(), deq.size());148equal(prev.toString(), deq.toString());149}150prev = deq;151}152153Deque<Iterator<Integer>> its = new ArrayDeque<Iterator<Integer>>();154for (Deque<Integer> deq : deqs)155its.addLast(deq.iterator());156equal(its);157158Deque<Iterator<Integer>> dits = new ArrayDeque<Iterator<Integer>>();159for (Deque<Integer> deq : deqs)160dits.addLast(deq.descendingIterator());161equal(dits);162}163164private static void equal(Deque<Iterator<Integer>> its) {165Iterator<Integer> it0 = its.remove();166while (it0.hasNext()) {167Integer i = it0.next();168for (Iterator<Integer> it : its)169equal(it.next(), i);170}171for (Iterator<Integer> it : its) {172check(! it.hasNext());173174try {it.next(); fail();}175catch (NoSuchElementException e) {pass();}176catch (Throwable t) {unexpected(t);}177}178}179180//--------------------- Infrastructure ---------------------------181static volatile int passed = 0, failed = 0;182static void pass() {passed++;}183static void fail() {failed++; Thread.dumpStack();}184static void fail(String msg) {System.out.println(msg); fail();}185static void unexpected(Throwable t) {failed++; t.printStackTrace();}186static void check(boolean cond) {if (cond) pass(); else fail();}187static void equal(Object x, Object y) {188if (x == null ? y == null : x.equals(y)) pass();189else fail(x + " not equal to " + y);}190public static void main(String[] args) throws Throwable {191try {realMain(args);} catch (Throwable t) {unexpected(t);}192System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);193if (failed > 0) throw new AssertionError("Some tests failed");}194}195196197