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.7 by dl, Sun Oct 5 23:00:40 2003 UTC vs.
Revision 1.14 by jsr166, Sat Nov 21 10:29:50 2009 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 12 | Line 13 | import java.io.*;
13  
14   public class PriorityQueueTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());  
16 >        junit.textui.TestRunner.run (suite());
17      }
18      public static Test suite() {
19 <        return new TestSuite(PriorityQueueTest.class);
19 >        return new TestSuite(PriorityQueueTest.class);
20      }
21  
22 <    static class MyReverseComparator implements Comparator {
22 >    static class MyReverseComparator implements Comparator {
23          public int compare(Object x, Object y) {
24              int i = ((Integer)x).intValue();
25              int j = ((Integer)y).intValue();
# Line 35 | Line 36 | public class PriorityQueueTest extends J
36      private PriorityQueue populatedQueue(int n) {
37          PriorityQueue q = new PriorityQueue(n);
38          assertTrue(q.isEmpty());
39 <        for(int i = n-1; i >= 0; i-=2)
40 <            assertTrue(q.offer(new Integer(i)));
41 <        for(int i = (n & 1); i < n; i+=2)
42 <            assertTrue(q.offer(new Integer(i)));
39 >        for (int i = n-1; i >= 0; i-=2)
40 >            assertTrue(q.offer(new Integer(i)));
41 >        for (int i = (n & 1); i < n; i+=2)
42 >            assertTrue(q.offer(new Integer(i)));
43          assertFalse(q.isEmpty());
44 <        assertEquals(n, q.size());
44 >        assertEquals(n, q.size());
45          return q;
46      }
47 <
47 >
48      /**
49       * A new queue has unbounded capacity
50       */
# Line 52 | Line 53 | public class PriorityQueueTest extends J
53      }
54  
55      /**
56 <     * Constructor throws IAE if  capacity argument nonpositive
56 >     * Constructor throws IAE if capacity argument nonpositive
57       */
58      public void testConstructor2() {
59          try {
60              PriorityQueue q = new PriorityQueue(0);
61              shouldThrow();
62 <        }
62 <        catch (IllegalArgumentException success) {}
62 >        } catch (IllegalArgumentException success) {}
63      }
64  
65      /**
# Line 69 | Line 69 | public class PriorityQueueTest extends J
69          try {
70              PriorityQueue q = new PriorityQueue((Collection)null);
71              shouldThrow();
72 <        }
73 <        catch (NullPointerException success) {}
72 >        } catch (NullPointerException success) {}
73      }
74  
75      /**
# Line 81 | Line 80 | public class PriorityQueueTest extends J
80              Integer[] ints = new Integer[SIZE];
81              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
82              shouldThrow();
83 <        }
85 <        catch (NullPointerException success) {}
83 >        } catch (NullPointerException success) {}
84      }
85  
86      /**
# Line 95 | Line 93 | public class PriorityQueueTest extends J
93                  ints[i] = new Integer(i);
94              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
95              shouldThrow();
96 <        }
99 <        catch (NullPointerException success) {}
96 >        } catch (NullPointerException success) {}
97      }
98  
99      /**
100       * Queue contains all elements of collection used to initialize
101       */
102      public void testConstructor6() {
103 <        try {
104 <            Integer[] ints = new Integer[SIZE];
105 <            for (int i = 0; i < SIZE; ++i)
106 <                ints[i] = new Integer(i);
107 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
108 <            for (int i = 0; i < SIZE; ++i)
112 <                assertEquals(ints[i], q.poll());
113 <        }
114 <        finally {}
103 >        Integer[] ints = new Integer[SIZE];
104 >        for (int i = 0; i < SIZE; ++i)
105 >            ints[i] = new Integer(i);
106 >        PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
107 >        for (int i = 0; i < SIZE; ++i)
108 >            assertEquals(ints[i], q.poll());
109      }
110  
111      /**
112       * The comparator used in constructor is used
113       */
114      public void testConstructor7() {
115 <        try {
116 <            MyReverseComparator cmp = new MyReverseComparator();
117 <            PriorityQueue q = new PriorityQueue(SIZE, cmp);
118 <            assertEquals(cmp, q.comparator());
119 <            Integer[] ints = new Integer[SIZE];
120 <            for (int i = 0; i < SIZE; ++i)
121 <                ints[i] = new Integer(i);
122 <            q.addAll(Arrays.asList(ints));
123 <            for (int i = SIZE-1; i >= 0; --i)
130 <                assertEquals(ints[i], q.poll());
131 <        }
132 <        finally {}
115 >        MyReverseComparator cmp = new MyReverseComparator();
116 >        PriorityQueue q = new PriorityQueue(SIZE, cmp);
117 >        assertEquals(cmp, q.comparator());
118 >        Integer[] ints = new Integer[SIZE];
119 >        for (int i = 0; i < SIZE; ++i)
120 >            ints[i] = new Integer(i);
121 >        q.addAll(Arrays.asList(ints));
122 >        for (int i = SIZE-1; i >= 0; --i)
123 >            assertEquals(ints[i], q.poll());
124      }
125  
126      /**
# Line 165 | Line 156 | public class PriorityQueueTest extends J
156       * offer(null) throws NPE
157       */
158      public void testOfferNull() {
159 <        try {
159 >        try {
160              PriorityQueue q = new PriorityQueue(1);
161              q.offer(null);
162              shouldThrow();
163 <        } catch (NullPointerException success) { }  
163 >        } catch (NullPointerException success) {}
164      }
165  
166      /**
167       * add(null) throws NPE
168       */
169      public void testAddNull() {
170 <        try {
170 >        try {
171              PriorityQueue q = new PriorityQueue(1);
172              q.add(null);
173              shouldThrow();
174 <        } catch (NullPointerException success) { }  
174 >        } catch (NullPointerException success) {}
175      }
176  
177      /**
# Line 202 | Line 193 | public class PriorityQueueTest extends J
193              q.offer(new Object());
194              q.offer(new Object());
195              shouldThrow();
196 <        }
206 <        catch(ClassCastException success) {}
196 >        } catch (ClassCastException success) {}
197      }
198  
199      /**
# Line 225 | Line 215 | public class PriorityQueueTest extends J
215              PriorityQueue q = new PriorityQueue(1);
216              q.addAll(null);
217              shouldThrow();
218 <        }
229 <        catch (NullPointerException success) {}
218 >        } catch (NullPointerException success) {}
219      }
220      /**
221       * addAll of a collection with null elements throws NPE
# Line 237 | Line 226 | public class PriorityQueueTest extends J
226              Integer[] ints = new Integer[SIZE];
227              q.addAll(Arrays.asList(ints));
228              shouldThrow();
229 <        }
241 <        catch (NullPointerException success) {}
229 >        } catch (NullPointerException success) {}
230      }
231      /**
232       * addAll of a collection with any null elements throws NPE after
# Line 252 | Line 240 | public class PriorityQueueTest extends J
240                  ints[i] = new Integer(i);
241              q.addAll(Arrays.asList(ints));
242              shouldThrow();
243 <        }
256 <        catch (NullPointerException success) {}
243 >        } catch (NullPointerException success) {}
244      }
245  
246      /**
247       * Queue contains all elements of successful addAll
248       */
249      public void testAddAll5() {
250 <        try {
251 <            Integer[] empty = new Integer[0];
252 <            Integer[] ints = new Integer[SIZE];
253 <            for (int i = 0; i < SIZE; ++i)
254 <                ints[i] = new Integer(SIZE-1-i);
255 <            PriorityQueue q = new PriorityQueue(SIZE);
256 <            assertFalse(q.addAll(Arrays.asList(empty)));
257 <            assertTrue(q.addAll(Arrays.asList(ints)));
258 <            for (int i = 0; i < SIZE; ++i)
272 <                assertEquals(new Integer(i), q.poll());
273 <        }
274 <        finally {}
250 >        Integer[] empty = new Integer[0];
251 >        Integer[] ints = new Integer[SIZE];
252 >        for (int i = 0; i < SIZE; ++i)
253 >            ints[i] = new Integer(SIZE-1-i);
254 >        PriorityQueue q = new PriorityQueue(SIZE);
255 >        assertFalse(q.addAll(Arrays.asList(empty)));
256 >        assertTrue(q.addAll(Arrays.asList(ints)));
257 >        for (int i = 0; i < SIZE; ++i)
258 >            assertEquals(new Integer(i), q.poll());
259      }
260  
261      /**
# Line 282 | Line 266 | public class PriorityQueueTest extends J
266          for (int i = 0; i < SIZE; ++i) {
267              assertEquals(i, ((Integer)q.poll()).intValue());
268          }
269 <        assertNull(q.poll());
269 >        assertNull(q.poll());
270      }
271  
272      /**
# Line 296 | Line 280 | public class PriorityQueueTest extends J
280              assertTrue(q.peek() == null ||
281                         i != ((Integer)q.peek()).intValue());
282          }
283 <        assertNull(q.peek());
283 >        assertNull(q.peek());
284      }
285  
286      /**
# Line 311 | Line 295 | public class PriorityQueueTest extends J
295          try {
296              q.element();
297              shouldThrow();
298 <        }
315 <        catch (NoSuchElementException success) {}
298 >        } catch (NoSuchElementException success) {}
299      }
300  
301      /**
# Line 326 | Line 309 | public class PriorityQueueTest extends J
309          try {
310              q.remove();
311              shouldThrow();
312 <        } catch (NoSuchElementException success){
330 <        }  
312 >        } catch (NoSuchElementException success) {}
313      }
314  
315      /**
# Line 344 | Line 326 | public class PriorityQueueTest extends J
326          }
327          assertTrue(q.isEmpty());
328      }
329 <        
329 >
330      /**
331       * contains(x) reports true when elements added but not yet removed
332       */
# Line 425 | Line 407 | public class PriorityQueueTest extends J
407       */
408      public void testToArray() {
409          PriorityQueue q = populatedQueue(SIZE);
410 <        Object[] o = q.toArray();
410 >        Object[] o = q.toArray();
411          Arrays.sort(o);
412 <        for(int i = 0; i < o.length; i++)
413 <            assertEquals(o[i], q.poll());
412 >        for (int i = 0; i < o.length; i++)
413 >            assertEquals(o[i], q.poll());
414      }
415  
416      /**
# Line 436 | Line 418 | public class PriorityQueueTest extends J
418       */
419      public void testToArray2() {
420          PriorityQueue q = populatedQueue(SIZE);
421 <        Integer[] ints = new Integer[SIZE];
422 <        ints = (Integer[])q.toArray(ints);
421 >        Integer[] ints = new Integer[SIZE];
422 >        ints = (Integer[])q.toArray(ints);
423          Arrays.sort(ints);
424 <        for(int i = 0; i < ints.length; i++)
424 >        for (int i = 0; i < ints.length; i++)
425              assertEquals(ints[i], q.poll());
426      }
427 <    
427 >
428      /**
429       * iterator iterates through all elements
430       */
431      public void testIterator() {
432          PriorityQueue q = populatedQueue(SIZE);
433          int i = 0;
434 <        Iterator it = q.iterator();
435 <        while(it.hasNext()) {
434 >        Iterator it = q.iterator();
435 >        while (it.hasNext()) {
436              assertTrue(q.contains(it.next()));
437              ++i;
438          }
# Line 486 | Line 468 | public class PriorityQueueTest extends J
468          for (int i = 0; i < SIZE; ++i) {
469              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
470          }
471 <    }        
471 >    }
472  
473      /**
474 <     * A deserialized serialized queue has same elements
474 >     * A deserialized serialized queue has same elements
475       */
476 <    public void testSerialization() {
476 >    public void testSerialization() throws Exception {
477          PriorityQueue q = populatedQueue(SIZE);
478 <        try {
479 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
480 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
481 <            out.writeObject(q);
482 <            out.close();
483 <
484 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
485 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
486 <            PriorityQueue r = (PriorityQueue)in.readObject();
487 <            assertEquals(q.size(), r.size());
488 <            while (!q.isEmpty())
507 <                assertEquals(q.remove(), r.remove());
508 <        } catch(Exception e){
509 <            unexpectedException();
510 <        }
478 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
479 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
480 >        out.writeObject(q);
481 >        out.close();
482 >
483 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
484 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
485 >        PriorityQueue r = (PriorityQueue)in.readObject();
486 >        assertEquals(q.size(), r.size());
487 >        while (!q.isEmpty())
488 >            assertEquals(q.remove(), r.remove());
489      }
490   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines