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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.7 by dl, Sun Oct 5 23:00:40 2003 UTC

# Line 11 | Line 11 | import java.util.concurrent.*;
11   import java.io.*;
12  
13   public class PriorityQueueTest extends JSR166TestCase {
14
14      public static void main(String[] args) {
15          junit.textui.TestRunner.run (suite());  
16      }
18
17      public static Test suite() {
18          return new TestSuite(PriorityQueueTest.class);
19      }
# Line 30 | Line 28 | public class PriorityQueueTest extends J
28          }
29      }
30  
33
31      /**
32       * Create a queue of given size containing consecutive
33       * Integers 0 ... n.
# Line 47 | Line 44 | public class PriorityQueueTest extends J
44          return q;
45      }
46  
47 <    public void testConstructor1(){
47 >    /**
48 >     * A new queue has unbounded capacity
49 >     */
50 >    public void testConstructor1() {
51          assertEquals(0, new PriorityQueue(SIZE).size());
52      }
53  
54 <    public void testConstructor2(){
54 >    /**
55 >     * Constructor throws IAE if  capacity argument nonpositive
56 >     */
57 >    public void testConstructor2() {
58          try {
59              PriorityQueue q = new PriorityQueue(0);
60 <            fail("Cannot make zero-sized");
60 >            shouldThrow();
61          }
62          catch (IllegalArgumentException success) {}
63      }
64  
65 +    /**
66 +     * Initializing from null Collection throws NPE
67 +     */
68      public void testConstructor3() {
63
69          try {
70              PriorityQueue q = new PriorityQueue((Collection)null);
71 <            fail("Cannot make from null collection");
71 >            shouldThrow();
72          }
73          catch (NullPointerException success) {}
74      }
75  
76 <    public void testConstructor4(){
76 >    /**
77 >     * Initializing from Collection of null elements throws NPE
78 >     */
79 >    public void testConstructor4() {
80          try {
81              Integer[] ints = new Integer[SIZE];
82              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
83 <            fail("Cannot make with null elements");
83 >            shouldThrow();
84          }
85          catch (NullPointerException success) {}
86      }
87  
88 <    public void testConstructor5(){
88 >    /**
89 >     * Initializing from Collection with some null elements throws NPE
90 >     */
91 >    public void testConstructor5() {
92          try {
93              Integer[] ints = new Integer[SIZE];
94              for (int i = 0; i < SIZE-1; ++i)
95                  ints[i] = new Integer(i);
96              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
97 <            fail("Cannot make with null elements");
97 >            shouldThrow();
98          }
99          catch (NullPointerException success) {}
100      }
101  
102 <    public void testConstructor6(){
102 >    /**
103 >     * Queue contains all elements of collection used to initialize
104 >     */
105 >    public void testConstructor6() {
106          try {
107              Integer[] ints = new Integer[SIZE];
108              for (int i = 0; i < SIZE; ++i)
# Line 100 | Line 114 | public class PriorityQueueTest extends J
114          finally {}
115      }
116  
117 <    public void testConstructor7(){
117 >    /**
118 >     * The comparator used in constructor is used
119 >     */
120 >    public void testConstructor7() {
121          try {
122 <            PriorityQueue q = new PriorityQueue(SIZE, new MyReverseComparator());
122 >            MyReverseComparator cmp = new MyReverseComparator();
123 >            PriorityQueue q = new PriorityQueue(SIZE, cmp);
124 >            assertEquals(cmp, q.comparator());
125              Integer[] ints = new Integer[SIZE];
126              for (int i = 0; i < SIZE; ++i)
127                  ints[i] = new Integer(i);
# Line 113 | Line 132 | public class PriorityQueueTest extends J
132          finally {}
133      }
134  
135 +    /**
136 +     * isEmpty is true before add, false after
137 +     */
138      public void testEmpty() {
139          PriorityQueue q = new PriorityQueue(2);
140          assertTrue(q.isEmpty());
# Line 124 | Line 146 | public class PriorityQueueTest extends J
146          assertTrue(q.isEmpty());
147      }
148  
149 +    /**
150 +     * size changes when elements added and removed
151 +     */
152      public void testSize() {
153          PriorityQueue q = populatedQueue(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
# Line 136 | Line 161 | public class PriorityQueueTest extends J
161          }
162      }
163  
164 <    public void testOfferNull(){
164 >    /**
165 >     * offer(null) throws NPE
166 >     */
167 >    public void testOfferNull() {
168          try {
169              PriorityQueue q = new PriorityQueue(1);
170              q.offer(null);
171 <            fail("should throw NPE");
171 >            shouldThrow();
172 >        } catch (NullPointerException success) { }  
173 >    }
174 >
175 >    /**
176 >     * add(null) throws NPE
177 >     */
178 >    public void testAddNull() {
179 >        try {
180 >            PriorityQueue q = new PriorityQueue(1);
181 >            q.add(null);
182 >            shouldThrow();
183          } catch (NullPointerException success) { }  
184      }
185  
186 +    /**
187 +     * Offer of comparable element succeeds
188 +     */
189      public void testOffer() {
190          PriorityQueue q = new PriorityQueue(1);
191 <        assertTrue(q.offer(new Integer(0)));
192 <        assertTrue(q.offer(new Integer(1)));
191 >        assertTrue(q.offer(zero));
192 >        assertTrue(q.offer(one));
193      }
194  
195 +    /**
196 +     * Offer of non-Comparable throws CCE
197 +     */
198      public void testOfferNonComparable() {
199          try {
200              PriorityQueue q = new PriorityQueue(1);
201              q.offer(new Object());
202              q.offer(new Object());
203              q.offer(new Object());
204 <            fail("should throw CCE");
204 >            shouldThrow();
205          }
206          catch(ClassCastException success) {}
207      }
208  
209 <    public void testAdd(){
209 >    /**
210 >     * add of comparable succeeds
211 >     */
212 >    public void testAdd() {
213          PriorityQueue q = new PriorityQueue(SIZE);
214          for (int i = 0; i < SIZE; ++i) {
215              assertEquals(i, q.size());
# Line 169 | Line 217 | public class PriorityQueueTest extends J
217          }
218      }
219  
220 <    public void testAddAll1(){
220 >    /**
221 >     * addAll(null) throws NPE
222 >     */
223 >    public void testAddAll1() {
224          try {
225              PriorityQueue q = new PriorityQueue(1);
226              q.addAll(null);
227 <            fail("Cannot add null collection");
227 >            shouldThrow();
228          }
229          catch (NullPointerException success) {}
230      }
231 <    public void testAddAll2(){
231 >    /**
232 >     * addAll of a collection with null elements throws NPE
233 >     */
234 >    public void testAddAll2() {
235          try {
236              PriorityQueue q = new PriorityQueue(SIZE);
237              Integer[] ints = new Integer[SIZE];
238              q.addAll(Arrays.asList(ints));
239 <            fail("Cannot add null elements");
239 >            shouldThrow();
240          }
241          catch (NullPointerException success) {}
242      }
243 <    public void testAddAll3(){
243 >    /**
244 >     * addAll of a collection with any null elements throws NPE after
245 >     * possibly adding some elements
246 >     */
247 >    public void testAddAll3() {
248          try {
249              PriorityQueue q = new PriorityQueue(SIZE);
250              Integer[] ints = new Integer[SIZE];
251              for (int i = 0; i < SIZE-1; ++i)
252                  ints[i] = new Integer(i);
253              q.addAll(Arrays.asList(ints));
254 <            fail("Cannot add null elements");
254 >            shouldThrow();
255          }
256          catch (NullPointerException success) {}
257      }
258  
259 <    public void testAddAll5(){
259 >    /**
260 >     * Queue contains all elements of successful addAll
261 >     */
262 >    public void testAddAll5() {
263          try {
264              Integer[] empty = new Integer[0];
265              Integer[] ints = new Integer[SIZE];
# Line 213 | Line 274 | public class PriorityQueueTest extends J
274          finally {}
275      }
276  
277 <    public void testPoll(){
277 >    /**
278 >     * poll succeeds unless empty
279 >     */
280 >    public void testPoll() {
281          PriorityQueue q = populatedQueue(SIZE);
282          for (int i = 0; i < SIZE; ++i) {
283              assertEquals(i, ((Integer)q.poll()).intValue());
# Line 221 | Line 285 | public class PriorityQueueTest extends J
285          assertNull(q.poll());
286      }
287  
288 <    public void testPeek(){
288 >    /**
289 >     * peek returns next element, or null if empty
290 >     */
291 >    public void testPeek() {
292          PriorityQueue q = populatedQueue(SIZE);
293          for (int i = 0; i < SIZE; ++i) {
294              assertEquals(i, ((Integer)q.peek()).intValue());
# Line 232 | Line 299 | public class PriorityQueueTest extends J
299          assertNull(q.peek());
300      }
301  
302 <    public void testElement(){
302 >    /**
303 >     * element returns next element, or throws NSEE if empty
304 >     */
305 >    public void testElement() {
306          PriorityQueue q = populatedQueue(SIZE);
307          for (int i = 0; i < SIZE; ++i) {
308              assertEquals(i, ((Integer)q.element()).intValue());
# Line 240 | Line 310 | public class PriorityQueueTest extends J
310          }
311          try {
312              q.element();
313 <            fail("no such element");
313 >            shouldThrow();
314          }
315          catch (NoSuchElementException success) {}
316      }
317  
318 <    public void testRemove(){
318 >    /**
319 >     * remove removes next element, or throws NSEE if empty
320 >     */
321 >    public void testRemove() {
322          PriorityQueue q = populatedQueue(SIZE);
323          for (int i = 0; i < SIZE; ++i) {
324              assertEquals(i, ((Integer)q.remove()).intValue());
325          }
326          try {
327              q.remove();
328 <            fail("remove should throw");
328 >            shouldThrow();
329          } catch (NoSuchElementException success){
330          }  
331      }
332  
333 <    public void testRemoveElement(){
333 >    /**
334 >     * remove(x) removes x and returns true if present
335 >     */
336 >    public void testRemoveElement() {
337          PriorityQueue q = populatedQueue(SIZE);
338          for (int i = 1; i < SIZE; i+=2) {
339              assertTrue(q.remove(new Integer(i)));
# Line 269 | Line 345 | public class PriorityQueueTest extends J
345          assertTrue(q.isEmpty());
346      }
347          
348 <    public void testContains(){
348 >    /**
349 >     * contains(x) reports true when elements added but not yet removed
350 >     */
351 >    public void testContains() {
352          PriorityQueue q = populatedQueue(SIZE);
353          for (int i = 0; i < SIZE; ++i) {
354              assertTrue(q.contains(new Integer(i)));
# Line 278 | Line 357 | public class PriorityQueueTest extends J
357          }
358      }
359  
360 <    public void testClear(){
360 >    /**
361 >     * clear removes all elements
362 >     */
363 >    public void testClear() {
364          PriorityQueue q = populatedQueue(SIZE);
365          q.clear();
366          assertTrue(q.isEmpty());
# Line 289 | Line 371 | public class PriorityQueueTest extends J
371          assertTrue(q.isEmpty());
372      }
373  
374 <    public void testContainsAll(){
374 >    /**
375 >     * containsAll(c) is true when c contains a subset of elements
376 >     */
377 >    public void testContainsAll() {
378          PriorityQueue q = populatedQueue(SIZE);
379          PriorityQueue p = new PriorityQueue(SIZE);
380          for (int i = 0; i < SIZE; ++i) {
# Line 300 | Line 385 | public class PriorityQueueTest extends J
385          assertTrue(p.containsAll(q));
386      }
387  
388 <    public void testRetainAll(){
388 >    /**
389 >     * retainAll(c) retains only those elements of c and reports true if changed
390 >     */
391 >    public void testRetainAll() {
392          PriorityQueue q = populatedQueue(SIZE);
393          PriorityQueue p = populatedQueue(SIZE);
394          for (int i = 0; i < SIZE; ++i) {
# Line 316 | Line 404 | public class PriorityQueueTest extends J
404          }
405      }
406  
407 <    public void testRemoveAll(){
407 >    /**
408 >     * removeAll(c) removes only those elements of c and reports true if changed
409 >     */
410 >    public void testRemoveAll() {
411          for (int i = 1; i < SIZE; ++i) {
412              PriorityQueue q = populatedQueue(SIZE);
413              PriorityQueue p = populatedQueue(i);
# Line 329 | Line 420 | public class PriorityQueueTest extends J
420          }
421      }
422  
423 <    public void testToArray(){
423 >    /**
424 >     * toArray contains all elements
425 >     */
426 >    public void testToArray() {
427          PriorityQueue q = populatedQueue(SIZE);
428          Object[] o = q.toArray();
429          Arrays.sort(o);
# Line 337 | Line 431 | public class PriorityQueueTest extends J
431              assertEquals(o[i], q.poll());
432      }
433  
434 <    public void testToArray2(){
434 >    /**
435 >     * toArray(a) contains all elements
436 >     */
437 >    public void testToArray2() {
438          PriorityQueue q = populatedQueue(SIZE);
439          Integer[] ints = new Integer[SIZE];
440          ints = (Integer[])q.toArray(ints);
# Line 346 | Line 443 | public class PriorityQueueTest extends J
443              assertEquals(ints[i], q.poll());
444      }
445      
446 <    public void testIterator(){
446 >    /**
447 >     * iterator iterates through all elements
448 >     */
449 >    public void testIterator() {
450          PriorityQueue q = populatedQueue(SIZE);
451          int i = 0;
452          Iterator it = q.iterator();
# Line 357 | Line 457 | public class PriorityQueueTest extends J
457          assertEquals(i, SIZE);
458      }
459  
460 +    /**
461 +     * iterator.remove removes current element
462 +     */
463      public void testIteratorRemove () {
361
464          final PriorityQueue q = new PriorityQueue(3);
363
465          q.add(new Integer(2));
466          q.add(new Integer(1));
467          q.add(new Integer(3));
# Line 376 | Line 477 | public class PriorityQueueTest extends J
477      }
478  
479  
480 <    public void testToString(){
480 >    /**
481 >     * toString contains toStrings of elements
482 >     */
483 >    public void testToString() {
484          PriorityQueue q = populatedQueue(SIZE);
485          String s = q.toString();
486          for (int i = 0; i < SIZE; ++i) {
# Line 384 | Line 488 | public class PriorityQueueTest extends J
488          }
489      }        
490  
491 +    /**
492 +     * A deserialized serialized queue has same elements
493 +     */
494      public void testSerialization() {
495          PriorityQueue q = populatedQueue(SIZE);
496          try {
# Line 399 | Line 506 | public class PriorityQueueTest extends J
506              while (!q.isEmpty())
507                  assertEquals(q.remove(), r.remove());
508          } catch(Exception e){
509 <            fail("unexpected exception");
509 >            unexpectedException();
510          }
511      }
512   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines