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