53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* Constructor throws IAE if capacity argument nonpositive |
56 |
> |
* Constructor throws IAE if capacity argument nonpositive |
57 |
|
*/ |
58 |
|
public void testConstructor2() { |
59 |
|
try { |
60 |
|
PriorityQueue q = new PriorityQueue(0); |
61 |
|
shouldThrow(); |
62 |
< |
} |
63 |
< |
catch (IllegalArgumentException success) {} |
62 |
> |
} catch (IllegalArgumentException success) {} |
63 |
|
} |
64 |
|
|
65 |
|
/** |
69 |
|
try { |
70 |
|
PriorityQueue q = new PriorityQueue((Collection)null); |
71 |
|
shouldThrow(); |
72 |
< |
} |
74 |
< |
catch (NullPointerException success) {} |
72 |
> |
} catch (NullPointerException success) {} |
73 |
|
} |
74 |
|
|
75 |
|
/** |
80 |
|
Integer[] ints = new Integer[SIZE]; |
81 |
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
82 |
|
shouldThrow(); |
83 |
< |
} |
86 |
< |
catch (NullPointerException success) {} |
83 |
> |
} catch (NullPointerException success) {} |
84 |
|
} |
85 |
|
|
86 |
|
/** |
93 |
|
ints[i] = new Integer(i); |
94 |
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
95 |
|
shouldThrow(); |
96 |
< |
} |
100 |
< |
catch (NullPointerException success) {} |
96 |
> |
} catch (NullPointerException success) {} |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* Queue contains all elements of collection used to initialize |
101 |
|
*/ |
102 |
|
public void testConstructor6() { |
103 |
< |
try { |
104 |
< |
Integer[] ints = new Integer[SIZE]; |
105 |
< |
for (int i = 0; i < SIZE; ++i) |
106 |
< |
ints[i] = new Integer(i); |
107 |
< |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
108 |
< |
for (int i = 0; i < SIZE; ++i) |
113 |
< |
assertEquals(ints[i], q.poll()); |
114 |
< |
} |
115 |
< |
finally {} |
103 |
> |
Integer[] ints = new Integer[SIZE]; |
104 |
> |
for (int i = 0; i < SIZE; ++i) |
105 |
> |
ints[i] = new Integer(i); |
106 |
> |
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
107 |
> |
for (int i = 0; i < SIZE; ++i) |
108 |
> |
assertEquals(ints[i], q.poll()); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
|
* The comparator used in constructor is used |
113 |
|
*/ |
114 |
|
public void testConstructor7() { |
115 |
< |
try { |
116 |
< |
MyReverseComparator cmp = new MyReverseComparator(); |
117 |
< |
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
118 |
< |
assertEquals(cmp, q.comparator()); |
119 |
< |
Integer[] ints = new Integer[SIZE]; |
120 |
< |
for (int i = 0; i < SIZE; ++i) |
121 |
< |
ints[i] = new Integer(i); |
122 |
< |
q.addAll(Arrays.asList(ints)); |
123 |
< |
for (int i = SIZE-1; i >= 0; --i) |
131 |
< |
assertEquals(ints[i], q.poll()); |
132 |
< |
} |
133 |
< |
finally {} |
115 |
> |
MyReverseComparator cmp = new MyReverseComparator(); |
116 |
> |
PriorityQueue q = new PriorityQueue(SIZE, cmp); |
117 |
> |
assertEquals(cmp, q.comparator()); |
118 |
> |
Integer[] ints = new Integer[SIZE]; |
119 |
> |
for (int i = 0; i < SIZE; ++i) |
120 |
> |
ints[i] = new Integer(i); |
121 |
> |
q.addAll(Arrays.asList(ints)); |
122 |
> |
for (int i = SIZE-1; i >= 0; --i) |
123 |
> |
assertEquals(ints[i], q.poll()); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
160 |
|
PriorityQueue q = new PriorityQueue(1); |
161 |
|
q.offer(null); |
162 |
|
shouldThrow(); |
163 |
< |
} catch (NullPointerException success) { } |
163 |
> |
} catch (NullPointerException success) {} |
164 |
|
} |
165 |
|
|
166 |
|
/** |
171 |
|
PriorityQueue q = new PriorityQueue(1); |
172 |
|
q.add(null); |
173 |
|
shouldThrow(); |
174 |
< |
} catch (NullPointerException success) { } |
174 |
> |
} catch (NullPointerException success) {} |
175 |
|
} |
176 |
|
|
177 |
|
/** |
193 |
|
q.offer(new Object()); |
194 |
|
q.offer(new Object()); |
195 |
|
shouldThrow(); |
196 |
< |
} |
207 |
< |
catch (ClassCastException success) {} |
196 |
> |
} catch (ClassCastException success) {} |
197 |
|
} |
198 |
|
|
199 |
|
/** |
215 |
|
PriorityQueue q = new PriorityQueue(1); |
216 |
|
q.addAll(null); |
217 |
|
shouldThrow(); |
218 |
< |
} |
230 |
< |
catch (NullPointerException success) {} |
218 |
> |
} catch (NullPointerException success) {} |
219 |
|
} |
220 |
|
/** |
221 |
|
* addAll of a collection with null elements throws NPE |
226 |
|
Integer[] ints = new Integer[SIZE]; |
227 |
|
q.addAll(Arrays.asList(ints)); |
228 |
|
shouldThrow(); |
229 |
< |
} |
242 |
< |
catch (NullPointerException success) {} |
229 |
> |
} catch (NullPointerException success) {} |
230 |
|
} |
231 |
|
/** |
232 |
|
* addAll of a collection with any null elements throws NPE after |
240 |
|
ints[i] = new Integer(i); |
241 |
|
q.addAll(Arrays.asList(ints)); |
242 |
|
shouldThrow(); |
243 |
< |
} |
257 |
< |
catch (NullPointerException success) {} |
243 |
> |
} catch (NullPointerException success) {} |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
|
* Queue contains all elements of successful addAll |
248 |
|
*/ |
249 |
|
public void testAddAll5() { |
250 |
< |
try { |
251 |
< |
Integer[] empty = new Integer[0]; |
252 |
< |
Integer[] ints = new Integer[SIZE]; |
253 |
< |
for (int i = 0; i < SIZE; ++i) |
254 |
< |
ints[i] = new Integer(SIZE-1-i); |
255 |
< |
PriorityQueue q = new PriorityQueue(SIZE); |
256 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
257 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
258 |
< |
for (int i = 0; i < SIZE; ++i) |
273 |
< |
assertEquals(new Integer(i), q.poll()); |
274 |
< |
} |
275 |
< |
finally {} |
250 |
> |
Integer[] empty = new Integer[0]; |
251 |
> |
Integer[] ints = new Integer[SIZE]; |
252 |
> |
for (int i = 0; i < SIZE; ++i) |
253 |
> |
ints[i] = new Integer(SIZE-1-i); |
254 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
255 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
256 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
257 |
> |
for (int i = 0; i < SIZE; ++i) |
258 |
> |
assertEquals(new Integer(i), q.poll()); |
259 |
|
} |
260 |
|
|
261 |
|
/** |
295 |
|
try { |
296 |
|
q.element(); |
297 |
|
shouldThrow(); |
298 |
< |
} |
316 |
< |
catch (NoSuchElementException success) {} |
298 |
> |
} catch (NoSuchElementException success) {} |
299 |
|
} |
300 |
|
|
301 |
|
/** |
309 |
|
try { |
310 |
|
q.remove(); |
311 |
|
shouldThrow(); |
312 |
< |
} catch (NoSuchElementException success) { |
331 |
< |
} |
312 |
> |
} catch (NoSuchElementException success) {} |
313 |
|
} |
314 |
|
|
315 |
|
/** |
473 |
|
/** |
474 |
|
* A deserialized serialized queue has same elements |
475 |
|
*/ |
476 |
< |
public void testSerialization() { |
476 |
> |
public void testSerialization() throws Exception { |
477 |
|
PriorityQueue q = populatedQueue(SIZE); |
478 |
< |
try { |
479 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
480 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
481 |
< |
out.writeObject(q); |
482 |
< |
out.close(); |
483 |
< |
|
484 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
485 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
486 |
< |
PriorityQueue r = (PriorityQueue)in.readObject(); |
487 |
< |
assertEquals(q.size(), r.size()); |
488 |
< |
while (!q.isEmpty()) |
508 |
< |
assertEquals(q.remove(), r.remove()); |
509 |
< |
} catch (Exception e) { |
510 |
< |
unexpectedException(); |
511 |
< |
} |
478 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
479 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
480 |
> |
out.writeObject(q); |
481 |
> |
out.close(); |
482 |
> |
|
483 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
484 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
485 |
> |
PriorityQueue r = (PriorityQueue)in.readObject(); |
486 |
> |
assertEquals(q.size(), r.size()); |
487 |
> |
while (!q.isEmpty()) |
488 |
> |
assertEquals(q.remove(), r.remove()); |
489 |
|
} |
490 |
|
} |