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.4 by dl, Sat Sep 20 00:31:57 2003 UTC vs.
Revision 1.6 by dl, Thu Sep 25 11:02:41 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              MyReverseComparator cmp = new MyReverseComparator();
123              PriorityQueue q = new PriorityQueue(SIZE, cmp);
# Line 115 | 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 126 | 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 138 | 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 +     * Offer of comparable element succeeds
177 +     */
178      public void testOffer() {
179          PriorityQueue q = new PriorityQueue(1);
180 <        assertTrue(q.offer(new Integer(0)));
181 <        assertTrue(q.offer(new Integer(1)));
180 >        assertTrue(q.offer(zero));
181 >        assertTrue(q.offer(one));
182      }
183  
184 +    /**
185 +     * Offer of non-Comparable throws CCE
186 +     */
187      public void testOfferNonComparable() {
188          try {
189              PriorityQueue q = new PriorityQueue(1);
190              q.offer(new Object());
191              q.offer(new Object());
192              q.offer(new Object());
193 <            fail("should throw CCE");
193 >            shouldThrow();
194          }
195          catch(ClassCastException success) {}
196      }
197  
198 <    public void testAdd(){
198 >    /**
199 >     * add of comparable succeeds
200 >     */
201 >    public void testAdd() {
202          PriorityQueue q = new PriorityQueue(SIZE);
203          for (int i = 0; i < SIZE; ++i) {
204              assertEquals(i, q.size());
# Line 171 | Line 206 | public class PriorityQueueTest extends J
206          }
207      }
208  
209 <    public void testAddAll1(){
209 >    /**
210 >     * addAll(null) throws NPE
211 >     */
212 >    public void testAddAll1() {
213          try {
214              PriorityQueue q = new PriorityQueue(1);
215              q.addAll(null);
216 <            fail("Cannot add null collection");
216 >            shouldThrow();
217          }
218          catch (NullPointerException success) {}
219      }
220 <    public void testAddAll2(){
220 >    /**
221 >     * addAll of a collection with null elements throws NPE
222 >     */
223 >    public void testAddAll2() {
224          try {
225              PriorityQueue q = new PriorityQueue(SIZE);
226              Integer[] ints = new Integer[SIZE];
227              q.addAll(Arrays.asList(ints));
228 <            fail("Cannot add null elements");
228 >            shouldThrow();
229          }
230          catch (NullPointerException success) {}
231      }
232 <    public void testAddAll3(){
232 >    /**
233 >     * addAll of a collection with any null elements throws NPE after
234 >     * possibly adding some elements
235 >     */
236 >    public void testAddAll3() {
237          try {
238              PriorityQueue q = new PriorityQueue(SIZE);
239              Integer[] ints = new Integer[SIZE];
240              for (int i = 0; i < SIZE-1; ++i)
241                  ints[i] = new Integer(i);
242              q.addAll(Arrays.asList(ints));
243 <            fail("Cannot add null elements");
243 >            shouldThrow();
244          }
245          catch (NullPointerException success) {}
246      }
247  
248 <    public void testAddAll5(){
248 >    /**
249 >     * Queue contains all elements of successful addAll
250 >     */
251 >    public void testAddAll5() {
252          try {
253              Integer[] empty = new Integer[0];
254              Integer[] ints = new Integer[SIZE];
# Line 215 | Line 263 | public class PriorityQueueTest extends J
263          finally {}
264      }
265  
266 <    public void testPoll(){
266 >    /**
267 >     * poll succeeds unless empty
268 >     */
269 >    public void testPoll() {
270          PriorityQueue q = populatedQueue(SIZE);
271          for (int i = 0; i < SIZE; ++i) {
272              assertEquals(i, ((Integer)q.poll()).intValue());
# Line 223 | Line 274 | public class PriorityQueueTest extends J
274          assertNull(q.poll());
275      }
276  
277 <    public void testPeek(){
277 >    /**
278 >     * peek returns next element, or null if empty
279 >     */
280 >    public void testPeek() {
281          PriorityQueue q = populatedQueue(SIZE);
282          for (int i = 0; i < SIZE; ++i) {
283              assertEquals(i, ((Integer)q.peek()).intValue());
# Line 234 | Line 288 | public class PriorityQueueTest extends J
288          assertNull(q.peek());
289      }
290  
291 <    public void testElement(){
291 >    /**
292 >     * element returns next element, or throws NSEE if empty
293 >     */
294 >    public void testElement() {
295          PriorityQueue q = populatedQueue(SIZE);
296          for (int i = 0; i < SIZE; ++i) {
297              assertEquals(i, ((Integer)q.element()).intValue());
# Line 242 | Line 299 | public class PriorityQueueTest extends J
299          }
300          try {
301              q.element();
302 <            fail("no such element");
302 >            shouldThrow();
303          }
304          catch (NoSuchElementException success) {}
305      }
306  
307 <    public void testRemove(){
307 >    /**
308 >     * remove removes next element, or throws NSEE if empty
309 >     */
310 >    public void testRemove() {
311          PriorityQueue q = populatedQueue(SIZE);
312          for (int i = 0; i < SIZE; ++i) {
313              assertEquals(i, ((Integer)q.remove()).intValue());
314          }
315          try {
316              q.remove();
317 <            fail("remove should throw");
317 >            shouldThrow();
318          } catch (NoSuchElementException success){
319          }  
320      }
321  
322 <    public void testRemoveElement(){
322 >    /**
323 >     * remove(x) removes x and returns true if present
324 >     */
325 >    public void testRemoveElement() {
326          PriorityQueue q = populatedQueue(SIZE);
327          for (int i = 1; i < SIZE; i+=2) {
328              assertTrue(q.remove(new Integer(i)));
# Line 271 | Line 334 | public class PriorityQueueTest extends J
334          assertTrue(q.isEmpty());
335      }
336          
337 <    public void testContains(){
337 >    /**
338 >     * contains(x) reports true when elements added but not yet removed
339 >     */
340 >    public void testContains() {
341          PriorityQueue q = populatedQueue(SIZE);
342          for (int i = 0; i < SIZE; ++i) {
343              assertTrue(q.contains(new Integer(i)));
# Line 280 | Line 346 | public class PriorityQueueTest extends J
346          }
347      }
348  
349 <    public void testClear(){
349 >    /**
350 >     * clear removes all elements
351 >     */
352 >    public void testClear() {
353          PriorityQueue q = populatedQueue(SIZE);
354          q.clear();
355          assertTrue(q.isEmpty());
# Line 291 | Line 360 | public class PriorityQueueTest extends J
360          assertTrue(q.isEmpty());
361      }
362  
363 <    public void testContainsAll(){
363 >    /**
364 >     * containsAll(c) is true when c contains a subset of elements
365 >     */
366 >    public void testContainsAll() {
367          PriorityQueue q = populatedQueue(SIZE);
368          PriorityQueue p = new PriorityQueue(SIZE);
369          for (int i = 0; i < SIZE; ++i) {
# Line 302 | Line 374 | public class PriorityQueueTest extends J
374          assertTrue(p.containsAll(q));
375      }
376  
377 <    public void testRetainAll(){
377 >    /**
378 >     * retainAll(c) retains only those elements of c and reports true if changed
379 >     */
380 >    public void testRetainAll() {
381          PriorityQueue q = populatedQueue(SIZE);
382          PriorityQueue p = populatedQueue(SIZE);
383          for (int i = 0; i < SIZE; ++i) {
# Line 318 | Line 393 | public class PriorityQueueTest extends J
393          }
394      }
395  
396 <    public void testRemoveAll(){
396 >    /**
397 >     * removeAll(c) removes only those elements of c and reports true if changed
398 >     */
399 >    public void testRemoveAll() {
400          for (int i = 1; i < SIZE; ++i) {
401              PriorityQueue q = populatedQueue(SIZE);
402              PriorityQueue p = populatedQueue(i);
# Line 331 | Line 409 | public class PriorityQueueTest extends J
409          }
410      }
411  
412 <    public void testToArray(){
412 >    /**
413 >     * toArray contains all elements
414 >     */
415 >    public void testToArray() {
416          PriorityQueue q = populatedQueue(SIZE);
417          Object[] o = q.toArray();
418          Arrays.sort(o);
# Line 339 | Line 420 | public class PriorityQueueTest extends J
420              assertEquals(o[i], q.poll());
421      }
422  
423 <    public void testToArray2(){
423 >    /**
424 >     * toArray(a) contains all elements
425 >     */
426 >    public void testToArray2() {
427          PriorityQueue q = populatedQueue(SIZE);
428          Integer[] ints = new Integer[SIZE];
429          ints = (Integer[])q.toArray(ints);
# Line 348 | Line 432 | public class PriorityQueueTest extends J
432              assertEquals(ints[i], q.poll());
433      }
434      
435 <    public void testIterator(){
435 >    /**
436 >     * iterator iterates through all elements
437 >     */
438 >    public void testIterator() {
439          PriorityQueue q = populatedQueue(SIZE);
440          int i = 0;
441          Iterator it = q.iterator();
# Line 359 | Line 446 | public class PriorityQueueTest extends J
446          assertEquals(i, SIZE);
447      }
448  
449 +    /**
450 +     * iterator.remove removes current element
451 +     */
452      public void testIteratorRemove () {
363
453          final PriorityQueue q = new PriorityQueue(3);
365
454          q.add(new Integer(2));
455          q.add(new Integer(1));
456          q.add(new Integer(3));
# Line 378 | Line 466 | public class PriorityQueueTest extends J
466      }
467  
468  
469 <    public void testToString(){
469 >    /**
470 >     * toString contains toStrings of elements
471 >     */
472 >    public void testToString() {
473          PriorityQueue q = populatedQueue(SIZE);
474          String s = q.toString();
475          for (int i = 0; i < SIZE; ++i) {
# Line 386 | Line 477 | public class PriorityQueueTest extends J
477          }
478      }        
479  
480 +    /**
481 +     * A deserialized serialized queue has same elements
482 +     */
483      public void testSerialization() {
484          PriorityQueue q = populatedQueue(SIZE);
485          try {
# Line 401 | Line 495 | public class PriorityQueueTest extends J
495              while (!q.isEmpty())
496                  assertEquals(q.remove(), r.remove());
497          } catch(Exception e){
498 <            fail("unexpected exception");
498 >            unexpectedException();
499          }
500      }
501   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines