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