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