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.22 by jsr166, Fri May 27 19:52:35 2011 UTC vs.
Revision 1.45 by jsr166, Sun May 6 22:09:42 2018 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
9 > import java.util.Arrays;
10 > import java.util.Collection;
11 > import java.util.Comparator;
12 > import java.util.Iterator;
13 > import java.util.NoSuchElementException;
14 > import java.util.PriorityQueue;
15 > import java.util.Queue;
16 >
17 > import junit.framework.Test;
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() { return new PriorityQueue(new MyReverseComparator()); }
34 >            public Object makeElement(int i) { return i; }
35 >            public boolean isConcurrent() { return false; }
36 >            public boolean permitsNulls() { return false; }
37 >        }
38 >        return newTestSuite(PriorityQueueTest.class,
39 >                            CollectionTest.testSuite(new Implementation()),
40 >                            CollectionTest.testSuite(new ComparatorImplementation()));
41      }
42  
43 <    static class MyReverseComparator implements Comparator {
43 >    static class MyReverseComparator implements Comparator, java.io.Serializable {
44          public int compare(Object x, Object y) {
45              return ((Comparable)y).compareTo(x);
46          }
47      }
48  
49      /**
50 <     * Create a queue of given size containing consecutive
51 <     * Integers 0 ... n.
50 >     * Returns a new queue of given size containing consecutive
51 >     * Integers 0 ... n - 1.
52       */
53 <    private PriorityQueue<Integer> populatedQueue(int n) {
54 <        PriorityQueue<Integer> q = new PriorityQueue<Integer>(n);
53 >    private static PriorityQueue<Integer> populatedQueue(int n) {
54 >        PriorityQueue<Integer> q = new PriorityQueue<>(n);
55          assertTrue(q.isEmpty());
56 <        for (int i = n-1; i >= 0; i-=2)
56 >        for (int i = n - 1; i >= 0; i -= 2)
57              assertTrue(q.offer(new Integer(i)));
58 <        for (int i = (n & 1); i < n; i+=2)
58 >        for (int i = (n & 1); i < n; i += 2)
59              assertTrue(q.offer(new Integer(i)));
60          assertFalse(q.isEmpty());
61          assertEquals(n, q.size());
62 +        assertEquals((Integer) 0, q.peek());
63          return q;
64      }
65  
# Line 49 | Line 71 | public class PriorityQueueTest extends J
71      }
72  
73      /**
74 <     * Constructor throws IAE if capacity argument nonpositive
74 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
75       */
76      public void testConstructor2() {
77          try {
78 <            PriorityQueue q = new PriorityQueue(0);
78 >            new PriorityQueue(0);
79              shouldThrow();
80          } catch (IllegalArgumentException success) {}
81      }
# Line 63 | Line 85 | public class PriorityQueueTest extends J
85       */
86      public void testConstructor3() {
87          try {
88 <            PriorityQueue q = new PriorityQueue((Collection)null);
88 >            new PriorityQueue((Collection)null);
89              shouldThrow();
90          } catch (NullPointerException success) {}
91      }
# Line 73 | Line 95 | public class PriorityQueueTest extends J
95       */
96      public void testConstructor4() {
97          try {
98 <            Integer[] ints = new Integer[SIZE];
77 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
98 >            new PriorityQueue(Arrays.asList(new Integer[SIZE]));
99              shouldThrow();
100          } catch (NullPointerException success) {}
101      }
# Line 83 | Line 104 | public class PriorityQueueTest extends J
104       * Initializing from Collection with some null elements throws NPE
105       */
106      public void testConstructor5() {
107 +        Integer[] ints = new Integer[SIZE];
108 +        for (int i = 0; i < SIZE - 1; ++i)
109 +            ints[i] = new Integer(i);
110          try {
111 <            Integer[] ints = new Integer[SIZE];
88 <            for (int i = 0; i < SIZE-1; ++i)
89 <                ints[i] = new Integer(i);
90 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
111 >            new PriorityQueue(Arrays.asList(ints));
112              shouldThrow();
113          } catch (NullPointerException success) {}
114      }
# Line 115 | Line 136 | public class PriorityQueueTest extends J
136          for (int i = 0; i < SIZE; ++i)
137              ints[i] = new Integer(i);
138          q.addAll(Arrays.asList(ints));
139 <        for (int i = SIZE-1; i >= 0; --i)
139 >        for (int i = SIZE - 1; i >= 0; --i)
140              assertEquals(ints[i], q.poll());
141      }
142  
# Line 139 | Line 160 | public class PriorityQueueTest extends J
160      public void testSize() {
161          PriorityQueue q = populatedQueue(SIZE);
162          for (int i = 0; i < SIZE; ++i) {
163 <            assertEquals(SIZE-i, q.size());
163 >            assertEquals(SIZE - i, q.size());
164              q.remove();
165          }
166          for (int i = 0; i < SIZE; ++i) {
# Line 152 | Line 173 | public class PriorityQueueTest extends J
173       * offer(null) throws NPE
174       */
175      public void testOfferNull() {
176 +        PriorityQueue q = new PriorityQueue(1);
177          try {
156            PriorityQueue q = new PriorityQueue(1);
178              q.offer(null);
179              shouldThrow();
180          } catch (NullPointerException success) {}
# Line 163 | Line 184 | public class PriorityQueueTest extends J
184       * add(null) throws NPE
185       */
186      public void testAddNull() {
187 +        PriorityQueue q = new PriorityQueue(1);
188          try {
167            PriorityQueue q = new PriorityQueue(1);
189              q.add(null);
190              shouldThrow();
191          } catch (NullPointerException success) {}
# Line 183 | Line 204 | public class PriorityQueueTest extends J
204       * Offer of non-Comparable throws CCE
205       */
206      public void testOfferNonComparable() {
207 +        PriorityQueue q = new PriorityQueue(1);
208          try {
187            PriorityQueue q = new PriorityQueue(1);
188            q.offer(new Object());
189            q.offer(new Object());
209              q.offer(new Object());
210              shouldThrow();
211 <        } catch (ClassCastException success) {}
211 >        } catch (ClassCastException success) {
212 >            assertTrue(q.isEmpty());
213 >            assertEquals(0, q.size());
214 >            assertNull(q.poll());
215 >        }
216      }
217  
218      /**
# Line 207 | Line 230 | public class PriorityQueueTest extends J
230       * addAll(null) throws NPE
231       */
232      public void testAddAll1() {
233 +        PriorityQueue q = new PriorityQueue(1);
234          try {
211            PriorityQueue q = new PriorityQueue(1);
235              q.addAll(null);
236              shouldThrow();
237          } catch (NullPointerException success) {}
# Line 218 | Line 241 | public class PriorityQueueTest extends J
241       * addAll of a collection with null elements throws NPE
242       */
243      public void testAddAll2() {
244 +        PriorityQueue q = new PriorityQueue(SIZE);
245          try {
246 <            PriorityQueue q = new PriorityQueue(SIZE);
223 <            Integer[] ints = new Integer[SIZE];
224 <            q.addAll(Arrays.asList(ints));
246 >            q.addAll(Arrays.asList(new Integer[SIZE]));
247              shouldThrow();
248          } catch (NullPointerException success) {}
249      }
# Line 231 | Line 253 | public class PriorityQueueTest extends J
253       * possibly adding some elements
254       */
255      public void testAddAll3() {
256 +        PriorityQueue q = new PriorityQueue(SIZE);
257 +        Integer[] ints = new Integer[SIZE];
258 +        for (int i = 0; i < SIZE - 1; ++i)
259 +            ints[i] = new Integer(i);
260          try {
235            PriorityQueue q = new PriorityQueue(SIZE);
236            Integer[] ints = new Integer[SIZE];
237            for (int i = 0; i < SIZE-1; ++i)
238                ints[i] = new Integer(i);
261              q.addAll(Arrays.asList(ints));
262              shouldThrow();
263          } catch (NullPointerException success) {}
# Line 248 | Line 270 | public class PriorityQueueTest extends J
270          Integer[] empty = new Integer[0];
271          Integer[] ints = new Integer[SIZE];
272          for (int i = 0; i < SIZE; ++i)
273 <            ints[i] = new Integer(SIZE-1-i);
273 >            ints[i] = new Integer(SIZE - 1 - i);
274          PriorityQueue q = new PriorityQueue(SIZE);
275          assertFalse(q.addAll(Arrays.asList(empty)));
276          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 315 | Line 337 | public class PriorityQueueTest extends J
337       */
338      public void testRemoveElement() {
339          PriorityQueue q = populatedQueue(SIZE);
340 <        for (int i = 1; i < SIZE; i+=2) {
340 >        for (int i = 1; i < SIZE; i += 2) {
341              assertTrue(q.contains(i));
342              assertTrue(q.remove(i));
343              assertFalse(q.contains(i));
344 <            assertTrue(q.contains(i-1));
344 >            assertTrue(q.contains(i - 1));
345          }
346 <        for (int i = 0; i < SIZE; i+=2) {
346 >        for (int i = 0; i < SIZE; i += 2) {
347              assertTrue(q.contains(i));
348              assertTrue(q.remove(i));
349              assertFalse(q.contains(i));
350 <            assertFalse(q.remove(i+1));
351 <            assertFalse(q.contains(i+1));
350 >            assertFalse(q.remove(i + 1));
351 >            assertFalse(q.contains(i + 1));
352          }
353          assertTrue(q.isEmpty());
354      }
# Line 385 | Line 407 | public class PriorityQueueTest extends J
407                  assertTrue(changed);
408  
409              assertTrue(q.containsAll(p));
410 <            assertEquals(SIZE-i, q.size());
410 >            assertEquals(SIZE - i, q.size());
411              p.remove();
412          }
413      }
# Line 398 | Line 420 | public class PriorityQueueTest extends J
420              PriorityQueue q = populatedQueue(SIZE);
421              PriorityQueue p = populatedQueue(i);
422              assertTrue(q.removeAll(p));
423 <            assertEquals(SIZE-i, q.size());
423 >            assertEquals(SIZE - i, q.size());
424              for (int j = 0; j < i; ++j) {
425 <                Integer I = (Integer)(p.remove());
426 <                assertFalse(q.contains(I));
425 >                Integer x = (Integer)(p.remove());
426 >                assertFalse(q.contains(x));
427              }
428          }
429      }
# Line 435 | Line 457 | public class PriorityQueueTest extends J
457       */
458      public void testIterator() {
459          PriorityQueue q = populatedQueue(SIZE);
438        int i = 0;
460          Iterator it = q.iterator();
461 <        while (it.hasNext()) {
461 >        int i;
462 >        for (i = 0; it.hasNext(); i++)
463              assertTrue(q.contains(it.next()));
442            ++i;
443        }
464          assertEquals(i, SIZE);
465 +        assertIteratorExhausted(it);
466 +    }
467 +
468 +    /**
469 +     * iterator of empty collection has no elements
470 +     */
471 +    public void testEmptyIterator() {
472 +        assertIteratorExhausted(new PriorityQueue().iterator());
473      }
474  
475      /**
# Line 475 | Line 503 | public class PriorityQueueTest extends J
503      }
504  
505      /**
506 <     * A deserialized serialized queue has same elements
506 >     * A deserialized/reserialized queue has same elements
507       */
508      public void testSerialization() throws Exception {
509 <        PriorityQueue q = populatedQueue(SIZE);
510 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
511 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
512 <        out.writeObject(q);
513 <        out.close();
514 <
515 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
516 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
517 <        PriorityQueue r = (PriorityQueue)in.readObject();
518 <        assertEquals(q.size(), r.size());
491 <        while (!q.isEmpty())
492 <            assertEquals(q.remove(), r.remove());
509 >        Queue x = populatedQueue(SIZE);
510 >        Queue y = serialClone(x);
511 >
512 >        assertNotSame(x, y);
513 >        assertEquals(x.size(), y.size());
514 >        while (!x.isEmpty()) {
515 >            assertFalse(y.isEmpty());
516 >            assertEquals(x.remove(), y.remove());
517 >        }
518 >        assertTrue(y.isEmpty());
519      }
520   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines