1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
|
13 |
< |
public class PriorityQueueTest extends TestCase { |
14 |
< |
|
15 |
< |
private static final int N = 10; |
15 |
< |
private static final long SHORT_DELAY_MS = 100; |
16 |
< |
private static final long MEDIUM_DELAY_MS = 1000; |
17 |
< |
private static final long LONG_DELAY_MS = 10000; |
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Comparator; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.NoSuchElementException; |
14 |
> |
import java.util.PriorityQueue; |
15 |
> |
import java.util.Queue; |
16 |
|
|
17 |
+ |
import junit.framework.Test; |
18 |
+ |
|
19 |
+ |
public class PriorityQueueTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run (suite()); |
21 |
> |
main(suite(), args); |
22 |
|
} |
22 |
– |
|
23 |
|
public static Test suite() { |
24 |
< |
return new TestSuite(PriorityQueueTest.class); |
24 |
> |
class Implementation implements CollectionImplementation { |
25 |
> |
public Class<?> klazz() { return PriorityQueue.class; } |
26 |
> |
public Collection emptyCollection() { return new PriorityQueue(); } |
27 |
> |
public Object makeElement(int i) { return i; } |
28 |
> |
public boolean isConcurrent() { return false; } |
29 |
> |
public boolean permitsNulls() { return false; } |
30 |
> |
} |
31 |
> |
class ComparatorImplementation implements CollectionImplementation { |
32 |
> |
public Class<?> klazz() { return PriorityQueue.class; } |
33 |
> |
public Collection emptyCollection() { return new PriorityQueue(new MyReverseComparator()); } |
34 |
> |
public Object makeElement(int i) { return i; } |
35 |
> |
public boolean isConcurrent() { return false; } |
36 |
> |
public boolean permitsNulls() { return false; } |
37 |
> |
} |
38 |
> |
return newTestSuite(PriorityQueueTest.class, |
39 |
> |
CollectionTest.testSuite(new Implementation()), |
40 |
> |
CollectionTest.testSuite(new ComparatorImplementation())); |
41 |
|
} |
42 |
|
|
43 |
< |
static class MyReverseComparator implements Comparator { |
43 |
> |
static class MyReverseComparator implements Comparator, java.io.Serializable { |
44 |
|
public int compare(Object x, Object y) { |
45 |
< |
int i = ((Integer)x).intValue(); |
30 |
< |
int j = ((Integer)y).intValue(); |
31 |
< |
if (i < j) return 1; |
32 |
< |
if (i > j) return -1; |
33 |
< |
return 0; |
45 |
> |
return ((Comparable)y).compareTo(x); |
46 |
|
} |
47 |
|
} |
48 |
|
|
37 |
– |
|
49 |
|
/** |
50 |
< |
* Create a queue of given size containing consecutive |
51 |
< |
* Integers 0 ... n. |
50 |
> |
* Returns a new queue of given size containing consecutive |
51 |
> |
* Integers 0 ... n - 1. |
52 |
|
*/ |
53 |
< |
private PriorityQueue fullQueue(int n) { |
54 |
< |
PriorityQueue q = new PriorityQueue(n); |
53 |
> |
private static PriorityQueue<Integer> populatedQueue(int n) { |
54 |
> |
PriorityQueue<Integer> q = new PriorityQueue<>(n); |
55 |
|
assertTrue(q.isEmpty()); |
56 |
< |
for(int i = n-1; i >= 0; i-=2) |
57 |
< |
assertTrue(q.offer(new Integer(i))); |
58 |
< |
for(int i = (n & 1); i < n; i+=2) |
59 |
< |
assertTrue(q.offer(new Integer(i))); |
56 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
57 |
> |
assertTrue(q.offer(new Integer(i))); |
58 |
> |
for (int i = (n & 1); i < n; i += 2) |
59 |
> |
assertTrue(q.offer(new Integer(i))); |
60 |
|
assertFalse(q.isEmpty()); |
61 |
< |
assertEquals(n, q.size()); |
61 |
> |
assertEquals(n, q.size()); |
62 |
> |
assertEquals((Integer) 0, q.peek()); |
63 |
|
return q; |
64 |
|
} |
65 |
< |
|
66 |
< |
public void testConstructor1(){ |
67 |
< |
assertEquals(0, new PriorityQueue(N).size()); |
65 |
> |
|
66 |
> |
/** |
67 |
> |
* A new queue has unbounded capacity |
68 |
> |
*/ |
69 |
> |
public void testConstructor1() { |
70 |
> |
assertEquals(0, new PriorityQueue(SIZE).size()); |
71 |
|
} |
72 |
|
|
73 |
< |
public void testConstructor2(){ |
73 |
> |
/** |
74 |
> |
* Constructor throws IllegalArgumentException if capacity argument nonpositive |
75 |
> |
*/ |
76 |
> |
public void testConstructor2() { |
77 |
|
try { |
78 |
< |
PriorityQueue q = new PriorityQueue(0); |
79 |
< |
fail("Cannot make zero-sized"); |
80 |
< |
} |
63 |
< |
catch (IllegalArgumentException success) {} |
78 |
> |
new PriorityQueue(0); |
79 |
> |
shouldThrow(); |
80 |
> |
} catch (IllegalArgumentException success) {} |
81 |
|
} |
82 |
|
|
83 |
+ |
/** |
84 |
+ |
* Initializing from null Collection throws NPE |
85 |
+ |
*/ |
86 |
|
public void testConstructor3() { |
67 |
– |
|
87 |
|
try { |
88 |
< |
PriorityQueue q = new PriorityQueue((Collection)null); |
89 |
< |
fail("Cannot make from null collection"); |
90 |
< |
} |
72 |
< |
catch (NullPointerException success) {} |
88 |
> |
new PriorityQueue((Collection)null); |
89 |
> |
shouldThrow(); |
90 |
> |
} catch (NullPointerException success) {} |
91 |
|
} |
92 |
|
|
93 |
< |
public void testConstructor4(){ |
93 |
> |
/** |
94 |
> |
* Initializing from Collection of null elements throws NPE |
95 |
> |
*/ |
96 |
> |
public void testConstructor4() { |
97 |
|
try { |
98 |
< |
Integer[] ints = new Integer[N]; |
99 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
100 |
< |
fail("Cannot make with null elements"); |
80 |
< |
} |
81 |
< |
catch (NullPointerException success) {} |
98 |
> |
new PriorityQueue(Arrays.asList(new Integer[SIZE])); |
99 |
> |
shouldThrow(); |
100 |
> |
} catch (NullPointerException success) {} |
101 |
|
} |
102 |
|
|
103 |
< |
public void testConstructor5(){ |
104 |
< |
try { |
105 |
< |
Integer[] ints = new Integer[N]; |
106 |
< |
for (int i = 0; i < N-1; ++i) |
107 |
< |
ints[i] = new Integer(i); |
108 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
109 |
< |
fail("Cannot make with null elements"); |
110 |
< |
} |
111 |
< |
catch (NullPointerException success) {} |
103 |
> |
/** |
104 |
> |
* Initializing from Collection with some null elements throws NPE |
105 |
> |
*/ |
106 |
> |
public void testConstructor5() { |
107 |
> |
Integer[] ints = new Integer[SIZE]; |
108 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
109 |
> |
ints[i] = new Integer(i); |
110 |
> |
try { |
111 |
> |
new PriorityQueue(Arrays.asList(ints)); |
112 |
> |
shouldThrow(); |
113 |
> |
} catch (NullPointerException success) {} |
114 |
|
} |
115 |
|
|
116 |
< |
public void testConstructor6(){ |
117 |
< |
try { |
118 |
< |
Integer[] ints = new Integer[N]; |
119 |
< |
for (int i = 0; i < N; ++i) |
120 |
< |
ints[i] = new Integer(i); |
121 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
122 |
< |
for (int i = 0; i < N; ++i) |
123 |
< |
assertEquals(ints[i], q.poll()); |
124 |
< |
} |
125 |
< |
finally {} |
116 |
> |
/** |
117 |
> |
* Queue contains all elements of collection used to initialize |
118 |
> |
*/ |
119 |
> |
public void testConstructor6() { |
120 |
> |
Integer[] ints = new Integer[SIZE]; |
121 |
> |
for (int i = 0; i < SIZE; ++i) |
122 |
> |
ints[i] = new Integer(i); |
123 |
> |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
124 |
> |
for (int i = 0; i < SIZE; ++i) |
125 |
> |
assertEquals(ints[i], q.poll()); |
126 |
|
} |
127 |
|
|
128 |
< |
public void testConstructor7(){ |
129 |
< |
try { |
130 |
< |
PriorityQueue q = new PriorityQueue(N, new MyReverseComparator()); |
131 |
< |
Integer[] ints = new Integer[N]; |
132 |
< |
for (int i = 0; i < N; ++i) |
133 |
< |
ints[i] = new Integer(i); |
134 |
< |
q.addAll(Arrays.asList(ints)); |
135 |
< |
for (int i = N-1; i >= 0; --i) |
136 |
< |
assertEquals(ints[i], q.poll()); |
137 |
< |
} |
138 |
< |
finally {} |
128 |
> |
/** |
129 |
> |
* The comparator used in constructor is used |
130 |
> |
*/ |
131 |
> |
public void testConstructor7() { |
132 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
133 |
> |
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
134 |
> |
assertEquals(cmp, q.comparator()); |
135 |
> |
Integer[] ints = new Integer[SIZE]; |
136 |
> |
for (int i = 0; i < SIZE; ++i) |
137 |
> |
ints[i] = new Integer(i); |
138 |
> |
q.addAll(Arrays.asList(ints)); |
139 |
> |
for (int i = SIZE - 1; i >= 0; --i) |
140 |
> |
assertEquals(ints[i], q.poll()); |
141 |
|
} |
142 |
|
|
143 |
+ |
/** |
144 |
+ |
* isEmpty is true before add, false after |
145 |
+ |
*/ |
146 |
|
public void testEmpty() { |
147 |
|
PriorityQueue q = new PriorityQueue(2); |
148 |
|
assertTrue(q.isEmpty()); |
154 |
|
assertTrue(q.isEmpty()); |
155 |
|
} |
156 |
|
|
157 |
+ |
/** |
158 |
+ |
* size changes when elements added and removed |
159 |
+ |
*/ |
160 |
|
public void testSize() { |
161 |
< |
PriorityQueue q = fullQueue(N); |
162 |
< |
for (int i = 0; i < N; ++i) { |
163 |
< |
assertEquals(N-i, q.size()); |
161 |
> |
PriorityQueue q = populatedQueue(SIZE); |
162 |
> |
for (int i = 0; i < SIZE; ++i) { |
163 |
> |
assertEquals(SIZE - i, q.size()); |
164 |
|
q.remove(); |
165 |
|
} |
166 |
< |
for (int i = 0; i < N; ++i) { |
166 |
> |
for (int i = 0; i < SIZE; ++i) { |
167 |
|
assertEquals(i, q.size()); |
168 |
|
q.add(new Integer(i)); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
< |
public void testOfferNull(){ |
173 |
< |
try { |
174 |
< |
PriorityQueue q = new PriorityQueue(1); |
172 |
> |
/** |
173 |
> |
* offer(null) throws NPE |
174 |
> |
*/ |
175 |
> |
public void testOfferNull() { |
176 |
> |
PriorityQueue q = new PriorityQueue(1); |
177 |
> |
try { |
178 |
|
q.offer(null); |
179 |
< |
fail("should throw NPE"); |
180 |
< |
} catch (NullPointerException success) { } |
179 |
> |
shouldThrow(); |
180 |
> |
} catch (NullPointerException success) {} |
181 |
|
} |
182 |
|
|
183 |
+ |
/** |
184 |
+ |
* add(null) throws NPE |
185 |
+ |
*/ |
186 |
+ |
public void testAddNull() { |
187 |
+ |
PriorityQueue q = new PriorityQueue(1); |
188 |
+ |
try { |
189 |
+ |
q.add(null); |
190 |
+ |
shouldThrow(); |
191 |
+ |
} catch (NullPointerException success) {} |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
/** |
195 |
+ |
* Offer of comparable element succeeds |
196 |
+ |
*/ |
197 |
|
public void testOffer() { |
198 |
|
PriorityQueue q = new PriorityQueue(1); |
199 |
< |
assertTrue(q.offer(new Integer(0))); |
200 |
< |
assertTrue(q.offer(new Integer(1))); |
199 |
> |
assertTrue(q.offer(zero)); |
200 |
> |
assertTrue(q.offer(one)); |
201 |
|
} |
202 |
|
|
203 |
+ |
/** |
204 |
+ |
* Offer of non-Comparable throws CCE |
205 |
+ |
*/ |
206 |
|
public void testOfferNonComparable() { |
207 |
+ |
PriorityQueue q = new PriorityQueue(1); |
208 |
|
try { |
159 |
– |
PriorityQueue q = new PriorityQueue(1); |
160 |
– |
q.offer(new Object()); |
209 |
|
q.offer(new Object()); |
210 |
< |
q.offer(new Object()); |
211 |
< |
fail("should throw CCE"); |
210 |
> |
shouldThrow(); |
211 |
> |
} catch (ClassCastException success) { |
212 |
> |
assertTrue(q.isEmpty()); |
213 |
> |
assertEquals(0, q.size()); |
214 |
> |
assertNull(q.poll()); |
215 |
|
} |
165 |
– |
catch(ClassCastException success) {} |
216 |
|
} |
217 |
|
|
218 |
< |
public void testAdd(){ |
219 |
< |
PriorityQueue q = new PriorityQueue(N); |
220 |
< |
for (int i = 0; i < N; ++i) { |
218 |
> |
/** |
219 |
> |
* add of comparable succeeds |
220 |
> |
*/ |
221 |
> |
public void testAdd() { |
222 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
223 |
> |
for (int i = 0; i < SIZE; ++i) { |
224 |
|
assertEquals(i, q.size()); |
225 |
|
assertTrue(q.add(new Integer(i))); |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
< |
public void testAddAll1(){ |
229 |
> |
/** |
230 |
> |
* addAll(null) throws NPE |
231 |
> |
*/ |
232 |
> |
public void testAddAll1() { |
233 |
> |
PriorityQueue q = new PriorityQueue(1); |
234 |
|
try { |
178 |
– |
PriorityQueue q = new PriorityQueue(1); |
235 |
|
q.addAll(null); |
236 |
< |
fail("Cannot add null collection"); |
237 |
< |
} |
182 |
< |
catch (NullPointerException success) {} |
236 |
> |
shouldThrow(); |
237 |
> |
} catch (NullPointerException success) {} |
238 |
|
} |
239 |
< |
public void testAddAll2(){ |
239 |
> |
|
240 |
> |
/** |
241 |
> |
* addAll of a collection with null elements throws NPE |
242 |
> |
*/ |
243 |
> |
public void testAddAll2() { |
244 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
245 |
|
try { |
246 |
< |
PriorityQueue q = new PriorityQueue(N); |
247 |
< |
Integer[] ints = new Integer[N]; |
248 |
< |
q.addAll(Arrays.asList(ints)); |
189 |
< |
fail("Cannot add null elements"); |
190 |
< |
} |
191 |
< |
catch (NullPointerException success) {} |
246 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
247 |
> |
shouldThrow(); |
248 |
> |
} catch (NullPointerException success) {} |
249 |
|
} |
250 |
< |
public void testAddAll3(){ |
250 |
> |
|
251 |
> |
/** |
252 |
> |
* addAll of a collection with any null elements throws NPE after |
253 |
> |
* possibly adding some elements |
254 |
> |
*/ |
255 |
> |
public void testAddAll3() { |
256 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
257 |
> |
Integer[] ints = new Integer[SIZE]; |
258 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
259 |
> |
ints[i] = new Integer(i); |
260 |
|
try { |
195 |
– |
PriorityQueue q = new PriorityQueue(N); |
196 |
– |
Integer[] ints = new Integer[N]; |
197 |
– |
for (int i = 0; i < N-1; ++i) |
198 |
– |
ints[i] = new Integer(i); |
261 |
|
q.addAll(Arrays.asList(ints)); |
262 |
< |
fail("Cannot add null elements"); |
263 |
< |
} |
202 |
< |
catch (NullPointerException success) {} |
262 |
> |
shouldThrow(); |
263 |
> |
} catch (NullPointerException success) {} |
264 |
|
} |
265 |
|
|
266 |
< |
public void testAddAll5(){ |
267 |
< |
try { |
268 |
< |
Integer[] empty = new Integer[0]; |
269 |
< |
Integer[] ints = new Integer[N]; |
270 |
< |
for (int i = 0; i < N; ++i) |
271 |
< |
ints[i] = new Integer(N-1-i); |
272 |
< |
PriorityQueue q = new PriorityQueue(N); |
273 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
274 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
275 |
< |
for (int i = 0; i < N; ++i) |
276 |
< |
assertEquals(new Integer(i), q.poll()); |
277 |
< |
} |
278 |
< |
finally {} |
266 |
> |
/** |
267 |
> |
* Queue contains all elements of successful addAll |
268 |
> |
*/ |
269 |
> |
public void testAddAll5() { |
270 |
> |
Integer[] empty = new Integer[0]; |
271 |
> |
Integer[] ints = new Integer[SIZE]; |
272 |
> |
for (int i = 0; i < SIZE; ++i) |
273 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
274 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
275 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
276 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
277 |
> |
for (int i = 0; i < SIZE; ++i) |
278 |
> |
assertEquals(new Integer(i), q.poll()); |
279 |
|
} |
280 |
|
|
281 |
< |
public void testPoll(){ |
282 |
< |
PriorityQueue q = fullQueue(N); |
283 |
< |
for (int i = 0; i < N; ++i) { |
284 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
281 |
> |
/** |
282 |
> |
* poll succeeds unless empty |
283 |
> |
*/ |
284 |
> |
public void testPoll() { |
285 |
> |
PriorityQueue q = populatedQueue(SIZE); |
286 |
> |
for (int i = 0; i < SIZE; ++i) { |
287 |
> |
assertEquals(i, q.poll()); |
288 |
|
} |
289 |
< |
assertNull(q.poll()); |
289 |
> |
assertNull(q.poll()); |
290 |
|
} |
291 |
|
|
292 |
< |
public void testPeek(){ |
293 |
< |
PriorityQueue q = fullQueue(N); |
294 |
< |
for (int i = 0; i < N; ++i) { |
295 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
296 |
< |
q.poll(); |
292 |
> |
/** |
293 |
> |
* peek returns next element, or null if empty |
294 |
> |
*/ |
295 |
> |
public void testPeek() { |
296 |
> |
PriorityQueue q = populatedQueue(SIZE); |
297 |
> |
for (int i = 0; i < SIZE; ++i) { |
298 |
> |
assertEquals(i, q.peek()); |
299 |
> |
assertEquals(i, q.poll()); |
300 |
|
assertTrue(q.peek() == null || |
301 |
< |
i != ((Integer)q.peek()).intValue()); |
301 |
> |
!q.peek().equals(i)); |
302 |
|
} |
303 |
< |
assertNull(q.peek()); |
303 |
> |
assertNull(q.peek()); |
304 |
|
} |
305 |
|
|
306 |
< |
public void testElement(){ |
307 |
< |
PriorityQueue q = fullQueue(N); |
308 |
< |
for (int i = 0; i < N; ++i) { |
309 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
310 |
< |
q.poll(); |
306 |
> |
/** |
307 |
> |
* element returns next element, or throws NSEE if empty |
308 |
> |
*/ |
309 |
> |
public void testElement() { |
310 |
> |
PriorityQueue q = populatedQueue(SIZE); |
311 |
> |
for (int i = 0; i < SIZE; ++i) { |
312 |
> |
assertEquals(i, q.element()); |
313 |
> |
assertEquals(i, q.poll()); |
314 |
|
} |
315 |
|
try { |
316 |
|
q.element(); |
317 |
< |
fail("no such element"); |
318 |
< |
} |
249 |
< |
catch (NoSuchElementException success) {} |
317 |
> |
shouldThrow(); |
318 |
> |
} catch (NoSuchElementException success) {} |
319 |
|
} |
320 |
|
|
321 |
< |
public void testRemove(){ |
322 |
< |
PriorityQueue q = fullQueue(N); |
323 |
< |
for (int i = 0; i < N; ++i) { |
324 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
321 |
> |
/** |
322 |
> |
* remove removes next element, or throws NSEE if empty |
323 |
> |
*/ |
324 |
> |
public void testRemove() { |
325 |
> |
PriorityQueue q = populatedQueue(SIZE); |
326 |
> |
for (int i = 0; i < SIZE; ++i) { |
327 |
> |
assertEquals(i, q.remove()); |
328 |
|
} |
329 |
|
try { |
330 |
|
q.remove(); |
331 |
< |
fail("remove should throw"); |
332 |
< |
} catch (NoSuchElementException success){ |
333 |
< |
} |
334 |
< |
} |
335 |
< |
|
336 |
< |
public void testRemoveElement(){ |
337 |
< |
PriorityQueue q = fullQueue(N); |
338 |
< |
for (int i = 1; i < N; i+=2) { |
339 |
< |
assertTrue(q.remove(new Integer(i))); |
340 |
< |
} |
341 |
< |
for (int i = 0; i < N; i+=2) { |
342 |
< |
assertTrue(q.remove(new Integer(i))); |
343 |
< |
assertFalse(q.remove(new Integer(i+1))); |
344 |
< |
} |
345 |
< |
assert(q.isEmpty()); |
346 |
< |
} |
347 |
< |
|
348 |
< |
public void testContains(){ |
349 |
< |
PriorityQueue q = fullQueue(N); |
350 |
< |
for (int i = 0; i < N; ++i) { |
331 |
> |
shouldThrow(); |
332 |
> |
} catch (NoSuchElementException success) {} |
333 |
> |
} |
334 |
> |
|
335 |
> |
/** |
336 |
> |
* remove(x) removes x and returns true if present |
337 |
> |
*/ |
338 |
> |
public void testRemoveElement() { |
339 |
> |
PriorityQueue q = populatedQueue(SIZE); |
340 |
> |
for (int i = 1; i < SIZE; i += 2) { |
341 |
> |
assertTrue(q.contains(i)); |
342 |
> |
assertTrue(q.remove(i)); |
343 |
> |
assertFalse(q.contains(i)); |
344 |
> |
assertTrue(q.contains(i - 1)); |
345 |
> |
} |
346 |
> |
for (int i = 0; i < SIZE; i += 2) { |
347 |
> |
assertTrue(q.contains(i)); |
348 |
> |
assertTrue(q.remove(i)); |
349 |
> |
assertFalse(q.contains(i)); |
350 |
> |
assertFalse(q.remove(i + 1)); |
351 |
> |
assertFalse(q.contains(i + 1)); |
352 |
> |
} |
353 |
> |
assertTrue(q.isEmpty()); |
354 |
> |
} |
355 |
> |
|
356 |
> |
/** |
357 |
> |
* contains(x) reports true when elements added but not yet removed |
358 |
> |
*/ |
359 |
> |
public void testContains() { |
360 |
> |
PriorityQueue q = populatedQueue(SIZE); |
361 |
> |
for (int i = 0; i < SIZE; ++i) { |
362 |
|
assertTrue(q.contains(new Integer(i))); |
363 |
|
q.poll(); |
364 |
|
assertFalse(q.contains(new Integer(i))); |
365 |
|
} |
366 |
|
} |
367 |
|
|
368 |
< |
public void testClear(){ |
369 |
< |
PriorityQueue q = fullQueue(N); |
368 |
> |
/** |
369 |
> |
* clear removes all elements |
370 |
> |
*/ |
371 |
> |
public void testClear() { |
372 |
> |
PriorityQueue q = populatedQueue(SIZE); |
373 |
|
q.clear(); |
374 |
|
assertTrue(q.isEmpty()); |
375 |
|
assertEquals(0, q.size()); |
379 |
|
assertTrue(q.isEmpty()); |
380 |
|
} |
381 |
|
|
382 |
< |
public void testContainsAll(){ |
383 |
< |
PriorityQueue q = fullQueue(N); |
384 |
< |
PriorityQueue p = new PriorityQueue(N); |
385 |
< |
for (int i = 0; i < N; ++i) { |
382 |
> |
/** |
383 |
> |
* containsAll(c) is true when c contains a subset of elements |
384 |
> |
*/ |
385 |
> |
public void testContainsAll() { |
386 |
> |
PriorityQueue q = populatedQueue(SIZE); |
387 |
> |
PriorityQueue p = new PriorityQueue(SIZE); |
388 |
> |
for (int i = 0; i < SIZE; ++i) { |
389 |
|
assertTrue(q.containsAll(p)); |
390 |
|
assertFalse(p.containsAll(q)); |
391 |
|
p.add(new Integer(i)); |
393 |
|
assertTrue(p.containsAll(q)); |
394 |
|
} |
395 |
|
|
396 |
< |
public void testRetainAll(){ |
397 |
< |
PriorityQueue q = fullQueue(N); |
398 |
< |
PriorityQueue p = fullQueue(N); |
399 |
< |
for (int i = 0; i < N; ++i) { |
396 |
> |
/** |
397 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
398 |
> |
*/ |
399 |
> |
public void testRetainAll() { |
400 |
> |
PriorityQueue q = populatedQueue(SIZE); |
401 |
> |
PriorityQueue p = populatedQueue(SIZE); |
402 |
> |
for (int i = 0; i < SIZE; ++i) { |
403 |
|
boolean changed = q.retainAll(p); |
404 |
|
if (i == 0) |
405 |
|
assertFalse(changed); |
407 |
|
assertTrue(changed); |
408 |
|
|
409 |
|
assertTrue(q.containsAll(p)); |
410 |
< |
assertEquals(N-i, q.size()); |
410 |
> |
assertEquals(SIZE - i, q.size()); |
411 |
|
p.remove(); |
412 |
|
} |
413 |
|
} |
414 |
|
|
415 |
< |
public void testRemoveAll(){ |
416 |
< |
for (int i = 1; i < N; ++i) { |
417 |
< |
PriorityQueue q = fullQueue(N); |
418 |
< |
PriorityQueue p = fullQueue(i); |
415 |
> |
/** |
416 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
417 |
> |
*/ |
418 |
> |
public void testRemoveAll() { |
419 |
> |
for (int i = 1; i < SIZE; ++i) { |
420 |
> |
PriorityQueue q = populatedQueue(SIZE); |
421 |
> |
PriorityQueue p = populatedQueue(i); |
422 |
|
assertTrue(q.removeAll(p)); |
423 |
< |
assertEquals(N-i, q.size()); |
423 |
> |
assertEquals(SIZE - i, q.size()); |
424 |
|
for (int j = 0; j < i; ++j) { |
425 |
< |
Integer I = (Integer)(p.remove()); |
426 |
< |
assertFalse(q.contains(I)); |
425 |
> |
Integer x = (Integer)(p.remove()); |
426 |
> |
assertFalse(q.contains(x)); |
427 |
|
} |
428 |
|
} |
429 |
|
} |
430 |
|
|
431 |
< |
public void testToArray(){ |
432 |
< |
PriorityQueue q = fullQueue(N); |
433 |
< |
Object[] o = q.toArray(); |
431 |
> |
/** |
432 |
> |
* toArray contains all elements |
433 |
> |
*/ |
434 |
> |
public void testToArray() { |
435 |
> |
PriorityQueue q = populatedQueue(SIZE); |
436 |
> |
Object[] o = q.toArray(); |
437 |
|
Arrays.sort(o); |
438 |
< |
for(int i = 0; i < o.length; i++) |
439 |
< |
assertEquals(o[i], q.poll()); |
438 |
> |
for (int i = 0; i < o.length; i++) |
439 |
> |
assertSame(o[i], q.poll()); |
440 |
|
} |
441 |
|
|
442 |
< |
public void testToArray2(){ |
443 |
< |
PriorityQueue q = fullQueue(N); |
444 |
< |
Integer[] ints = new Integer[N]; |
445 |
< |
ints = (Integer[])q.toArray(ints); |
442 |
> |
/** |
443 |
> |
* toArray(a) contains all elements |
444 |
> |
*/ |
445 |
> |
public void testToArray2() { |
446 |
> |
PriorityQueue<Integer> q = populatedQueue(SIZE); |
447 |
> |
Integer[] ints = new Integer[SIZE]; |
448 |
> |
Integer[] array = q.toArray(ints); |
449 |
> |
assertSame(ints, array); |
450 |
|
Arrays.sort(ints); |
451 |
< |
for(int i = 0; i < ints.length; i++) |
452 |
< |
assertEquals(ints[i], q.poll()); |
451 |
> |
for (int i = 0; i < ints.length; i++) |
452 |
> |
assertSame(ints[i], q.poll()); |
453 |
|
} |
454 |
< |
|
455 |
< |
public void testIterator(){ |
456 |
< |
PriorityQueue q = fullQueue(N); |
457 |
< |
int i = 0; |
458 |
< |
Iterator it = q.iterator(); |
459 |
< |
while(it.hasNext()) { |
454 |
> |
|
455 |
> |
/** |
456 |
> |
* iterator iterates through all elements |
457 |
> |
*/ |
458 |
> |
public void testIterator() { |
459 |
> |
PriorityQueue q = populatedQueue(SIZE); |
460 |
> |
Iterator it = q.iterator(); |
461 |
> |
int i; |
462 |
> |
for (i = 0; it.hasNext(); i++) |
463 |
|
assertTrue(q.contains(it.next())); |
464 |
< |
++i; |
465 |
< |
} |
361 |
< |
assertEquals(i, N); |
464 |
> |
assertEquals(i, SIZE); |
465 |
> |
assertIteratorExhausted(it); |
466 |
|
} |
467 |
|
|
468 |
< |
public void testIteratorRemove () { |
468 |
> |
/** |
469 |
> |
* iterator of empty collection has no elements |
470 |
> |
*/ |
471 |
> |
public void testEmptyIterator() { |
472 |
> |
assertIteratorExhausted(new PriorityQueue().iterator()); |
473 |
> |
} |
474 |
|
|
475 |
+ |
/** |
476 |
+ |
* iterator.remove removes current element |
477 |
+ |
*/ |
478 |
+ |
public void testIteratorRemove() { |
479 |
|
final PriorityQueue q = new PriorityQueue(3); |
367 |
– |
|
480 |
|
q.add(new Integer(2)); |
481 |
|
q.add(new Integer(1)); |
482 |
|
q.add(new Integer(3)); |
491 |
|
assertFalse(it.hasNext()); |
492 |
|
} |
493 |
|
|
494 |
< |
|
495 |
< |
public void testToString(){ |
496 |
< |
PriorityQueue q = fullQueue(N); |
494 |
> |
/** |
495 |
> |
* toString contains toStrings of elements |
496 |
> |
*/ |
497 |
> |
public void testToString() { |
498 |
> |
PriorityQueue q = populatedQueue(SIZE); |
499 |
|
String s = q.toString(); |
500 |
< |
for (int i = 0; i < N; ++i) { |
501 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
500 |
> |
for (int i = 0; i < SIZE; ++i) { |
501 |
> |
assertTrue(s.contains(String.valueOf(i))); |
502 |
|
} |
503 |
< |
} |
503 |
> |
} |
504 |
|
|
505 |
+ |
/** |
506 |
+ |
* A deserialized/reserialized queue has same elements |
507 |
+ |
*/ |
508 |
+ |
public void testSerialization() throws Exception { |
509 |
+ |
Queue x = populatedQueue(SIZE); |
510 |
+ |
Queue y = serialClone(x); |
511 |
+ |
|
512 |
+ |
assertNotSame(x, y); |
513 |
+ |
assertEquals(x.size(), y.size()); |
514 |
+ |
while (!x.isEmpty()) { |
515 |
+ |
assertFalse(y.isEmpty()); |
516 |
+ |
assertEquals(x.remove(), y.remove()); |
517 |
+ |
} |
518 |
+ |
assertTrue(y.isEmpty()); |
519 |
+ |
} |
520 |
|
} |