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.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.7 by dl, Sun Oct 5 23:00:40 2003 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11 + import java.io.*;
12  
13 < public class PriorityQueueTest extends TestCase {
13 <
14 <    private static final int N = 10;
15 <    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;
18 <
13 > public class PriorityQueueTest extends JSR166TestCase {
14      public static void main(String[] args) {
15          junit.textui.TestRunner.run (suite());  
16      }
22
17      public static Test suite() {
18          return new TestSuite(PriorityQueueTest.class);
19      }
# Line 34 | Line 28 | public class PriorityQueueTest extends T
28          }
29      }
30  
37
31      /**
32       * Create a queue of given size containing consecutive
33       * Integers 0 ... n.
34       */
35 <    private PriorityQueue fullQueue(int n) {
35 >    private PriorityQueue populatedQueue(int n) {
36          PriorityQueue q = new PriorityQueue(n);
37          assertTrue(q.isEmpty());
38          for(int i = n-1; i >= 0; i-=2)
# Line 51 | Line 44 | public class PriorityQueueTest extends T
44          return q;
45      }
46  
47 <    public void testConstructor1(){
48 <        assertEquals(0, new PriorityQueue(N).size());
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() {
67
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[N];
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[N];
94 <            for (int i = 0; i < N-1; ++i)
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[N];
108 <            for (int i = 0; i < N; ++i)
107 >            Integer[] ints = new Integer[SIZE];
108 >            for (int i = 0; i < SIZE; ++i)
109                  ints[i] = new Integer(i);
110              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
111 <            for (int i = 0; i < N; ++i)
111 >            for (int i = 0; i < SIZE; ++i)
112                  assertEquals(ints[i], q.poll());
113          }
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(N, new MyReverseComparator());
123 <            Integer[] ints = new Integer[N];
124 <            for (int i = 0; i < N; ++i)
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);
128              q.addAll(Arrays.asList(ints));
129 <            for (int i = N-1; i >= 0; --i)
129 >            for (int i = SIZE-1; i >= 0; --i)
130                  assertEquals(ints[i], q.poll());
131          }
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 128 | Line 146 | public class PriorityQueueTest extends T
146          assertTrue(q.isEmpty());
147      }
148  
149 +    /**
150 +     * size changes when elements added and removed
151 +     */
152      public void testSize() {
153 <        PriorityQueue q = fullQueue(N);
154 <        for (int i = 0; i < N; ++i) {
155 <            assertEquals(N-i, q.size());
153 >        PriorityQueue q = populatedQueue(SIZE);
154 >        for (int i = 0; i < SIZE; ++i) {
155 >            assertEquals(SIZE-i, q.size());
156              q.remove();
157          }
158 <        for (int i = 0; i < N; ++i) {
158 >        for (int i = 0; i < SIZE; ++i) {
159              assertEquals(i, q.size());
160              q.add(new Integer(i));
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(){
210 <        PriorityQueue q = new PriorityQueue(N);
211 <        for (int i = 0; i < N; ++i) {
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());
216              assertTrue(q.add(new Integer(i)));
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(N);
237 <            Integer[] ints = new Integer[N];
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(N);
250 <            Integer[] ints = new Integer[N];
251 <            for (int i = 0; i < N-1; ++i)
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[N];
266 <            for (int i = 0; i < N; ++i)
267 <                ints[i] = new Integer(N-1-i);
268 <            PriorityQueue q = new PriorityQueue(N);
265 >            Integer[] ints = new Integer[SIZE];
266 >            for (int i = 0; i < SIZE; ++i)
267 >                ints[i] = new Integer(SIZE-1-i);
268 >            PriorityQueue q = new PriorityQueue(SIZE);
269              assertFalse(q.addAll(Arrays.asList(empty)));
270              assertTrue(q.addAll(Arrays.asList(ints)));
271 <            for (int i = 0; i < N; ++i)
271 >            for (int i = 0; i < SIZE; ++i)
272                  assertEquals(new Integer(i), q.poll());
273          }
274          finally {}
275      }
276  
277 <    public void testPoll(){
278 <        PriorityQueue q = fullQueue(N);
279 <        for (int i = 0; i < N; ++i) {
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());
284          }
285          assertNull(q.poll());
286      }
287  
288 <    public void testPeek(){
289 <        PriorityQueue q = fullQueue(N);
290 <        for (int i = 0; i < N; ++i) {
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());
295              q.poll();
296              assertTrue(q.peek() == null ||
# Line 236 | Line 299 | public class PriorityQueueTest extends T
299          assertNull(q.peek());
300      }
301  
302 <    public void testElement(){
303 <        PriorityQueue q = fullQueue(N);
304 <        for (int i = 0; i < N; ++i) {
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());
309              q.poll();
310          }
311          try {
312              q.element();
313 <            fail("no such element");
313 >            shouldThrow();
314          }
315          catch (NoSuchElementException success) {}
316      }
317  
318 <    public void testRemove(){
319 <        PriorityQueue q = fullQueue(N);
320 <        for (int i = 0; i < N; ++i) {
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(){
334 <        PriorityQueue q = fullQueue(N);
335 <        for (int i = 1; i < N; i+=2) {
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)));
340          }
341 <        for (int i = 0; i < N; i+=2) {
341 >        for (int i = 0; i < SIZE; i+=2) {
342              assertTrue(q.remove(new Integer(i)));
343              assertFalse(q.remove(new Integer(i+1)));
344          }
345 <        assert(q.isEmpty());
345 >        assertTrue(q.isEmpty());
346      }
347          
348 <    public void testContains(){
349 <        PriorityQueue q = fullQueue(N);
350 <        for (int i = 0; i < N; ++i) {
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)));
355              q.poll();
356              assertFalse(q.contains(new Integer(i)));
357          }
358      }
359  
360 <    public void testClear(){
361 <        PriorityQueue q = fullQueue(N);
360 >    /**
361 >     * clear removes all elements
362 >     */
363 >    public void testClear() {
364 >        PriorityQueue q = populatedQueue(SIZE);
365          q.clear();
366          assertTrue(q.isEmpty());
367          assertEquals(0, q.size());
# Line 293 | Line 371 | public class PriorityQueueTest extends T
371          assertTrue(q.isEmpty());
372      }
373  
374 <    public void testContainsAll(){
375 <        PriorityQueue q = fullQueue(N);
376 <        PriorityQueue p = new PriorityQueue(N);
377 <        for (int i = 0; i < N; ++i) {
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) {
381              assertTrue(q.containsAll(p));
382              assertFalse(p.containsAll(q));
383              p.add(new Integer(i));
# Line 304 | Line 385 | public class PriorityQueueTest extends T
385          assertTrue(p.containsAll(q));
386      }
387  
388 <    public void testRetainAll(){
389 <        PriorityQueue q = fullQueue(N);
390 <        PriorityQueue p = fullQueue(N);
391 <        for (int i = 0; i < N; ++i) {
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) {
395              boolean changed = q.retainAll(p);
396              if (i == 0)
397                  assertFalse(changed);
# Line 315 | Line 399 | public class PriorityQueueTest extends T
399                  assertTrue(changed);
400  
401              assertTrue(q.containsAll(p));
402 <            assertEquals(N-i, q.size());
402 >            assertEquals(SIZE-i, q.size());
403              p.remove();
404          }
405      }
406  
407 <    public void testRemoveAll(){
408 <        for (int i = 1; i < N; ++i) {
409 <            PriorityQueue q = fullQueue(N);
410 <            PriorityQueue p = fullQueue(i);
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);
414              assertTrue(q.removeAll(p));
415 <            assertEquals(N-i, q.size());
415 >            assertEquals(SIZE-i, q.size());
416              for (int j = 0; j < i; ++j) {
417                  Integer I = (Integer)(p.remove());
418                  assertFalse(q.contains(I));
# Line 333 | Line 420 | public class PriorityQueueTest extends T
420          }
421      }
422  
423 <    public void testToArray(){
424 <        PriorityQueue q = fullQueue(N);
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);
430          for(int i = 0; i < o.length; i++)
431              assertEquals(o[i], q.poll());
432      }
433  
434 <    public void testToArray2(){
435 <        PriorityQueue q = fullQueue(N);
436 <        Integer[] ints = new Integer[N];
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);
441          Arrays.sort(ints);
442          for(int i = 0; i < ints.length; i++)
443              assertEquals(ints[i], q.poll());
444      }
445      
446 <    public void testIterator(){
447 <        PriorityQueue q = fullQueue(N);
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();
453          while(it.hasNext()) {
454              assertTrue(q.contains(it.next()));
455              ++i;
456          }
457 <        assertEquals(i, N);
457 >        assertEquals(i, SIZE);
458      }
459  
460 +    /**
461 +     * iterator.remove removes current element
462 +     */
463      public void testIteratorRemove () {
365
464          final PriorityQueue q = new PriorityQueue(3);
367
465          q.add(new Integer(2));
466          q.add(new Integer(1));
467          q.add(new Integer(3));
# Line 380 | Line 477 | public class PriorityQueueTest extends T
477      }
478  
479  
480 <    public void testToString(){
481 <        PriorityQueue q = fullQueue(N);
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 < N; ++i) {
486 >        for (int i = 0; i < SIZE; ++i) {
487              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
488          }
489      }        
490  
491 +    /**
492 +     * A deserialized serialized queue has same elements
493 +     */
494 +    public void testSerialization() {
495 +        PriorityQueue q = populatedQueue(SIZE);
496 +        try {
497 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
498 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
499 +            out.writeObject(q);
500 +            out.close();
501 +
502 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
503 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
504 +            PriorityQueue r = (PriorityQueue)in.readObject();
505 +            assertEquals(q.size(), r.size());
506 +            while (!q.isEmpty())
507 +                assertEquals(q.remove(), r.remove());
508 +        } catch(Exception e){
509 +            unexpectedException();
510 +        }
511 +    }
512   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines