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.29 by jsr166, Wed Dec 31 20:17:40 2014 UTC vs.
Revision 1.47 by jsr166, Mon May 28 21:19:50 2018 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 >        class ComparatorImplementation implements CollectionImplementation {
32 >            public Class<?> klazz() { return PriorityQueue.class; }
33 >            public Collection emptyCollection() {
34 >                return new PriorityQueue(new MyReverseComparator());
35 >            }
36 >            public Object makeElement(int i) { return i; }
37 >            public boolean isConcurrent() { return false; }
38 >            public boolean permitsNulls() { return false; }
39 >        }
40 >        return newTestSuite(
41 >            PriorityQueueTest.class,
42 >            CollectionTest.testSuite(new Implementation()),
43 >            CollectionTest.testSuite(new ComparatorImplementation()));
44      }
45  
46 <    static class MyReverseComparator implements Comparator {
46 >    static class MyReverseComparator implements Comparator, java.io.Serializable {
47          public int compare(Object x, Object y) {
48              return ((Comparable)y).compareTo(x);
49          }
# Line 33 | Line 51 | public class PriorityQueueTest extends J
51  
52      /**
53       * Returns a new queue of given size containing consecutive
54 <     * Integers 0 ... n.
54 >     * Integers 0 ... n - 1.
55       */
56 <    private PriorityQueue<Integer> populatedQueue(int n) {
57 <        PriorityQueue<Integer> q = new PriorityQueue<Integer>(n);
56 >    private static PriorityQueue<Integer> populatedQueue(int n) {
57 >        PriorityQueue<Integer> q = new PriorityQueue<>(n);
58          assertTrue(q.isEmpty());
59 <        for (int i = n-1; i >= 0; i -= 2)
59 >        for (int i = n - 1; i >= 0; i -= 2)
60              assertTrue(q.offer(new Integer(i)));
61          for (int i = (n & 1); i < n; i += 2)
62              assertTrue(q.offer(new Integer(i)));
63          assertFalse(q.isEmpty());
64          assertEquals(n, q.size());
65 +        assertEquals((Integer) 0, q.peek());
66          return q;
67      }
68  
# Line 55 | Line 74 | public class PriorityQueueTest extends J
74      }
75  
76      /**
77 <     * Constructor throws IAE if capacity argument nonpositive
77 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
78       */
79      public void testConstructor2() {
80          try {
81 <            PriorityQueue q = new PriorityQueue(0);
81 >            new PriorityQueue(0);
82              shouldThrow();
83          } catch (IllegalArgumentException success) {}
84      }
# Line 69 | Line 88 | public class PriorityQueueTest extends J
88       */
89      public void testConstructor3() {
90          try {
91 <            PriorityQueue q = new PriorityQueue((Collection)null);
91 >            new PriorityQueue((Collection)null);
92              shouldThrow();
93          } catch (NullPointerException success) {}
94      }
# Line 79 | Line 98 | public class PriorityQueueTest extends J
98       */
99      public void testConstructor4() {
100          try {
101 <            Integer[] ints = new Integer[SIZE];
83 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
101 >            new PriorityQueue(Arrays.asList(new Integer[SIZE]));
102              shouldThrow();
103          } catch (NullPointerException success) {}
104      }
# Line 89 | Line 107 | public class PriorityQueueTest extends J
107       * Initializing from Collection with some null elements throws NPE
108       */
109      public void testConstructor5() {
110 +        Integer[] ints = new Integer[SIZE];
111 +        for (int i = 0; i < SIZE - 1; ++i)
112 +            ints[i] = new Integer(i);
113          try {
114 <            Integer[] ints = new Integer[SIZE];
94 <            for (int i = 0; i < SIZE-1; ++i)
95 <                ints[i] = new Integer(i);
96 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
114 >            new PriorityQueue(Arrays.asList(ints));
115              shouldThrow();
116          } catch (NullPointerException success) {}
117      }
# Line 121 | Line 139 | public class PriorityQueueTest extends J
139          for (int i = 0; i < SIZE; ++i)
140              ints[i] = new Integer(i);
141          q.addAll(Arrays.asList(ints));
142 <        for (int i = SIZE-1; i >= 0; --i)
142 >        for (int i = SIZE - 1; i >= 0; --i)
143              assertEquals(ints[i], q.poll());
144      }
145  
# Line 145 | Line 163 | public class PriorityQueueTest extends J
163      public void testSize() {
164          PriorityQueue q = populatedQueue(SIZE);
165          for (int i = 0; i < SIZE; ++i) {
166 <            assertEquals(SIZE-i, q.size());
166 >            assertEquals(SIZE - i, q.size());
167              q.remove();
168          }
169          for (int i = 0; i < SIZE; ++i) {
# Line 158 | Line 176 | public class PriorityQueueTest extends J
176       * offer(null) throws NPE
177       */
178      public void testOfferNull() {
179 +        PriorityQueue q = new PriorityQueue(1);
180          try {
162            PriorityQueue q = new PriorityQueue(1);
181              q.offer(null);
182              shouldThrow();
183          } catch (NullPointerException success) {}
# Line 169 | Line 187 | public class PriorityQueueTest extends J
187       * add(null) throws NPE
188       */
189      public void testAddNull() {
190 +        PriorityQueue q = new PriorityQueue(1);
191          try {
173            PriorityQueue q = new PriorityQueue(1);
192              q.add(null);
193              shouldThrow();
194          } catch (NullPointerException success) {}
# Line 189 | Line 207 | public class PriorityQueueTest extends J
207       * Offer of non-Comparable throws CCE
208       */
209      public void testOfferNonComparable() {
210 +        PriorityQueue q = new PriorityQueue(1);
211          try {
193            PriorityQueue q = new PriorityQueue(1);
194            q.offer(new Object());
195            q.offer(new Object());
212              q.offer(new Object());
213              shouldThrow();
214 <        } catch (ClassCastException success) {}
214 >        } catch (ClassCastException success) {
215 >            assertTrue(q.isEmpty());
216 >            assertEquals(0, q.size());
217 >            assertNull(q.poll());
218 >        }
219      }
220  
221      /**
# Line 213 | Line 233 | public class PriorityQueueTest extends J
233       * addAll(null) throws NPE
234       */
235      public void testAddAll1() {
236 +        PriorityQueue q = new PriorityQueue(1);
237          try {
217            PriorityQueue q = new PriorityQueue(1);
238              q.addAll(null);
239              shouldThrow();
240          } catch (NullPointerException success) {}
# Line 224 | Line 244 | public class PriorityQueueTest extends J
244       * addAll of a collection with null elements throws NPE
245       */
246      public void testAddAll2() {
247 +        PriorityQueue q = new PriorityQueue(SIZE);
248          try {
249 <            PriorityQueue q = new PriorityQueue(SIZE);
229 <            Integer[] ints = new Integer[SIZE];
230 <            q.addAll(Arrays.asList(ints));
249 >            q.addAll(Arrays.asList(new Integer[SIZE]));
250              shouldThrow();
251          } catch (NullPointerException success) {}
252      }
# Line 237 | Line 256 | public class PriorityQueueTest extends J
256       * possibly adding some elements
257       */
258      public void testAddAll3() {
259 +        PriorityQueue q = new PriorityQueue(SIZE);
260 +        Integer[] ints = new Integer[SIZE];
261 +        for (int i = 0; i < SIZE - 1; ++i)
262 +            ints[i] = new Integer(i);
263          try {
241            PriorityQueue q = new PriorityQueue(SIZE);
242            Integer[] ints = new Integer[SIZE];
243            for (int i = 0; i < SIZE-1; ++i)
244                ints[i] = new Integer(i);
264              q.addAll(Arrays.asList(ints));
265              shouldThrow();
266          } catch (NullPointerException success) {}
# Line 254 | Line 273 | public class PriorityQueueTest extends J
273          Integer[] empty = new Integer[0];
274          Integer[] ints = new Integer[SIZE];
275          for (int i = 0; i < SIZE; ++i)
276 <            ints[i] = new Integer(SIZE-1-i);
276 >            ints[i] = new Integer(SIZE - 1 - i);
277          PriorityQueue q = new PriorityQueue(SIZE);
278          assertFalse(q.addAll(Arrays.asList(empty)));
279          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 325 | Line 344 | public class PriorityQueueTest extends J
344              assertTrue(q.contains(i));
345              assertTrue(q.remove(i));
346              assertFalse(q.contains(i));
347 <            assertTrue(q.contains(i-1));
347 >            assertTrue(q.contains(i - 1));
348          }
349          for (int i = 0; i < SIZE; i += 2) {
350              assertTrue(q.contains(i));
351              assertTrue(q.remove(i));
352              assertFalse(q.contains(i));
353 <            assertFalse(q.remove(i+1));
354 <            assertFalse(q.contains(i+1));
353 >            assertFalse(q.remove(i + 1));
354 >            assertFalse(q.contains(i + 1));
355          }
356          assertTrue(q.isEmpty());
357      }
# Line 391 | Line 410 | public class PriorityQueueTest extends J
410                  assertTrue(changed);
411  
412              assertTrue(q.containsAll(p));
413 <            assertEquals(SIZE-i, q.size());
413 >            assertEquals(SIZE - i, q.size());
414              p.remove();
415          }
416      }
# Line 404 | Line 423 | public class PriorityQueueTest extends J
423              PriorityQueue q = populatedQueue(SIZE);
424              PriorityQueue p = populatedQueue(i);
425              assertTrue(q.removeAll(p));
426 <            assertEquals(SIZE-i, q.size());
426 >            assertEquals(SIZE - i, q.size());
427              for (int j = 0; j < i; ++j) {
428                  Integer x = (Integer)(p.remove());
429                  assertFalse(q.contains(x));
# Line 417 | Line 436 | public class PriorityQueueTest extends J
436       */
437      public void testToArray() {
438          PriorityQueue q = populatedQueue(SIZE);
439 <        Object[] o = q.toArray();
440 <        Arrays.sort(o);
441 <        for (int i = 0; i < o.length; i++)
442 <            assertSame(o[i], q.poll());
439 >        Object[] a = q.toArray();
440 >        assertSame(Object[].class, a.getClass());
441 >        Arrays.sort(a);
442 >        for (Object o : a)
443 >            assertSame(o, q.poll());
444 >        assertTrue(q.isEmpty());
445      }
446  
447      /**
# Line 432 | Line 453 | public class PriorityQueueTest extends J
453          Integer[] array = q.toArray(ints);
454          assertSame(ints, array);
455          Arrays.sort(ints);
456 <        for (int i = 0; i < ints.length; i++)
457 <            assertSame(ints[i], q.poll());
456 >        for (Integer o : ints)
457 >            assertSame(o, q.poll());
458 >        assertTrue(q.isEmpty());
459      }
460  
461      /**
# Line 441 | Line 463 | public class PriorityQueueTest extends J
463       */
464      public void testIterator() {
465          PriorityQueue q = populatedQueue(SIZE);
444        int i = 0;
466          Iterator it = q.iterator();
467 <        while (it.hasNext()) {
467 >        int i;
468 >        for (i = 0; it.hasNext(); i++)
469              assertTrue(q.contains(it.next()));
448            ++i;
449        }
470          assertEquals(i, SIZE);
471 +        assertIteratorExhausted(it);
472 +    }
473 +
474 +    /**
475 +     * iterator of empty collection has no elements
476 +     */
477 +    public void testEmptyIterator() {
478 +        assertIteratorExhausted(new PriorityQueue().iterator());
479      }
480  
481      /**
# Line 481 | Line 509 | public class PriorityQueueTest extends J
509      }
510  
511      /**
512 <     * A deserialized serialized queue has same elements
512 >     * A deserialized/reserialized queue has same elements
513       */
514      public void testSerialization() throws Exception {
515          Queue x = populatedQueue(SIZE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines