ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/PriorityQueueTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.5 by dl, Sat Sep 20 18:20:08 2003 UTC

# Line 10 | Line 10 | import java.util.*;
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());  
# Line 40 | Line 35 | public class PriorityQueueTest extends T
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)
# Line 52 | Line 47 | public class PriorityQueueTest extends T
47          return q;
48      }
49  
50 <    public void testConstructor1(){
51 <        assertEquals(0, new PriorityQueue(N).size());
50 >    /**
51 >     *
52 >     */
53 >    public void testConstructor1() {
54 >        assertEquals(0, new PriorityQueue(SIZE).size());
55      }
56  
57 <    public void testConstructor2(){
57 >    /**
58 >     *
59 >     */
60 >    public void testConstructor2() {
61          try {
62              PriorityQueue q = new PriorityQueue(0);
63 <            fail("Cannot make zero-sized");
63 >            shouldThrow();
64          }
65          catch (IllegalArgumentException success) {}
66      }
67  
68 +    /**
69 +     *
70 +     */
71      public void testConstructor3() {
72  
73          try {
74              PriorityQueue q = new PriorityQueue((Collection)null);
75 <            fail("Cannot make from null collection");
75 >            shouldThrow();
76          }
77          catch (NullPointerException success) {}
78      }
79  
80 <    public void testConstructor4(){
80 >    /**
81 >     *
82 >     */
83 >    public void testConstructor4() {
84          try {
85 <            Integer[] ints = new Integer[N];
85 >            Integer[] ints = new Integer[SIZE];
86              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
87 <            fail("Cannot make with null elements");
87 >            shouldThrow();
88          }
89          catch (NullPointerException success) {}
90      }
91  
92 <    public void testConstructor5(){
92 >    /**
93 >     *
94 >     */
95 >    public void testConstructor5() {
96          try {
97 <            Integer[] ints = new Integer[N];
98 <            for (int i = 0; i < N-1; ++i)
97 >            Integer[] ints = new Integer[SIZE];
98 >            for (int i = 0; i < SIZE-1; ++i)
99                  ints[i] = new Integer(i);
100              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
101 <            fail("Cannot make with null elements");
101 >            shouldThrow();
102          }
103          catch (NullPointerException success) {}
104      }
105  
106 <    public void testConstructor6(){
106 >    /**
107 >     *
108 >     */
109 >    public void testConstructor6() {
110          try {
111 <            Integer[] ints = new Integer[N];
112 <            for (int i = 0; i < N; ++i)
111 >            Integer[] ints = new Integer[SIZE];
112 >            for (int i = 0; i < SIZE; ++i)
113                  ints[i] = new Integer(i);
114              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
115 <            for (int i = 0; i < N; ++i)
115 >            for (int i = 0; i < SIZE; ++i)
116                  assertEquals(ints[i], q.poll());
117          }
118          finally {}
119      }
120  
121 <    public void testConstructor7(){
121 >    /**
122 >     *
123 >     */
124 >    public void testConstructor7() {
125          try {
126 <            PriorityQueue q = new PriorityQueue(N, new MyReverseComparator());
127 <            Integer[] ints = new Integer[N];
128 <            for (int i = 0; i < N; ++i)
126 >            MyReverseComparator cmp = new MyReverseComparator();
127 >            PriorityQueue q = new PriorityQueue(SIZE, cmp);
128 >            assertEquals(cmp, q.comparator());
129 >            Integer[] ints = new Integer[SIZE];
130 >            for (int i = 0; i < SIZE; ++i)
131                  ints[i] = new Integer(i);
132              q.addAll(Arrays.asList(ints));
133 <            for (int i = N-1; i >= 0; --i)
133 >            for (int i = SIZE-1; i >= 0; --i)
134                  assertEquals(ints[i], q.poll());
135          }
136          finally {}
137      }
138  
139 +    /**
140 +     *
141 +     */
142      public void testEmpty() {
143          PriorityQueue q = new PriorityQueue(2);
144          assertTrue(q.isEmpty());
# Line 129 | Line 150 | public class PriorityQueueTest extends T
150          assertTrue(q.isEmpty());
151      }
152  
153 +    /**
154 +     *
155 +     */
156      public void testSize() {
157 <        PriorityQueue q = fullQueue(N);
158 <        for (int i = 0; i < N; ++i) {
159 <            assertEquals(N-i, q.size());
157 >        PriorityQueue q = populatedQueue(SIZE);
158 >        for (int i = 0; i < SIZE; ++i) {
159 >            assertEquals(SIZE-i, q.size());
160              q.remove();
161          }
162 <        for (int i = 0; i < N; ++i) {
162 >        for (int i = 0; i < SIZE; ++i) {
163              assertEquals(i, q.size());
164              q.add(new Integer(i));
165          }
166      }
167  
168 <    public void testOfferNull(){
168 >    /**
169 >     *
170 >     */
171 >    public void testOfferNull() {
172          try {
173              PriorityQueue q = new PriorityQueue(1);
174              q.offer(null);
175 <            fail("should throw NPE");
175 >            shouldThrow();
176          } catch (NullPointerException success) { }  
177      }
178  
179 +    /**
180 +     *
181 +     */
182      public void testOffer() {
183          PriorityQueue q = new PriorityQueue(1);
184          assertTrue(q.offer(new Integer(0)));
185          assertTrue(q.offer(new Integer(1)));
186      }
187  
188 +    /**
189 +     *
190 +     */
191      public void testOfferNonComparable() {
192          try {
193              PriorityQueue q = new PriorityQueue(1);
194              q.offer(new Object());
195              q.offer(new Object());
196              q.offer(new Object());
197 <            fail("should throw CCE");
197 >            shouldThrow();
198          }
199          catch(ClassCastException success) {}
200      }
201  
202 <    public void testAdd(){
203 <        PriorityQueue q = new PriorityQueue(N);
204 <        for (int i = 0; i < N; ++i) {
202 >    /**
203 >     *
204 >     */
205 >    public void testAdd() {
206 >        PriorityQueue q = new PriorityQueue(SIZE);
207 >        for (int i = 0; i < SIZE; ++i) {
208              assertEquals(i, q.size());
209              assertTrue(q.add(new Integer(i)));
210          }
211      }
212  
213 <    public void testAddAll1(){
213 >    /**
214 >     *
215 >     */
216 >    public void testAddAll1() {
217          try {
218              PriorityQueue q = new PriorityQueue(1);
219              q.addAll(null);
220 <            fail("Cannot add null collection");
220 >            shouldThrow();
221          }
222          catch (NullPointerException success) {}
223      }
224 <    public void testAddAll2(){
224 >    /**
225 >     *
226 >     */
227 >    public void testAddAll2() {
228          try {
229 <            PriorityQueue q = new PriorityQueue(N);
230 <            Integer[] ints = new Integer[N];
229 >            PriorityQueue q = new PriorityQueue(SIZE);
230 >            Integer[] ints = new Integer[SIZE];
231              q.addAll(Arrays.asList(ints));
232 <            fail("Cannot add null elements");
232 >            shouldThrow();
233          }
234          catch (NullPointerException success) {}
235      }
236 <    public void testAddAll3(){
236 >    /**
237 >     *
238 >     */
239 >    public void testAddAll3() {
240          try {
241 <            PriorityQueue q = new PriorityQueue(N);
242 <            Integer[] ints = new Integer[N];
243 <            for (int i = 0; i < N-1; ++i)
241 >            PriorityQueue q = new PriorityQueue(SIZE);
242 >            Integer[] ints = new Integer[SIZE];
243 >            for (int i = 0; i < SIZE-1; ++i)
244                  ints[i] = new Integer(i);
245              q.addAll(Arrays.asList(ints));
246 <            fail("Cannot add null elements");
246 >            shouldThrow();
247          }
248          catch (NullPointerException success) {}
249      }
250  
251 <    public void testAddAll5(){
251 >    /**
252 >     *
253 >     */
254 >    public void testAddAll5() {
255          try {
256              Integer[] empty = new Integer[0];
257 <            Integer[] ints = new Integer[N];
258 <            for (int i = 0; i < N; ++i)
259 <                ints[i] = new Integer(N-1-i);
260 <            PriorityQueue q = new PriorityQueue(N);
257 >            Integer[] ints = new Integer[SIZE];
258 >            for (int i = 0; i < SIZE; ++i)
259 >                ints[i] = new Integer(SIZE-1-i);
260 >            PriorityQueue q = new PriorityQueue(SIZE);
261              assertFalse(q.addAll(Arrays.asList(empty)));
262              assertTrue(q.addAll(Arrays.asList(ints)));
263 <            for (int i = 0; i < N; ++i)
263 >            for (int i = 0; i < SIZE; ++i)
264                  assertEquals(new Integer(i), q.poll());
265          }
266          finally {}
267      }
268  
269 <    public void testPoll(){
270 <        PriorityQueue q = fullQueue(N);
271 <        for (int i = 0; i < N; ++i) {
269 >    /**
270 >     *
271 >     */
272 >    public void testPoll() {
273 >        PriorityQueue q = populatedQueue(SIZE);
274 >        for (int i = 0; i < SIZE; ++i) {
275              assertEquals(i, ((Integer)q.poll()).intValue());
276          }
277          assertNull(q.poll());
278      }
279  
280 <    public void testPeek(){
281 <        PriorityQueue q = fullQueue(N);
282 <        for (int i = 0; i < N; ++i) {
280 >    /**
281 >     *
282 >     */
283 >    public void testPeek() {
284 >        PriorityQueue q = populatedQueue(SIZE);
285 >        for (int i = 0; i < SIZE; ++i) {
286              assertEquals(i, ((Integer)q.peek()).intValue());
287              q.poll();
288              assertTrue(q.peek() == null ||
# Line 237 | Line 291 | public class PriorityQueueTest extends T
291          assertNull(q.peek());
292      }
293  
294 <    public void testElement(){
295 <        PriorityQueue q = fullQueue(N);
296 <        for (int i = 0; i < N; ++i) {
294 >    /**
295 >     *
296 >     */
297 >    public void testElement() {
298 >        PriorityQueue q = populatedQueue(SIZE);
299 >        for (int i = 0; i < SIZE; ++i) {
300              assertEquals(i, ((Integer)q.element()).intValue());
301              q.poll();
302          }
303          try {
304              q.element();
305 <            fail("no such element");
305 >            shouldThrow();
306          }
307          catch (NoSuchElementException success) {}
308      }
309  
310 <    public void testRemove(){
311 <        PriorityQueue q = fullQueue(N);
312 <        for (int i = 0; i < N; ++i) {
310 >    /**
311 >     *
312 >     */
313 >    public void testRemove() {
314 >        PriorityQueue q = populatedQueue(SIZE);
315 >        for (int i = 0; i < SIZE; ++i) {
316              assertEquals(i, ((Integer)q.remove()).intValue());
317          }
318          try {
319              q.remove();
320 <            fail("remove should throw");
320 >            shouldThrow();
321          } catch (NoSuchElementException success){
322          }  
323      }
324  
325 <    public void testRemoveElement(){
326 <        PriorityQueue q = fullQueue(N);
327 <        for (int i = 1; i < N; i+=2) {
325 >    /**
326 >     *
327 >     */
328 >    public void testRemoveElement() {
329 >        PriorityQueue q = populatedQueue(SIZE);
330 >        for (int i = 1; i < SIZE; i+=2) {
331              assertTrue(q.remove(new Integer(i)));
332          }
333 <        for (int i = 0; i < N; i+=2) {
333 >        for (int i = 0; i < SIZE; i+=2) {
334              assertTrue(q.remove(new Integer(i)));
335              assertFalse(q.remove(new Integer(i+1)));
336          }
337          assertTrue(q.isEmpty());
338      }
339          
340 <    public void testContains(){
341 <        PriorityQueue q = fullQueue(N);
342 <        for (int i = 0; i < N; ++i) {
340 >    /**
341 >     *
342 >     */
343 >    public void testContains() {
344 >        PriorityQueue q = populatedQueue(SIZE);
345 >        for (int i = 0; i < SIZE; ++i) {
346              assertTrue(q.contains(new Integer(i)));
347              q.poll();
348              assertFalse(q.contains(new Integer(i)));
349          }
350      }
351  
352 <    public void testClear(){
353 <        PriorityQueue q = fullQueue(N);
352 >    /**
353 >     *
354 >     */
355 >    public void testClear() {
356 >        PriorityQueue q = populatedQueue(SIZE);
357          q.clear();
358          assertTrue(q.isEmpty());
359          assertEquals(0, q.size());
# Line 294 | Line 363 | public class PriorityQueueTest extends T
363          assertTrue(q.isEmpty());
364      }
365  
366 <    public void testContainsAll(){
367 <        PriorityQueue q = fullQueue(N);
368 <        PriorityQueue p = new PriorityQueue(N);
369 <        for (int i = 0; i < N; ++i) {
366 >    /**
367 >     *
368 >     */
369 >    public void testContainsAll() {
370 >        PriorityQueue q = populatedQueue(SIZE);
371 >        PriorityQueue p = new PriorityQueue(SIZE);
372 >        for (int i = 0; i < SIZE; ++i) {
373              assertTrue(q.containsAll(p));
374              assertFalse(p.containsAll(q));
375              p.add(new Integer(i));
# Line 305 | Line 377 | public class PriorityQueueTest extends T
377          assertTrue(p.containsAll(q));
378      }
379  
380 <    public void testRetainAll(){
381 <        PriorityQueue q = fullQueue(N);
382 <        PriorityQueue p = fullQueue(N);
383 <        for (int i = 0; i < N; ++i) {
380 >    /**
381 >     *
382 >     */
383 >    public void testRetainAll() {
384 >        PriorityQueue q = populatedQueue(SIZE);
385 >        PriorityQueue p = populatedQueue(SIZE);
386 >        for (int i = 0; i < SIZE; ++i) {
387              boolean changed = q.retainAll(p);
388              if (i == 0)
389                  assertFalse(changed);
# Line 316 | Line 391 | public class PriorityQueueTest extends T
391                  assertTrue(changed);
392  
393              assertTrue(q.containsAll(p));
394 <            assertEquals(N-i, q.size());
394 >            assertEquals(SIZE-i, q.size());
395              p.remove();
396          }
397      }
398  
399 <    public void testRemoveAll(){
400 <        for (int i = 1; i < N; ++i) {
401 <            PriorityQueue q = fullQueue(N);
402 <            PriorityQueue p = fullQueue(i);
399 >    /**
400 >     *
401 >     */
402 >    public void testRemoveAll() {
403 >        for (int i = 1; i < SIZE; ++i) {
404 >            PriorityQueue q = populatedQueue(SIZE);
405 >            PriorityQueue p = populatedQueue(i);
406              assertTrue(q.removeAll(p));
407 <            assertEquals(N-i, q.size());
407 >            assertEquals(SIZE-i, q.size());
408              for (int j = 0; j < i; ++j) {
409                  Integer I = (Integer)(p.remove());
410                  assertFalse(q.contains(I));
# Line 334 | Line 412 | public class PriorityQueueTest extends T
412          }
413      }
414  
415 <    public void testToArray(){
416 <        PriorityQueue q = fullQueue(N);
415 >    /**
416 >     *
417 >     */
418 >    public void testToArray() {
419 >        PriorityQueue q = populatedQueue(SIZE);
420          Object[] o = q.toArray();
421          Arrays.sort(o);
422          for(int i = 0; i < o.length; i++)
423              assertEquals(o[i], q.poll());
424      }
425  
426 <    public void testToArray2(){
427 <        PriorityQueue q = fullQueue(N);
428 <        Integer[] ints = new Integer[N];
426 >    /**
427 >     *
428 >     */
429 >    public void testToArray2() {
430 >        PriorityQueue q = populatedQueue(SIZE);
431 >        Integer[] ints = new Integer[SIZE];
432          ints = (Integer[])q.toArray(ints);
433          Arrays.sort(ints);
434          for(int i = 0; i < ints.length; i++)
435              assertEquals(ints[i], q.poll());
436      }
437      
438 <    public void testIterator(){
439 <        PriorityQueue q = fullQueue(N);
438 >    /**
439 >     *
440 >     */
441 >    public void testIterator() {
442 >        PriorityQueue q = populatedQueue(SIZE);
443          int i = 0;
444          Iterator it = q.iterator();
445          while(it.hasNext()) {
446              assertTrue(q.contains(it.next()));
447              ++i;
448          }
449 <        assertEquals(i, N);
449 >        assertEquals(i, SIZE);
450      }
451  
452 +    /**
453 +     *
454 +     */
455      public void testIteratorRemove () {
366
456          final PriorityQueue q = new PriorityQueue(3);
368
457          q.add(new Integer(2));
458          q.add(new Integer(1));
459          q.add(new Integer(3));
# Line 381 | Line 469 | public class PriorityQueueTest extends T
469      }
470  
471  
472 <    public void testToString(){
473 <        PriorityQueue q = fullQueue(N);
472 >    /**
473 >     *
474 >     */
475 >    public void testToString() {
476 >        PriorityQueue q = populatedQueue(SIZE);
477          String s = q.toString();
478 <        for (int i = 0; i < N; ++i) {
478 >        for (int i = 0; i < SIZE; ++i) {
479              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
480          }
481      }        
482  
483 +    /**
484 +     *
485 +     */
486      public void testSerialization() {
487 <        PriorityQueue q = fullQueue(N);
487 >        PriorityQueue q = populatedQueue(SIZE);
488          try {
489              ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
490              ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
# Line 404 | Line 498 | public class PriorityQueueTest extends T
498              while (!q.isEmpty())
499                  assertEquals(q.remove(), r.remove());
500          } catch(Exception e){
501 <            fail("unexpected exception");
501 >            unexpectedException();
502          }
503      }
504   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines