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