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.9 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.16 by jsr166, Wed Aug 25 00:07:03 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);
19 >        return new TestSuite(PriorityQueueTest.class);
20      }
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 36 | Line 32 | public class PriorityQueueTest extends J
32      private PriorityQueue populatedQueue(int n) {
33          PriorityQueue q = new PriorityQueue(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  
# 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 166 | 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 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       * addAll of a collection with null elements throws NPE
# Line 238 | Line 222 | public class PriorityQueueTest extends J
222              Integer[] ints = new Integer[SIZE];
223              q.addAll(Arrays.asList(ints));
224              shouldThrow();
225 <        }
242 <        catch (NullPointerException success) {}
225 >        } catch (NullPointerException success) {}
226      }
227      /**
228       * addAll of a collection with any null elements throws NPE after
# Line 253 | Line 236 | public class PriorityQueueTest extends J
236                  ints[i] = new Integer(i);
237              q.addAll(Arrays.asList(ints));
238              shouldThrow();
239 <        }
257 <        catch (NullPointerException success) {}
239 >        } catch (NullPointerException success) {}
240      }
241  
242      /**
243       * Queue contains all elements of successful addAll
244       */
245      public void testAddAll5() {
246 <        try {
247 <            Integer[] empty = new Integer[0];
248 <            Integer[] ints = new Integer[SIZE];
249 <            for (int i = 0; i < SIZE; ++i)
250 <                ints[i] = new Integer(SIZE-1-i);
251 <            PriorityQueue q = new PriorityQueue(SIZE);
252 <            assertFalse(q.addAll(Arrays.asList(empty)));
253 <            assertTrue(q.addAll(Arrays.asList(ints)));
254 <            for (int i = 0; i < SIZE; ++i)
273 <                assertEquals(new Integer(i), q.poll());
274 <        }
275 <        finally {}
246 >        Integer[] empty = new Integer[0];
247 >        Integer[] ints = new Integer[SIZE];
248 >        for (int i = 0; i < SIZE; ++i)
249 >            ints[i] = new Integer(SIZE-1-i);
250 >        PriorityQueue q = new PriorityQueue(SIZE);
251 >        assertFalse(q.addAll(Arrays.asList(empty)));
252 >        assertTrue(q.addAll(Arrays.asList(ints)));
253 >        for (int i = 0; i < SIZE; ++i)
254 >            assertEquals(new Integer(i), q.poll());
255      }
256  
257      /**
# Line 281 | Line 260 | public class PriorityQueueTest extends J
260      public void testPoll() {
261          PriorityQueue q = populatedQueue(SIZE);
262          for (int i = 0; i < SIZE; ++i) {
263 <            assertEquals(i, ((Integer)q.poll()).intValue());
263 >            assertEquals(i, q.poll());
264          }
265 <        assertNull(q.poll());
265 >        assertNull(q.poll());
266      }
267  
268      /**
# Line 292 | Line 271 | public class PriorityQueueTest extends J
271      public void testPeek() {
272          PriorityQueue q = populatedQueue(SIZE);
273          for (int i = 0; i < SIZE; ++i) {
274 <            assertEquals(i, ((Integer)q.peek()).intValue());
275 <            q.poll();
274 >            assertEquals(i, q.peek());
275 >            assertEquals(i, q.poll());
276              assertTrue(q.peek() == null ||
277 <                       i != ((Integer)q.peek()).intValue());
277 >                       !q.peek().equals(i));
278          }
279 <        assertNull(q.peek());
279 >        assertNull(q.peek());
280      }
281  
282      /**
# Line 306 | Line 285 | public class PriorityQueueTest extends J
285      public void testElement() {
286          PriorityQueue q = populatedQueue(SIZE);
287          for (int i = 0; i < SIZE; ++i) {
288 <            assertEquals(i, ((Integer)q.element()).intValue());
289 <            q.poll();
288 >            assertEquals(i, q.element());
289 >            assertEquals(i, q.poll());
290          }
291          try {
292              q.element();
293              shouldThrow();
294 <        }
316 <        catch (NoSuchElementException success) {}
294 >        } catch (NoSuchElementException success) {}
295      }
296  
297      /**
# Line 322 | Line 300 | public class PriorityQueueTest extends J
300      public void testRemove() {
301          PriorityQueue q = populatedQueue(SIZE);
302          for (int i = 0; i < SIZE; ++i) {
303 <            assertEquals(i, ((Integer)q.remove()).intValue());
303 >            assertEquals(i, q.remove());
304          }
305          try {
306              q.remove();
307              shouldThrow();
308 <        } catch (NoSuchElementException success){
331 <        }
308 >        } catch (NoSuchElementException success) {}
309      }
310  
311      /**
# Line 426 | Line 403 | public class PriorityQueueTest extends J
403       */
404      public void testToArray() {
405          PriorityQueue q = populatedQueue(SIZE);
406 <        Object[] o = q.toArray();
406 >        Object[] o = q.toArray();
407          Arrays.sort(o);
408 <        for(int i = 0; i < o.length; i++)
409 <            assertEquals(o[i], q.poll());
408 >        for (int i = 0; i < o.length; i++)
409 >            assertEquals(o[i], q.poll());
410      }
411  
412      /**
# Line 437 | Line 414 | public class PriorityQueueTest extends J
414       */
415      public void testToArray2() {
416          PriorityQueue q = populatedQueue(SIZE);
417 <        Integer[] ints = new Integer[SIZE];
418 <        ints = (Integer[])q.toArray(ints);
417 >        Integer[] ints = new Integer[SIZE];
418 >        ints = (Integer[])q.toArray(ints);
419          Arrays.sort(ints);
420 <        for(int i = 0; i < ints.length; i++)
420 >        for (int i = 0; i < ints.length; i++)
421              assertEquals(ints[i], q.poll());
422      }
423  
# Line 450 | Line 427 | public class PriorityQueueTest extends J
427      public void testIterator() {
428          PriorityQueue q = populatedQueue(SIZE);
429          int i = 0;
430 <        Iterator it = q.iterator();
431 <        while(it.hasNext()) {
430 >        Iterator it = q.iterator();
431 >        while (it.hasNext()) {
432              assertTrue(q.contains(it.next()));
433              ++i;
434          }
# Line 461 | Line 438 | public class PriorityQueueTest extends J
438      /**
439       * iterator.remove removes current element
440       */
441 <    public void testIteratorRemove () {
441 >    public void testIteratorRemove() {
442          final PriorityQueue q = new PriorityQueue(3);
443          q.add(new Integer(2));
444          q.add(new Integer(1));
# Line 492 | Line 469 | public class PriorityQueueTest extends J
469      /**
470       * A deserialized serialized queue has same elements
471       */
472 <    public void testSerialization() {
472 >    public void testSerialization() throws Exception {
473          PriorityQueue q = populatedQueue(SIZE);
474 <        try {
475 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
476 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
477 <            out.writeObject(q);
478 <            out.close();
479 <
480 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
481 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
482 <            PriorityQueue r = (PriorityQueue)in.readObject();
483 <            assertEquals(q.size(), r.size());
484 <            while (!q.isEmpty())
508 <                assertEquals(q.remove(), r.remove());
509 <        } catch(Exception e){
510 <            unexpectedException();
511 <        }
474 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
475 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
476 >        out.writeObject(q);
477 >        out.close();
478 >
479 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
480 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
481 >        PriorityQueue r = (PriorityQueue)in.readObject();
482 >        assertEquals(q.size(), r.size());
483 >        while (!q.isEmpty())
484 >            assertEquals(q.remove(), r.remove());
485      }
486   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines