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.21 by jsr166, Tue Mar 15 19:47:07 2011 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/publicdomain/zero/1.0/
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();
24 <            int j = ((Integer)y).intValue();
25 <            if (i < j) return 1;
26 <            if (i > j) return -1;
27 <            return 0;
24 >            return ((Comparable)y).compareTo(x);
25          }
26      }
27  
# Line 32 | Line 29 | public class PriorityQueueTest extends J
29       * Create a queue of given size containing consecutive
30       * Integers 0 ... n.
31       */
32 <    private PriorityQueue populatedQueue(int n) {
33 <        PriorityQueue q = new PriorityQueue(n);
32 >    private PriorityQueue<Integer> populatedQueue(int n) {
33 >        PriorityQueue<Integer> q = new PriorityQueue<Integer>(n);
34          assertTrue(q.isEmpty());
35 <        for(int i = n-1; i >= 0; i-=2)
36 <            assertTrue(q.offer(new Integer(i)));
37 <        for(int i = (n & 1); i < n; i+=2)
38 <            assertTrue(q.offer(new Integer(i)));
35 >        for (int i = n-1; i >= 0; i-=2)
36 >            assertTrue(q.offer(new Integer(i)));
37 >        for (int i = (n & 1); i < n; i+=2)
38 >            assertTrue(q.offer(new Integer(i)));
39          assertFalse(q.isEmpty());
40 <        assertEquals(n, q.size());
40 >        assertEquals(n, q.size());
41          return q;
42      }
43 <
43 >
44      /**
45       * A new queue has unbounded capacity
46       */
# Line 52 | Line 49 | public class PriorityQueueTest extends J
49      }
50  
51      /**
52 <     * Constructor throws IAE if  capacity argument nonpositive
52 >     * Constructor throws IAE if capacity argument nonpositive
53       */
54      public void testConstructor2() {
55          try {
56              PriorityQueue q = new PriorityQueue(0);
57              shouldThrow();
58 <        }
62 <        catch (IllegalArgumentException success) {}
58 >        } catch (IllegalArgumentException success) {}
59      }
60  
61      /**
# Line 69 | Line 65 | public class PriorityQueueTest extends J
65          try {
66              PriorityQueue q = new PriorityQueue((Collection)null);
67              shouldThrow();
68 <        }
73 <        catch (NullPointerException success) {}
68 >        } catch (NullPointerException success) {}
69      }
70  
71      /**
# Line 81 | Line 76 | public class PriorityQueueTest extends J
76              Integer[] ints = new Integer[SIZE];
77              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
78              shouldThrow();
79 <        }
85 <        catch (NullPointerException success) {}
79 >        } catch (NullPointerException success) {}
80      }
81  
82      /**
# Line 95 | Line 89 | public class PriorityQueueTest extends J
89                  ints[i] = new Integer(i);
90              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
91              shouldThrow();
92 <        }
99 <        catch (NullPointerException success) {}
92 >        } catch (NullPointerException success) {}
93      }
94  
95      /**
96       * Queue contains all elements of collection used to initialize
97       */
98      public void testConstructor6() {
99 <        try {
100 <            Integer[] ints = new Integer[SIZE];
101 <            for (int i = 0; i < SIZE; ++i)
102 <                ints[i] = new Integer(i);
103 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
104 <            for (int i = 0; i < SIZE; ++i)
112 <                assertEquals(ints[i], q.poll());
113 <        }
114 <        finally {}
99 >        Integer[] ints = new Integer[SIZE];
100 >        for (int i = 0; i < SIZE; ++i)
101 >            ints[i] = new Integer(i);
102 >        PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
103 >        for (int i = 0; i < SIZE; ++i)
104 >            assertEquals(ints[i], q.poll());
105      }
106  
107      /**
108       * The comparator used in constructor is used
109       */
110      public void testConstructor7() {
111 <        try {
112 <            MyReverseComparator cmp = new MyReverseComparator();
113 <            PriorityQueue q = new PriorityQueue(SIZE, cmp);
114 <            assertEquals(cmp, q.comparator());
115 <            Integer[] ints = new Integer[SIZE];
116 <            for (int i = 0; i < SIZE; ++i)
117 <                ints[i] = new Integer(i);
118 <            q.addAll(Arrays.asList(ints));
119 <            for (int i = SIZE-1; i >= 0; --i)
130 <                assertEquals(ints[i], q.poll());
131 <        }
132 <        finally {}
111 >        MyReverseComparator cmp = new MyReverseComparator();
112 >        PriorityQueue q = new PriorityQueue(SIZE, cmp);
113 >        assertEquals(cmp, q.comparator());
114 >        Integer[] ints = new Integer[SIZE];
115 >        for (int i = 0; i < SIZE; ++i)
116 >            ints[i] = new Integer(i);
117 >        q.addAll(Arrays.asList(ints));
118 >        for (int i = SIZE-1; i >= 0; --i)
119 >            assertEquals(ints[i], q.poll());
120      }
121  
122      /**
# Line 165 | Line 152 | public class PriorityQueueTest extends J
152       * offer(null) throws NPE
153       */
154      public void testOfferNull() {
155 <        try {
155 >        try {
156              PriorityQueue q = new PriorityQueue(1);
157              q.offer(null);
158              shouldThrow();
159 <        } catch (NullPointerException success) { }  
159 >        } catch (NullPointerException success) {}
160      }
161  
162      /**
163       * add(null) throws NPE
164       */
165      public void testAddNull() {
166 <        try {
166 >        try {
167              PriorityQueue q = new PriorityQueue(1);
168              q.add(null);
169              shouldThrow();
170 <        } catch (NullPointerException success) { }  
170 >        } catch (NullPointerException success) {}
171      }
172  
173      /**
# Line 202 | Line 189 | public class PriorityQueueTest extends J
189              q.offer(new Object());
190              q.offer(new Object());
191              shouldThrow();
192 <        }
206 <        catch(ClassCastException success) {}
192 >        } catch (ClassCastException success) {}
193      }
194  
195      /**
# Line 225 | Line 211 | public class PriorityQueueTest extends J
211              PriorityQueue q = new PriorityQueue(1);
212              q.addAll(null);
213              shouldThrow();
214 <        }
229 <        catch (NullPointerException success) {}
214 >        } catch (NullPointerException success) {}
215      }
216 +
217      /**
218       * addAll of a collection with null elements throws NPE
219       */
# Line 237 | Line 223 | public class PriorityQueueTest extends J
223              Integer[] ints = new Integer[SIZE];
224              q.addAll(Arrays.asList(ints));
225              shouldThrow();
226 <        }
241 <        catch (NullPointerException success) {}
226 >        } catch (NullPointerException success) {}
227      }
228 +
229      /**
230       * addAll of a collection with any null elements throws NPE after
231       * possibly adding some elements
# Line 252 | Line 238 | public class PriorityQueueTest extends J
238                  ints[i] = new Integer(i);
239              q.addAll(Arrays.asList(ints));
240              shouldThrow();
241 <        }
256 <        catch (NullPointerException success) {}
241 >        } catch (NullPointerException success) {}
242      }
243  
244      /**
245       * Queue contains all elements of successful addAll
246       */
247      public void testAddAll5() {
248 <        try {
249 <            Integer[] empty = new Integer[0];
250 <            Integer[] ints = new Integer[SIZE];
251 <            for (int i = 0; i < SIZE; ++i)
252 <                ints[i] = new Integer(SIZE-1-i);
253 <            PriorityQueue q = new PriorityQueue(SIZE);
254 <            assertFalse(q.addAll(Arrays.asList(empty)));
255 <            assertTrue(q.addAll(Arrays.asList(ints)));
256 <            for (int i = 0; i < SIZE; ++i)
272 <                assertEquals(new Integer(i), q.poll());
273 <        }
274 <        finally {}
248 >        Integer[] empty = new Integer[0];
249 >        Integer[] ints = new Integer[SIZE];
250 >        for (int i = 0; i < SIZE; ++i)
251 >            ints[i] = new Integer(SIZE-1-i);
252 >        PriorityQueue q = new PriorityQueue(SIZE);
253 >        assertFalse(q.addAll(Arrays.asList(empty)));
254 >        assertTrue(q.addAll(Arrays.asList(ints)));
255 >        for (int i = 0; i < SIZE; ++i)
256 >            assertEquals(new Integer(i), q.poll());
257      }
258  
259      /**
# Line 280 | Line 262 | public class PriorityQueueTest extends J
262      public void testPoll() {
263          PriorityQueue q = populatedQueue(SIZE);
264          for (int i = 0; i < SIZE; ++i) {
265 <            assertEquals(i, ((Integer)q.poll()).intValue());
265 >            assertEquals(i, q.poll());
266          }
267 <        assertNull(q.poll());
267 >        assertNull(q.poll());
268      }
269  
270      /**
# Line 291 | Line 273 | public class PriorityQueueTest extends J
273      public void testPeek() {
274          PriorityQueue q = populatedQueue(SIZE);
275          for (int i = 0; i < SIZE; ++i) {
276 <            assertEquals(i, ((Integer)q.peek()).intValue());
277 <            q.poll();
276 >            assertEquals(i, q.peek());
277 >            assertEquals(i, q.poll());
278              assertTrue(q.peek() == null ||
279 <                       i != ((Integer)q.peek()).intValue());
279 >                       !q.peek().equals(i));
280          }
281 <        assertNull(q.peek());
281 >        assertNull(q.peek());
282      }
283  
284      /**
# Line 305 | Line 287 | public class PriorityQueueTest extends J
287      public void testElement() {
288          PriorityQueue q = populatedQueue(SIZE);
289          for (int i = 0; i < SIZE; ++i) {
290 <            assertEquals(i, ((Integer)q.element()).intValue());
291 <            q.poll();
290 >            assertEquals(i, q.element());
291 >            assertEquals(i, q.poll());
292          }
293          try {
294              q.element();
295              shouldThrow();
296 <        }
315 <        catch (NoSuchElementException success) {}
296 >        } catch (NoSuchElementException success) {}
297      }
298  
299      /**
# Line 321 | Line 302 | public class PriorityQueueTest extends J
302      public void testRemove() {
303          PriorityQueue q = populatedQueue(SIZE);
304          for (int i = 0; i < SIZE; ++i) {
305 <            assertEquals(i, ((Integer)q.remove()).intValue());
305 >            assertEquals(i, q.remove());
306          }
307          try {
308              q.remove();
309              shouldThrow();
310 <        } catch (NoSuchElementException success){
330 <        }  
310 >        } catch (NoSuchElementException success) {}
311      }
312  
313      /**
# Line 336 | Line 316 | public class PriorityQueueTest extends J
316      public void testRemoveElement() {
317          PriorityQueue q = populatedQueue(SIZE);
318          for (int i = 1; i < SIZE; i+=2) {
319 <            assertTrue(q.remove(new Integer(i)));
319 >            assertTrue(q.contains(i));
320 >            assertTrue(q.remove(i));
321 >            assertFalse(q.contains(i));
322 >            assertTrue(q.contains(i-1));
323          }
324          for (int i = 0; i < SIZE; i+=2) {
325 <            assertTrue(q.remove(new Integer(i)));
326 <            assertFalse(q.remove(new Integer(i+1)));
325 >            assertTrue(q.contains(i));
326 >            assertTrue(q.remove(i));
327 >            assertFalse(q.contains(i));
328 >            assertFalse(q.remove(i+1));
329 >            assertFalse(q.contains(i+1));
330          }
331          assertTrue(q.isEmpty());
332      }
333 <        
333 >
334      /**
335       * contains(x) reports true when elements added but not yet removed
336       */
# Line 425 | Line 411 | public class PriorityQueueTest extends J
411       */
412      public void testToArray() {
413          PriorityQueue q = populatedQueue(SIZE);
414 <        Object[] o = q.toArray();
414 >        Object[] o = q.toArray();
415          Arrays.sort(o);
416 <        for(int i = 0; i < o.length; i++)
417 <            assertEquals(o[i], q.poll());
416 >        for (int i = 0; i < o.length; i++)
417 >            assertSame(o[i], q.poll());
418      }
419  
420      /**
421       * toArray(a) contains all elements
422       */
423      public void testToArray2() {
424 <        PriorityQueue q = populatedQueue(SIZE);
425 <        Integer[] ints = new Integer[SIZE];
426 <        ints = (Integer[])q.toArray(ints);
424 >        PriorityQueue<Integer> q = populatedQueue(SIZE);
425 >        Integer[] ints = new Integer[SIZE];
426 >        Integer[] array = q.toArray(ints);
427 >        assertSame(ints, array);
428          Arrays.sort(ints);
429 <        for(int i = 0; i < ints.length; i++)
430 <            assertEquals(ints[i], q.poll());
429 >        for (int i = 0; i < ints.length; i++)
430 >            assertSame(ints[i], q.poll());
431      }
432 <    
432 >
433      /**
434       * iterator iterates through all elements
435       */
436      public void testIterator() {
437          PriorityQueue q = populatedQueue(SIZE);
438          int i = 0;
439 <        Iterator it = q.iterator();
440 <        while(it.hasNext()) {
439 >        Iterator it = q.iterator();
440 >        while (it.hasNext()) {
441              assertTrue(q.contains(it.next()));
442              ++i;
443          }
# Line 460 | Line 447 | public class PriorityQueueTest extends J
447      /**
448       * iterator.remove removes current element
449       */
450 <    public void testIteratorRemove () {
450 >    public void testIteratorRemove() {
451          final PriorityQueue q = new PriorityQueue(3);
452          q.add(new Integer(2));
453          q.add(new Integer(1));
# Line 486 | Line 473 | public class PriorityQueueTest extends J
473          for (int i = 0; i < SIZE; ++i) {
474              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
475          }
476 <    }        
476 >    }
477  
478      /**
479 <     * A deserialized serialized queue has same elements
479 >     * A deserialized serialized queue has same elements
480       */
481 <    public void testSerialization() {
481 >    public void testSerialization() throws Exception {
482          PriorityQueue q = populatedQueue(SIZE);
483 <        try {
484 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
485 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
486 <            out.writeObject(q);
487 <            out.close();
488 <
489 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
490 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
491 <            PriorityQueue r = (PriorityQueue)in.readObject();
492 <            assertEquals(q.size(), r.size());
493 <            while (!q.isEmpty())
507 <                assertEquals(q.remove(), r.remove());
508 <        } catch(Exception e){
509 <            unexpectedException();
510 <        }
483 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
484 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
485 >        out.writeObject(q);
486 >        out.close();
487 >
488 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
489 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
490 >        PriorityQueue r = (PriorityQueue)in.readObject();
491 >        assertEquals(q.size(), r.size());
492 >        while (!q.isEmpty())
493 >            assertEquals(q.remove(), r.remove());
494      }
495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines