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