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