Path: blob/master/test/jdk/java/util/PriorityQueue/ForgetMeNot.java
41149 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.Arrays;31import java.util.Iterator;32import java.util.NoSuchElementException;33import java.util.PriorityQueue;34import java.util.Queue;3536public class ForgetMeNot {37private static void checkQ(PriorityQueue<Integer> q, Integer...elts) {38check(Arrays.equals(q.toArray(), elts));39}4041private static void noMoreElements(final Iterator<Integer> it) {42for (int j = 0; j < 2; j++) {43THROWS(NoSuchElementException.class, () -> it.next());44check(! it.hasNext());45}46}4748private static void removeIsCurrentlyIllegal(final Iterator<Integer> it) {49for (int j = 0; j < 2; j++) {50THROWS(IllegalStateException.class, () -> it.remove());51}52}5354private static void remove(Iterator<Integer> it,55Queue<Integer> q) {56int size = q.size();57it.remove();58removeIsCurrentlyIllegal(it);59equal(size, q.size()+1);60}6162private static void realMain(String[] args) throws Throwable {63final PriorityQueue<Integer> q = new PriorityQueue<>();64Iterator<Integer> it;6566//----------------------------------------------------------------67// Empty68//----------------------------------------------------------------69checkQ(q);70check(q.isEmpty());71check(! q.contains(1));72it = q.iterator();73removeIsCurrentlyIllegal(it);74noMoreElements(it);75q.clear();76check(q.isEmpty());7778//----------------------------------------------------------------79// Singleton80//----------------------------------------------------------------81q.add(1);82checkQ(q, 1);83check(! q.isEmpty());84check(q.contains(1));85it = q.iterator();86removeIsCurrentlyIllegal(it);87check(it.hasNext());88equal(it.next(), 1);89noMoreElements(it);90remove(it, q);91check(q.isEmpty());92noMoreElements(it);93checkQ(q);94q.clear();9596//----------------------------------------------------------------97// @see PriorityQueue.forgetMeNot98//----------------------------------------------------------------99final Integer[] a = {0, 4, 1, 6, 7, 2, 3}; // Carefully chosen!100q.addAll(Arrays.asList(a));101checkQ(q, a);102it = q.iterator();103checkQ(q, a);104removeIsCurrentlyIllegal(it);105checkQ(q, a);106check(it.hasNext());107removeIsCurrentlyIllegal(it);108checkQ(q, a);109check(it.hasNext());110equal(it.next(), 0);111equal(it.next(), 4);112equal(it.next(), 1);113equal(it.next(), 6);114check(it.hasNext());115checkQ(q, a);116remove(it, q);117checkQ(q, 0, 3, 1, 4, 7, 2);118check(it.hasNext());119removeIsCurrentlyIllegal(it);120equal(it.next(), 7);121remove(it, q);122checkQ(q, 0, 2, 1, 4, 3);123check(it.hasNext());124removeIsCurrentlyIllegal(it);125check(it.hasNext());126equal(it.next(), 3);127equal(it.next(), 2);128check(! it.hasNext());129remove(it, q);130checkQ(q, 0, 3, 1, 4);131check(! it.hasNext());132noMoreElements(it);133removeIsCurrentlyIllegal(it);134}135136//--------------------- Infrastructure ---------------------------137static volatile int passed = 0, failed = 0;138static void pass() {passed++;}139static void fail() {failed++; Thread.dumpStack();}140static void fail(String msg) {System.out.println(msg); fail();}141static void unexpected(Throwable t) {failed++; t.printStackTrace();}142static void check(boolean cond) {if (cond) pass(); else fail();}143static void equal(Object x, Object y) {144if (x == null ? y == null : x.equals(y)) pass();145else fail(x + " not equal to " + y);}146public static void main(String[] args) throws Throwable {147try {realMain(args);} catch (Throwable t) {unexpected(t);}148System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);149if (failed > 0) throw new AssertionError("Some tests failed");}150interface Fun {void f() throws Throwable;}151static void THROWS(Class<? extends Throwable> k, Fun... fs) {152for (Fun f : fs)153try { f.f(); fail("Expected " + k.getName() + " not thrown"); }154catch (Throwable t) {155if (k.isAssignableFrom(t.getClass())) pass();156else unexpected(t);}}157}158159160