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.32 by jsr166, Sat Feb 28 20:13:46 2015 UTC vs.
Revision 1.43 by jsr166, Mon May 29 22:44:27 2017 UTC

# Line 15 | Line 15 | import java.util.PriorityQueue;
15   import java.util.Queue;
16  
17   import junit.framework.Test;
18 import junit.framework.TestSuite;
18  
19   public class PriorityQueueTest extends JSR166TestCase {
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run(suite());
21 >        main(suite(), args);
22      }
23      public static Test suite() {
24 <        return new TestSuite(PriorityQueueTest.class);
24 >        class Implementation implements CollectionImplementation {
25 >            public Class<?> klazz() { return PriorityQueue.class; }
26 >            public Collection emptyCollection() { return new PriorityQueue(); }
27 >            public Object makeElement(int i) { return i; }
28 >            public boolean isConcurrent() { return false; }
29 >            public boolean permitsNulls() { return false; }
30 >        }
31 >        return newTestSuite(PriorityQueueTest.class,
32 >                            CollectionTest.testSuite(new Implementation()));
33      }
34  
35      static class MyReverseComparator implements Comparator {
# Line 33 | Line 40 | public class PriorityQueueTest extends J
40  
41      /**
42       * Returns a new queue of given size containing consecutive
43 <     * Integers 0 ... n.
43 >     * Integers 0 ... n - 1.
44       */
45 <    private PriorityQueue<Integer> populatedQueue(int n) {
46 <        PriorityQueue<Integer> q = new PriorityQueue<Integer>(n);
45 >    private static PriorityQueue<Integer> populatedQueue(int n) {
46 >        PriorityQueue<Integer> q = new PriorityQueue<>(n);
47          assertTrue(q.isEmpty());
48 <        for (int i = n-1; i >= 0; i -= 2)
48 >        for (int i = n - 1; i >= 0; i -= 2)
49              assertTrue(q.offer(new Integer(i)));
50          for (int i = (n & 1); i < n; i += 2)
51              assertTrue(q.offer(new Integer(i)));
52          assertFalse(q.isEmpty());
53          assertEquals(n, q.size());
54 +        assertEquals((Integer) 0, q.peek());
55          return q;
56      }
57  
# Line 55 | Line 63 | public class PriorityQueueTest extends J
63      }
64  
65      /**
66 <     * Constructor throws IAE if capacity argument nonpositive
66 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
67       */
68      public void testConstructor2() {
69          try {
# Line 79 | Line 87 | public class PriorityQueueTest extends J
87       */
88      public void testConstructor4() {
89          try {
90 <            Integer[] ints = new Integer[SIZE];
83 <            new PriorityQueue(Arrays.asList(ints));
90 >            new PriorityQueue(Arrays.asList(new Integer[SIZE]));
91              shouldThrow();
92          } catch (NullPointerException success) {}
93      }
# Line 89 | Line 96 | public class PriorityQueueTest extends J
96       * Initializing from Collection with some null elements throws NPE
97       */
98      public void testConstructor5() {
99 +        Integer[] ints = new Integer[SIZE];
100 +        for (int i = 0; i < SIZE - 1; ++i)
101 +            ints[i] = new Integer(i);
102          try {
93            Integer[] ints = new Integer[SIZE];
94            for (int i = 0; i < SIZE-1; ++i)
95                ints[i] = new Integer(i);
103              new PriorityQueue(Arrays.asList(ints));
104              shouldThrow();
105          } catch (NullPointerException success) {}
# Line 121 | Line 128 | public class PriorityQueueTest extends J
128          for (int i = 0; i < SIZE; ++i)
129              ints[i] = new Integer(i);
130          q.addAll(Arrays.asList(ints));
131 <        for (int i = SIZE-1; i >= 0; --i)
131 >        for (int i = SIZE - 1; i >= 0; --i)
132              assertEquals(ints[i], q.poll());
133      }
134  
# Line 145 | Line 152 | public class PriorityQueueTest extends J
152      public void testSize() {
153          PriorityQueue q = populatedQueue(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
155 <            assertEquals(SIZE-i, q.size());
155 >            assertEquals(SIZE - i, q.size());
156              q.remove();
157          }
158          for (int i = 0; i < SIZE; ++i) {
# Line 158 | Line 165 | public class PriorityQueueTest extends J
165       * offer(null) throws NPE
166       */
167      public void testOfferNull() {
168 +        PriorityQueue q = new PriorityQueue(1);
169          try {
162            PriorityQueue q = new PriorityQueue(1);
170              q.offer(null);
171              shouldThrow();
172          } catch (NullPointerException success) {}
# Line 169 | Line 176 | public class PriorityQueueTest extends J
176       * add(null) throws NPE
177       */
178      public void testAddNull() {
179 +        PriorityQueue q = new PriorityQueue(1);
180          try {
173            PriorityQueue q = new PriorityQueue(1);
181              q.add(null);
182              shouldThrow();
183          } catch (NullPointerException success) {}
# Line 192 | Line 199 | public class PriorityQueueTest extends J
199          PriorityQueue q = new PriorityQueue(1);
200          try {
201              q.offer(new Object());
195            q.offer(new Object());
202              shouldThrow();
203 <        } catch (ClassCastException success) {}
203 >        } catch (ClassCastException success) {
204 >            assertTrue(q.isEmpty());
205 >            assertEquals(0, q.size());
206 >            assertNull(q.poll());
207 >        }
208      }
209  
210      /**
# Line 212 | Line 222 | public class PriorityQueueTest extends J
222       * addAll(null) throws NPE
223       */
224      public void testAddAll1() {
225 +        PriorityQueue q = new PriorityQueue(1);
226          try {
216            PriorityQueue q = new PriorityQueue(1);
227              q.addAll(null);
228              shouldThrow();
229          } catch (NullPointerException success) {}
# Line 223 | Line 233 | public class PriorityQueueTest extends J
233       * addAll of a collection with null elements throws NPE
234       */
235      public void testAddAll2() {
236 +        PriorityQueue q = new PriorityQueue(SIZE);
237          try {
238 <            PriorityQueue q = new PriorityQueue(SIZE);
228 <            Integer[] ints = new Integer[SIZE];
229 <            q.addAll(Arrays.asList(ints));
238 >            q.addAll(Arrays.asList(new Integer[SIZE]));
239              shouldThrow();
240          } catch (NullPointerException success) {}
241      }
# Line 236 | Line 245 | public class PriorityQueueTest extends J
245       * possibly adding some elements
246       */
247      public void testAddAll3() {
248 +        PriorityQueue q = new PriorityQueue(SIZE);
249 +        Integer[] ints = new Integer[SIZE];
250 +        for (int i = 0; i < SIZE - 1; ++i)
251 +            ints[i] = new Integer(i);
252          try {
240            PriorityQueue q = new PriorityQueue(SIZE);
241            Integer[] ints = new Integer[SIZE];
242            for (int i = 0; i < SIZE-1; ++i)
243                ints[i] = new Integer(i);
253              q.addAll(Arrays.asList(ints));
254              shouldThrow();
255          } catch (NullPointerException success) {}
# Line 253 | Line 262 | public class PriorityQueueTest extends J
262          Integer[] empty = new Integer[0];
263          Integer[] ints = new Integer[SIZE];
264          for (int i = 0; i < SIZE; ++i)
265 <            ints[i] = new Integer(SIZE-1-i);
265 >            ints[i] = new Integer(SIZE - 1 - i);
266          PriorityQueue q = new PriorityQueue(SIZE);
267          assertFalse(q.addAll(Arrays.asList(empty)));
268          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 324 | Line 333 | public class PriorityQueueTest extends J
333              assertTrue(q.contains(i));
334              assertTrue(q.remove(i));
335              assertFalse(q.contains(i));
336 <            assertTrue(q.contains(i-1));
336 >            assertTrue(q.contains(i - 1));
337          }
338          for (int i = 0; i < SIZE; i += 2) {
339              assertTrue(q.contains(i));
340              assertTrue(q.remove(i));
341              assertFalse(q.contains(i));
342 <            assertFalse(q.remove(i+1));
343 <            assertFalse(q.contains(i+1));
342 >            assertFalse(q.remove(i + 1));
343 >            assertFalse(q.contains(i + 1));
344          }
345          assertTrue(q.isEmpty());
346      }
# Line 390 | Line 399 | public class PriorityQueueTest extends J
399                  assertTrue(changed);
400  
401              assertTrue(q.containsAll(p));
402 <            assertEquals(SIZE-i, q.size());
402 >            assertEquals(SIZE - i, q.size());
403              p.remove();
404          }
405      }
# Line 403 | Line 412 | public class PriorityQueueTest extends J
412              PriorityQueue q = populatedQueue(SIZE);
413              PriorityQueue p = populatedQueue(i);
414              assertTrue(q.removeAll(p));
415 <            assertEquals(SIZE-i, q.size());
415 >            assertEquals(SIZE - i, q.size());
416              for (int j = 0; j < i; ++j) {
417                  Integer x = (Integer)(p.remove());
418                  assertFalse(q.contains(x));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines