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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.40 by jsr166, Wed Jan 4 06:09:58 2017 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
13 <
14 < public class PriorityQueueTest extends TestCase {
15 <
15 <    private static final int N = 10;
16 <    private static final long SHORT_DELAY_MS = 100;
17 <    private static final long MEDIUM_DELAY_MS = 1000;
18 <    private static final long LONG_DELAY_MS = 10000;
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 + import junit.framework.TestSuite;
19 +
20 + public class PriorityQueueTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());  
22 >        main(suite(), args);
23      }
23
24      public static Test suite() {
25 <        return new TestSuite(PriorityQueueTest.class);
25 >        class Implementation implements CollectionImplementation {
26 >            public Class<?> klazz() { return PriorityQueue.class; }
27 >            public Collection emptyCollection() { return new PriorityQueue(); }
28 >            public Object makeElement(int i) { return i; }
29 >            public boolean isConcurrent() { return false; }
30 >            public boolean permitsNulls() { return false; }
31 >        }
32 >        return newTestSuite(PriorityQueueTest.class,
33 >                            CollectionTest.testSuite(new Implementation()));
34      }
35  
36 <    static class MyReverseComparator implements Comparator {
36 >    static class MyReverseComparator implements Comparator {
37          public int compare(Object x, Object y) {
38 <            int i = ((Integer)x).intValue();
31 <            int j = ((Integer)y).intValue();
32 <            if (i < j) return 1;
33 <            if (i > j) return -1;
34 <            return 0;
38 >            return ((Comparable)y).compareTo(x);
39          }
40      }
41  
38
42      /**
43 <     * Create a queue of given size containing consecutive
44 <     * Integers 0 ... n.
43 >     * Returns a new queue of given size containing consecutive
44 >     * Integers 0 ... n - 1.
45       */
46 <    private PriorityQueue fullQueue(int n) {
47 <        PriorityQueue q = new PriorityQueue(n);
46 >    private PriorityQueue<Integer> populatedQueue(int n) {
47 >        PriorityQueue<Integer> q = new PriorityQueue<>(n);
48          assertTrue(q.isEmpty());
49 <        for(int i = n-1; i >= 0; i-=2)
50 <            assertTrue(q.offer(new Integer(i)));
51 <        for(int i = (n & 1); i < n; i+=2)
52 <            assertTrue(q.offer(new Integer(i)));
49 >        for (int i = n - 1; i >= 0; i -= 2)
50 >            assertTrue(q.offer(new Integer(i)));
51 >        for (int i = (n & 1); i < n; i += 2)
52 >            assertTrue(q.offer(new Integer(i)));
53          assertFalse(q.isEmpty());
54 <        assertEquals(n, q.size());
54 >        assertEquals(n, q.size());
55 >        assertEquals((Integer) 0, q.peek());
56          return q;
57      }
58 <
59 <    public void testConstructor1(){
60 <        assertEquals(0, new PriorityQueue(N).size());
58 >
59 >    /**
60 >     * A new queue has unbounded capacity
61 >     */
62 >    public void testConstructor1() {
63 >        assertEquals(0, new PriorityQueue(SIZE).size());
64      }
65  
66 <    public void testConstructor2(){
66 >    /**
67 >     * Constructor throws IAE if capacity argument nonpositive
68 >     */
69 >    public void testConstructor2() {
70          try {
71 <            PriorityQueue q = new PriorityQueue(0);
72 <            fail("Cannot make zero-sized");
73 <        }
64 <        catch (IllegalArgumentException success) {}
71 >            new PriorityQueue(0);
72 >            shouldThrow();
73 >        } catch (IllegalArgumentException success) {}
74      }
75  
76 +    /**
77 +     * Initializing from null Collection throws NPE
78 +     */
79      public void testConstructor3() {
68
80          try {
81 <            PriorityQueue q = new PriorityQueue((Collection)null);
82 <            fail("Cannot make from null collection");
83 <        }
73 <        catch (NullPointerException success) {}
81 >            new PriorityQueue((Collection)null);
82 >            shouldThrow();
83 >        } catch (NullPointerException success) {}
84      }
85  
86 <    public void testConstructor4(){
86 >    /**
87 >     * Initializing from Collection of null elements throws NPE
88 >     */
89 >    public void testConstructor4() {
90          try {
91 <            Integer[] ints = new Integer[N];
92 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
93 <            fail("Cannot make with null elements");
81 <        }
82 <        catch (NullPointerException success) {}
91 >            new PriorityQueue(Arrays.asList(new Integer[SIZE]));
92 >            shouldThrow();
93 >        } catch (NullPointerException success) {}
94      }
95  
96 <    public void testConstructor5(){
97 <        try {
98 <            Integer[] ints = new Integer[N];
99 <            for (int i = 0; i < N-1; ++i)
100 <                ints[i] = new Integer(i);
101 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
102 <            fail("Cannot make with null elements");
103 <        }
104 <        catch (NullPointerException success) {}
96 >    /**
97 >     * Initializing from Collection with some null elements throws NPE
98 >     */
99 >    public void testConstructor5() {
100 >        Integer[] ints = new Integer[SIZE];
101 >        for (int i = 0; i < SIZE - 1; ++i)
102 >            ints[i] = new Integer(i);
103 >        try {
104 >            new PriorityQueue(Arrays.asList(ints));
105 >            shouldThrow();
106 >        } catch (NullPointerException success) {}
107      }
108  
109 <    public void testConstructor6(){
110 <        try {
111 <            Integer[] ints = new Integer[N];
112 <            for (int i = 0; i < N; ++i)
113 <                ints[i] = new Integer(i);
114 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
115 <            for (int i = 0; i < N; ++i)
116 <                assertEquals(ints[i], q.poll());
117 <        }
118 <        finally {}
109 >    /**
110 >     * Queue contains all elements of collection used to initialize
111 >     */
112 >    public void testConstructor6() {
113 >        Integer[] ints = new Integer[SIZE];
114 >        for (int i = 0; i < SIZE; ++i)
115 >            ints[i] = new Integer(i);
116 >        PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
117 >        for (int i = 0; i < SIZE; ++i)
118 >            assertEquals(ints[i], q.poll());
119      }
120  
121 <    public void testConstructor7(){
122 <        try {
123 <            PriorityQueue q = new PriorityQueue(N, new MyReverseComparator());
124 <            Integer[] ints = new Integer[N];
125 <            for (int i = 0; i < N; ++i)
126 <                ints[i] = new Integer(i);
127 <            q.addAll(Arrays.asList(ints));
128 <            for (int i = N-1; i >= 0; --i)
129 <                assertEquals(ints[i], q.poll());
130 <        }
131 <        finally {}
121 >    /**
122 >     * The comparator used in constructor is used
123 >     */
124 >    public void testConstructor7() {
125 >        MyReverseComparator cmp = new MyReverseComparator();
126 >        PriorityQueue q = new PriorityQueue(SIZE, cmp);
127 >        assertEquals(cmp, q.comparator());
128 >        Integer[] ints = new Integer[SIZE];
129 >        for (int i = 0; i < SIZE; ++i)
130 >            ints[i] = new Integer(i);
131 >        q.addAll(Arrays.asList(ints));
132 >        for (int i = SIZE - 1; i >= 0; --i)
133 >            assertEquals(ints[i], q.poll());
134      }
135  
136 +    /**
137 +     * isEmpty is true before add, false after
138 +     */
139      public void testEmpty() {
140          PriorityQueue q = new PriorityQueue(2);
141          assertTrue(q.isEmpty());
# Line 129 | Line 147 | public class PriorityQueueTest extends T
147          assertTrue(q.isEmpty());
148      }
149  
150 +    /**
151 +     * size changes when elements added and removed
152 +     */
153      public void testSize() {
154 <        PriorityQueue q = fullQueue(N);
155 <        for (int i = 0; i < N; ++i) {
156 <            assertEquals(N-i, q.size());
154 >        PriorityQueue q = populatedQueue(SIZE);
155 >        for (int i = 0; i < SIZE; ++i) {
156 >            assertEquals(SIZE - i, q.size());
157              q.remove();
158          }
159 <        for (int i = 0; i < N; ++i) {
159 >        for (int i = 0; i < SIZE; ++i) {
160              assertEquals(i, q.size());
161              q.add(new Integer(i));
162          }
163      }
164  
165 <    public void testOfferNull(){
166 <        try {
167 <            PriorityQueue q = new PriorityQueue(1);
165 >    /**
166 >     * offer(null) throws NPE
167 >     */
168 >    public void testOfferNull() {
169 >        PriorityQueue q = new PriorityQueue(1);
170 >        try {
171              q.offer(null);
172 <            fail("should throw NPE");
173 <        } catch (NullPointerException success) { }  
172 >            shouldThrow();
173 >        } catch (NullPointerException success) {}
174 >    }
175 >
176 >    /**
177 >     * add(null) throws NPE
178 >     */
179 >    public void testAddNull() {
180 >        PriorityQueue q = new PriorityQueue(1);
181 >        try {
182 >            q.add(null);
183 >            shouldThrow();
184 >        } catch (NullPointerException success) {}
185      }
186  
187 +    /**
188 +     * Offer of comparable element succeeds
189 +     */
190      public void testOffer() {
191          PriorityQueue q = new PriorityQueue(1);
192 <        assertTrue(q.offer(new Integer(0)));
193 <        assertTrue(q.offer(new Integer(1)));
192 >        assertTrue(q.offer(zero));
193 >        assertTrue(q.offer(one));
194      }
195  
196 +    /**
197 +     * Offer of non-Comparable throws CCE
198 +     */
199      public void testOfferNonComparable() {
200 +        PriorityQueue q = new PriorityQueue(1);
201          try {
160            PriorityQueue q = new PriorityQueue(1);
161            q.offer(new Object());
162            q.offer(new Object());
202              q.offer(new Object());
203 <            fail("should throw CCE");
203 >            shouldThrow();
204 >        } catch (ClassCastException success) {
205 >            assertTrue(q.isEmpty());
206 >            assertEquals(0, q.size());
207 >            assertNull(q.poll());
208          }
166        catch(ClassCastException success) {}
209      }
210  
211 <    public void testAdd(){
212 <        PriorityQueue q = new PriorityQueue(N);
213 <        for (int i = 0; i < N; ++i) {
211 >    /**
212 >     * add of comparable succeeds
213 >     */
214 >    public void testAdd() {
215 >        PriorityQueue q = new PriorityQueue(SIZE);
216 >        for (int i = 0; i < SIZE; ++i) {
217              assertEquals(i, q.size());
218              assertTrue(q.add(new Integer(i)));
219          }
220      }
221  
222 <    public void testAddAll1(){
222 >    /**
223 >     * addAll(null) throws NPE
224 >     */
225 >    public void testAddAll1() {
226 >        PriorityQueue q = new PriorityQueue(1);
227          try {
179            PriorityQueue q = new PriorityQueue(1);
228              q.addAll(null);
229 <            fail("Cannot add null collection");
230 <        }
183 <        catch (NullPointerException success) {}
229 >            shouldThrow();
230 >        } catch (NullPointerException success) {}
231      }
232 <    public void testAddAll2(){
232 >
233 >    /**
234 >     * addAll of a collection with null elements throws NPE
235 >     */
236 >    public void testAddAll2() {
237 >        PriorityQueue q = new PriorityQueue(SIZE);
238          try {
239 <            PriorityQueue q = new PriorityQueue(N);
240 <            Integer[] ints = new Integer[N];
241 <            q.addAll(Arrays.asList(ints));
190 <            fail("Cannot add null elements");
191 <        }
192 <        catch (NullPointerException success) {}
239 >            q.addAll(Arrays.asList(new Integer[SIZE]));
240 >            shouldThrow();
241 >        } catch (NullPointerException success) {}
242      }
243 <    public void testAddAll3(){
243 >
244 >    /**
245 >     * addAll of a collection with any null elements throws NPE after
246 >     * possibly adding some elements
247 >     */
248 >    public void testAddAll3() {
249 >        PriorityQueue q = new PriorityQueue(SIZE);
250 >        Integer[] ints = new Integer[SIZE];
251 >        for (int i = 0; i < SIZE - 1; ++i)
252 >            ints[i] = new Integer(i);
253          try {
196            PriorityQueue q = new PriorityQueue(N);
197            Integer[] ints = new Integer[N];
198            for (int i = 0; i < N-1; ++i)
199                ints[i] = new Integer(i);
254              q.addAll(Arrays.asList(ints));
255 <            fail("Cannot add null elements");
256 <        }
203 <        catch (NullPointerException success) {}
255 >            shouldThrow();
256 >        } catch (NullPointerException success) {}
257      }
258  
259 <    public void testAddAll5(){
260 <        try {
261 <            Integer[] empty = new Integer[0];
262 <            Integer[] ints = new Integer[N];
263 <            for (int i = 0; i < N; ++i)
264 <                ints[i] = new Integer(N-1-i);
265 <            PriorityQueue q = new PriorityQueue(N);
266 <            assertFalse(q.addAll(Arrays.asList(empty)));
267 <            assertTrue(q.addAll(Arrays.asList(ints)));
268 <            for (int i = 0; i < N; ++i)
269 <                assertEquals(new Integer(i), q.poll());
270 <        }
271 <        finally {}
259 >    /**
260 >     * Queue contains all elements of successful addAll
261 >     */
262 >    public void testAddAll5() {
263 >        Integer[] empty = new Integer[0];
264 >        Integer[] ints = new Integer[SIZE];
265 >        for (int i = 0; i < SIZE; ++i)
266 >            ints[i] = new Integer(SIZE - 1 - i);
267 >        PriorityQueue q = new PriorityQueue(SIZE);
268 >        assertFalse(q.addAll(Arrays.asList(empty)));
269 >        assertTrue(q.addAll(Arrays.asList(ints)));
270 >        for (int i = 0; i < SIZE; ++i)
271 >            assertEquals(new Integer(i), q.poll());
272      }
273  
274 <    public void testPoll(){
275 <        PriorityQueue q = fullQueue(N);
276 <        for (int i = 0; i < N; ++i) {
277 <            assertEquals(i, ((Integer)q.poll()).intValue());
274 >    /**
275 >     * poll succeeds unless empty
276 >     */
277 >    public void testPoll() {
278 >        PriorityQueue q = populatedQueue(SIZE);
279 >        for (int i = 0; i < SIZE; ++i) {
280 >            assertEquals(i, q.poll());
281          }
282 <        assertNull(q.poll());
282 >        assertNull(q.poll());
283      }
284  
285 <    public void testPeek(){
286 <        PriorityQueue q = fullQueue(N);
287 <        for (int i = 0; i < N; ++i) {
288 <            assertEquals(i, ((Integer)q.peek()).intValue());
289 <            q.poll();
285 >    /**
286 >     * peek returns next element, or null if empty
287 >     */
288 >    public void testPeek() {
289 >        PriorityQueue q = populatedQueue(SIZE);
290 >        for (int i = 0; i < SIZE; ++i) {
291 >            assertEquals(i, q.peek());
292 >            assertEquals(i, q.poll());
293              assertTrue(q.peek() == null ||
294 <                       i != ((Integer)q.peek()).intValue());
294 >                       !q.peek().equals(i));
295          }
296 <        assertNull(q.peek());
296 >        assertNull(q.peek());
297      }
298  
299 <    public void testElement(){
300 <        PriorityQueue q = fullQueue(N);
301 <        for (int i = 0; i < N; ++i) {
302 <            assertEquals(i, ((Integer)q.element()).intValue());
303 <            q.poll();
299 >    /**
300 >     * element returns next element, or throws NSEE if empty
301 >     */
302 >    public void testElement() {
303 >        PriorityQueue q = populatedQueue(SIZE);
304 >        for (int i = 0; i < SIZE; ++i) {
305 >            assertEquals(i, q.element());
306 >            assertEquals(i, q.poll());
307          }
308          try {
309              q.element();
310 <            fail("no such element");
311 <        }
250 <        catch (NoSuchElementException success) {}
310 >            shouldThrow();
311 >        } catch (NoSuchElementException success) {}
312      }
313  
314 <    public void testRemove(){
315 <        PriorityQueue q = fullQueue(N);
316 <        for (int i = 0; i < N; ++i) {
317 <            assertEquals(i, ((Integer)q.remove()).intValue());
314 >    /**
315 >     * remove removes next element, or throws NSEE if empty
316 >     */
317 >    public void testRemove() {
318 >        PriorityQueue q = populatedQueue(SIZE);
319 >        for (int i = 0; i < SIZE; ++i) {
320 >            assertEquals(i, q.remove());
321          }
322          try {
323              q.remove();
324 <            fail("remove should throw");
325 <        } catch (NoSuchElementException success){
262 <        }  
324 >            shouldThrow();
325 >        } catch (NoSuchElementException success) {}
326      }
327  
328 <    public void testRemoveElement(){
329 <        PriorityQueue q = fullQueue(N);
330 <        for (int i = 1; i < N; i+=2) {
331 <            assertTrue(q.remove(new Integer(i)));
332 <        }
333 <        for (int i = 0; i < N; i+=2) {
334 <            assertTrue(q.remove(new Integer(i)));
335 <            assertFalse(q.remove(new Integer(i+1)));
328 >    /**
329 >     * remove(x) removes x and returns true if present
330 >     */
331 >    public void testRemoveElement() {
332 >        PriorityQueue q = populatedQueue(SIZE);
333 >        for (int i = 1; i < SIZE; i += 2) {
334 >            assertTrue(q.contains(i));
335 >            assertTrue(q.remove(i));
336 >            assertFalse(q.contains(i));
337 >            assertTrue(q.contains(i - 1));
338 >        }
339 >        for (int i = 0; i < SIZE; i += 2) {
340 >            assertTrue(q.contains(i));
341 >            assertTrue(q.remove(i));
342 >            assertFalse(q.contains(i));
343 >            assertFalse(q.remove(i + 1));
344 >            assertFalse(q.contains(i + 1));
345          }
346          assertTrue(q.isEmpty());
347      }
348 <        
349 <    public void testContains(){
350 <        PriorityQueue q = fullQueue(N);
351 <        for (int i = 0; i < N; ++i) {
348 >
349 >    /**
350 >     * contains(x) reports true when elements added but not yet removed
351 >     */
352 >    public void testContains() {
353 >        PriorityQueue q = populatedQueue(SIZE);
354 >        for (int i = 0; i < SIZE; ++i) {
355              assertTrue(q.contains(new Integer(i)));
356              q.poll();
357              assertFalse(q.contains(new Integer(i)));
358          }
359      }
360  
361 <    public void testClear(){
362 <        PriorityQueue q = fullQueue(N);
361 >    /**
362 >     * clear removes all elements
363 >     */
364 >    public void testClear() {
365 >        PriorityQueue q = populatedQueue(SIZE);
366          q.clear();
367          assertTrue(q.isEmpty());
368          assertEquals(0, q.size());
# Line 294 | Line 372 | public class PriorityQueueTest extends T
372          assertTrue(q.isEmpty());
373      }
374  
375 <    public void testContainsAll(){
376 <        PriorityQueue q = fullQueue(N);
377 <        PriorityQueue p = new PriorityQueue(N);
378 <        for (int i = 0; i < N; ++i) {
375 >    /**
376 >     * containsAll(c) is true when c contains a subset of elements
377 >     */
378 >    public void testContainsAll() {
379 >        PriorityQueue q = populatedQueue(SIZE);
380 >        PriorityQueue p = new PriorityQueue(SIZE);
381 >        for (int i = 0; i < SIZE; ++i) {
382              assertTrue(q.containsAll(p));
383              assertFalse(p.containsAll(q));
384              p.add(new Integer(i));
# Line 305 | Line 386 | public class PriorityQueueTest extends T
386          assertTrue(p.containsAll(q));
387      }
388  
389 <    public void testRetainAll(){
390 <        PriorityQueue q = fullQueue(N);
391 <        PriorityQueue p = fullQueue(N);
392 <        for (int i = 0; i < N; ++i) {
389 >    /**
390 >     * retainAll(c) retains only those elements of c and reports true if changed
391 >     */
392 >    public void testRetainAll() {
393 >        PriorityQueue q = populatedQueue(SIZE);
394 >        PriorityQueue p = populatedQueue(SIZE);
395 >        for (int i = 0; i < SIZE; ++i) {
396              boolean changed = q.retainAll(p);
397              if (i == 0)
398                  assertFalse(changed);
# Line 316 | Line 400 | public class PriorityQueueTest extends T
400                  assertTrue(changed);
401  
402              assertTrue(q.containsAll(p));
403 <            assertEquals(N-i, q.size());
403 >            assertEquals(SIZE - i, q.size());
404              p.remove();
405          }
406      }
407  
408 <    public void testRemoveAll(){
409 <        for (int i = 1; i < N; ++i) {
410 <            PriorityQueue q = fullQueue(N);
411 <            PriorityQueue p = fullQueue(i);
408 >    /**
409 >     * removeAll(c) removes only those elements of c and reports true if changed
410 >     */
411 >    public void testRemoveAll() {
412 >        for (int i = 1; i < SIZE; ++i) {
413 >            PriorityQueue q = populatedQueue(SIZE);
414 >            PriorityQueue p = populatedQueue(i);
415              assertTrue(q.removeAll(p));
416 <            assertEquals(N-i, q.size());
416 >            assertEquals(SIZE - i, q.size());
417              for (int j = 0; j < i; ++j) {
418 <                Integer I = (Integer)(p.remove());
419 <                assertFalse(q.contains(I));
418 >                Integer x = (Integer)(p.remove());
419 >                assertFalse(q.contains(x));
420              }
421          }
422      }
423  
424 <    public void testToArray(){
425 <        PriorityQueue q = fullQueue(N);
426 <        Object[] o = q.toArray();
424 >    /**
425 >     * toArray contains all elements
426 >     */
427 >    public void testToArray() {
428 >        PriorityQueue q = populatedQueue(SIZE);
429 >        Object[] o = q.toArray();
430          Arrays.sort(o);
431 <        for(int i = 0; i < o.length; i++)
432 <            assertEquals(o[i], q.poll());
431 >        for (int i = 0; i < o.length; i++)
432 >            assertSame(o[i], q.poll());
433      }
434  
435 <    public void testToArray2(){
436 <        PriorityQueue q = fullQueue(N);
437 <        Integer[] ints = new Integer[N];
438 <        ints = (Integer[])q.toArray(ints);
435 >    /**
436 >     * toArray(a) contains all elements
437 >     */
438 >    public void testToArray2() {
439 >        PriorityQueue<Integer> q = populatedQueue(SIZE);
440 >        Integer[] ints = new Integer[SIZE];
441 >        Integer[] array = q.toArray(ints);
442 >        assertSame(ints, array);
443          Arrays.sort(ints);
444 <        for(int i = 0; i < ints.length; i++)
445 <            assertEquals(ints[i], q.poll());
444 >        for (int i = 0; i < ints.length; i++)
445 >            assertSame(ints[i], q.poll());
446      }
447 <    
448 <    public void testIterator(){
449 <        PriorityQueue q = fullQueue(N);
450 <        int i = 0;
451 <        Iterator it = q.iterator();
452 <        while(it.hasNext()) {
447 >
448 >    /**
449 >     * iterator iterates through all elements
450 >     */
451 >    public void testIterator() {
452 >        PriorityQueue q = populatedQueue(SIZE);
453 >        Iterator it = q.iterator();
454 >        int i;
455 >        for (i = 0; it.hasNext(); i++)
456              assertTrue(q.contains(it.next()));
457 <            ++i;
458 <        }
362 <        assertEquals(i, N);
457 >        assertEquals(i, SIZE);
458 >        assertIteratorExhausted(it);
459      }
460  
461 <    public void testIteratorRemove () {
461 >    /**
462 >     * iterator of empty collection has no elements
463 >     */
464 >    public void testEmptyIterator() {
465 >        assertIteratorExhausted(new PriorityQueue().iterator());
466 >    }
467  
468 +    /**
469 +     * iterator.remove removes current element
470 +     */
471 +    public void testIteratorRemove() {
472          final PriorityQueue q = new PriorityQueue(3);
368
473          q.add(new Integer(2));
474          q.add(new Integer(1));
475          q.add(new Integer(3));
# Line 380 | Line 484 | public class PriorityQueueTest extends T
484          assertFalse(it.hasNext());
485      }
486  
487 <
488 <    public void testToString(){
489 <        PriorityQueue q = fullQueue(N);
487 >    /**
488 >     * toString contains toStrings of elements
489 >     */
490 >    public void testToString() {
491 >        PriorityQueue q = populatedQueue(SIZE);
492          String s = q.toString();
493 <        for (int i = 0; i < N; ++i) {
494 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
493 >        for (int i = 0; i < SIZE; ++i) {
494 >            assertTrue(s.contains(String.valueOf(i)));
495          }
496 <    }        
496 >    }
497  
498 <    public void testSerialization() {
499 <        PriorityQueue q = fullQueue(N);
500 <        try {
501 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
502 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
503 <            out.writeObject(q);
504 <            out.close();
505 <
506 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
507 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
508 <            PriorityQueue r = (PriorityQueue)in.readObject();
509 <            assertEquals(q.size(), r.size());
404 <            while (!q.isEmpty())
405 <                assertEquals(q.remove(), r.remove());
406 <        } catch(Exception e){
407 <            fail("unexpected exception");
498 >    /**
499 >     * A deserialized serialized queue has same elements
500 >     */
501 >    public void testSerialization() throws Exception {
502 >        Queue x = populatedQueue(SIZE);
503 >        Queue y = serialClone(x);
504 >
505 >        assertNotSame(x, y);
506 >        assertEquals(x.size(), y.size());
507 >        while (!x.isEmpty()) {
508 >            assertFalse(y.isEmpty());
509 >            assertEquals(x.remove(), y.remove());
510          }
511 +        assertTrue(y.isEmpty());
512      }
513   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines