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.1 by dl, Sun Aug 31 19:24:55 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 00:31:57 2003 UTC

# Line 8 | Line 8
8   import junit.framework.*;
9   import java.util.*;
10   import java.util.concurrent.*;
11 + import java.io.*;
12  
13 < public class PriorityQueueTest extends TestCase {
13 <
14 <    private static final int N = 10;
15 <    private static final long SHORT_DELAY_MS = 100;
16 <    private static final long MEDIUM_DELAY_MS = 1000;
17 <    private static final long LONG_DELAY_MS = 10000;
13 > public class PriorityQueueTest extends JSR166TestCase {
14  
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
# Line 39 | Line 35 | public class PriorityQueueTest extends T
35       * Create a queue of given size containing consecutive
36       * Integers 0 ... n.
37       */
38 <    private PriorityQueue fullQueue(int n) {
38 >    private PriorityQueue populatedQueue(int n) {
39          PriorityQueue q = new PriorityQueue(n);
40          assertTrue(q.isEmpty());
41          for(int i = n-1; i >= 0; i-=2)
# Line 52 | Line 48 | public class PriorityQueueTest extends T
48      }
49  
50      public void testConstructor1(){
51 <        assertEquals(0, new PriorityQueue(N).size());
51 >        assertEquals(0, new PriorityQueue(SIZE).size());
52      }
53  
54      public void testConstructor2(){
# Line 74 | Line 70 | public class PriorityQueueTest extends T
70  
71      public void testConstructor4(){
72          try {
73 <            Integer[] ints = new Integer[N];
73 >            Integer[] ints = new Integer[SIZE];
74              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
75              fail("Cannot make with null elements");
76          }
# Line 83 | Line 79 | public class PriorityQueueTest extends T
79  
80      public void testConstructor5(){
81          try {
82 <            Integer[] ints = new Integer[N];
83 <            for (int i = 0; i < N-1; ++i)
82 >            Integer[] ints = new Integer[SIZE];
83 >            for (int i = 0; i < SIZE-1; ++i)
84                  ints[i] = new Integer(i);
85              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
86              fail("Cannot make with null elements");
# Line 94 | Line 90 | public class PriorityQueueTest extends T
90  
91      public void testConstructor6(){
92          try {
93 <            Integer[] ints = new Integer[N];
94 <            for (int i = 0; i < N; ++i)
93 >            Integer[] ints = new Integer[SIZE];
94 >            for (int i = 0; i < SIZE; ++i)
95                  ints[i] = new Integer(i);
96              PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
97 <            for (int i = 0; i < N; ++i)
97 >            for (int i = 0; i < SIZE; ++i)
98                  assertEquals(ints[i], q.poll());
99          }
100          finally {}
# Line 106 | Line 102 | public class PriorityQueueTest extends T
102  
103      public void testConstructor7(){
104          try {
105 <            PriorityQueue q = new PriorityQueue(N, new MyReverseComparator());
106 <            Integer[] ints = new Integer[N];
107 <            for (int i = 0; i < N; ++i)
105 >            MyReverseComparator cmp = new MyReverseComparator();
106 >            PriorityQueue q = new PriorityQueue(SIZE, cmp);
107 >            assertEquals(cmp, q.comparator());
108 >            Integer[] ints = new Integer[SIZE];
109 >            for (int i = 0; i < SIZE; ++i)
110                  ints[i] = new Integer(i);
111              q.addAll(Arrays.asList(ints));
112 <            for (int i = N-1; i >= 0; --i)
112 >            for (int i = SIZE-1; i >= 0; --i)
113                  assertEquals(ints[i], q.poll());
114          }
115          finally {}
# Line 129 | Line 127 | public class PriorityQueueTest extends T
127      }
128  
129      public void testSize() {
130 <        PriorityQueue q = fullQueue(N);
131 <        for (int i = 0; i < N; ++i) {
132 <            assertEquals(N-i, q.size());
130 >        PriorityQueue q = populatedQueue(SIZE);
131 >        for (int i = 0; i < SIZE; ++i) {
132 >            assertEquals(SIZE-i, q.size());
133              q.remove();
134          }
135 <        for (int i = 0; i < N; ++i) {
135 >        for (int i = 0; i < SIZE; ++i) {
136              assertEquals(i, q.size());
137              q.add(new Integer(i));
138          }
# Line 166 | Line 164 | public class PriorityQueueTest extends T
164      }
165  
166      public void testAdd(){
167 <        PriorityQueue q = new PriorityQueue(N);
168 <        for (int i = 0; i < N; ++i) {
167 >        PriorityQueue q = new PriorityQueue(SIZE);
168 >        for (int i = 0; i < SIZE; ++i) {
169              assertEquals(i, q.size());
170              assertTrue(q.add(new Integer(i)));
171          }
# Line 183 | Line 181 | public class PriorityQueueTest extends T
181      }
182      public void testAddAll2(){
183          try {
184 <            PriorityQueue q = new PriorityQueue(N);
185 <            Integer[] ints = new Integer[N];
184 >            PriorityQueue q = new PriorityQueue(SIZE);
185 >            Integer[] ints = new Integer[SIZE];
186              q.addAll(Arrays.asList(ints));
187              fail("Cannot add null elements");
188          }
# Line 192 | Line 190 | public class PriorityQueueTest extends T
190      }
191      public void testAddAll3(){
192          try {
193 <            PriorityQueue q = new PriorityQueue(N);
194 <            Integer[] ints = new Integer[N];
195 <            for (int i = 0; i < N-1; ++i)
193 >            PriorityQueue q = new PriorityQueue(SIZE);
194 >            Integer[] ints = new Integer[SIZE];
195 >            for (int i = 0; i < SIZE-1; ++i)
196                  ints[i] = new Integer(i);
197              q.addAll(Arrays.asList(ints));
198              fail("Cannot add null elements");
# Line 205 | Line 203 | public class PriorityQueueTest extends T
203      public void testAddAll5(){
204          try {
205              Integer[] empty = new Integer[0];
206 <            Integer[] ints = new Integer[N];
207 <            for (int i = 0; i < N; ++i)
208 <                ints[i] = new Integer(N-1-i);
209 <            PriorityQueue q = new PriorityQueue(N);
206 >            Integer[] ints = new Integer[SIZE];
207 >            for (int i = 0; i < SIZE; ++i)
208 >                ints[i] = new Integer(SIZE-1-i);
209 >            PriorityQueue q = new PriorityQueue(SIZE);
210              assertFalse(q.addAll(Arrays.asList(empty)));
211              assertTrue(q.addAll(Arrays.asList(ints)));
212 <            for (int i = 0; i < N; ++i)
212 >            for (int i = 0; i < SIZE; ++i)
213                  assertEquals(new Integer(i), q.poll());
214          }
215          finally {}
216      }
217  
218      public void testPoll(){
219 <        PriorityQueue q = fullQueue(N);
220 <        for (int i = 0; i < N; ++i) {
219 >        PriorityQueue q = populatedQueue(SIZE);
220 >        for (int i = 0; i < SIZE; ++i) {
221              assertEquals(i, ((Integer)q.poll()).intValue());
222          }
223          assertNull(q.poll());
224      }
225  
226      public void testPeek(){
227 <        PriorityQueue q = fullQueue(N);
228 <        for (int i = 0; i < N; ++i) {
227 >        PriorityQueue q = populatedQueue(SIZE);
228 >        for (int i = 0; i < SIZE; ++i) {
229              assertEquals(i, ((Integer)q.peek()).intValue());
230              q.poll();
231              assertTrue(q.peek() == null ||
# Line 237 | Line 235 | public class PriorityQueueTest extends T
235      }
236  
237      public void testElement(){
238 <        PriorityQueue q = fullQueue(N);
239 <        for (int i = 0; i < N; ++i) {
238 >        PriorityQueue q = populatedQueue(SIZE);
239 >        for (int i = 0; i < SIZE; ++i) {
240              assertEquals(i, ((Integer)q.element()).intValue());
241              q.poll();
242          }
# Line 250 | Line 248 | public class PriorityQueueTest extends T
248      }
249  
250      public void testRemove(){
251 <        PriorityQueue q = fullQueue(N);
252 <        for (int i = 0; i < N; ++i) {
251 >        PriorityQueue q = populatedQueue(SIZE);
252 >        for (int i = 0; i < SIZE; ++i) {
253              assertEquals(i, ((Integer)q.remove()).intValue());
254          }
255          try {
# Line 262 | Line 260 | public class PriorityQueueTest extends T
260      }
261  
262      public void testRemoveElement(){
263 <        PriorityQueue q = fullQueue(N);
264 <        for (int i = 1; i < N; i+=2) {
263 >        PriorityQueue q = populatedQueue(SIZE);
264 >        for (int i = 1; i < SIZE; i+=2) {
265              assertTrue(q.remove(new Integer(i)));
266          }
267 <        for (int i = 0; i < N; i+=2) {
267 >        for (int i = 0; i < SIZE; i+=2) {
268              assertTrue(q.remove(new Integer(i)));
269              assertFalse(q.remove(new Integer(i+1)));
270          }
271 <        assert(q.isEmpty());
271 >        assertTrue(q.isEmpty());
272      }
273          
274      public void testContains(){
275 <        PriorityQueue q = fullQueue(N);
276 <        for (int i = 0; i < N; ++i) {
275 >        PriorityQueue q = populatedQueue(SIZE);
276 >        for (int i = 0; i < SIZE; ++i) {
277              assertTrue(q.contains(new Integer(i)));
278              q.poll();
279              assertFalse(q.contains(new Integer(i)));
# Line 283 | Line 281 | public class PriorityQueueTest extends T
281      }
282  
283      public void testClear(){
284 <        PriorityQueue q = fullQueue(N);
284 >        PriorityQueue q = populatedQueue(SIZE);
285          q.clear();
286          assertTrue(q.isEmpty());
287          assertEquals(0, q.size());
# Line 294 | Line 292 | public class PriorityQueueTest extends T
292      }
293  
294      public void testContainsAll(){
295 <        PriorityQueue q = fullQueue(N);
296 <        PriorityQueue p = new PriorityQueue(N);
297 <        for (int i = 0; i < N; ++i) {
295 >        PriorityQueue q = populatedQueue(SIZE);
296 >        PriorityQueue p = new PriorityQueue(SIZE);
297 >        for (int i = 0; i < SIZE; ++i) {
298              assertTrue(q.containsAll(p));
299              assertFalse(p.containsAll(q));
300              p.add(new Integer(i));
# Line 305 | Line 303 | public class PriorityQueueTest extends T
303      }
304  
305      public void testRetainAll(){
306 <        PriorityQueue q = fullQueue(N);
307 <        PriorityQueue p = fullQueue(N);
308 <        for (int i = 0; i < N; ++i) {
306 >        PriorityQueue q = populatedQueue(SIZE);
307 >        PriorityQueue p = populatedQueue(SIZE);
308 >        for (int i = 0; i < SIZE; ++i) {
309              boolean changed = q.retainAll(p);
310              if (i == 0)
311                  assertFalse(changed);
# Line 315 | Line 313 | public class PriorityQueueTest extends T
313                  assertTrue(changed);
314  
315              assertTrue(q.containsAll(p));
316 <            assertEquals(N-i, q.size());
316 >            assertEquals(SIZE-i, q.size());
317              p.remove();
318          }
319      }
320  
321      public void testRemoveAll(){
322 <        for (int i = 1; i < N; ++i) {
323 <            PriorityQueue q = fullQueue(N);
324 <            PriorityQueue p = fullQueue(i);
322 >        for (int i = 1; i < SIZE; ++i) {
323 >            PriorityQueue q = populatedQueue(SIZE);
324 >            PriorityQueue p = populatedQueue(i);
325              assertTrue(q.removeAll(p));
326 <            assertEquals(N-i, q.size());
326 >            assertEquals(SIZE-i, q.size());
327              for (int j = 0; j < i; ++j) {
328                  Integer I = (Integer)(p.remove());
329                  assertFalse(q.contains(I));
# Line 334 | Line 332 | public class PriorityQueueTest extends T
332      }
333  
334      public void testToArray(){
335 <        PriorityQueue q = fullQueue(N);
335 >        PriorityQueue q = populatedQueue(SIZE);
336          Object[] o = q.toArray();
337          Arrays.sort(o);
338          for(int i = 0; i < o.length; i++)
# Line 342 | Line 340 | public class PriorityQueueTest extends T
340      }
341  
342      public void testToArray2(){
343 <        PriorityQueue q = fullQueue(N);
344 <        Integer[] ints = new Integer[N];
343 >        PriorityQueue q = populatedQueue(SIZE);
344 >        Integer[] ints = new Integer[SIZE];
345          ints = (Integer[])q.toArray(ints);
346          Arrays.sort(ints);
347          for(int i = 0; i < ints.length; i++)
# Line 351 | Line 349 | public class PriorityQueueTest extends T
349      }
350      
351      public void testIterator(){
352 <        PriorityQueue q = fullQueue(N);
352 >        PriorityQueue q = populatedQueue(SIZE);
353          int i = 0;
354          Iterator it = q.iterator();
355          while(it.hasNext()) {
356              assertTrue(q.contains(it.next()));
357              ++i;
358          }
359 <        assertEquals(i, N);
359 >        assertEquals(i, SIZE);
360      }
361  
362      public void testIteratorRemove () {
# Line 381 | Line 379 | public class PriorityQueueTest extends T
379  
380  
381      public void testToString(){
382 <        PriorityQueue q = fullQueue(N);
382 >        PriorityQueue q = populatedQueue(SIZE);
383          String s = q.toString();
384 <        for (int i = 0; i < N; ++i) {
384 >        for (int i = 0; i < SIZE; ++i) {
385              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
386          }
387      }        
388  
389 +    public void testSerialization() {
390 +        PriorityQueue q = populatedQueue(SIZE);
391 +        try {
392 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
393 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
394 +            out.writeObject(q);
395 +            out.close();
396 +
397 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
398 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
399 +            PriorityQueue r = (PriorityQueue)in.readObject();
400 +            assertEquals(q.size(), r.size());
401 +            while (!q.isEmpty())
402 +                assertEquals(q.remove(), r.remove());
403 +        } catch(Exception e){
404 +            fail("unexpected exception");
405 +        }
406 +    }
407   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines