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.3 by dl, Sun Sep 14 20:42:40 2003 UTC

# Line 10 | Line 10 | import java.util.*;
10   import java.util.concurrent.*;
11   import java.io.*;
12  
13 < public class PriorityQueueTest extends TestCase {
14 <
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;
13 > public class PriorityQueueTest extends JSR166TestCase {
14  
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
# Line 40 | 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 53 | 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 75 | 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 84 | 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 95 | 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 107 | 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 >            PriorityQueue q = new PriorityQueue(SIZE, new MyReverseComparator());
106 >            Integer[] ints = new Integer[SIZE];
107 >            for (int i = 0; i < SIZE; ++i)
108                  ints[i] = new Integer(i);
109              q.addAll(Arrays.asList(ints));
110 <            for (int i = N-1; i >= 0; --i)
110 >            for (int i = SIZE-1; i >= 0; --i)
111                  assertEquals(ints[i], q.poll());
112          }
113          finally {}
# Line 130 | Line 125 | public class PriorityQueueTest extends T
125      }
126  
127      public void testSize() {
128 <        PriorityQueue q = fullQueue(N);
129 <        for (int i = 0; i < N; ++i) {
130 <            assertEquals(N-i, q.size());
128 >        PriorityQueue q = populatedQueue(SIZE);
129 >        for (int i = 0; i < SIZE; ++i) {
130 >            assertEquals(SIZE-i, q.size());
131              q.remove();
132          }
133 <        for (int i = 0; i < N; ++i) {
133 >        for (int i = 0; i < SIZE; ++i) {
134              assertEquals(i, q.size());
135              q.add(new Integer(i));
136          }
# Line 167 | Line 162 | public class PriorityQueueTest extends T
162      }
163  
164      public void testAdd(){
165 <        PriorityQueue q = new PriorityQueue(N);
166 <        for (int i = 0; i < N; ++i) {
165 >        PriorityQueue q = new PriorityQueue(SIZE);
166 >        for (int i = 0; i < SIZE; ++i) {
167              assertEquals(i, q.size());
168              assertTrue(q.add(new Integer(i)));
169          }
# Line 184 | Line 179 | public class PriorityQueueTest extends T
179      }
180      public void testAddAll2(){
181          try {
182 <            PriorityQueue q = new PriorityQueue(N);
183 <            Integer[] ints = new Integer[N];
182 >            PriorityQueue q = new PriorityQueue(SIZE);
183 >            Integer[] ints = new Integer[SIZE];
184              q.addAll(Arrays.asList(ints));
185              fail("Cannot add null elements");
186          }
# Line 193 | Line 188 | public class PriorityQueueTest extends T
188      }
189      public void testAddAll3(){
190          try {
191 <            PriorityQueue q = new PriorityQueue(N);
192 <            Integer[] ints = new Integer[N];
193 <            for (int i = 0; i < N-1; ++i)
191 >            PriorityQueue q = new PriorityQueue(SIZE);
192 >            Integer[] ints = new Integer[SIZE];
193 >            for (int i = 0; i < SIZE-1; ++i)
194                  ints[i] = new Integer(i);
195              q.addAll(Arrays.asList(ints));
196              fail("Cannot add null elements");
# Line 206 | Line 201 | public class PriorityQueueTest extends T
201      public void testAddAll5(){
202          try {
203              Integer[] empty = new Integer[0];
204 <            Integer[] ints = new Integer[N];
205 <            for (int i = 0; i < N; ++i)
206 <                ints[i] = new Integer(N-1-i);
207 <            PriorityQueue q = new PriorityQueue(N);
204 >            Integer[] ints = new Integer[SIZE];
205 >            for (int i = 0; i < SIZE; ++i)
206 >                ints[i] = new Integer(SIZE-1-i);
207 >            PriorityQueue q = new PriorityQueue(SIZE);
208              assertFalse(q.addAll(Arrays.asList(empty)));
209              assertTrue(q.addAll(Arrays.asList(ints)));
210 <            for (int i = 0; i < N; ++i)
210 >            for (int i = 0; i < SIZE; ++i)
211                  assertEquals(new Integer(i), q.poll());
212          }
213          finally {}
214      }
215  
216      public void testPoll(){
217 <        PriorityQueue q = fullQueue(N);
218 <        for (int i = 0; i < N; ++i) {
217 >        PriorityQueue q = populatedQueue(SIZE);
218 >        for (int i = 0; i < SIZE; ++i) {
219              assertEquals(i, ((Integer)q.poll()).intValue());
220          }
221          assertNull(q.poll());
222      }
223  
224      public void testPeek(){
225 <        PriorityQueue q = fullQueue(N);
226 <        for (int i = 0; i < N; ++i) {
225 >        PriorityQueue q = populatedQueue(SIZE);
226 >        for (int i = 0; i < SIZE; ++i) {
227              assertEquals(i, ((Integer)q.peek()).intValue());
228              q.poll();
229              assertTrue(q.peek() == null ||
# Line 238 | Line 233 | public class PriorityQueueTest extends T
233      }
234  
235      public void testElement(){
236 <        PriorityQueue q = fullQueue(N);
237 <        for (int i = 0; i < N; ++i) {
236 >        PriorityQueue q = populatedQueue(SIZE);
237 >        for (int i = 0; i < SIZE; ++i) {
238              assertEquals(i, ((Integer)q.element()).intValue());
239              q.poll();
240          }
# Line 251 | Line 246 | public class PriorityQueueTest extends T
246      }
247  
248      public void testRemove(){
249 <        PriorityQueue q = fullQueue(N);
250 <        for (int i = 0; i < N; ++i) {
249 >        PriorityQueue q = populatedQueue(SIZE);
250 >        for (int i = 0; i < SIZE; ++i) {
251              assertEquals(i, ((Integer)q.remove()).intValue());
252          }
253          try {
# Line 263 | Line 258 | public class PriorityQueueTest extends T
258      }
259  
260      public void testRemoveElement(){
261 <        PriorityQueue q = fullQueue(N);
262 <        for (int i = 1; i < N; i+=2) {
261 >        PriorityQueue q = populatedQueue(SIZE);
262 >        for (int i = 1; i < SIZE; i+=2) {
263              assertTrue(q.remove(new Integer(i)));
264          }
265 <        for (int i = 0; i < N; i+=2) {
265 >        for (int i = 0; i < SIZE; i+=2) {
266              assertTrue(q.remove(new Integer(i)));
267              assertFalse(q.remove(new Integer(i+1)));
268          }
# Line 275 | Line 270 | public class PriorityQueueTest extends T
270      }
271          
272      public void testContains(){
273 <        PriorityQueue q = fullQueue(N);
274 <        for (int i = 0; i < N; ++i) {
273 >        PriorityQueue q = populatedQueue(SIZE);
274 >        for (int i = 0; i < SIZE; ++i) {
275              assertTrue(q.contains(new Integer(i)));
276              q.poll();
277              assertFalse(q.contains(new Integer(i)));
# Line 284 | Line 279 | public class PriorityQueueTest extends T
279      }
280  
281      public void testClear(){
282 <        PriorityQueue q = fullQueue(N);
282 >        PriorityQueue q = populatedQueue(SIZE);
283          q.clear();
284          assertTrue(q.isEmpty());
285          assertEquals(0, q.size());
# Line 295 | Line 290 | public class PriorityQueueTest extends T
290      }
291  
292      public void testContainsAll(){
293 <        PriorityQueue q = fullQueue(N);
294 <        PriorityQueue p = new PriorityQueue(N);
295 <        for (int i = 0; i < N; ++i) {
293 >        PriorityQueue q = populatedQueue(SIZE);
294 >        PriorityQueue p = new PriorityQueue(SIZE);
295 >        for (int i = 0; i < SIZE; ++i) {
296              assertTrue(q.containsAll(p));
297              assertFalse(p.containsAll(q));
298              p.add(new Integer(i));
# Line 306 | Line 301 | public class PriorityQueueTest extends T
301      }
302  
303      public void testRetainAll(){
304 <        PriorityQueue q = fullQueue(N);
305 <        PriorityQueue p = fullQueue(N);
306 <        for (int i = 0; i < N; ++i) {
304 >        PriorityQueue q = populatedQueue(SIZE);
305 >        PriorityQueue p = populatedQueue(SIZE);
306 >        for (int i = 0; i < SIZE; ++i) {
307              boolean changed = q.retainAll(p);
308              if (i == 0)
309                  assertFalse(changed);
# Line 316 | Line 311 | public class PriorityQueueTest extends T
311                  assertTrue(changed);
312  
313              assertTrue(q.containsAll(p));
314 <            assertEquals(N-i, q.size());
314 >            assertEquals(SIZE-i, q.size());
315              p.remove();
316          }
317      }
318  
319      public void testRemoveAll(){
320 <        for (int i = 1; i < N; ++i) {
321 <            PriorityQueue q = fullQueue(N);
322 <            PriorityQueue p = fullQueue(i);
320 >        for (int i = 1; i < SIZE; ++i) {
321 >            PriorityQueue q = populatedQueue(SIZE);
322 >            PriorityQueue p = populatedQueue(i);
323              assertTrue(q.removeAll(p));
324 <            assertEquals(N-i, q.size());
324 >            assertEquals(SIZE-i, q.size());
325              for (int j = 0; j < i; ++j) {
326                  Integer I = (Integer)(p.remove());
327                  assertFalse(q.contains(I));
# Line 335 | Line 330 | public class PriorityQueueTest extends T
330      }
331  
332      public void testToArray(){
333 <        PriorityQueue q = fullQueue(N);
333 >        PriorityQueue q = populatedQueue(SIZE);
334          Object[] o = q.toArray();
335          Arrays.sort(o);
336          for(int i = 0; i < o.length; i++)
# Line 343 | Line 338 | public class PriorityQueueTest extends T
338      }
339  
340      public void testToArray2(){
341 <        PriorityQueue q = fullQueue(N);
342 <        Integer[] ints = new Integer[N];
341 >        PriorityQueue q = populatedQueue(SIZE);
342 >        Integer[] ints = new Integer[SIZE];
343          ints = (Integer[])q.toArray(ints);
344          Arrays.sort(ints);
345          for(int i = 0; i < ints.length; i++)
# Line 352 | Line 347 | public class PriorityQueueTest extends T
347      }
348      
349      public void testIterator(){
350 <        PriorityQueue q = fullQueue(N);
350 >        PriorityQueue q = populatedQueue(SIZE);
351          int i = 0;
352          Iterator it = q.iterator();
353          while(it.hasNext()) {
354              assertTrue(q.contains(it.next()));
355              ++i;
356          }
357 <        assertEquals(i, N);
357 >        assertEquals(i, SIZE);
358      }
359  
360      public void testIteratorRemove () {
# Line 382 | Line 377 | public class PriorityQueueTest extends T
377  
378  
379      public void testToString(){
380 <        PriorityQueue q = fullQueue(N);
380 >        PriorityQueue q = populatedQueue(SIZE);
381          String s = q.toString();
382 <        for (int i = 0; i < N; ++i) {
382 >        for (int i = 0; i < SIZE; ++i) {
383              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
384          }
385      }        
386  
387      public void testSerialization() {
388 <        PriorityQueue q = fullQueue(N);
388 >        PriorityQueue q = populatedQueue(SIZE);
389          try {
390              ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
391              ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines