10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
public class PriorityQueueTest extends TestCase { |
14 |
< |
|
15 |
< |
private static final int N = 10; |
16 |
< |
private static final long SHORT_DELAY_MS = 100; |
17 |
< |
private static final long MEDIUM_DELAY_MS = 1000; |
18 |
< |
private static final long LONG_DELAY_MS = 10000; |
13 |
> |
public class PriorityQueueTest extends JSR166TestCase { |
14 |
|
|
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
35 |
|
* Create a queue of given size containing consecutive |
36 |
|
* Integers 0 ... n. |
37 |
|
*/ |
38 |
< |
private PriorityQueue fullQueue(int n) { |
38 |
> |
private PriorityQueue populatedQueue(int n) { |
39 |
|
PriorityQueue q = new PriorityQueue(n); |
40 |
|
assertTrue(q.isEmpty()); |
41 |
|
for(int i = n-1; i >= 0; i-=2) |
48 |
|
} |
49 |
|
|
50 |
|
public void testConstructor1(){ |
51 |
< |
assertEquals(0, new PriorityQueue(N).size()); |
51 |
> |
assertEquals(0, new PriorityQueue(SIZE).size()); |
52 |
|
} |
53 |
|
|
54 |
|
public void testConstructor2(){ |
70 |
|
|
71 |
|
public void testConstructor4(){ |
72 |
|
try { |
73 |
< |
Integer[] ints = new Integer[N]; |
73 |
> |
Integer[] ints = new Integer[SIZE]; |
74 |
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
75 |
|
fail("Cannot make with null elements"); |
76 |
|
} |
79 |
|
|
80 |
|
public void testConstructor5(){ |
81 |
|
try { |
82 |
< |
Integer[] ints = new Integer[N]; |
83 |
< |
for (int i = 0; i < N-1; ++i) |
82 |
> |
Integer[] ints = new Integer[SIZE]; |
83 |
> |
for (int i = 0; i < SIZE-1; ++i) |
84 |
|
ints[i] = new Integer(i); |
85 |
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
86 |
|
fail("Cannot make with null elements"); |
90 |
|
|
91 |
|
public void testConstructor6(){ |
92 |
|
try { |
93 |
< |
Integer[] ints = new Integer[N]; |
94 |
< |
for (int i = 0; i < N; ++i) |
93 |
> |
Integer[] ints = new Integer[SIZE]; |
94 |
> |
for (int i = 0; i < SIZE; ++i) |
95 |
|
ints[i] = new Integer(i); |
96 |
|
PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); |
97 |
< |
for (int i = 0; i < N; ++i) |
97 |
> |
for (int i = 0; i < SIZE; ++i) |
98 |
|
assertEquals(ints[i], q.poll()); |
99 |
|
} |
100 |
|
finally {} |
102 |
|
|
103 |
|
public void testConstructor7(){ |
104 |
|
try { |
105 |
< |
PriorityQueue q = new PriorityQueue(N, new MyReverseComparator()); |
106 |
< |
Integer[] ints = new Integer[N]; |
107 |
< |
for (int i = 0; i < N; ++i) |
105 |
> |
PriorityQueue q = new PriorityQueue(SIZE, new MyReverseComparator()); |
106 |
> |
Integer[] ints = new Integer[SIZE]; |
107 |
> |
for (int i = 0; i < SIZE; ++i) |
108 |
|
ints[i] = new Integer(i); |
109 |
|
q.addAll(Arrays.asList(ints)); |
110 |
< |
for (int i = N-1; i >= 0; --i) |
110 |
> |
for (int i = SIZE-1; i >= 0; --i) |
111 |
|
assertEquals(ints[i], q.poll()); |
112 |
|
} |
113 |
|
finally {} |
125 |
|
} |
126 |
|
|
127 |
|
public void testSize() { |
128 |
< |
PriorityQueue q = fullQueue(N); |
129 |
< |
for (int i = 0; i < N; ++i) { |
130 |
< |
assertEquals(N-i, q.size()); |
128 |
> |
PriorityQueue q = populatedQueue(SIZE); |
129 |
> |
for (int i = 0; i < SIZE; ++i) { |
130 |
> |
assertEquals(SIZE-i, q.size()); |
131 |
|
q.remove(); |
132 |
|
} |
133 |
< |
for (int i = 0; i < N; ++i) { |
133 |
> |
for (int i = 0; i < SIZE; ++i) { |
134 |
|
assertEquals(i, q.size()); |
135 |
|
q.add(new Integer(i)); |
136 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
public void testAdd(){ |
165 |
< |
PriorityQueue q = new PriorityQueue(N); |
166 |
< |
for (int i = 0; i < N; ++i) { |
165 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
166 |
> |
for (int i = 0; i < SIZE; ++i) { |
167 |
|
assertEquals(i, q.size()); |
168 |
|
assertTrue(q.add(new Integer(i))); |
169 |
|
} |
179 |
|
} |
180 |
|
public void testAddAll2(){ |
181 |
|
try { |
182 |
< |
PriorityQueue q = new PriorityQueue(N); |
183 |
< |
Integer[] ints = new Integer[N]; |
182 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
183 |
> |
Integer[] ints = new Integer[SIZE]; |
184 |
|
q.addAll(Arrays.asList(ints)); |
185 |
|
fail("Cannot add null elements"); |
186 |
|
} |
188 |
|
} |
189 |
|
public void testAddAll3(){ |
190 |
|
try { |
191 |
< |
PriorityQueue q = new PriorityQueue(N); |
192 |
< |
Integer[] ints = new Integer[N]; |
193 |
< |
for (int i = 0; i < N-1; ++i) |
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); |
195 |
|
q.addAll(Arrays.asList(ints)); |
196 |
|
fail("Cannot add null elements"); |
201 |
|
public void testAddAll5(){ |
202 |
|
try { |
203 |
|
Integer[] empty = new Integer[0]; |
204 |
< |
Integer[] ints = new Integer[N]; |
205 |
< |
for (int i = 0; i < N; ++i) |
206 |
< |
ints[i] = new Integer(N-1-i); |
207 |
< |
PriorityQueue q = new PriorityQueue(N); |
204 |
> |
Integer[] ints = new Integer[SIZE]; |
205 |
> |
for (int i = 0; i < SIZE; ++i) |
206 |
> |
ints[i] = new Integer(SIZE-1-i); |
207 |
> |
PriorityQueue q = new PriorityQueue(SIZE); |
208 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
209 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
210 |
< |
for (int i = 0; i < N; ++i) |
210 |
> |
for (int i = 0; i < SIZE; ++i) |
211 |
|
assertEquals(new Integer(i), q.poll()); |
212 |
|
} |
213 |
|
finally {} |
214 |
|
} |
215 |
|
|
216 |
|
public void testPoll(){ |
217 |
< |
PriorityQueue q = fullQueue(N); |
218 |
< |
for (int i = 0; i < N; ++i) { |
217 |
> |
PriorityQueue q = populatedQueue(SIZE); |
218 |
> |
for (int i = 0; i < SIZE; ++i) { |
219 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
220 |
|
} |
221 |
|
assertNull(q.poll()); |
222 |
|
} |
223 |
|
|
224 |
|
public void testPeek(){ |
225 |
< |
PriorityQueue q = fullQueue(N); |
226 |
< |
for (int i = 0; i < N; ++i) { |
225 |
> |
PriorityQueue q = populatedQueue(SIZE); |
226 |
> |
for (int i = 0; i < SIZE; ++i) { |
227 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
228 |
|
q.poll(); |
229 |
|
assertTrue(q.peek() == null || |
233 |
|
} |
234 |
|
|
235 |
|
public void testElement(){ |
236 |
< |
PriorityQueue q = fullQueue(N); |
237 |
< |
for (int i = 0; i < N; ++i) { |
236 |
> |
PriorityQueue q = populatedQueue(SIZE); |
237 |
> |
for (int i = 0; i < SIZE; ++i) { |
238 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
239 |
|
q.poll(); |
240 |
|
} |
246 |
|
} |
247 |
|
|
248 |
|
public void testRemove(){ |
249 |
< |
PriorityQueue q = fullQueue(N); |
250 |
< |
for (int i = 0; i < N; ++i) { |
249 |
> |
PriorityQueue q = populatedQueue(SIZE); |
250 |
> |
for (int i = 0; i < SIZE; ++i) { |
251 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
252 |
|
} |
253 |
|
try { |
258 |
|
} |
259 |
|
|
260 |
|
public void testRemoveElement(){ |
261 |
< |
PriorityQueue q = fullQueue(N); |
262 |
< |
for (int i = 1; i < N; i+=2) { |
261 |
> |
PriorityQueue q = populatedQueue(SIZE); |
262 |
> |
for (int i = 1; i < SIZE; i+=2) { |
263 |
|
assertTrue(q.remove(new Integer(i))); |
264 |
|
} |
265 |
< |
for (int i = 0; i < N; i+=2) { |
265 |
> |
for (int i = 0; i < SIZE; i+=2) { |
266 |
|
assertTrue(q.remove(new Integer(i))); |
267 |
|
assertFalse(q.remove(new Integer(i+1))); |
268 |
|
} |
270 |
|
} |
271 |
|
|
272 |
|
public void testContains(){ |
273 |
< |
PriorityQueue q = fullQueue(N); |
274 |
< |
for (int i = 0; i < N; ++i) { |
273 |
> |
PriorityQueue q = populatedQueue(SIZE); |
274 |
> |
for (int i = 0; i < SIZE; ++i) { |
275 |
|
assertTrue(q.contains(new Integer(i))); |
276 |
|
q.poll(); |
277 |
|
assertFalse(q.contains(new Integer(i))); |
279 |
|
} |
280 |
|
|
281 |
|
public void testClear(){ |
282 |
< |
PriorityQueue q = fullQueue(N); |
282 |
> |
PriorityQueue q = populatedQueue(SIZE); |
283 |
|
q.clear(); |
284 |
|
assertTrue(q.isEmpty()); |
285 |
|
assertEquals(0, q.size()); |
290 |
|
} |
291 |
|
|
292 |
|
public void testContainsAll(){ |
293 |
< |
PriorityQueue q = fullQueue(N); |
294 |
< |
PriorityQueue p = new PriorityQueue(N); |
295 |
< |
for (int i = 0; i < N; ++i) { |
293 |
> |
PriorityQueue q = populatedQueue(SIZE); |
294 |
> |
PriorityQueue p = new PriorityQueue(SIZE); |
295 |
> |
for (int i = 0; i < SIZE; ++i) { |
296 |
|
assertTrue(q.containsAll(p)); |
297 |
|
assertFalse(p.containsAll(q)); |
298 |
|
p.add(new Integer(i)); |
301 |
|
} |
302 |
|
|
303 |
|
public void testRetainAll(){ |
304 |
< |
PriorityQueue q = fullQueue(N); |
305 |
< |
PriorityQueue p = fullQueue(N); |
306 |
< |
for (int i = 0; i < N; ++i) { |
304 |
> |
PriorityQueue q = populatedQueue(SIZE); |
305 |
> |
PriorityQueue p = populatedQueue(SIZE); |
306 |
> |
for (int i = 0; i < SIZE; ++i) { |
307 |
|
boolean changed = q.retainAll(p); |
308 |
|
if (i == 0) |
309 |
|
assertFalse(changed); |
311 |
|
assertTrue(changed); |
312 |
|
|
313 |
|
assertTrue(q.containsAll(p)); |
314 |
< |
assertEquals(N-i, q.size()); |
314 |
> |
assertEquals(SIZE-i, q.size()); |
315 |
|
p.remove(); |
316 |
|
} |
317 |
|
} |
318 |
|
|
319 |
|
public void testRemoveAll(){ |
320 |
< |
for (int i = 1; i < N; ++i) { |
321 |
< |
PriorityQueue q = fullQueue(N); |
322 |
< |
PriorityQueue p = fullQueue(i); |
320 |
> |
for (int i = 1; i < SIZE; ++i) { |
321 |
> |
PriorityQueue q = populatedQueue(SIZE); |
322 |
> |
PriorityQueue p = populatedQueue(i); |
323 |
|
assertTrue(q.removeAll(p)); |
324 |
< |
assertEquals(N-i, q.size()); |
324 |
> |
assertEquals(SIZE-i, q.size()); |
325 |
|
for (int j = 0; j < i; ++j) { |
326 |
|
Integer I = (Integer)(p.remove()); |
327 |
|
assertFalse(q.contains(I)); |
330 |
|
} |
331 |
|
|
332 |
|
public void testToArray(){ |
333 |
< |
PriorityQueue q = fullQueue(N); |
333 |
> |
PriorityQueue q = populatedQueue(SIZE); |
334 |
|
Object[] o = q.toArray(); |
335 |
|
Arrays.sort(o); |
336 |
|
for(int i = 0; i < o.length; i++) |
338 |
|
} |
339 |
|
|
340 |
|
public void testToArray2(){ |
341 |
< |
PriorityQueue q = fullQueue(N); |
342 |
< |
Integer[] ints = new Integer[N]; |
341 |
> |
PriorityQueue q = populatedQueue(SIZE); |
342 |
> |
Integer[] ints = new Integer[SIZE]; |
343 |
|
ints = (Integer[])q.toArray(ints); |
344 |
|
Arrays.sort(ints); |
345 |
|
for(int i = 0; i < ints.length; i++) |
347 |
|
} |
348 |
|
|
349 |
|
public void testIterator(){ |
350 |
< |
PriorityQueue q = fullQueue(N); |
350 |
> |
PriorityQueue q = populatedQueue(SIZE); |
351 |
|
int i = 0; |
352 |
|
Iterator it = q.iterator(); |
353 |
|
while(it.hasNext()) { |
354 |
|
assertTrue(q.contains(it.next())); |
355 |
|
++i; |
356 |
|
} |
357 |
< |
assertEquals(i, N); |
357 |
> |
assertEquals(i, SIZE); |
358 |
|
} |
359 |
|
|
360 |
|
public void testIteratorRemove () { |
377 |
|
|
378 |
|
|
379 |
|
public void testToString(){ |
380 |
< |
PriorityQueue q = fullQueue(N); |
380 |
> |
PriorityQueue q = populatedQueue(SIZE); |
381 |
|
String s = q.toString(); |
382 |
< |
for (int i = 0; i < N; ++i) { |
382 |
> |
for (int i = 0; i < SIZE; ++i) { |
383 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
384 |
|
} |
385 |
|
} |
386 |
|
|
387 |
|
public void testSerialization() { |
388 |
< |
PriorityQueue q = fullQueue(N); |
388 |
> |
PriorityQueue q = populatedQueue(SIZE); |
389 |
|
try { |
390 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
391 |
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |