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

Comparing jsr166/src/test/tck/LinkedListTest.java (file contents):
Revision 1.16 by jsr166, Sun Nov 22 18:57:17 2009 UTC vs.
Revision 1.33 by jsr166, Wed Dec 31 20:09:08 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.*;
9 > import java.util.Arrays;
10 > import java.util.Collection;
11 > import java.util.Iterator;
12 > import java.util.LinkedList;
13 > import java.util.NoSuchElementException;
14 >
15 > import junit.framework.Test;
16 > import junit.framework.TestSuite;
17  
18   public class LinkedListTest extends JSR166TestCase {
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 20 | Line 25 | public class LinkedListTest extends JSR1
25      }
26  
27      /**
28 <     * Create a queue of given size containing consecutive
28 >     * Returns a new queue of given size containing consecutive
29       * Integers 0 ... n.
30       */
31 <    private LinkedList populatedQueue(int n) {
32 <        LinkedList q = new LinkedList();
31 >    private LinkedList<Integer> populatedQueue(int n) {
32 >        LinkedList<Integer> q = new LinkedList<Integer>();
33          assertTrue(q.isEmpty());
34          for (int i = 0; i < n; ++i)
35              assertTrue(q.offer(new Integer(i)));
# Line 174 | Line 179 | public class LinkedListTest extends JSR1
179      }
180  
181      /**
182 <     *  poll succeeds unless empty
182 >     * poll succeeds unless empty
183       */
184      public void testPoll() {
185          LinkedList q = populatedQueue(SIZE);
# Line 185 | Line 190 | public class LinkedListTest extends JSR1
190      }
191  
192      /**
193 <     *  peek returns next element, or null if empty
193 >     * peek returns next element, or null if empty
194       */
195      public void testPeek() {
196          LinkedList q = populatedQueue(SIZE);
# Line 214 | Line 219 | public class LinkedListTest extends JSR1
219      }
220  
221      /**
222 <     *  remove removes next element, or throws NSEE if empty
222 >     * remove removes next element, or throws NSEE if empty
223       */
224      public void testRemove() {
225          LinkedList q = populatedQueue(SIZE);
# Line 232 | Line 237 | public class LinkedListTest extends JSR1
237       */
238      public void testRemoveElement() {
239          LinkedList q = populatedQueue(SIZE);
240 <        for (int i = 1; i < SIZE; i+=2) {
241 <            assertTrue(q.remove(new Integer(i)));
242 <        }
243 <        for (int i = 0; i < SIZE; i+=2) {
244 <            assertTrue(q.remove(new Integer(i)));
245 <            assertFalse(q.remove(new Integer(i+1)));
240 >        for (int i = 1; i < SIZE; i += 2) {
241 >            assertTrue(q.contains(i));
242 >            assertTrue(q.remove((Integer)i));
243 >            assertFalse(q.contains(i));
244 >            assertTrue(q.contains(i-1));
245 >        }
246 >        for (int i = 0; i < SIZE; i += 2) {
247 >            assertTrue(q.contains(i));
248 >            assertTrue(q.remove((Integer)i));
249 >            assertFalse(q.contains(i));
250 >            assertFalse(q.remove((Integer)(i+1)));
251 >            assertFalse(q.contains(i+1));
252          }
253          assertTrue(q.isEmpty());
254      }
# Line 318 | Line 329 | public class LinkedListTest extends JSR1
329      }
330  
331      /**
332 <     *  toArray contains all elements
332 >     * toArray contains all elements in FIFO order
333       */
334      public void testToArray() {
335          LinkedList q = populatedQueue(SIZE);
336          Object[] o = q.toArray();
326        Arrays.sort(o);
337          for (int i = 0; i < o.length; i++)
338 <            assertEquals(o[i], q.poll());
338 >            assertSame(o[i], q.poll());
339      }
340  
341      /**
342 <     *  toArray(a) contains all elements
342 >     * toArray(a) contains all elements in FIFO order
343       */
344      public void testToArray2() {
345 <        LinkedList q = populatedQueue(SIZE);
345 >        LinkedList<Integer> q = populatedQueue(SIZE);
346          Integer[] ints = new Integer[SIZE];
347 <        ints = (Integer[])q.toArray(ints);
348 <        Arrays.sort(ints);
347 >        Integer[] array = q.toArray(ints);
348 >        assertSame(ints, array);
349          for (int i = 0; i < ints.length; i++)
350 <            assertEquals(ints[i], q.poll());
350 >            assertSame(ints[i], q.poll());
351      }
352  
353      /**
354 <     * toArray(null) throws NPE
354 >     * toArray(null) throws NullPointerException
355       */
356 <    public void testToArray_BadArg() {
356 >    public void testToArray_NullArg() {
357          LinkedList l = new LinkedList();
358          l.add(new Object());
359          try {
360 <            Object o[] = l.toArray(null);
360 >            l.toArray(null);
361              shouldThrow();
362          } catch (NullPointerException success) {}
363      }
364  
365      /**
366 <     * toArray with incompatable aray type throws CCE
366 >     * toArray(incompatible array type) throws ArrayStoreException
367       */
368      public void testToArray1_BadArg() {
369          LinkedList l = new LinkedList();
370          l.add(new Integer(5));
371          try {
372 <            Object o[] = l.toArray(new String[10]);
372 >            l.toArray(new String[10]);
373              shouldThrow();
374          } catch (ArrayStoreException success) {}
375      }
376  
377      /**
378 <     *  iterator iterates through all elements
378 >     * iterator iterates through all elements
379       */
380      public void testIterator() {
381          LinkedList q = populatedQueue(SIZE);
# Line 379 | Line 389 | public class LinkedListTest extends JSR1
389      }
390  
391      /**
392 <     *  iterator ordering is FIFO
392 >     * iterator ordering is FIFO
393       */
394      public void testIteratorOrdering() {
395          final LinkedList q = new LinkedList();
# Line 397 | Line 407 | public class LinkedListTest extends JSR1
407      /**
408       * iterator.remove removes current element
409       */
410 <    public void testIteratorRemove () {
410 >    public void testIteratorRemove() {
411          final LinkedList q = new LinkedList();
412          q.add(new Integer(1));
413          q.add(new Integer(2));
414          q.add(new Integer(3));
415          Iterator it = q.iterator();
416 <        assertEquals(it.next(), 1);
416 >        assertEquals(1, it.next());
417          it.remove();
418          it = q.iterator();
419 <        assertEquals(it.next(), 2);
420 <        assertEquals(it.next(), 3);
419 >        assertEquals(2, it.next());
420 >        assertEquals(3, it.next());
421          assertFalse(it.hasNext());
422      }
423  
424      /**
425 <     *  Descending iterator iterates through all elements
425 >     * Descending iterator iterates through all elements
426       */
427      public void testDescendingIterator() {
428          LinkedList q = populatedQueue(SIZE);
# Line 431 | Line 441 | public class LinkedListTest extends JSR1
441      }
442  
443      /**
444 <     *  Descending iterator ordering is reverse FIFO
444 >     * Descending iterator ordering is reverse FIFO
445       */
446      public void testDescendingIteratorOrdering() {
447          final LinkedList q = new LinkedList();
# Line 449 | Line 459 | public class LinkedListTest extends JSR1
459      /**
460       * descendingIterator.remove removes current element
461       */
462 <    public void testDescendingIteratorRemove () {
462 >    public void testDescendingIteratorRemove() {
463          final LinkedList q = new LinkedList();
464 <        q.add(new Integer(3));
465 <        q.add(new Integer(2));
466 <        q.add(new Integer(1));
464 >        q.add(three);
465 >        q.add(two);
466 >        q.add(one);
467          Iterator it = q.descendingIterator();
468          it.next();
469          it.remove();
470          it = q.descendingIterator();
471 <        assertEquals(it.next(), 2);
472 <        assertEquals(it.next(), 3);
471 >        assertSame(it.next(), two);
472 >        assertSame(it.next(), three);
473          assertFalse(it.hasNext());
474      }
475  
466
476      /**
477       * toString contains toStrings of elements
478       */
# Line 471 | Line 480 | public class LinkedListTest extends JSR1
480          LinkedList q = populatedQueue(SIZE);
481          String s = q.toString();
482          for (int i = 0; i < SIZE; ++i) {
483 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
483 >            assertTrue(s.contains(String.valueOf(i)));
484          }
485      }
486  
# Line 481 | Line 490 | public class LinkedListTest extends JSR1
490      public void testAddFirst() {
491          LinkedList q = populatedQueue(3);
492          q.addFirst(four);
493 <        assertEquals(four,q.peek());
493 >        assertSame(four, q.peek());
494      }
495  
496      /**
# Line 489 | Line 498 | public class LinkedListTest extends JSR1
498       */
499      public void testPush() {
500          LinkedList q = populatedQueue(3);
492        q.pollLast();
501          q.push(four);
502 <        assertEquals(four,q.peekFirst());
502 >        assertSame(four, q.peekFirst());
503      }
504  
505      /**
506 <     *  pop removes next element, or throws NSEE if empty
506 >     * pop removes next element, or throws NSEE if empty
507       */
508      public void testPop() {
509          LinkedList q = populatedQueue(SIZE);
# Line 527 | Line 535 | public class LinkedListTest extends JSR1
535      }
536  
537      /**
538 <     *  pollLast succeeds unless empty
538 >     * pollLast succeeds unless empty
539       */
540      public void testPollLast() {
541          LinkedList q = populatedQueue(SIZE);
# Line 538 | Line 546 | public class LinkedListTest extends JSR1
546      }
547  
548      /**
549 <     *  peekFirst returns next element, or null if empty
549 >     * peekFirst returns next element, or null if empty
550       */
551      public void testPeekFirst() {
552          LinkedList q = populatedQueue(SIZE);
# Line 551 | Line 559 | public class LinkedListTest extends JSR1
559          assertNull(q.peekFirst());
560      }
561  
554
562      /**
563 <     *  peekLast returns next element, or null if empty
563 >     * peekLast returns next element, or null if empty
564       */
565      public void testPeekLast() {
566          LinkedList q = populatedQueue(SIZE);
# Line 579 | Line 586 | public class LinkedListTest extends JSR1
586      }
587  
588      /**
589 <     *  getLast returns next element, or throws NSEE if empty
589 >     * getLast returns next element, or throws NSEE if empty
590       */
591      public void testLastElement() {
592          LinkedList q = populatedQueue(SIZE);
# Line 599 | Line 606 | public class LinkedListTest extends JSR1
606       */
607      public void testRemoveFirstOccurrence() {
608          LinkedList q = populatedQueue(SIZE);
609 <        for (int i = 1; i < SIZE; i+=2) {
609 >        for (int i = 1; i < SIZE; i += 2) {
610              assertTrue(q.removeFirstOccurrence(new Integer(i)));
611          }
612 <        for (int i = 0; i < SIZE; i+=2) {
612 >        for (int i = 0; i < SIZE; i += 2) {
613              assertTrue(q.removeFirstOccurrence(new Integer(i)));
614              assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
615          }
# Line 614 | Line 621 | public class LinkedListTest extends JSR1
621       */
622      public void testRemoveLastOccurrence() {
623          LinkedList q = populatedQueue(SIZE);
624 <        for (int i = 1; i < SIZE; i+=2) {
624 >        for (int i = 1; i < SIZE; i += 2) {
625              assertTrue(q.removeLastOccurrence(new Integer(i)));
626          }
627 <        for (int i = 0; i < SIZE; i+=2) {
627 >        for (int i = 0; i < SIZE; i += 2) {
628              assertTrue(q.removeLastOccurrence(new Integer(i)));
629              assertFalse(q.removeLastOccurrence(new Integer(i+1)));
630          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines