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.15 by jsr166, Sat Nov 21 20:35:18 2009 UTC vs.
Revision 1.34 by jsr166, Wed Dec 31 20:17:39 2014 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.*;
9 > import java.util.Arrays;
10 > import java.util.Collection;
11 > import java.util.Iterator;
12 > import java.util.NoSuchElementException;
13 > import java.util.Queue;
14 > import java.util.concurrent.ConcurrentLinkedQueue;
15 >
16 > import junit.framework.Test;
17 > import junit.framework.TestSuite;
18  
19   public class ConcurrentLinkedQueueTest extends JSR166TestCase {
20  
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());
22 >        junit.textui.TestRunner.run(suite());
23      }
24  
25      public static Test suite() {
# Line 22 | Line 27 | public class ConcurrentLinkedQueueTest e
27      }
28  
29      /**
30 <     * Create a queue of given size containing consecutive
30 >     * Returns a new queue of given size containing consecutive
31       * Integers 0 ... n.
32       */
33 <    private ConcurrentLinkedQueue populatedQueue(int n) {
34 <        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
33 >    private ConcurrentLinkedQueue<Integer> populatedQueue(int n) {
34 >        ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>();
35          assertTrue(q.isEmpty());
36          for (int i = 0; i < n; ++i)
37              assertTrue(q.offer(new Integer(i)));
# Line 43 | Line 48 | public class ConcurrentLinkedQueueTest e
48      }
49  
50      /**
51 <     *  Initializing from null Collection throws NPE
51 >     * Initializing from null Collection throws NPE
52       */
53      public void testConstructor3() {
54          try {
# Line 139 | Line 144 | public class ConcurrentLinkedQueueTest e
144          } catch (NullPointerException success) {}
145      }
146  
142
147      /**
148       * Offer returns true
149       */
# Line 193 | Line 197 | public class ConcurrentLinkedQueueTest e
197              shouldThrow();
198          } catch (NullPointerException success) {}
199      }
200 +
201      /**
202 <     *  addAll of a collection with any null elements throws NPE after
202 >     * addAll of a collection with any null elements throws NPE after
203       * possibly adding some elements
204       */
205      public void testAddAll3() {
# Line 229 | Line 234 | public class ConcurrentLinkedQueueTest e
234      public void testPoll() {
235          ConcurrentLinkedQueue q = populatedQueue(SIZE);
236          for (int i = 0; i < SIZE; ++i) {
237 <            assertEquals(i, ((Integer)q.poll()).intValue());
237 >            assertEquals(i, q.poll());
238          }
239          assertNull(q.poll());
240      }
# Line 240 | Line 245 | public class ConcurrentLinkedQueueTest e
245      public void testPeek() {
246          ConcurrentLinkedQueue q = populatedQueue(SIZE);
247          for (int i = 0; i < SIZE; ++i) {
248 <            assertEquals(i, ((Integer)q.peek()).intValue());
249 <            q.poll();
248 >            assertEquals(i, q.peek());
249 >            assertEquals(i, q.poll());
250              assertTrue(q.peek() == null ||
251 <                       i != ((Integer)q.peek()).intValue());
251 >                       !q.peek().equals(i));
252          }
253          assertNull(q.peek());
254      }
# Line 254 | Line 259 | public class ConcurrentLinkedQueueTest e
259      public void testElement() {
260          ConcurrentLinkedQueue q = populatedQueue(SIZE);
261          for (int i = 0; i < SIZE; ++i) {
262 <            assertEquals(i, ((Integer)q.element()).intValue());
263 <            q.poll();
262 >            assertEquals(i, q.element());
263 >            assertEquals(i, q.poll());
264          }
265          try {
266              q.element();
# Line 264 | Line 269 | public class ConcurrentLinkedQueueTest e
269      }
270  
271      /**
272 <     *  remove removes next element, or throws NSEE if empty
272 >     * remove removes next element, or throws NSEE if empty
273       */
274      public void testRemove() {
275          ConcurrentLinkedQueue q = populatedQueue(SIZE);
276          for (int i = 0; i < SIZE; ++i) {
277 <            assertEquals(i, ((Integer)q.remove()).intValue());
277 >            assertEquals(i, q.remove());
278          }
279          try {
280              q.remove();
# Line 282 | Line 287 | public class ConcurrentLinkedQueueTest e
287       */
288      public void testRemoveElement() {
289          ConcurrentLinkedQueue q = populatedQueue(SIZE);
290 <        for (int i = 1; i < SIZE; i+=2) {
291 <            assertTrue(q.remove(new Integer(i)));
292 <        }
293 <        for (int i = 0; i < SIZE; i+=2) {
294 <            assertTrue(q.remove(new Integer(i)));
295 <            assertFalse(q.remove(new Integer(i+1)));
290 >        for (int i = 1; i < SIZE; i += 2) {
291 >            assertTrue(q.contains(i));
292 >            assertTrue(q.remove(i));
293 >            assertFalse(q.contains(i));
294 >            assertTrue(q.contains(i-1));
295 >        }
296 >        for (int i = 0; i < SIZE; i += 2) {
297 >            assertTrue(q.contains(i));
298 >            assertTrue(q.remove(i));
299 >            assertFalse(q.contains(i));
300 >            assertFalse(q.remove(i+1));
301 >            assertFalse(q.contains(i+1));
302          }
303          assertTrue(q.isEmpty());
304      }
# Line 361 | Line 372 | public class ConcurrentLinkedQueueTest e
372              assertTrue(q.removeAll(p));
373              assertEquals(SIZE-i, q.size());
374              for (int j = 0; j < i; ++j) {
375 <                Integer I = (Integer)(p.remove());
376 <                assertFalse(q.contains(I));
375 >                Integer x = (Integer)(p.remove());
376 >                assertFalse(q.contains(x));
377              }
378          }
379      }
380  
381      /**
382 <     * toArray contains all elements
382 >     * toArray contains all elements in FIFO order
383       */
384      public void testToArray() {
385          ConcurrentLinkedQueue q = populatedQueue(SIZE);
386          Object[] o = q.toArray();
376        Arrays.sort(o);
387          for (int i = 0; i < o.length; i++)
388 <            assertEquals(o[i], q.poll());
388 >            assertSame(o[i], q.poll());
389      }
390  
391      /**
392 <     *  toArray(a) contains all elements
392 >     * toArray(a) contains all elements in FIFO order
393       */
394      public void testToArray2() {
395 <        ConcurrentLinkedQueue q = populatedQueue(SIZE);
395 >        ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE);
396          Integer[] ints = new Integer[SIZE];
397 <        ints = (Integer[])q.toArray(ints);
398 <        Arrays.sort(ints);
397 >        Integer[] array = q.toArray(ints);
398 >        assertSame(ints, array);
399          for (int i = 0; i < ints.length; i++)
400 <            assertEquals(ints[i], q.poll());
400 >            assertSame(ints[i], q.poll());
401      }
402  
403      /**
404 <     * toArray(null) throws NPE
404 >     * toArray(null) throws NullPointerException
405       */
406 <    public void testToArray_BadArg() {
406 >    public void testToArray_NullArg() {
407 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
408          try {
409 <            ConcurrentLinkedQueue q = populatedQueue(SIZE);
399 <            Object o[] = q.toArray(null);
409 >            q.toArray(null);
410              shouldThrow();
411          } catch (NullPointerException success) {}
412      }
413  
414      /**
415 <     * toArray with incompatible array type throws ArrayStoreException
415 >     * toArray(incompatible array type) throws ArrayStoreException
416       */
417      public void testToArray1_BadArg() {
418 +        ConcurrentLinkedQueue q = populatedQueue(SIZE);
419          try {
420 <            ConcurrentLinkedQueue q = populatedQueue(SIZE);
410 <            Object o[] = q.toArray(new String[10] );
420 >            q.toArray(new String[10]);
421              shouldThrow();
422          } catch (ArrayStoreException success) {}
423      }
424  
425      /**
426 <     *  iterator iterates through all elements
426 >     * iterator iterates through all elements
427       */
428      public void testIterator() {
429          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 437 | Line 447 | public class ConcurrentLinkedQueueTest e
447  
448          int k = 0;
449          for (Iterator it = q.iterator(); it.hasNext();) {
450 <            int i = ((Integer)(it.next())).intValue();
441 <            assertEquals(++k, i);
450 >            assertEquals(++k, it.next());
451          }
452  
453          assertEquals(3, k);
# Line 447 | Line 456 | public class ConcurrentLinkedQueueTest e
456      /**
457       * Modifications do not cause iterators to fail
458       */
459 <    public void testWeaklyConsistentIteration () {
459 >    public void testWeaklyConsistentIteration() {
460          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
461          q.add(one);
462          q.add(two);
# Line 464 | Line 473 | public class ConcurrentLinkedQueueTest e
473      /**
474       * iterator.remove removes current element
475       */
476 <    public void testIteratorRemove () {
476 >    public void testIteratorRemove() {
477          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
478          q.add(one);
479          q.add(two);
# Line 473 | Line 482 | public class ConcurrentLinkedQueueTest e
482          it.next();
483          it.remove();
484          it = q.iterator();
485 <        assertEquals(it.next(), two);
486 <        assertEquals(it.next(), three);
485 >        assertSame(it.next(), two);
486 >        assertSame(it.next(), three);
487          assertFalse(it.hasNext());
488      }
489  
481
490      /**
491       * toString contains toStrings of elements
492       */
# Line 486 | Line 494 | public class ConcurrentLinkedQueueTest e
494          ConcurrentLinkedQueue q = populatedQueue(SIZE);
495          String s = q.toString();
496          for (int i = 0; i < SIZE; ++i) {
497 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
497 >            assertTrue(s.contains(String.valueOf(i)));
498          }
499      }
500  
# Line 494 | Line 502 | public class ConcurrentLinkedQueueTest e
502       * A deserialized serialized queue has same elements in same order
503       */
504      public void testSerialization() throws Exception {
505 <        ConcurrentLinkedQueue q = populatedQueue(SIZE);
506 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
507 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
508 <        out.writeObject(q);
509 <        out.close();
510 <
511 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
512 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
513 <        ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
514 <        assertEquals(q.size(), r.size());
515 <        while (!q.isEmpty())
516 <            assertEquals(q.remove(), r.remove());
505 >        Queue x = populatedQueue(SIZE);
506 >        Queue y = serialClone(x);
507 >
508 >        assertNotSame(x, y);
509 >        assertEquals(x.size(), y.size());
510 >        assertEquals(x.toString(), y.toString());
511 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
512 >        while (!x.isEmpty()) {
513 >            assertFalse(y.isEmpty());
514 >            assertEquals(x.remove(), y.remove());
515 >        }
516 >        assertTrue(y.isEmpty());
517      }
518  
519 +    /**
520 +     * remove(null), contains(null) always return false
521 +     */
522 +    public void testNeverContainsNull() {
523 +        Collection<?>[] qs = {
524 +            new ConcurrentLinkedQueue<Object>(),
525 +            populatedQueue(2),
526 +        };
527 +
528 +        for (Collection<?> q : qs) {
529 +            assertFalse(q.contains(null));
530 +            assertFalse(q.remove(null));
531 +        }
532 +    }
533   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines