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.12 by jsr166, Sat Nov 21 02:07:27 2009 UTC vs.
Revision 1.17 by jsr166, Wed Aug 25 01:44:48 2010 UTC

# Line 13 | 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);
# Line 21 | Line 21 | public class PriorityQueueTest extends J
21  
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();
26 <            if (i < j) return 1;
27 <            if (i > j) return -1;
28 <            return 0;
24 >            return ((Comparable)y).compareTo(x);
25          }
26      }
27  
# Line 53 | 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 <        }
63 <        catch (IllegalArgumentException success) {}
58 >        } catch (IllegalArgumentException success) {}
59      }
60  
61      /**
# Line 70 | Line 65 | public class PriorityQueueTest extends J
65          try {
66              PriorityQueue q = new PriorityQueue((Collection)null);
67              shouldThrow();
68 <        }
74 <        catch (NullPointerException success) {}
68 >        } catch (NullPointerException success) {}
69      }
70  
71      /**
# Line 82 | Line 76 | public class PriorityQueueTest extends J
76              Integer[] ints = new Integer[SIZE];
77              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
78              shouldThrow();
79 <        }
86 <        catch (NullPointerException success) {}
79 >        } catch (NullPointerException success) {}
80      }
81  
82      /**
# Line 96 | Line 89 | public class PriorityQueueTest extends J
89                  ints[i] = new Integer(i);
90              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
91              shouldThrow();
92 <        }
100 <        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)
113 <                assertEquals(ints[i], q.poll());
114 <        }
115 <        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)
131 <                assertEquals(ints[i], q.poll());
132 <        }
133 <        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 170 | Line 156 | public class PriorityQueueTest extends J
156              PriorityQueue q = new PriorityQueue(1);
157              q.offer(null);
158              shouldThrow();
159 <        } catch (NullPointerException success) { }
159 >        } catch (NullPointerException success) {}
160      }
161  
162      /**
# Line 181 | Line 167 | public class PriorityQueueTest extends J
167              PriorityQueue q = new PriorityQueue(1);
168              q.add(null);
169              shouldThrow();
170 <        } catch (NullPointerException success) { }
170 >        } catch (NullPointerException success) {}
171      }
172  
173      /**
# Line 203 | Line 189 | public class PriorityQueueTest extends J
189              q.offer(new Object());
190              q.offer(new Object());
191              shouldThrow();
192 <        }
207 <        catch (ClassCastException success) {}
192 >        } catch (ClassCastException success) {}
193      }
194  
195      /**
# Line 226 | Line 211 | public class PriorityQueueTest extends J
211              PriorityQueue q = new PriorityQueue(1);
212              q.addAll(null);
213              shouldThrow();
214 <        }
230 <        catch (NullPointerException success) {}
214 >        } catch (NullPointerException success) {}
215      }
216 +
217      /**
218       * addAll of a collection with null elements throws NPE
219       */
# Line 238 | Line 223 | public class PriorityQueueTest extends J
223              Integer[] ints = new Integer[SIZE];
224              q.addAll(Arrays.asList(ints));
225              shouldThrow();
226 <        }
242 <        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 253 | Line 238 | public class PriorityQueueTest extends J
238                  ints[i] = new Integer(i);
239              q.addAll(Arrays.asList(ints));
240              shouldThrow();
241 <        }
257 <        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)
273 <                assertEquals(new Integer(i), q.poll());
274 <        }
275 <        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 281 | 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());
268      }
# Line 292 | 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());
282      }
# Line 306 | 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 <        }
316 <        catch (NoSuchElementException success) {}
296 >        } catch (NoSuchElementException success) {}
297      }
298  
299      /**
# Line 322 | 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) {
331 <        }
310 >        } catch (NoSuchElementException success) {}
311      }
312  
313      /**
# Line 461 | Line 440 | public class PriorityQueueTest extends J
440      /**
441       * iterator.remove removes current element
442       */
443 <    public void testIteratorRemove () {
443 >    public void testIteratorRemove() {
444          final PriorityQueue q = new PriorityQueue(3);
445          q.add(new Integer(2));
446          q.add(new Integer(1));
# Line 492 | Line 471 | public class PriorityQueueTest extends J
471      /**
472       * A deserialized serialized queue has same elements
473       */
474 <    public void testSerialization() {
474 >    public void testSerialization() throws Exception {
475          PriorityQueue q = populatedQueue(SIZE);
476 <        try {
477 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
478 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
479 <            out.writeObject(q);
480 <            out.close();
481 <
482 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
483 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
484 <            PriorityQueue r = (PriorityQueue)in.readObject();
485 <            assertEquals(q.size(), r.size());
486 <            while (!q.isEmpty())
508 <                assertEquals(q.remove(), r.remove());
509 <        } catch (Exception e) {
510 <            unexpectedException();
511 <        }
476 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
477 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
478 >        out.writeObject(q);
479 >        out.close();
480 >
481 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
482 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
483 >        PriorityQueue r = (PriorityQueue)in.readObject();
484 >        assertEquals(q.size(), r.size());
485 >        while (!q.isEmpty())
486 >            assertEquals(q.remove(), r.remove());
487      }
488   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines