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.13 by jsr166, Sat Nov 21 02:07:27 2009 UTC vs.
Revision 1.28 by jsr166, Tue May 31 16:16:24 2011 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.*;
10 > import java.util.Arrays;
11 > import java.util.Collection;
12 > import java.util.Iterator;
13 > import java.util.LinkedList;
14 > import java.util.NoSuchElementException;
15  
16   public class LinkedListTest extends JSR166TestCase {
17      public static void main(String[] args) {
18 <        junit.textui.TestRunner.run (suite());
18 >        junit.textui.TestRunner.run(suite());
19      }
20  
21      public static Test suite() {
# Line 23 | Line 26 | public class LinkedListTest extends JSR1
26       * Create a queue of given size containing consecutive
27       * Integers 0 ... n.
28       */
29 <    private LinkedList populatedQueue(int n) {
30 <        LinkedList q = new LinkedList();
29 >    private LinkedList<Integer> populatedQueue(int n) {
30 >        LinkedList<Integer> q = new LinkedList<Integer>();
31          assertTrue(q.isEmpty());
32          for (int i = 0; i < n; ++i)
33              assertTrue(q.offer(new Integer(i)));
# Line 47 | Line 50 | public class LinkedListTest extends JSR1
50          try {
51              LinkedList q = new LinkedList((Collection)null);
52              shouldThrow();
53 <        }
51 <        catch (NullPointerException success) {}
53 >        } catch (NullPointerException success) {}
54      }
55  
56      /**
57       * Queue contains all elements of collection used to initialize
56
58       */
59      public void testConstructor6() {
60 <        try {
61 <            Integer[] ints = new Integer[SIZE];
62 <            for (int i = 0; i < SIZE; ++i)
63 <                ints[i] = new Integer(i);
64 <            LinkedList q = new LinkedList(Arrays.asList(ints));
65 <            for (int i = 0; i < SIZE; ++i)
65 <                assertEquals(ints[i], q.poll());
66 <        }
67 <        finally {}
60 >        Integer[] ints = new Integer[SIZE];
61 >        for (int i = 0; i < SIZE; ++i)
62 >            ints[i] = i;
63 >        LinkedList q = new LinkedList(Arrays.asList(ints));
64 >        for (int i = 0; i < SIZE; ++i)
65 >            assertEquals(ints[i], q.poll());
66      }
67  
68      /**
# Line 100 | Line 98 | public class LinkedListTest extends JSR1
98       * offer(null) succeeds
99       */
100      public void testOfferNull() {
101 <        try {
102 <            LinkedList q = new LinkedList();
105 <            q.offer(null);
106 <        } catch (NullPointerException ie) {
107 <            unexpectedException();
108 <        }
101 >        LinkedList q = new LinkedList();
102 >        q.offer(null);
103      }
104  
105      /**
# Line 136 | Line 130 | public class LinkedListTest extends JSR1
130              LinkedList q = new LinkedList();
131              q.addAll(null);
132              shouldThrow();
133 <        }
140 <        catch (NullPointerException success) {}
133 >        } catch (NullPointerException success) {}
134      }
135  
136      /**
137       * Queue contains all elements, in traversal order, of successful addAll
138       */
139      public void testAddAll5() {
140 <        try {
141 <            Integer[] empty = new Integer[0];
142 <            Integer[] ints = new Integer[SIZE];
143 <            for (int i = 0; i < SIZE; ++i)
144 <                ints[i] = new Integer(i);
145 <            LinkedList q = new LinkedList();
146 <            assertFalse(q.addAll(Arrays.asList(empty)));
147 <            assertTrue(q.addAll(Arrays.asList(ints)));
148 <            for (int i = 0; i < SIZE; ++i)
156 <                assertEquals(ints[i], q.poll());
157 <        }
158 <        finally {}
140 >        Integer[] empty = new Integer[0];
141 >        Integer[] ints = new Integer[SIZE];
142 >        for (int i = 0; i < SIZE; ++i)
143 >            ints[i] = i;
144 >        LinkedList q = new LinkedList();
145 >        assertFalse(q.addAll(Arrays.asList(empty)));
146 >        assertTrue(q.addAll(Arrays.asList(ints)));
147 >        for (int i = 0; i < SIZE; ++i)
148 >            assertEquals(ints[i], q.poll());
149      }
150  
151      /**
152       * addAll with too large an index throws IOOBE
153       */
154      public void testAddAll2_IndexOutOfBoundsException() {
155 +        LinkedList l = new LinkedList();
156 +        l.add(new Object());
157 +        LinkedList m = new LinkedList();
158 +        m.add(new Object());
159          try {
166            LinkedList l = new LinkedList();
167            l.add(new Object());
168            LinkedList m = new LinkedList();
169            m.add(new Object());
160              l.addAll(4,m);
161              shouldThrow();
162 <        } catch (IndexOutOfBoundsException  success) {}
162 >        } catch (IndexOutOfBoundsException success) {}
163      }
164  
165      /**
166       * addAll with negative index throws IOOBE
167       */
168      public void testAddAll4_BadIndex() {
169 +        LinkedList l = new LinkedList();
170 +        l.add(new Object());
171 +        LinkedList m = new LinkedList();
172 +        m.add(new Object());
173          try {
180            LinkedList l = new LinkedList();
181            l.add(new Object());
182            LinkedList m = new LinkedList();
183            m.add(new Object());
174              l.addAll(-1,m);
175              shouldThrow();
176 <        } catch (IndexOutOfBoundsException  success) {}
176 >        } catch (IndexOutOfBoundsException success) {}
177      }
178  
179      /**
180 <     *  poll succeeds unless empty
180 >     * poll succeeds unless empty
181       */
182      public void testPoll() {
183          LinkedList q = populatedQueue(SIZE);
184          for (int i = 0; i < SIZE; ++i) {
185 <            assertEquals(i, ((Integer)q.poll()).intValue());
185 >            assertEquals(i, q.poll());
186          }
187          assertNull(q.poll());
188      }
189  
190      /**
191 <     *  peek returns next element, or null if empty
191 >     * peek returns next element, or null if empty
192       */
193      public void testPeek() {
194          LinkedList q = populatedQueue(SIZE);
195          for (int i = 0; i < SIZE; ++i) {
196 <            assertEquals(i, ((Integer)q.peek()).intValue());
197 <            q.poll();
196 >            assertEquals(i, q.peek());
197 >            assertEquals(i, q.poll());
198              assertTrue(q.peek() == null ||
199 <                       i != ((Integer)q.peek()).intValue());
199 >                       !q.peek().equals(i));
200          }
201          assertNull(q.peek());
202      }
# Line 217 | Line 207 | public class LinkedListTest extends JSR1
207      public void testElement() {
208          LinkedList q = populatedQueue(SIZE);
209          for (int i = 0; i < SIZE; ++i) {
210 <            assertEquals(i, ((Integer)q.element()).intValue());
211 <            q.poll();
210 >            assertEquals(i, q.element());
211 >            assertEquals(i, q.poll());
212          }
213          try {
214              q.element();
215              shouldThrow();
216 <        }
227 <        catch (NoSuchElementException success) {}
216 >        } catch (NoSuchElementException success) {}
217      }
218  
219      /**
220 <     *  remove removes next element, or throws NSEE if empty
220 >     * remove removes next element, or throws NSEE if empty
221       */
222      public void testRemove() {
223          LinkedList q = populatedQueue(SIZE);
224          for (int i = 0; i < SIZE; ++i) {
225 <            assertEquals(i, ((Integer)q.remove()).intValue());
225 >            assertEquals(i, q.remove());
226          }
227          try {
228              q.remove();
229              shouldThrow();
230 <        } catch (NoSuchElementException success) {
242 <        }
230 >        } catch (NoSuchElementException success) {}
231      }
232  
233      /**
# Line 248 | Line 236 | public class LinkedListTest extends JSR1
236      public void testRemoveElement() {
237          LinkedList q = populatedQueue(SIZE);
238          for (int i = 1; i < SIZE; i+=2) {
239 <            assertTrue(q.remove(new Integer(i)));
239 >            assertTrue(q.contains(i));
240 >            assertTrue(q.remove((Integer)i));
241 >            assertFalse(q.contains(i));
242 >            assertTrue(q.contains(i-1));
243          }
244          for (int i = 0; i < SIZE; i+=2) {
245 <            assertTrue(q.remove(new Integer(i)));
246 <            assertFalse(q.remove(new Integer(i+1)));
245 >            assertTrue(q.contains(i));
246 >            assertTrue(q.remove((Integer)i));
247 >            assertFalse(q.contains(i));
248 >            assertFalse(q.remove((Integer)(i+1)));
249 >            assertFalse(q.contains(i+1));
250          }
251          assertTrue(q.isEmpty());
252      }
# Line 277 | Line 271 | public class LinkedListTest extends JSR1
271          q.clear();
272          assertTrue(q.isEmpty());
273          assertEquals(0, q.size());
274 <        q.add(new Integer(1));
274 >        assertTrue(q.add(new Integer(1)));
275          assertFalse(q.isEmpty());
276          q.clear();
277          assertTrue(q.isEmpty());
# Line 292 | Line 286 | public class LinkedListTest extends JSR1
286          for (int i = 0; i < SIZE; ++i) {
287              assertTrue(q.containsAll(p));
288              assertFalse(p.containsAll(q));
289 <            p.add(new Integer(i));
289 >            assertTrue(p.add(new Integer(i)));
290          }
291          assertTrue(p.containsAll(q));
292      }
# Line 333 | Line 327 | public class LinkedListTest extends JSR1
327      }
328  
329      /**
330 <     *  toArray contains all elements
330 >     * toArray contains all elements in FIFO order
331       */
332      public void testToArray() {
333          LinkedList q = populatedQueue(SIZE);
334          Object[] o = q.toArray();
341        Arrays.sort(o);
335          for (int i = 0; i < o.length; i++)
336 <            assertEquals(o[i], q.poll());
336 >            assertSame(o[i], q.poll());
337      }
338  
339      /**
340 <     *  toArray(a) contains all elements
340 >     * toArray(a) contains all elements in FIFO order
341       */
342      public void testToArray2() {
343 <        LinkedList q = populatedQueue(SIZE);
343 >        LinkedList<Integer> q = populatedQueue(SIZE);
344          Integer[] ints = new Integer[SIZE];
345 <        ints = (Integer[])q.toArray(ints);
346 <        Arrays.sort(ints);
345 >        Integer[] array = q.toArray(ints);
346 >        assertSame(ints, array);
347          for (int i = 0; i < ints.length; i++)
348 <            assertEquals(ints[i], q.poll());
348 >            assertSame(ints[i], q.poll());
349      }
350  
351      /**
352 <     * toArray(null) throws NPE
352 >     * toArray(null) throws NullPointerException
353       */
354 <    public void testToArray_BadArg() {
354 >    public void testToArray_NullArg() {
355 >        LinkedList l = new LinkedList();
356 >        l.add(new Object());
357          try {
358 <            LinkedList l = new LinkedList();
364 <            l.add(new Object());
365 <            Object o[] = l.toArray(null);
358 >            l.toArray(null);
359              shouldThrow();
360          } catch (NullPointerException success) {}
361      }
362  
363      /**
364 <     * toArray with incompatable aray type throws CCE
364 >     * toArray(incompatible array type) throws ArrayStoreException
365       */
366      public void testToArray1_BadArg() {
367 +        LinkedList l = new LinkedList();
368 +        l.add(new Integer(5));
369          try {
370 <            LinkedList l = new LinkedList();
376 <            l.add(new Integer(5));
377 <            Object o[] = l.toArray(new String[10] );
370 >            l.toArray(new String[10]);
371              shouldThrow();
372 <        } catch (ArrayStoreException  success) {}
372 >        } catch (ArrayStoreException success) {}
373      }
374  
375      /**
376 <     *  iterator iterates through all elements
376 >     * iterator iterates through all elements
377       */
378      public void testIterator() {
379          LinkedList q = populatedQueue(SIZE);
# Line 394 | Line 387 | public class LinkedListTest extends JSR1
387      }
388  
389      /**
390 <     *  iterator ordering is FIFO
390 >     * iterator ordering is FIFO
391       */
392      public void testIteratorOrdering() {
393          final LinkedList q = new LinkedList();
# Line 403 | Line 396 | public class LinkedListTest extends JSR1
396          q.add(new Integer(3));
397          int k = 0;
398          for (Iterator it = q.iterator(); it.hasNext();) {
399 <            int i = ((Integer)(it.next())).intValue();
407 <            assertEquals(++k, i);
399 >            assertEquals(++k, it.next());
400          }
401  
402          assertEquals(3, k);
# Line 413 | Line 405 | public class LinkedListTest extends JSR1
405      /**
406       * iterator.remove removes current element
407       */
408 <    public void testIteratorRemove () {
408 >    public void testIteratorRemove() {
409          final LinkedList q = new LinkedList();
410          q.add(new Integer(1));
411          q.add(new Integer(2));
412          q.add(new Integer(3));
413          Iterator it = q.iterator();
414 <        it.next();
414 >        assertEquals(it.next(), 1);
415          it.remove();
416          it = q.iterator();
417 <        assertEquals(it.next(), new Integer(2));
418 <        assertEquals(it.next(), new Integer(3));
417 >        assertEquals(it.next(), 2);
418 >        assertEquals(it.next(), 3);
419          assertFalse(it.hasNext());
420      }
421  
422      /**
423 <     *  Descending iterator iterates through all elements
423 >     * Descending iterator iterates through all elements
424       */
425      public void testDescendingIterator() {
426          LinkedList q = populatedQueue(SIZE);
# Line 442 | Line 434 | public class LinkedListTest extends JSR1
434          assertFalse(it.hasNext());
435          try {
436              it.next();
437 <        } catch (NoSuchElementException success) {
438 <        }
437 >            shouldThrow();
438 >        } catch (NoSuchElementException success) {}
439      }
440  
441      /**
442 <     *  Descending iterator ordering is reverse FIFO
442 >     * Descending iterator ordering is reverse FIFO
443       */
444      public void testDescendingIteratorOrdering() {
445          final LinkedList q = new LinkedList();
# Line 456 | Line 448 | public class LinkedListTest extends JSR1
448          q.add(new Integer(1));
449          int k = 0;
450          for (Iterator it = q.descendingIterator(); it.hasNext();) {
451 <            int i = ((Integer)(it.next())).intValue();
460 <            assertEquals(++k, i);
451 >            assertEquals(++k, it.next());
452          }
453  
454          assertEquals(3, k);
# Line 466 | Line 457 | public class LinkedListTest extends JSR1
457      /**
458       * descendingIterator.remove removes current element
459       */
460 <    public void testDescendingIteratorRemove () {
460 >    public void testDescendingIteratorRemove() {
461          final LinkedList q = new LinkedList();
462 <        q.add(new Integer(3));
463 <        q.add(new Integer(2));
464 <        q.add(new Integer(1));
462 >        q.add(three);
463 >        q.add(two);
464 >        q.add(one);
465          Iterator it = q.descendingIterator();
466          it.next();
467          it.remove();
468          it = q.descendingIterator();
469 <        assertEquals(it.next(), new Integer(2));
470 <        assertEquals(it.next(), new Integer(3));
469 >        assertSame(it.next(), two);
470 >        assertSame(it.next(), three);
471          assertFalse(it.hasNext());
472      }
473  
483
474      /**
475       * toString contains toStrings of elements
476       */
# Line 488 | Line 478 | public class LinkedListTest extends JSR1
478          LinkedList q = populatedQueue(SIZE);
479          String s = q.toString();
480          for (int i = 0; i < SIZE; ++i) {
481 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
481 >            assertTrue(s.contains(String.valueOf(i)));
482          }
483      }
484  
# Line 498 | Line 488 | public class LinkedListTest extends JSR1
488      public void testAddFirst() {
489          LinkedList q = populatedQueue(3);
490          q.addFirst(four);
491 <        assertEquals(four,q.peek());
491 >        assertSame(four, q.peek());
492      }
493  
494      /**
# Line 506 | Line 496 | public class LinkedListTest extends JSR1
496       */
497      public void testPush() {
498          LinkedList q = populatedQueue(3);
509        q.pollLast();
499          q.push(four);
500 <        assertEquals(four,q.peekFirst());
500 >        assertSame(four, q.peekFirst());
501      }
502  
503      /**
504 <     *  pop removes next element, or throws NSEE if empty
504 >     * pop removes next element, or throws NSEE if empty
505       */
506      public void testPop() {
507          LinkedList q = populatedQueue(SIZE);
508          for (int i = 0; i < SIZE; ++i) {
509 <            assertEquals(i, ((Integer)q.pop()).intValue());
509 >            assertEquals(i, q.pop());
510          }
511          try {
512              q.pop();
513              shouldThrow();
514 <        } catch (NoSuchElementException success) {
526 <        }
514 >        } catch (NoSuchElementException success) {}
515      }
516  
517      /**
# Line 545 | Line 533 | public class LinkedListTest extends JSR1
533      }
534  
535      /**
536 <     *  pollLast succeeds unless empty
536 >     * pollLast succeeds unless empty
537       */
538      public void testPollLast() {
539          LinkedList q = populatedQueue(SIZE);
540          for (int i = SIZE-1; i >= 0; --i) {
541 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
541 >            assertEquals(i, q.pollLast());
542          }
543          assertNull(q.pollLast());
544      }
545  
546      /**
547 <     *  peekFirst returns next element, or null if empty
547 >     * peekFirst returns next element, or null if empty
548       */
549      public void testPeekFirst() {
550          LinkedList q = populatedQueue(SIZE);
551          for (int i = 0; i < SIZE; ++i) {
552 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
553 <            q.pollFirst();
552 >            assertEquals(i, q.peekFirst());
553 >            assertEquals(i, q.pollFirst());
554              assertTrue(q.peekFirst() == null ||
555 <                       i != ((Integer)q.peekFirst()).intValue());
555 >                       !q.peekFirst().equals(i));
556          }
557          assertNull(q.peekFirst());
558      }
559  
572
560      /**
561 <     *  peekLast returns next element, or null if empty
561 >     * peekLast returns next element, or null if empty
562       */
563      public void testPeekLast() {
564          LinkedList q = populatedQueue(SIZE);
565          for (int i = SIZE-1; i >= 0; --i) {
566 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
567 <            q.pollLast();
566 >            assertEquals(i, q.peekLast());
567 >            assertEquals(i, q.pollLast());
568              assertTrue(q.peekLast() == null ||
569 <                       i != ((Integer)q.peekLast()).intValue());
569 >                       !q.peekLast().equals(i));
570          }
571          assertNull(q.peekLast());
572      }
# Line 587 | Line 574 | public class LinkedListTest extends JSR1
574      public void testFirstElement() {
575          LinkedList q = populatedQueue(SIZE);
576          for (int i = 0; i < SIZE; ++i) {
577 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
578 <            q.pollFirst();
577 >            assertEquals(i, q.getFirst());
578 >            assertEquals(i, q.pollFirst());
579          }
580          try {
581              q.getFirst();
582              shouldThrow();
583 <        }
597 <        catch (NoSuchElementException success) {}
583 >        } catch (NoSuchElementException success) {}
584      }
585  
586      /**
587 <     *  getLast returns next element, or throws NSEE if empty
587 >     * getLast returns next element, or throws NSEE if empty
588       */
589      public void testLastElement() {
590          LinkedList q = populatedQueue(SIZE);
591          for (int i = SIZE-1; i >= 0; --i) {
592 <            assertEquals(i, ((Integer)q.getLast()).intValue());
593 <            q.pollLast();
592 >            assertEquals(i, q.getLast());
593 >            assertEquals(i, q.pollLast());
594          }
595          try {
596              q.getLast();
597              shouldThrow();
598 <        }
613 <        catch (NoSuchElementException success) {}
598 >        } catch (NoSuchElementException success) {}
599          assertNull(q.peekLast());
600      }
601  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines