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.5 by dl, Sun Oct 5 23:00:40 2003 UTC vs.
Revision 1.17 by jsr166, Tue Dec 1 09:56:28 2009 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/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 13 | Line 14 | import java.io.*;
14   public class ConcurrentLinkedQueueTest extends JSR166TestCase {
15  
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());
18      }
19  
20      public static Test suite() {
21 <        return new TestSuite(ConcurrentLinkedQueueTest.class);
21 >        return new TestSuite(ConcurrentLinkedQueueTest.class);
22      }
23  
24      /**
# Line 27 | Line 28 | public class ConcurrentLinkedQueueTest e
28      private ConcurrentLinkedQueue populatedQueue(int n) {
29          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
30          assertTrue(q.isEmpty());
31 <        for(int i = 0; i < n; ++i)
32 <            assertTrue(q.offer(new Integer(i)));
31 >        for (int i = 0; i < n; ++i)
32 >            assertTrue(q.offer(new Integer(i)));
33          assertFalse(q.isEmpty());
34 <        assertEquals(n, q.size());
34 >        assertEquals(n, q.size());
35          return q;
36      }
37 <
37 >
38      /**
39       * new queue is empty
40       */
# Line 48 | Line 49 | public class ConcurrentLinkedQueueTest e
49          try {
50              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
51              shouldThrow();
52 <        }
52 <        catch (NullPointerException success) {}
52 >        } catch (NullPointerException success) {}
53      }
54  
55      /**
# Line 60 | Line 60 | public class ConcurrentLinkedQueueTest e
60              Integer[] ints = new Integer[SIZE];
61              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
62              shouldThrow();
63 <        }
64 <        catch (NullPointerException success) {}
63 >        } catch (NullPointerException success) {}
64      }
65  
66      /**
# Line 74 | Line 73 | public class ConcurrentLinkedQueueTest e
73                  ints[i] = new Integer(i);
74              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
75              shouldThrow();
76 <        }
78 <        catch (NullPointerException success) {}
76 >        } catch (NullPointerException success) {}
77      }
78  
79      /**
80       * Queue contains all elements of collection used to initialize
81       */
82      public void testConstructor6() {
83 <        try {
84 <            Integer[] ints = new Integer[SIZE];
85 <            for (int i = 0; i < SIZE; ++i)
86 <                ints[i] = new Integer(i);
87 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
88 <            for (int i = 0; i < SIZE; ++i)
91 <                assertEquals(ints[i], q.poll());
92 <        }
93 <        finally {}
83 >        Integer[] ints = new Integer[SIZE];
84 >        for (int i = 0; i < SIZE; ++i)
85 >            ints[i] = new Integer(i);
86 >        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
87 >        for (int i = 0; i < SIZE; ++i)
88 >            assertEquals(ints[i], q.poll());
89      }
90  
91      /**
# Line 126 | Line 121 | public class ConcurrentLinkedQueueTest e
121       * offer(null) throws NPE
122       */
123      public void testOfferNull() {
124 <        try {
124 >        try {
125              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
126              q.offer(null);
127              shouldThrow();
128 <        } catch (NullPointerException success) { }  
128 >        } catch (NullPointerException success) {}
129      }
130  
131      /**
132       * add(null) throws NPE
133       */
134      public void testAddNull() {
135 <        try {
135 >        try {
136              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
137              q.add(null);
138              shouldThrow();
139 <        } catch (NullPointerException success) { }  
139 >        } catch (NullPointerException success) {}
140      }
141  
142  
# Line 173 | Line 168 | public class ConcurrentLinkedQueueTest e
168              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
169              q.addAll(null);
170              shouldThrow();
171 <        }
177 <        catch (NullPointerException success) {}
171 >        } catch (NullPointerException success) {}
172      }
173  
174      /**
# Line 185 | Line 179 | public class ConcurrentLinkedQueueTest e
179              ConcurrentLinkedQueue q = populatedQueue(SIZE);
180              q.addAll(q);
181              shouldThrow();
182 <        }
189 <        catch (IllegalArgumentException success) {}
182 >        } catch (IllegalArgumentException success) {}
183      }
184  
185      /**
# Line 198 | Line 191 | public class ConcurrentLinkedQueueTest e
191              Integer[] ints = new Integer[SIZE];
192              q.addAll(Arrays.asList(ints));
193              shouldThrow();
194 <        }
202 <        catch (NullPointerException success) {}
194 >        } catch (NullPointerException success) {}
195      }
196      /**
197       *  addAll of a collection with any null elements throws NPE after
# Line 213 | Line 205 | public class ConcurrentLinkedQueueTest e
205                  ints[i] = new Integer(i);
206              q.addAll(Arrays.asList(ints));
207              shouldThrow();
208 <        }
217 <        catch (NullPointerException success) {}
208 >        } catch (NullPointerException success) {}
209      }
210  
211      /**
212       * Queue contains all elements, in traversal order, of successful addAll
213       */
214      public void testAddAll5() {
215 <        try {
216 <            Integer[] empty = new Integer[0];
217 <            Integer[] ints = new Integer[SIZE];
218 <            for (int i = 0; i < SIZE; ++i)
219 <                ints[i] = new Integer(i);
220 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
221 <            assertFalse(q.addAll(Arrays.asList(empty)));
222 <            assertTrue(q.addAll(Arrays.asList(ints)));
223 <            for (int i = 0; i < SIZE; ++i)
233 <                assertEquals(ints[i], q.poll());
234 <        }
235 <        finally {}
215 >        Integer[] empty = new Integer[0];
216 >        Integer[] ints = new Integer[SIZE];
217 >        for (int i = 0; i < SIZE; ++i)
218 >            ints[i] = new Integer(i);
219 >        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
220 >        assertFalse(q.addAll(Arrays.asList(empty)));
221 >        assertTrue(q.addAll(Arrays.asList(ints)));
222 >        for (int i = 0; i < SIZE; ++i)
223 >            assertEquals(ints[i], q.poll());
224      }
225  
226      /**
# Line 241 | Line 229 | public class ConcurrentLinkedQueueTest e
229      public void testPoll() {
230          ConcurrentLinkedQueue q = populatedQueue(SIZE);
231          for (int i = 0; i < SIZE; ++i) {
232 <            assertEquals(i, ((Integer)q.poll()).intValue());
232 >            assertEquals(i, q.poll());
233          }
234 <        assertNull(q.poll());
234 >        assertNull(q.poll());
235      }
236  
237      /**
# Line 252 | Line 240 | public class ConcurrentLinkedQueueTest e
240      public void testPeek() {
241          ConcurrentLinkedQueue q = populatedQueue(SIZE);
242          for (int i = 0; i < SIZE; ++i) {
243 <            assertEquals(i, ((Integer)q.peek()).intValue());
244 <            q.poll();
243 >            assertEquals(i, q.peek());
244 >            assertEquals(i, q.poll());
245              assertTrue(q.peek() == null ||
246 <                       i != ((Integer)q.peek()).intValue());
246 >                       !q.peek().equals(i));
247          }
248 <        assertNull(q.peek());
248 >        assertNull(q.peek());
249      }
250  
251      /**
# Line 266 | Line 254 | public class ConcurrentLinkedQueueTest e
254      public void testElement() {
255          ConcurrentLinkedQueue q = populatedQueue(SIZE);
256          for (int i = 0; i < SIZE; ++i) {
257 <            assertEquals(i, ((Integer)q.element()).intValue());
258 <            q.poll();
257 >            assertEquals(i, q.element());
258 >            assertEquals(i, q.poll());
259          }
260          try {
261              q.element();
262              shouldThrow();
263 <        }
276 <        catch (NoSuchElementException success) {}
263 >        } catch (NoSuchElementException success) {}
264      }
265  
266      /**
# Line 282 | Line 269 | public class ConcurrentLinkedQueueTest e
269      public void testRemove() {
270          ConcurrentLinkedQueue q = populatedQueue(SIZE);
271          for (int i = 0; i < SIZE; ++i) {
272 <            assertEquals(i, ((Integer)q.remove()).intValue());
272 >            assertEquals(i, q.remove());
273          }
274          try {
275              q.remove();
276              shouldThrow();
277 <        } catch (NoSuchElementException success){
291 <        }  
277 >        } catch (NoSuchElementException success) {}
278      }
279  
280      /**
# Line 305 | Line 291 | public class ConcurrentLinkedQueueTest e
291          }
292          assertTrue(q.isEmpty());
293      }
294 <        
294 >
295      /**
296       * contains(x) reports true when elements added but not yet removed
297       */
# Line 386 | Line 372 | public class ConcurrentLinkedQueueTest e
372       */
373      public void testToArray() {
374          ConcurrentLinkedQueue q = populatedQueue(SIZE);
375 <        Object[] o = q.toArray();
375 >        Object[] o = q.toArray();
376          Arrays.sort(o);
377 <        for(int i = 0; i < o.length; i++)
378 <            assertEquals(o[i], q.poll());
377 >        for (int i = 0; i < o.length; i++)
378 >            assertEquals(o[i], q.poll());
379      }
380  
381      /**
# Line 397 | Line 383 | public class ConcurrentLinkedQueueTest e
383       */
384      public void testToArray2() {
385          ConcurrentLinkedQueue q = populatedQueue(SIZE);
386 <        Integer[] ints = new Integer[SIZE];
387 <        ints = (Integer[])q.toArray(ints);
386 >        Integer[] ints = new Integer[SIZE];
387 >        ints = (Integer[])q.toArray(ints);
388          Arrays.sort(ints);
389 <        for(int i = 0; i < ints.length; i++)
389 >        for (int i = 0; i < ints.length; i++)
390              assertEquals(ints[i], q.poll());
391      }
392  
# Line 408 | Line 394 | public class ConcurrentLinkedQueueTest e
394       * toArray(null) throws NPE
395       */
396      public void testToArray_BadArg() {
397 <        try {
398 <            ConcurrentLinkedQueue q = populatedQueue(SIZE);
399 <            Object o[] = q.toArray(null);
400 <            shouldThrow();
401 <        } catch(NullPointerException success){}
397 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
398 >        try {
399 >            Object o[] = q.toArray(null);
400 >            shouldThrow();
401 >        } catch (NullPointerException success) {}
402      }
403  
404      /**
405 <     * toArray with incompatable array type throws CCE
405 >     * toArray with incompatible array type throws ArrayStoreException
406       */
407      public void testToArray1_BadArg() {
408 <        try {
409 <            ConcurrentLinkedQueue q = populatedQueue(SIZE);
410 <            Object o[] = q.toArray(new String[10] );
411 <            shouldThrow();
412 <        } catch(ArrayStoreException  success){}
408 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
409 >        try {
410 >            Object o[] = q.toArray(new String[10]);
411 >            shouldThrow();
412 >        } catch (ArrayStoreException success) {}
413      }
414 <    
414 >
415      /**
416       *  iterator iterates through all elements
417       */
418      public void testIterator() {
419          ConcurrentLinkedQueue q = populatedQueue(SIZE);
420          int i = 0;
421 <        Iterator it = q.iterator();
422 <        while(it.hasNext()) {
421 >        Iterator it = q.iterator();
422 >        while (it.hasNext()) {
423              assertTrue(q.contains(it.next()));
424              ++i;
425          }
# Line 451 | Line 437 | public class ConcurrentLinkedQueueTest e
437  
438          int k = 0;
439          for (Iterator it = q.iterator(); it.hasNext();) {
440 <            int i = ((Integer)(it.next())).intValue();
455 <            assertEquals(++k, i);
440 >            assertEquals(++k, it.next());
441          }
442  
443          assertEquals(3, k);
# Line 467 | Line 452 | public class ConcurrentLinkedQueueTest e
452          q.add(two);
453          q.add(three);
454  
455 <        try {
456 <            for (Iterator it = q.iterator(); it.hasNext();) {
457 <                q.remove();
473 <                it.next();
474 <            }
475 <        }
476 <        catch (ConcurrentModificationException e) {
477 <            shouldThrow();
455 >        for (Iterator it = q.iterator(); it.hasNext();) {
456 >            q.remove();
457 >            it.next();
458          }
459  
460          assertEquals("queue should be empty again", 0, q.size());
# Line 492 | Line 472 | public class ConcurrentLinkedQueueTest e
472          it.next();
473          it.remove();
474          it = q.iterator();
475 <        assertEquals(it.next(), two);
476 <        assertEquals(it.next(), three);
475 >        assertSame(it.next(), two);
476 >        assertSame(it.next(), three);
477          assertFalse(it.hasNext());
478      }
479  
# Line 507 | Line 487 | public class ConcurrentLinkedQueueTest e
487          for (int i = 0; i < SIZE; ++i) {
488              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
489          }
490 <    }        
490 >    }
491  
492      /**
493       * A deserialized serialized queue has same elements in same order
494       */
495 <    public void testSerialization() {
495 >    public void testSerialization() throws Exception {
496          ConcurrentLinkedQueue q = populatedQueue(SIZE);
497 <        try {
498 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
499 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
500 <            out.writeObject(q);
501 <            out.close();
502 <
503 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
504 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
505 <            ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
506 <            assertEquals(q.size(), r.size());
507 <            while (!q.isEmpty())
528 <                assertEquals(q.remove(), r.remove());
529 <        } catch(Exception e){
530 <            unexpectedException();
531 <        }
497 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
498 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
499 >        out.writeObject(q);
500 >        out.close();
501 >
502 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
503 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
504 >        ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
505 >        assertEquals(q.size(), r.size());
506 >        while (!q.isEmpty())
507 >            assertEquals(q.remove(), r.remove());
508      }
509  
510   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines