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.1 by dl, Sun Aug 31 19:24:54 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 18:20:07 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 ConcurrentLinkedQueueTest extends TestCase {
13 <    private static final int N = 10;
14 <    private static final long SHORT_DELAY_MS = 100;
15 <    private static final long MEDIUM_DELAY_MS = 1000;
16 <    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 27 | 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 37 | Line 34 | public class ConcurrentLinkedQueueTest e
34          return q;
35      }
36  
37 <    public void testConstructor1(){
37 >    /**
38 >     *
39 >     */
40 >    public void testConstructor1() {
41          assertEquals(0, new ConcurrentLinkedQueue().size());
42      }
43  
44 +    /**
45 +     *
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 >     *
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 >     *
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 >     *
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 +     *
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 +     *
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 >     *
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 +     *
138 +     */
139      public void testOffer() {
140          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
141 <        assertTrue(q.offer(new Integer(0)));
142 <        assertTrue(q.offer(new Integer(1)));
141 >        assertTrue(q.offer(zero));
142 >        assertTrue(q.offer(one));
143      }
144  
145 <    public void testAdd(){
145 >    /**
146 >     *
147 >     */
148 >    public void testAdd() {
149          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
150 <        for (int i = 0; i < N; ++i) {
150 >        for (int i = 0; i < SIZE; ++i) {
151              assertEquals(i, q.size());
152              assertTrue(q.add(new Integer(i)));
153          }
154      }
155  
156 <    public void testAddAll1(){
156 >    /**
157 >     *
158 >     */
159 >    public void testAddAll1() {
160          try {
161              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
162              q.addAll(null);
163 <            fail("Cannot add null collection");
163 >            shouldThrow();
164          }
165          catch (NullPointerException success) {}
166      }
167 <    public void testAddAll2(){
167 >    /**
168 >     *
169 >     */
170 >    public void testAddAll2() {
171          try {
172              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
173 <            Integer[] ints = new Integer[N];
173 >            Integer[] ints = new Integer[SIZE];
174              q.addAll(Arrays.asList(ints));
175 <            fail("Cannot add null elements");
175 >            shouldThrow();
176          }
177          catch (NullPointerException success) {}
178      }
179 <    public void testAddAll3(){
179 >    /**
180 >     *
181 >     */
182 >    public void testAddAll3() {
183          try {
184              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
185 <            Integer[] ints = new Integer[N];
186 <            for (int i = 0; i < N-1; ++i)
185 >            Integer[] ints = new Integer[SIZE];
186 >            for (int i = 0; i < SIZE-1; ++i)
187                  ints[i] = new Integer(i);
188              q.addAll(Arrays.asList(ints));
189 <            fail("Cannot add null elements");
189 >            shouldThrow();
190          }
191          catch (NullPointerException success) {}
192      }
193  
194 <    public void testAddAll5(){
194 >    /**
195 >     *
196 >     */
197 >    public void testAddAll5() {
198          try {
199              Integer[] empty = new Integer[0];
200 <            Integer[] ints = new Integer[N];
201 <            for (int i = 0; i < N; ++i)
200 >            Integer[] ints = new Integer[SIZE];
201 >            for (int i = 0; i < SIZE; ++i)
202                  ints[i] = new Integer(i);
203              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
204              assertFalse(q.addAll(Arrays.asList(empty)));
205              assertTrue(q.addAll(Arrays.asList(ints)));
206 <            for (int i = 0; i < N; ++i)
206 >            for (int i = 0; i < SIZE; ++i)
207                  assertEquals(ints[i], q.poll());
208          }
209          finally {}
210      }
211  
212 <    public void testPoll(){
213 <        ConcurrentLinkedQueue q = fullQueue(N);
214 <        for (int i = 0; i < N; ++i) {
212 >    /**
213 >     *
214 >     */
215 >    public void testPoll() {
216 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
217 >        for (int i = 0; i < SIZE; ++i) {
218              assertEquals(i, ((Integer)q.poll()).intValue());
219          }
220          assertNull(q.poll());
221      }
222  
223 <    public void testPeek(){
224 <        ConcurrentLinkedQueue q = fullQueue(N);
225 <        for (int i = 0; i < N; ++i) {
223 >    /**
224 >     *
225 >     */
226 >    public void testPeek() {
227 >        ConcurrentLinkedQueue 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 189 | Line 234 | public class ConcurrentLinkedQueueTest e
234          assertNull(q.peek());
235      }
236  
237 <    public void testElement(){
238 <        ConcurrentLinkedQueue q = fullQueue(N);
239 <        for (int i = 0; i < N; ++i) {
237 >    /**
238 >     *
239 >     */
240 >    public void testElement() {
241 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
242 >        for (int i = 0; i < SIZE; ++i) {
243              assertEquals(i, ((Integer)q.element()).intValue());
244              q.poll();
245          }
246          try {
247              q.element();
248 <            fail("no such element");
248 >            shouldThrow();
249          }
250          catch (NoSuchElementException success) {}
251      }
252  
253 <    public void testRemove(){
254 <        ConcurrentLinkedQueue q = fullQueue(N);
255 <        for (int i = 0; i < N; ++i) {
253 >    /**
254 >     *
255 >     */
256 >    public void testRemove() {
257 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
258 >        for (int i = 0; i < SIZE; ++i) {
259              assertEquals(i, ((Integer)q.remove()).intValue());
260          }
261          try {
262              q.remove();
263 <            fail("remove should throw");
263 >            shouldThrow();
264          } catch (NoSuchElementException success){
265          }  
266      }
267  
268 <    public void testRemoveElement(){
269 <        ConcurrentLinkedQueue q = fullQueue(N);
270 <        for (int i = 1; i < N; i+=2) {
268 >    /**
269 >     *
270 >     */
271 >    public void testRemoveElement() {
272 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
273 >        for (int i = 1; i < SIZE; i+=2) {
274              assertTrue(q.remove(new Integer(i)));
275          }
276 <        for (int i = 0; i < N; i+=2) {
276 >        for (int i = 0; i < SIZE; i+=2) {
277              assertTrue(q.remove(new Integer(i)));
278              assertFalse(q.remove(new Integer(i+1)));
279          }
280 <        assert(q.isEmpty());
280 >        assertTrue(q.isEmpty());
281      }
282          
283 <    public void testContains(){
284 <        ConcurrentLinkedQueue q = fullQueue(N);
285 <        for (int i = 0; i < N; ++i) {
283 >    /**
284 >     *
285 >     */
286 >    public void testContains() {
287 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
288 >        for (int i = 0; i < SIZE; ++i) {
289              assertTrue(q.contains(new Integer(i)));
290              q.poll();
291              assertFalse(q.contains(new Integer(i)));
292          }
293      }
294  
295 <    public void testClear(){
296 <        ConcurrentLinkedQueue q = fullQueue(N);
295 >    /**
296 >     *
297 >     */
298 >    public void testClear() {
299 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
300          q.clear();
301          assertTrue(q.isEmpty());
302          assertEquals(0, q.size());
303 <        q.add(new Integer(1));
303 >        q.add(one);
304          assertFalse(q.isEmpty());
305          q.clear();
306          assertTrue(q.isEmpty());
307      }
308  
309 <    public void testContainsAll(){
310 <        ConcurrentLinkedQueue q = fullQueue(N);
309 >    /**
310 >     *
311 >     */
312 >    public void testContainsAll() {
313 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
314          ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
315 <        for (int i = 0; i < N; ++i) {
315 >        for (int i = 0; i < SIZE; ++i) {
316              assertTrue(q.containsAll(p));
317              assertFalse(p.containsAll(q));
318              p.add(new Integer(i));
# Line 257 | Line 320 | public class ConcurrentLinkedQueueTest e
320          assertTrue(p.containsAll(q));
321      }
322  
323 <    public void testRetainAll(){
324 <        ConcurrentLinkedQueue q = fullQueue(N);
325 <        ConcurrentLinkedQueue p = fullQueue(N);
326 <        for (int i = 0; i < N; ++i) {
323 >    /**
324 >     *
325 >     */
326 >    public void testRetainAll() {
327 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
328 >        ConcurrentLinkedQueue p = populatedQueue(SIZE);
329 >        for (int i = 0; i < SIZE; ++i) {
330              boolean changed = q.retainAll(p);
331              if (i == 0)
332                  assertFalse(changed);
# Line 268 | Line 334 | public class ConcurrentLinkedQueueTest e
334                  assertTrue(changed);
335  
336              assertTrue(q.containsAll(p));
337 <            assertEquals(N-i, q.size());
337 >            assertEquals(SIZE-i, q.size());
338              p.remove();
339          }
340      }
341  
342 <    public void testRemoveAll(){
343 <        for (int i = 1; i < N; ++i) {
344 <            ConcurrentLinkedQueue q = fullQueue(N);
345 <            ConcurrentLinkedQueue p = fullQueue(i);
342 >    /**
343 >     *
344 >     */
345 >    public void testRemoveAll() {
346 >        for (int i = 1; i < SIZE; ++i) {
347 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
348 >            ConcurrentLinkedQueue p = populatedQueue(i);
349              assertTrue(q.removeAll(p));
350 <            assertEquals(N-i, q.size());
350 >            assertEquals(SIZE-i, q.size());
351              for (int j = 0; j < i; ++j) {
352                  Integer I = (Integer)(p.remove());
353                  assertFalse(q.contains(I));
# Line 286 | Line 355 | public class ConcurrentLinkedQueueTest e
355          }
356      }
357  
358 <    public void testToArray(){
359 <        ConcurrentLinkedQueue q = fullQueue(N);
358 >    /**
359 >     *
360 >     */
361 >    public void testToArray() {
362 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
363          Object[] o = q.toArray();
364          Arrays.sort(o);
365          for(int i = 0; i < o.length; i++)
366              assertEquals(o[i], q.poll());
367      }
368  
369 <    public void testToArray2(){
370 <        ConcurrentLinkedQueue q = fullQueue(N);
371 <        Integer[] ints = new Integer[N];
369 >    /**
370 >     *
371 >     */
372 >    public void testToArray2() {
373 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
374 >        Integer[] ints = new Integer[SIZE];
375          ints = (Integer[])q.toArray(ints);
376          Arrays.sort(ints);
377          for(int i = 0; i < ints.length; i++)
378              assertEquals(ints[i], q.poll());
379      }
380      
381 <    public void testIterator(){
382 <        ConcurrentLinkedQueue q = fullQueue(N);
381 >    /**
382 >     *
383 >     */
384 >    public void testIterator() {
385 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
386          int i = 0;
387          Iterator it = q.iterator();
388          while(it.hasNext()) {
389              assertTrue(q.contains(it.next()));
390              ++i;
391          }
392 <        assertEquals(i, N);
392 >        assertEquals(i, SIZE);
393      }
394  
395 +    /**
396 +     *
397 +     */
398      public void testIteratorOrdering() {
318
399          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
400 <
401 <        q.add(new Integer(1));
402 <        q.add(new Integer(2));
323 <        q.add(new Integer(3));
400 >        q.add(one);
401 >        q.add(two);
402 >        q.add(three);
403  
404          int k = 0;
405          for (Iterator it = q.iterator(); it.hasNext();) {
406              int i = ((Integer)(it.next())).intValue();
407 <            assertEquals("items should come out in order", ++k, i);
407 >            assertEquals(++k, i);
408          }
409  
410 <        assertEquals("should go through 3 elements", 3, k);
410 >        assertEquals(3, k);
411      }
412  
413 +    /**
414 +     *
415 +     */
416      public void testWeaklyConsistentIteration () {
335
417          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
418 <
419 <        q.add(new Integer(1));
420 <        q.add(new Integer(2));
340 <        q.add(new Integer(3));
418 >        q.add(one);
419 >        q.add(two);
420 >        q.add(three);
421  
422          try {
423              for (Iterator it = q.iterator(); it.hasNext();) {
# Line 346 | Line 426 | public class ConcurrentLinkedQueueTest e
426              }
427          }
428          catch (ConcurrentModificationException e) {
429 <            fail("weakly consistent iterator; should not get CME");
429 >            shouldThrow();
430          }
431  
432          assertEquals("queue should be empty again", 0, q.size());
433      }
434  
435 +    /**
436 +     *
437 +     */
438      public void testIteratorRemove () {
356
439          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
440 <
441 <        q.add(new Integer(1));
442 <        q.add(new Integer(2));
361 <        q.add(new Integer(3));
362 <
440 >        q.add(one);
441 >        q.add(two);
442 >        q.add(three);
443          Iterator it = q.iterator();
444          it.next();
445          it.remove();
366
446          it = q.iterator();
447 <        assertEquals(it.next(), new Integer(2));
448 <        assertEquals(it.next(), new Integer(3));
447 >        assertEquals(it.next(), two);
448 >        assertEquals(it.next(), three);
449          assertFalse(it.hasNext());
450      }
451  
452  
453 <    public void testToString(){
454 <        ConcurrentLinkedQueue q = fullQueue(N);
453 >    /**
454 >     *
455 >     */
456 >    public void testToString() {
457 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
458          String s = q.toString();
459 <        for (int i = 0; i < N; ++i) {
459 >        for (int i = 0; i < SIZE; ++i) {
460              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
461          }
462      }        
463  
464 +    /**
465 +     *
466 +     */
467 +    public void testSerialization() {
468 +        ConcurrentLinkedQueue q = populatedQueue(SIZE);
469 +        try {
470 +            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
471 +            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
472 +            out.writeObject(q);
473 +            out.close();
474 +
475 +            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
476 +            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
477 +            ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
478 +            assertEquals(q.size(), r.size());
479 +            while (!q.isEmpty())
480 +                assertEquals(q.remove(), r.remove());
481 +        } catch(Exception e){
482 +            unexpectedException();
483 +        }
484 +    }
485 +
486   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines