ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentLinkedQueueTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.5 by dl, Sun Oct 5 23:00:40 2003 UTC

# Line 10 | Line 10 | import java.util.*;
10   import java.util.concurrent.*;
11   import java.io.*;
12  
13 < public class ConcurrentLinkedQueueTest extends TestCase {
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 ConcurrentLinkedQueueTest extends JSR166TestCase {
14  
15      public static void main(String[] args) {
16          junit.textui.TestRunner.run (suite());  
# Line 28 | Line 24 | public class ConcurrentLinkedQueueTest e
24       * Create a queue of given size containing consecutive
25       * Integers 0 ... n.
26       */
27 <    private ConcurrentLinkedQueue fullQueue(int n) {
27 >    private ConcurrentLinkedQueue populatedQueue(int n) {
28          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
29          assertTrue(q.isEmpty());
30          for(int i = 0; i < n; ++i)
# Line 38 | Line 34 | public class ConcurrentLinkedQueueTest e
34          return q;
35      }
36  
37 <    public void testConstructor1(){
37 >    /**
38 >     * new queue is empty
39 >     */
40 >    public void testConstructor1() {
41          assertEquals(0, new ConcurrentLinkedQueue().size());
42      }
43  
44 +    /**
45 +     *  Initializing from null Collection throws NPE
46 +     */
47      public void testConstructor3() {
48          try {
49              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
50 <            fail("Cannot make from null collection");
50 >            shouldThrow();
51          }
52          catch (NullPointerException success) {}
53      }
54  
55 <    public void testConstructor4(){
55 >    /**
56 >     * Initializing from Collection of null elements throws NPE
57 >     */
58 >    public void testConstructor4() {
59          try {
60 <            Integer[] ints = new Integer[N];
60 >            Integer[] ints = new Integer[SIZE];
61              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
62 <            fail("Cannot make with null elements");
62 >            shouldThrow();
63          }
64          catch (NullPointerException success) {}
65      }
66  
67 <    public void testConstructor5(){
67 >    /**
68 >     * Initializing from Collection with some null elements throws NPE
69 >     */
70 >    public void testConstructor5() {
71          try {
72 <            Integer[] ints = new Integer[N];
73 <            for (int i = 0; i < N-1; ++i)
72 >            Integer[] ints = new Integer[SIZE];
73 >            for (int i = 0; i < SIZE-1; ++i)
74                  ints[i] = new Integer(i);
75              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
76 <            fail("Cannot make with null elements");
76 >            shouldThrow();
77          }
78          catch (NullPointerException success) {}
79      }
80  
81 <    public void testConstructor6(){
81 >    /**
82 >     * Queue contains all elements of collection used to initialize
83 >     */
84 >    public void testConstructor6() {
85          try {
86 <            Integer[] ints = new Integer[N];
87 <            for (int i = 0; i < N; ++i)
86 >            Integer[] ints = new Integer[SIZE];
87 >            for (int i = 0; i < SIZE; ++i)
88                  ints[i] = new Integer(i);
89              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
90 <            for (int i = 0; i < N; ++i)
90 >            for (int i = 0; i < SIZE; ++i)
91                  assertEquals(ints[i], q.poll());
92          }
93          finally {}
94      }
95  
96 +    /**
97 +     * isEmpty is true before add, false after
98 +     */
99      public void testEmpty() {
100          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
101          assertTrue(q.isEmpty());
102 <        q.add(new Integer(1));
102 >        q.add(one);
103          assertFalse(q.isEmpty());
104 <        q.add(new Integer(2));
104 >        q.add(two);
105          q.remove();
106          q.remove();
107          assertTrue(q.isEmpty());
108      }
109  
110 +    /**
111 +     * size changes when elements added and removed
112 +     */
113      public void testSize() {
114 <        ConcurrentLinkedQueue q = fullQueue(N);
115 <        for (int i = 0; i < N; ++i) {
116 <            assertEquals(N-i, q.size());
114 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
115 >        for (int i = 0; i < SIZE; ++i) {
116 >            assertEquals(SIZE-i, q.size());
117              q.remove();
118          }
119 <        for (int i = 0; i < N; ++i) {
119 >        for (int i = 0; i < SIZE; ++i) {
120              assertEquals(i, q.size());
121              q.add(new Integer(i));
122          }
123      }
124  
125 <    public void testOfferNull(){
125 >    /**
126 >     * offer(null) throws NPE
127 >     */
128 >    public void testOfferNull() {
129          try {
130              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
131              q.offer(null);
132 <            fail("should throw NPE");
132 >            shouldThrow();
133 >        } catch (NullPointerException success) { }  
134 >    }
135 >
136 >    /**
137 >     * add(null) throws NPE
138 >     */
139 >    public void testAddNull() {
140 >        try {
141 >            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
142 >            q.add(null);
143 >            shouldThrow();
144          } catch (NullPointerException success) { }  
145      }
146  
147 +
148 +    /**
149 +     * Offer returns true
150 +     */
151      public void testOffer() {
152          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
153 <        assertTrue(q.offer(new Integer(0)));
154 <        assertTrue(q.offer(new Integer(1)));
153 >        assertTrue(q.offer(zero));
154 >        assertTrue(q.offer(one));
155      }
156  
157 <    public void testAdd(){
157 >    /**
158 >     * add returns true
159 >     */
160 >    public void testAdd() {
161          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
162 <        for (int i = 0; i < N; ++i) {
162 >        for (int i = 0; i < SIZE; ++i) {
163              assertEquals(i, q.size());
164              assertTrue(q.add(new Integer(i)));
165          }
166      }
167  
168 <    public void testAddAll1(){
168 >    /**
169 >     * addAll(null) throws NPE
170 >     */
171 >    public void testAddAll1() {
172          try {
173              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
174              q.addAll(null);
175 <            fail("Cannot add null collection");
175 >            shouldThrow();
176          }
177          catch (NullPointerException success) {}
178      }
179 <    public void testAddAll2(){
179 >
180 >    /**
181 >     * addAll(this) throws IAE
182 >     */
183 >    public void testAddAllSelf() {
184 >        try {
185 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
186 >            q.addAll(q);
187 >            shouldThrow();
188 >        }
189 >        catch (IllegalArgumentException success) {}
190 >    }
191 >
192 >    /**
193 >     * addAll of a collection with null elements throws NPE
194 >     */
195 >    public void testAddAll2() {
196          try {
197              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
198 <            Integer[] ints = new Integer[N];
198 >            Integer[] ints = new Integer[SIZE];
199              q.addAll(Arrays.asList(ints));
200 <            fail("Cannot add null elements");
200 >            shouldThrow();
201          }
202          catch (NullPointerException success) {}
203      }
204 <    public void testAddAll3(){
204 >    /**
205 >     *  addAll of a collection with any null elements throws NPE after
206 >     * possibly adding some elements
207 >     */
208 >    public void testAddAll3() {
209          try {
210              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
211 <            Integer[] ints = new Integer[N];
212 <            for (int i = 0; i < N-1; ++i)
211 >            Integer[] ints = new Integer[SIZE];
212 >            for (int i = 0; i < SIZE-1; ++i)
213                  ints[i] = new Integer(i);
214              q.addAll(Arrays.asList(ints));
215 <            fail("Cannot add null elements");
215 >            shouldThrow();
216          }
217          catch (NullPointerException success) {}
218      }
219  
220 <    public void testAddAll5(){
220 >    /**
221 >     * Queue contains all elements, in traversal order, of successful addAll
222 >     */
223 >    public void testAddAll5() {
224          try {
225              Integer[] empty = new Integer[0];
226 <            Integer[] ints = new Integer[N];
227 <            for (int i = 0; i < N; ++i)
226 >            Integer[] ints = new Integer[SIZE];
227 >            for (int i = 0; i < SIZE; ++i)
228                  ints[i] = new Integer(i);
229              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
230              assertFalse(q.addAll(Arrays.asList(empty)));
231              assertTrue(q.addAll(Arrays.asList(ints)));
232 <            for (int i = 0; i < N; ++i)
232 >            for (int i = 0; i < SIZE; ++i)
233                  assertEquals(ints[i], q.poll());
234          }
235          finally {}
236      }
237  
238 <    public void testPoll(){
239 <        ConcurrentLinkedQueue q = fullQueue(N);
240 <        for (int i = 0; i < N; ++i) {
238 >    /**
239 >     * poll succeeds unless empty
240 >     */
241 >    public void testPoll() {
242 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
243 >        for (int i = 0; i < SIZE; ++i) {
244              assertEquals(i, ((Integer)q.poll()).intValue());
245          }
246          assertNull(q.poll());
247      }
248  
249 <    public void testPeek(){
250 <        ConcurrentLinkedQueue q = fullQueue(N);
251 <        for (int i = 0; i < N; ++i) {
249 >    /**
250 >     * peek returns next element, or null if empty
251 >     */
252 >    public void testPeek() {
253 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
254 >        for (int i = 0; i < SIZE; ++i) {
255              assertEquals(i, ((Integer)q.peek()).intValue());
256              q.poll();
257              assertTrue(q.peek() == null ||
# Line 190 | Line 260 | public class ConcurrentLinkedQueueTest e
260          assertNull(q.peek());
261      }
262  
263 <    public void testElement(){
264 <        ConcurrentLinkedQueue q = fullQueue(N);
265 <        for (int i = 0; i < N; ++i) {
263 >    /**
264 >     * element returns next element, or throws NSEE if empty
265 >     */
266 >    public void testElement() {
267 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
268 >        for (int i = 0; i < SIZE; ++i) {
269              assertEquals(i, ((Integer)q.element()).intValue());
270              q.poll();
271          }
272          try {
273              q.element();
274 <            fail("no such element");
274 >            shouldThrow();
275          }
276          catch (NoSuchElementException success) {}
277      }
278  
279 <    public void testRemove(){
280 <        ConcurrentLinkedQueue q = fullQueue(N);
281 <        for (int i = 0; i < N; ++i) {
279 >    /**
280 >     *  remove removes next element, or throws NSEE if empty
281 >     */
282 >    public void testRemove() {
283 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
284 >        for (int i = 0; i < SIZE; ++i) {
285              assertEquals(i, ((Integer)q.remove()).intValue());
286          }
287          try {
288              q.remove();
289 <            fail("remove should throw");
289 >            shouldThrow();
290          } catch (NoSuchElementException success){
291          }  
292      }
293  
294 <    public void testRemoveElement(){
295 <        ConcurrentLinkedQueue q = fullQueue(N);
296 <        for (int i = 1; i < N; i+=2) {
294 >    /**
295 >     * remove(x) removes x and returns true if present
296 >     */
297 >    public void testRemoveElement() {
298 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
299 >        for (int i = 1; i < SIZE; i+=2) {
300              assertTrue(q.remove(new Integer(i)));
301          }
302 <        for (int i = 0; i < N; i+=2) {
302 >        for (int i = 0; i < SIZE; i+=2) {
303              assertTrue(q.remove(new Integer(i)));
304              assertFalse(q.remove(new Integer(i+1)));
305          }
306          assertTrue(q.isEmpty());
307      }
308          
309 <    public void testContains(){
310 <        ConcurrentLinkedQueue q = fullQueue(N);
311 <        for (int i = 0; i < N; ++i) {
309 >    /**
310 >     * contains(x) reports true when elements added but not yet removed
311 >     */
312 >    public void testContains() {
313 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
314 >        for (int i = 0; i < SIZE; ++i) {
315              assertTrue(q.contains(new Integer(i)));
316              q.poll();
317              assertFalse(q.contains(new Integer(i)));
318          }
319      }
320  
321 <    public void testClear(){
322 <        ConcurrentLinkedQueue q = fullQueue(N);
321 >    /**
322 >     * clear removes all elements
323 >     */
324 >    public void testClear() {
325 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
326          q.clear();
327          assertTrue(q.isEmpty());
328          assertEquals(0, q.size());
329 <        q.add(new Integer(1));
329 >        q.add(one);
330          assertFalse(q.isEmpty());
331          q.clear();
332          assertTrue(q.isEmpty());
333      }
334  
335 <    public void testContainsAll(){
336 <        ConcurrentLinkedQueue q = fullQueue(N);
335 >    /**
336 >     * containsAll(c) is true when c contains a subset of elements
337 >     */
338 >    public void testContainsAll() {
339 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
340          ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
341 <        for (int i = 0; i < N; ++i) {
341 >        for (int i = 0; i < SIZE; ++i) {
342              assertTrue(q.containsAll(p));
343              assertFalse(p.containsAll(q));
344              p.add(new Integer(i));
# Line 258 | Line 346 | public class ConcurrentLinkedQueueTest e
346          assertTrue(p.containsAll(q));
347      }
348  
349 <    public void testRetainAll(){
350 <        ConcurrentLinkedQueue q = fullQueue(N);
351 <        ConcurrentLinkedQueue p = fullQueue(N);
352 <        for (int i = 0; i < N; ++i) {
349 >    /**
350 >     * retainAll(c) retains only those elements of c and reports true if change
351 >     */
352 >    public void testRetainAll() {
353 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
354 >        ConcurrentLinkedQueue p = populatedQueue(SIZE);
355 >        for (int i = 0; i < SIZE; ++i) {
356              boolean changed = q.retainAll(p);
357              if (i == 0)
358                  assertFalse(changed);
# Line 269 | Line 360 | public class ConcurrentLinkedQueueTest e
360                  assertTrue(changed);
361  
362              assertTrue(q.containsAll(p));
363 <            assertEquals(N-i, q.size());
363 >            assertEquals(SIZE-i, q.size());
364              p.remove();
365          }
366      }
367  
368 <    public void testRemoveAll(){
369 <        for (int i = 1; i < N; ++i) {
370 <            ConcurrentLinkedQueue q = fullQueue(N);
371 <            ConcurrentLinkedQueue p = fullQueue(i);
368 >    /**
369 >     * removeAll(c) removes only those elements of c and reports true if changed
370 >     */
371 >    public void testRemoveAll() {
372 >        for (int i = 1; i < SIZE; ++i) {
373 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
374 >            ConcurrentLinkedQueue p = populatedQueue(i);
375              assertTrue(q.removeAll(p));
376 <            assertEquals(N-i, q.size());
376 >            assertEquals(SIZE-i, q.size());
377              for (int j = 0; j < i; ++j) {
378                  Integer I = (Integer)(p.remove());
379                  assertFalse(q.contains(I));
# Line 287 | Line 381 | public class ConcurrentLinkedQueueTest e
381          }
382      }
383  
384 <    public void testToArray(){
385 <        ConcurrentLinkedQueue q = fullQueue(N);
384 >    /**
385 >     * toArray contains all elements
386 >     */
387 >    public void testToArray() {
388 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
389          Object[] o = q.toArray();
390          Arrays.sort(o);
391          for(int i = 0; i < o.length; i++)
392              assertEquals(o[i], q.poll());
393      }
394  
395 <    public void testToArray2(){
396 <        ConcurrentLinkedQueue q = fullQueue(N);
397 <        Integer[] ints = new Integer[N];
395 >    /**
396 >     *  toArray(a) contains all elements
397 >     */
398 >    public void testToArray2() {
399 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
400 >        Integer[] ints = new Integer[SIZE];
401          ints = (Integer[])q.toArray(ints);
402          Arrays.sort(ints);
403          for(int i = 0; i < ints.length; i++)
404              assertEquals(ints[i], q.poll());
405      }
406 +
407 +    /**
408 +     * toArray(null) throws NPE
409 +     */
410 +    public void testToArray_BadArg() {
411 +        try {
412 +            ConcurrentLinkedQueue q = populatedQueue(SIZE);
413 +            Object o[] = q.toArray(null);
414 +            shouldThrow();
415 +        } catch(NullPointerException success){}
416 +    }
417 +
418 +    /**
419 +     * toArray with incompatable array type throws CCE
420 +     */
421 +    public void testToArray1_BadArg() {
422 +        try {
423 +            ConcurrentLinkedQueue q = populatedQueue(SIZE);
424 +            Object o[] = q.toArray(new String[10] );
425 +            shouldThrow();
426 +        } catch(ArrayStoreException  success){}
427 +    }
428      
429 <    public void testIterator(){
430 <        ConcurrentLinkedQueue q = fullQueue(N);
429 >    /**
430 >     *  iterator iterates through all elements
431 >     */
432 >    public void testIterator() {
433 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
434          int i = 0;
435          Iterator it = q.iterator();
436          while(it.hasNext()) {
437              assertTrue(q.contains(it.next()));
438              ++i;
439          }
440 <        assertEquals(i, N);
440 >        assertEquals(i, SIZE);
441      }
442  
443 +    /**
444 +     * iterator ordering is FIFO
445 +     */
446      public void testIteratorOrdering() {
319
447          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
448 <
449 <        q.add(new Integer(1));
450 <        q.add(new Integer(2));
324 <        q.add(new Integer(3));
448 >        q.add(one);
449 >        q.add(two);
450 >        q.add(three);
451  
452          int k = 0;
453          for (Iterator it = q.iterator(); it.hasNext();) {
454              int i = ((Integer)(it.next())).intValue();
455 <            assertEquals("items should come out in order", ++k, i);
455 >            assertEquals(++k, i);
456          }
457  
458 <        assertEquals("should go through 3 elements", 3, k);
458 >        assertEquals(3, k);
459      }
460  
461 +    /**
462 +     * Modifications do not cause iterators to fail
463 +     */
464      public void testWeaklyConsistentIteration () {
336
465          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
466 <
467 <        q.add(new Integer(1));
468 <        q.add(new Integer(2));
341 <        q.add(new Integer(3));
466 >        q.add(one);
467 >        q.add(two);
468 >        q.add(three);
469  
470          try {
471              for (Iterator it = q.iterator(); it.hasNext();) {
# Line 347 | Line 474 | public class ConcurrentLinkedQueueTest e
474              }
475          }
476          catch (ConcurrentModificationException e) {
477 <            fail("weakly consistent iterator; should not get CME");
477 >            shouldThrow();
478          }
479  
480          assertEquals("queue should be empty again", 0, q.size());
481      }
482  
483 +    /**
484 +     * iterator.remove removes current element
485 +     */
486      public void testIteratorRemove () {
357
487          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
488 <
489 <        q.add(new Integer(1));
490 <        q.add(new Integer(2));
362 <        q.add(new Integer(3));
363 <
488 >        q.add(one);
489 >        q.add(two);
490 >        q.add(three);
491          Iterator it = q.iterator();
492          it.next();
493          it.remove();
367
494          it = q.iterator();
495 <        assertEquals(it.next(), new Integer(2));
496 <        assertEquals(it.next(), new Integer(3));
495 >        assertEquals(it.next(), two);
496 >        assertEquals(it.next(), three);
497          assertFalse(it.hasNext());
498      }
499  
500  
501 <    public void testToString(){
502 <        ConcurrentLinkedQueue q = fullQueue(N);
501 >    /**
502 >     * toString contains toStrings of elements
503 >     */
504 >    public void testToString() {
505 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
506          String s = q.toString();
507 <        for (int i = 0; i < N; ++i) {
507 >        for (int i = 0; i < SIZE; ++i) {
508              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
509          }
510      }        
511  
512 +    /**
513 +     * A deserialized serialized queue has same elements in same order
514 +     */
515      public void testSerialization() {
516 <        ConcurrentLinkedQueue q = fullQueue(N);
516 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
517          try {
518              ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
519              ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
# Line 395 | Line 527 | public class ConcurrentLinkedQueueTest e
527              while (!q.isEmpty())
528                  assertEquals(q.remove(), r.remove());
529          } catch(Exception e){
530 <            e.printStackTrace();
399 <            fail("unexpected exception");
530 >            unexpectedException();
531          }
532      }
533  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines