0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6394004
0N/A * @summary Test ForgetMeNot implementation feature (and more)
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.util.*;
0N/A
0N/Apublic class ForgetMeNot {
0N/A private static void checkQ(PriorityQueue<Integer> q, Integer...elts) {
0N/A check(Arrays.equals(q.toArray(), elts));
0N/A }
0N/A
0N/A private static void noMoreElements(final Iterator<Integer> it) {
0N/A for (int j = 0; j < 2; j++) {
0N/A THROWS(NoSuchElementException.class,
0N/A new Fun() { void f() { it.next(); }});
0N/A check(! it.hasNext());
0N/A }
0N/A }
0N/A
0N/A private static void removeIsCurrentlyIllegal(final Iterator<Integer> it) {
0N/A for (int j = 0; j < 2; j++) {
0N/A THROWS(IllegalStateException.class,
0N/A new Fun() { void f() { it.remove(); }});
0N/A }
0N/A }
0N/A
0N/A private static void remove(Iterator<Integer> it,
0N/A Queue<Integer> q) {
0N/A int size = q.size();
0N/A it.remove();
0N/A removeIsCurrentlyIllegal(it);
0N/A equal(size, q.size()+1);
0N/A }
0N/A
0N/A private static void realMain(String[] args) throws Throwable {
0N/A final PriorityQueue<Integer> q = new PriorityQueue<Integer>();
0N/A Iterator<Integer> it;
0N/A
0N/A //----------------------------------------------------------------
0N/A // Empty
0N/A //----------------------------------------------------------------
0N/A checkQ(q);
0N/A check(q.isEmpty());
0N/A check(! q.contains(1));
0N/A it = q.iterator();
0N/A removeIsCurrentlyIllegal(it);
0N/A noMoreElements(it);
0N/A q.clear();
0N/A check(q.isEmpty());
0N/A
0N/A //----------------------------------------------------------------
0N/A // Singleton
0N/A //----------------------------------------------------------------
0N/A q.add(1);
0N/A checkQ(q, 1);
0N/A check(! q.isEmpty());
0N/A check(q.contains(1));
0N/A it = q.iterator();
0N/A removeIsCurrentlyIllegal(it);
0N/A check(it.hasNext());
0N/A equal(it.next(), 1);
0N/A noMoreElements(it);
0N/A remove(it, q);
0N/A check(q.isEmpty());
0N/A noMoreElements(it);
0N/A checkQ(q);
0N/A q.clear();
0N/A
0N/A //----------------------------------------------------------------
0N/A // @see PriorityQueue.forgetMeNot
0N/A //----------------------------------------------------------------
0N/A final Integer[] a = {0, 4, 1, 6, 7, 2, 3}; // Carefully chosen!
0N/A q.addAll(Arrays.asList(a));
0N/A checkQ(q, a);
0N/A it = q.iterator();
0N/A checkQ(q, a);
0N/A removeIsCurrentlyIllegal(it);
0N/A checkQ(q, a);
0N/A check(it.hasNext());
0N/A removeIsCurrentlyIllegal(it);
0N/A checkQ(q, a);
0N/A check(it.hasNext());
0N/A equal(it.next(), 0);
0N/A equal(it.next(), 4);
0N/A equal(it.next(), 1);
0N/A equal(it.next(), 6);
0N/A check(it.hasNext());
0N/A checkQ(q, a);
0N/A remove(it, q);
0N/A checkQ(q, 0, 3, 1, 4, 7, 2);
0N/A check(it.hasNext());
0N/A removeIsCurrentlyIllegal(it);
0N/A equal(it.next(), 7);
0N/A remove(it, q);
0N/A checkQ(q, 0, 2, 1, 4, 3);
0N/A check(it.hasNext());
0N/A removeIsCurrentlyIllegal(it);
0N/A check(it.hasNext());
0N/A equal(it.next(), 3);
0N/A equal(it.next(), 2);
0N/A check(! it.hasNext());
0N/A remove(it, q);
0N/A checkQ(q, 0, 3, 1, 4);
0N/A check(! it.hasNext());
0N/A noMoreElements(it);
0N/A removeIsCurrentlyIllegal(it);
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static void pass() {passed++;}
0N/A static void fail() {failed++; Thread.dumpStack();}
0N/A static void fail(String msg) {System.out.println(msg); fail();}
0N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A static void check(boolean cond) {if (cond) pass(); else fail();}
0N/A static void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
0N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
0N/A private static abstract class Fun {abstract void f() throws Throwable;}
0N/A static void THROWS(Class<? extends Throwable> k, Fun... fs) {
0N/A for (Fun f : fs)
0N/A try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
0N/A catch (Throwable t) {
0N/A if (k.isAssignableFrom(t.getClass())) pass();
0N/A else unexpected(t);}}
0N/A}