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