Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PriorityQueue/ForgetMeNot.java
38811 views
/*1* Copyright (c) 2006, 2014, 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 639400426* @summary Test ForgetMeNot implementation feature (and more)27* @author Martin Buchholz28*/2930import java.util.*;3132public class ForgetMeNot {33private static void checkQ(PriorityQueue<Integer> q, Integer...elts) {34check(Arrays.equals(q.toArray(), elts));35}3637private static void noMoreElements(final Iterator<Integer> it) {38for (int j = 0; j < 2; j++) {39THROWS(NoSuchElementException.class, () -> it.next());40check(! it.hasNext());41}42}4344private static void removeIsCurrentlyIllegal(final Iterator<Integer> it) {45for (int j = 0; j < 2; j++) {46THROWS(IllegalStateException.class, () -> it.remove());47}48}4950private static void remove(Iterator<Integer> it,51Queue<Integer> q) {52int size = q.size();53it.remove();54removeIsCurrentlyIllegal(it);55equal(size, q.size()+1);56}5758private static void realMain(String[] args) throws Throwable {59final PriorityQueue<Integer> q = new PriorityQueue<Integer>();60Iterator<Integer> it;6162//----------------------------------------------------------------63// Empty64//----------------------------------------------------------------65checkQ(q);66check(q.isEmpty());67check(! q.contains(1));68it = q.iterator();69removeIsCurrentlyIllegal(it);70noMoreElements(it);71q.clear();72check(q.isEmpty());7374//----------------------------------------------------------------75// Singleton76//----------------------------------------------------------------77q.add(1);78checkQ(q, 1);79check(! q.isEmpty());80check(q.contains(1));81it = q.iterator();82removeIsCurrentlyIllegal(it);83check(it.hasNext());84equal(it.next(), 1);85noMoreElements(it);86remove(it, q);87check(q.isEmpty());88noMoreElements(it);89checkQ(q);90q.clear();9192//----------------------------------------------------------------93// @see PriorityQueue.forgetMeNot94//----------------------------------------------------------------95final Integer[] a = {0, 4, 1, 6, 7, 2, 3}; // Carefully chosen!96q.addAll(Arrays.asList(a));97checkQ(q, a);98it = q.iterator();99checkQ(q, a);100removeIsCurrentlyIllegal(it);101checkQ(q, a);102check(it.hasNext());103removeIsCurrentlyIllegal(it);104checkQ(q, a);105check(it.hasNext());106equal(it.next(), 0);107equal(it.next(), 4);108equal(it.next(), 1);109equal(it.next(), 6);110check(it.hasNext());111checkQ(q, a);112remove(it, q);113checkQ(q, 0, 3, 1, 4, 7, 2);114check(it.hasNext());115removeIsCurrentlyIllegal(it);116equal(it.next(), 7);117remove(it, q);118checkQ(q, 0, 2, 1, 4, 3);119check(it.hasNext());120removeIsCurrentlyIllegal(it);121check(it.hasNext());122equal(it.next(), 3);123equal(it.next(), 2);124check(! it.hasNext());125remove(it, q);126checkQ(q, 0, 3, 1, 4);127check(! it.hasNext());128noMoreElements(it);129removeIsCurrentlyIllegal(it);130}131132//--------------------- Infrastructure ---------------------------133static volatile int passed = 0, failed = 0;134static void pass() {passed++;}135static void fail() {failed++; Thread.dumpStack();}136static void fail(String msg) {System.out.println(msg); fail();}137static void unexpected(Throwable t) {failed++; t.printStackTrace();}138static void check(boolean cond) {if (cond) pass(); else fail();}139static void equal(Object x, Object y) {140if (x == null ? y == null : x.equals(y)) pass();141else fail(x + " not equal to " + y);}142public static void main(String[] args) throws Throwable {143try {realMain(args);} catch (Throwable t) {unexpected(t);}144System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);145if (failed > 0) throw new AssertionError("Some tests failed");}146interface Fun {void f() throws Throwable;}147static void THROWS(Class<? extends Throwable> k, Fun... fs) {148for (Fun f : fs)149try { f.f(); fail("Expected " + k.getName() + " not thrown"); }150catch (Throwable t) {151if (k.isAssignableFrom(t.getClass())) pass();152else unexpected(t);}}153}154155156