1/*
2 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6394004
27 * @summary Test ForgetMeNot implementation feature (and more)
28 * @author Martin Buchholz
29 */
30
31import java.util.Arrays;
32import java.util.Iterator;
33import java.util.NoSuchElementException;
34import java.util.PriorityQueue;
35import java.util.Queue;
36
37public class ForgetMeNot {
38    private static void checkQ(PriorityQueue<Integer> q, Integer...elts) {
39        check(Arrays.equals(q.toArray(), elts));
40    }
41
42    private static void noMoreElements(final Iterator<Integer> it) {
43        for (int j = 0; j < 2; j++) {
44            THROWS(NoSuchElementException.class, () -> it.next());
45            check(! it.hasNext());
46        }
47    }
48
49    private static void removeIsCurrentlyIllegal(final Iterator<Integer> it) {
50        for (int j = 0; j < 2; j++) {
51            THROWS(IllegalStateException.class, () -> it.remove());
52        }
53    }
54
55    private static void remove(Iterator<Integer> it,
56                               Queue<Integer> q) {
57        int size = q.size();
58        it.remove();
59        removeIsCurrentlyIllegal(it);
60        equal(size, q.size()+1);
61    }
62
63    private static void realMain(String[] args) throws Throwable {
64        final PriorityQueue<Integer> q = new PriorityQueue<>();
65        Iterator<Integer> it;
66
67        //----------------------------------------------------------------
68        // Empty
69        //----------------------------------------------------------------
70        checkQ(q);
71        check(q.isEmpty());
72        check(! q.contains(1));
73        it = q.iterator();
74        removeIsCurrentlyIllegal(it);
75        noMoreElements(it);
76        q.clear();
77        check(q.isEmpty());
78
79        //----------------------------------------------------------------
80        // Singleton
81        //----------------------------------------------------------------
82        q.add(1);
83        checkQ(q, 1);
84        check(! q.isEmpty());
85        check(q.contains(1));
86        it = q.iterator();
87        removeIsCurrentlyIllegal(it);
88        check(it.hasNext());
89        equal(it.next(), 1);
90        noMoreElements(it);
91        remove(it, q);
92        check(q.isEmpty());
93        noMoreElements(it);
94        checkQ(q);
95        q.clear();
96
97        //----------------------------------------------------------------
98        // @see PriorityQueue.forgetMeNot
99        //----------------------------------------------------------------
100        final Integer[] a = {0, 4, 1, 6, 7, 2, 3}; // Carefully chosen!
101        q.addAll(Arrays.asList(a));
102        checkQ(q, a);
103        it = q.iterator();
104        checkQ(q, a);
105        removeIsCurrentlyIllegal(it);
106        checkQ(q, a);
107        check(it.hasNext());
108        removeIsCurrentlyIllegal(it);
109        checkQ(q, a);
110        check(it.hasNext());
111        equal(it.next(), 0);
112        equal(it.next(), 4);
113        equal(it.next(), 1);
114        equal(it.next(), 6);
115        check(it.hasNext());
116        checkQ(q, a);
117        remove(it, q);
118        checkQ(q, 0, 3, 1, 4, 7, 2);
119        check(it.hasNext());
120        removeIsCurrentlyIllegal(it);
121        equal(it.next(), 7);
122        remove(it, q);
123        checkQ(q, 0, 2, 1, 4, 3);
124        check(it.hasNext());
125        removeIsCurrentlyIllegal(it);
126        check(it.hasNext());
127        equal(it.next(), 3);
128        equal(it.next(), 2);
129        check(! it.hasNext());
130        remove(it, q);
131        checkQ(q, 0, 3, 1, 4);
132        check(! it.hasNext());
133        noMoreElements(it);
134        removeIsCurrentlyIllegal(it);
135    }
136
137    //--------------------- Infrastructure ---------------------------
138    static volatile int passed = 0, failed = 0;
139    static void pass() {passed++;}
140    static void fail() {failed++; Thread.dumpStack();}
141    static void fail(String msg) {System.out.println(msg); fail();}
142    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
143    static void check(boolean cond) {if (cond) pass(); else fail();}
144    static void equal(Object x, Object y) {
145        if (x == null ? y == null : x.equals(y)) pass();
146        else fail(x + " not equal to " + y);}
147    public static void main(String[] args) throws Throwable {
148        try {realMain(args);} catch (Throwable t) {unexpected(t);}
149        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
150        if (failed > 0) throw new AssertionError("Some tests failed");}
151    interface Fun {void f() throws Throwable;}
152    static void THROWS(Class<? extends Throwable> k, Fun... fs) {
153        for (Fun f : fs)
154            try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
155            catch (Throwable t) {
156                if (k.isAssignableFrom(t.getClass())) pass();
157                else unexpected(t);}}
158}
159