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.16 by jsr166, Sun Nov 22 18:57:17 2009 UTC vs.
Revision 1.28 by jsr166, Tue Feb 21 01:54:03 2012 UTC

# Line 1 | Line 1
1   /*
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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Iterator;
13 > import java.util.NoSuchElementException;
14 > import java.util.Queue;
15 > import java.util.concurrent.ConcurrentLinkedQueue;
16  
17   public class ConcurrentLinkedQueueTest extends JSR166TestCase {
18  
19      public static void main(String[] args) {
20 <        junit.textui.TestRunner.run (suite());
20 >        junit.textui.TestRunner.run(suite());
21      }
22  
23      public static Test suite() {
# Line 22 | Line 25 | public class ConcurrentLinkedQueueTest e
25      }
26  
27      /**
28 <     * Create a queue of given size containing consecutive
28 >     * Creates a queue of given size containing consecutive
29       * Integers 0 ... n.
30       */
31 <    private ConcurrentLinkedQueue populatedQueue(int n) {
32 <        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
31 >    private ConcurrentLinkedQueue<Integer> populatedQueue(int n) {
32 >        ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>();
33          assertTrue(q.isEmpty());
34          for (int i = 0; i < n; ++i)
35              assertTrue(q.offer(new Integer(i)));
# Line 43 | Line 46 | public class ConcurrentLinkedQueueTest e
46      }
47  
48      /**
49 <     *  Initializing from null Collection throws NPE
49 >     * Initializing from null Collection throws NPE
50       */
51      public void testConstructor3() {
52          try {
# Line 139 | Line 142 | public class ConcurrentLinkedQueueTest e
142          } catch (NullPointerException success) {}
143      }
144  
142
145      /**
146       * Offer returns true
147       */
# Line 193 | Line 195 | public class ConcurrentLinkedQueueTest e
195              shouldThrow();
196          } catch (NullPointerException success) {}
197      }
198 +
199      /**
200 <     *  addAll of a collection with any null elements throws NPE after
200 >     * addAll of a collection with any null elements throws NPE after
201       * possibly adding some elements
202       */
203      public void testAddAll3() {
# Line 264 | Line 267 | public class ConcurrentLinkedQueueTest e
267      }
268  
269      /**
270 <     *  remove removes next element, or throws NSEE if empty
270 >     * remove removes next element, or throws NSEE if empty
271       */
272      public void testRemove() {
273          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 283 | Line 286 | public class ConcurrentLinkedQueueTest e
286      public void testRemoveElement() {
287          ConcurrentLinkedQueue q = populatedQueue(SIZE);
288          for (int i = 1; i < SIZE; i+=2) {
289 <            assertTrue(q.remove(new Integer(i)));
289 >            assertTrue(q.contains(i));
290 >            assertTrue(q.remove(i));
291 >            assertFalse(q.contains(i));
292 >            assertTrue(q.contains(i-1));
293          }
294          for (int i = 0; i < SIZE; i+=2) {
295 <            assertTrue(q.remove(new Integer(i)));
296 <            assertFalse(q.remove(new Integer(i+1)));
295 >            assertTrue(q.contains(i));
296 >            assertTrue(q.remove(i));
297 >            assertFalse(q.contains(i));
298 >            assertFalse(q.remove(i+1));
299 >            assertFalse(q.contains(i+1));
300          }
301          assertTrue(q.isEmpty());
302      }
# Line 368 | Line 377 | public class ConcurrentLinkedQueueTest e
377      }
378  
379      /**
380 <     * toArray contains all elements
380 >     * toArray contains all elements in FIFO order
381       */
382      public void testToArray() {
383          ConcurrentLinkedQueue q = populatedQueue(SIZE);
384          Object[] o = q.toArray();
376        Arrays.sort(o);
385          for (int i = 0; i < o.length; i++)
386 <            assertEquals(o[i], q.poll());
386 >            assertSame(o[i], q.poll());
387      }
388  
389      /**
390 <     *  toArray(a) contains all elements
390 >     * toArray(a) contains all elements in FIFO order
391       */
392      public void testToArray2() {
393 <        ConcurrentLinkedQueue q = populatedQueue(SIZE);
393 >        ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE);
394          Integer[] ints = new Integer[SIZE];
395 <        ints = (Integer[])q.toArray(ints);
396 <        Arrays.sort(ints);
395 >        Integer[] array = q.toArray(ints);
396 >        assertSame(ints, array);
397          for (int i = 0; i < ints.length; i++)
398 <            assertEquals(ints[i], q.poll());
398 >            assertSame(ints[i], q.poll());
399      }
400  
401      /**
402 <     * toArray(null) throws NPE
402 >     * toArray(null) throws NullPointerException
403       */
404 <    public void testToArray_BadArg() {
404 >    public void testToArray_NullArg() {
405          ConcurrentLinkedQueue q = populatedQueue(SIZE);
406          try {
407 <            Object o[] = q.toArray(null);
407 >            q.toArray(null);
408              shouldThrow();
409          } catch (NullPointerException success) {}
410      }
411  
412      /**
413 <     * toArray with incompatible array type throws ArrayStoreException
413 >     * toArray(incompatible array type) throws ArrayStoreException
414       */
415      public void testToArray1_BadArg() {
416          ConcurrentLinkedQueue q = populatedQueue(SIZE);
417          try {
418 <            Object o[] = q.toArray(new String[10]);
418 >            q.toArray(new String[10]);
419              shouldThrow();
420          } catch (ArrayStoreException success) {}
421      }
422  
423      /**
424 <     *  iterator iterates through all elements
424 >     * iterator iterates through all elements
425       */
426      public void testIterator() {
427          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 446 | Line 454 | public class ConcurrentLinkedQueueTest e
454      /**
455       * Modifications do not cause iterators to fail
456       */
457 <    public void testWeaklyConsistentIteration () {
457 >    public void testWeaklyConsistentIteration() {
458          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
459          q.add(one);
460          q.add(two);
# Line 463 | Line 471 | public class ConcurrentLinkedQueueTest e
471      /**
472       * iterator.remove removes current element
473       */
474 <    public void testIteratorRemove () {
474 >    public void testIteratorRemove() {
475          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
476          q.add(one);
477          q.add(two);
# Line 472 | Line 480 | public class ConcurrentLinkedQueueTest e
480          it.next();
481          it.remove();
482          it = q.iterator();
483 <        assertEquals(it.next(), two);
484 <        assertEquals(it.next(), three);
483 >        assertSame(it.next(), two);
484 >        assertSame(it.next(), three);
485          assertFalse(it.hasNext());
486      }
487  
480
488      /**
489       * toString contains toStrings of elements
490       */
# Line 485 | Line 492 | public class ConcurrentLinkedQueueTest e
492          ConcurrentLinkedQueue q = populatedQueue(SIZE);
493          String s = q.toString();
494          for (int i = 0; i < SIZE; ++i) {
495 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
495 >            assertTrue(s.contains(String.valueOf(i)));
496          }
497      }
498  
# Line 493 | Line 500 | public class ConcurrentLinkedQueueTest e
500       * A deserialized serialized queue has same elements in same order
501       */
502      public void testSerialization() throws Exception {
503 <        ConcurrentLinkedQueue q = populatedQueue(SIZE);
504 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
505 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
506 <        out.writeObject(q);
507 <        out.close();
508 <
509 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
510 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
511 <        ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
512 <        assertEquals(q.size(), r.size());
513 <        while (!q.isEmpty())
514 <            assertEquals(q.remove(), r.remove());
503 >        Queue x = populatedQueue(SIZE);
504 >        Queue y = serialClone(x);
505 >
506 >        assertTrue(x != y);
507 >        assertEquals(x.size(), y.size());
508 >        assertEquals(x.toString(), y.toString());
509 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
510 >        while (!x.isEmpty()) {
511 >            assertFalse(y.isEmpty());
512 >            assertEquals(x.remove(), y.remove());
513 >        }
514 >        assertTrue(y.isEmpty());
515      }
516  
517   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines