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