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.11 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.23 by jsr166, Thu Nov 4 01:04:54 2010 UTC

# Line 12 | Line 12 | import java.util.concurrent.*;
12  
13   public class LinkedListTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());
15 >        junit.textui.TestRunner.run(suite());
16      }
17  
18      public static Test suite() {
19 <        return new TestSuite(LinkedListTest.class);
19 >        return new TestSuite(LinkedListTest.class);
20      }
21  
22      /**
# Line 26 | Line 26 | public class LinkedListTest extends JSR1
26      private LinkedList populatedQueue(int n) {
27          LinkedList q = new LinkedList();
28          assertTrue(q.isEmpty());
29 <        for (int i = 0; i < n; ++i)
30 <            assertTrue(q.offer(new Integer(i)));
29 >        for (int i = 0; i < n; ++i)
30 >            assertTrue(q.offer(new Integer(i)));
31          assertFalse(q.isEmpty());
32 <        assertEquals(n, q.size());
32 >        assertEquals(n, q.size());
33          return q;
34      }
35  
# Line 47 | Line 47 | public class LinkedListTest extends JSR1
47          try {
48              LinkedList q = new LinkedList((Collection)null);
49              shouldThrow();
50 <        }
51 <        catch (NullPointerException success) {}
50 >        } catch (NullPointerException success) {}
51      }
52  
53      /**
54       * Queue contains all elements of collection used to initialize
56
55       */
56      public void testConstructor6() {
57 <        try {
58 <            Integer[] ints = new Integer[SIZE];
59 <            for (int i = 0; i < SIZE; ++i)
60 <                ints[i] = new Integer(i);
61 <            LinkedList q = new LinkedList(Arrays.asList(ints));
62 <            for (int i = 0; i < SIZE; ++i)
65 <                assertEquals(ints[i], q.poll());
66 <        }
67 <        finally {}
57 >        Integer[] ints = new Integer[SIZE];
58 >        for (int i = 0; i < SIZE; ++i)
59 >            ints[i] = i;
60 >        LinkedList q = new LinkedList(Arrays.asList(ints));
61 >        for (int i = 0; i < SIZE; ++i)
62 >            assertEquals(ints[i], q.poll());
63      }
64  
65      /**
# Line 100 | Line 95 | public class LinkedListTest extends JSR1
95       * offer(null) succeeds
96       */
97      public void testOfferNull() {
98 <        try {
99 <            LinkedList q = new LinkedList();
105 <            q.offer(null);
106 <        } catch (NullPointerException ie) {
107 <            unexpectedException();
108 <        }
98 >        LinkedList q = new LinkedList();
99 >        q.offer(null);
100      }
101  
102      /**
# Line 136 | Line 127 | public class LinkedListTest extends JSR1
127              LinkedList q = new LinkedList();
128              q.addAll(null);
129              shouldThrow();
130 <        }
140 <        catch (NullPointerException success) {}
130 >        } catch (NullPointerException success) {}
131      }
132  
133      /**
134       * Queue contains all elements, in traversal order, of successful addAll
135       */
136      public void testAddAll5() {
137 <        try {
138 <            Integer[] empty = new Integer[0];
139 <            Integer[] ints = new Integer[SIZE];
140 <            for (int i = 0; i < SIZE; ++i)
141 <                ints[i] = new Integer(i);
142 <            LinkedList q = new LinkedList();
143 <            assertFalse(q.addAll(Arrays.asList(empty)));
144 <            assertTrue(q.addAll(Arrays.asList(ints)));
145 <            for (int i = 0; i < SIZE; ++i)
156 <                assertEquals(ints[i], q.poll());
157 <        }
158 <        finally {}
137 >        Integer[] empty = new Integer[0];
138 >        Integer[] ints = new Integer[SIZE];
139 >        for (int i = 0; i < SIZE; ++i)
140 >            ints[i] = i;
141 >        LinkedList q = new LinkedList();
142 >        assertFalse(q.addAll(Arrays.asList(empty)));
143 >        assertTrue(q.addAll(Arrays.asList(ints)));
144 >        for (int i = 0; i < SIZE; ++i)
145 >            assertEquals(ints[i], q.poll());
146      }
147  
148      /**
149       * addAll with too large an index throws IOOBE
150       */
151      public void testAddAll2_IndexOutOfBoundsException() {
152 <        try {
153 <            LinkedList l = new LinkedList();
154 <            l.add(new Object());
155 <            LinkedList m = new LinkedList();
156 <            m.add(new Object());
157 <            l.addAll(4,m);
158 <            shouldThrow();
159 <        } catch (IndexOutOfBoundsException  success) {}
152 >        LinkedList l = new LinkedList();
153 >        l.add(new Object());
154 >        LinkedList m = new LinkedList();
155 >        m.add(new Object());
156 >        try {
157 >            l.addAll(4,m);
158 >            shouldThrow();
159 >        } catch (IndexOutOfBoundsException success) {}
160      }
161  
162      /**
163       * addAll with negative index throws IOOBE
164       */
165      public void testAddAll4_BadIndex() {
166 <        try {
167 <            LinkedList l = new LinkedList();
168 <            l.add(new Object());
169 <            LinkedList m = new LinkedList();
170 <            m.add(new Object());
171 <            l.addAll(-1,m);
172 <            shouldThrow();
173 <        } catch (IndexOutOfBoundsException  success){}
166 >        LinkedList l = new LinkedList();
167 >        l.add(new Object());
168 >        LinkedList m = new LinkedList();
169 >        m.add(new Object());
170 >        try {
171 >            l.addAll(-1,m);
172 >            shouldThrow();
173 >        } catch (IndexOutOfBoundsException success) {}
174      }
175  
176      /**
177 <     *  poll succeeds unless empty
177 >     * poll succeeds unless empty
178       */
179      public void testPoll() {
180          LinkedList q = populatedQueue(SIZE);
181          for (int i = 0; i < SIZE; ++i) {
182 <            assertEquals(i, ((Integer)q.poll()).intValue());
182 >            assertEquals(i, q.poll());
183          }
184 <        assertNull(q.poll());
184 >        assertNull(q.poll());
185      }
186  
187      /**
188 <     *  peek returns next element, or null if empty
188 >     * peek returns next element, or null if empty
189       */
190      public void testPeek() {
191          LinkedList q = populatedQueue(SIZE);
192          for (int i = 0; i < SIZE; ++i) {
193 <            assertEquals(i, ((Integer)q.peek()).intValue());
194 <            q.poll();
193 >            assertEquals(i, q.peek());
194 >            assertEquals(i, q.poll());
195              assertTrue(q.peek() == null ||
196 <                       i != ((Integer)q.peek()).intValue());
196 >                       !q.peek().equals(i));
197          }
198 <        assertNull(q.peek());
198 >        assertNull(q.peek());
199      }
200  
201      /**
# Line 217 | Line 204 | public class LinkedListTest extends JSR1
204      public void testElement() {
205          LinkedList q = populatedQueue(SIZE);
206          for (int i = 0; i < SIZE; ++i) {
207 <            assertEquals(i, ((Integer)q.element()).intValue());
208 <            q.poll();
207 >            assertEquals(i, q.element());
208 >            assertEquals(i, q.poll());
209          }
210          try {
211              q.element();
212              shouldThrow();
213 <        }
227 <        catch (NoSuchElementException success) {}
213 >        } catch (NoSuchElementException success) {}
214      }
215  
216      /**
217 <     *  remove removes next element, or throws NSEE if empty
217 >     * remove removes next element, or throws NSEE if empty
218       */
219      public void testRemove() {
220          LinkedList q = populatedQueue(SIZE);
221          for (int i = 0; i < SIZE; ++i) {
222 <            assertEquals(i, ((Integer)q.remove()).intValue());
222 >            assertEquals(i, q.remove());
223          }
224          try {
225              q.remove();
226              shouldThrow();
227 <        } catch (NoSuchElementException success){
242 <        }
227 >        } catch (NoSuchElementException success) {}
228      }
229  
230      /**
# Line 277 | Line 262 | public class LinkedListTest extends JSR1
262          q.clear();
263          assertTrue(q.isEmpty());
264          assertEquals(0, q.size());
265 <        q.add(new Integer(1));
265 >        assertTrue(q.add(new Integer(1)));
266          assertFalse(q.isEmpty());
267          q.clear();
268          assertTrue(q.isEmpty());
# Line 292 | Line 277 | public class LinkedListTest extends JSR1
277          for (int i = 0; i < SIZE; ++i) {
278              assertTrue(q.containsAll(p));
279              assertFalse(p.containsAll(q));
280 <            p.add(new Integer(i));
280 >            assertTrue(p.add(new Integer(i)));
281          }
282          assertTrue(p.containsAll(q));
283      }
# Line 333 | Line 318 | public class LinkedListTest extends JSR1
318      }
319  
320      /**
321 <     *  toArray contains all elements
321 >     * toArray contains all elements in FIFO order
322       */
323      public void testToArray() {
324          LinkedList q = populatedQueue(SIZE);
325 <        Object[] o = q.toArray();
326 <        Arrays.sort(o);
327 <        for (int i = 0; i < o.length; i++)
343 <            assertEquals(o[i], q.poll());
325 >        Object[] o = q.toArray();
326 >        for (int i = 0; i < o.length; i++)
327 >            assertSame(o[i], q.poll());
328      }
329  
330      /**
331 <     *  toArray(a) contains all elements
331 >     * toArray(a) contains all elements in FIFO order
332       */
333      public void testToArray2() {
334          LinkedList q = populatedQueue(SIZE);
335 <        Integer[] ints = new Integer[SIZE];
336 <        ints = (Integer[])q.toArray(ints);
353 <        Arrays.sort(ints);
335 >        Integer[] ints = new Integer[SIZE];
336 >        assertSame(ints, q.toArray(ints));
337          for (int i = 0; i < ints.length; i++)
338 <            assertEquals(ints[i], q.poll());
338 >            assertSame(ints[i], q.poll());
339      }
340  
341      /**
342 <     * toArray(null) throws NPE
342 >     * toArray(null) throws NullPointerException
343       */
344 <    public void testToArray_BadArg() {
345 <        try {
346 <            LinkedList l = new LinkedList();
347 <            l.add(new Object());
348 <            Object o[] = l.toArray(null);
349 <            shouldThrow();
350 <        } catch (NullPointerException success){}
344 >    public void testToArray_NullArg() {
345 >        LinkedList l = new LinkedList();
346 >        l.add(new Object());
347 >        try {
348 >            l.toArray(null);
349 >            shouldThrow();
350 >        } catch (NullPointerException success) {}
351      }
352  
353      /**
354 <     * toArray with incompatable aray type throws CCE
354 >     * toArray(incompatible array type) throws ArrayStoreException
355       */
356      public void testToArray1_BadArg() {
357 <        try {
358 <            LinkedList l = new LinkedList();
359 <            l.add(new Integer(5));
360 <            Object o[] = l.toArray(new String[10] );
361 <            shouldThrow();
362 <        } catch (ArrayStoreException  success){}
357 >        LinkedList l = new LinkedList();
358 >        l.add(new Integer(5));
359 >        try {
360 >            l.toArray(new String[10]);
361 >            shouldThrow();
362 >        } catch (ArrayStoreException success) {}
363      }
364  
365      /**
366 <     *  iterator iterates through all elements
366 >     * iterator iterates through all elements
367       */
368      public void testIterator() {
369          LinkedList q = populatedQueue(SIZE);
370          int i = 0;
371 <        Iterator it = q.iterator();
371 >        Iterator it = q.iterator();
372          while (it.hasNext()) {
373              assertTrue(q.contains(it.next()));
374              ++i;
# Line 394 | Line 377 | public class LinkedListTest extends JSR1
377      }
378  
379      /**
380 <     *  iterator ordering is FIFO
380 >     * iterator ordering is FIFO
381       */
382      public void testIteratorOrdering() {
383          final LinkedList q = new LinkedList();
# Line 403 | Line 386 | public class LinkedListTest extends JSR1
386          q.add(new Integer(3));
387          int k = 0;
388          for (Iterator it = q.iterator(); it.hasNext();) {
389 <            int i = ((Integer)(it.next())).intValue();
407 <            assertEquals(++k, i);
389 >            assertEquals(++k, it.next());
390          }
391  
392          assertEquals(3, k);
# Line 413 | Line 395 | public class LinkedListTest extends JSR1
395      /**
396       * iterator.remove removes current element
397       */
398 <    public void testIteratorRemove () {
398 >    public void testIteratorRemove() {
399          final LinkedList q = new LinkedList();
400          q.add(new Integer(1));
401          q.add(new Integer(2));
402          q.add(new Integer(3));
403          Iterator it = q.iterator();
404 <        it.next();
404 >        assertEquals(it.next(), 1);
405          it.remove();
406          it = q.iterator();
407 <        assertEquals(it.next(), new Integer(2));
408 <        assertEquals(it.next(), new Integer(3));
407 >        assertEquals(it.next(), 2);
408 >        assertEquals(it.next(), 3);
409          assertFalse(it.hasNext());
410      }
411  
412      /**
413 <     *  Descending iterator iterates through all elements
413 >     * Descending iterator iterates through all elements
414       */
415      public void testDescendingIterator() {
416          LinkedList q = populatedQueue(SIZE);
417          int i = 0;
418 <        Iterator it = q.descendingIterator();
418 >        Iterator it = q.descendingIterator();
419          while (it.hasNext()) {
420              assertTrue(q.contains(it.next()));
421              ++i;
# Line 442 | Line 424 | public class LinkedListTest extends JSR1
424          assertFalse(it.hasNext());
425          try {
426              it.next();
427 <        } catch (NoSuchElementException success) {
428 <        }
427 >            shouldThrow();
428 >        } catch (NoSuchElementException success) {}
429      }
430  
431      /**
432 <     *  Descending iterator ordering is reverse FIFO
432 >     * Descending iterator ordering is reverse FIFO
433       */
434      public void testDescendingIteratorOrdering() {
435          final LinkedList q = new LinkedList();
# Line 456 | Line 438 | public class LinkedListTest extends JSR1
438          q.add(new Integer(1));
439          int k = 0;
440          for (Iterator it = q.descendingIterator(); it.hasNext();) {
441 <            int i = ((Integer)(it.next())).intValue();
460 <            assertEquals(++k, i);
441 >            assertEquals(++k, it.next());
442          }
443  
444          assertEquals(3, k);
# Line 466 | Line 447 | public class LinkedListTest extends JSR1
447      /**
448       * descendingIterator.remove removes current element
449       */
450 <    public void testDescendingIteratorRemove () {
450 >    public void testDescendingIteratorRemove() {
451          final LinkedList q = new LinkedList();
452 <        q.add(new Integer(3));
453 <        q.add(new Integer(2));
454 <        q.add(new Integer(1));
452 >        q.add(three);
453 >        q.add(two);
454 >        q.add(one);
455          Iterator it = q.descendingIterator();
456          it.next();
457          it.remove();
458          it = q.descendingIterator();
459 <        assertEquals(it.next(), new Integer(2));
460 <        assertEquals(it.next(), new Integer(3));
459 >        assertSame(it.next(), two);
460 >        assertSame(it.next(), three);
461          assertFalse(it.hasNext());
462      }
463  
# Line 497 | Line 478 | public class LinkedListTest extends JSR1
478       */
479      public void testAddFirst() {
480          LinkedList q = populatedQueue(3);
481 <        q.addFirst(four);
482 <        assertEquals(four,q.peek());
481 >        q.addFirst(four);
482 >        assertSame(four, q.peek());
483      }
484  
485      /**
# Line 506 | Line 487 | public class LinkedListTest extends JSR1
487       */
488      public void testPush() {
489          LinkedList q = populatedQueue(3);
490 <        q.pollLast();
491 <        q.push(four);
511 <        assertEquals(four,q.peekFirst());
490 >        q.push(four);
491 >        assertSame(four, q.peekFirst());
492      }
493  
494      /**
495 <     *  pop removes next element, or throws NSEE if empty
495 >     * pop removes next element, or throws NSEE if empty
496       */
497      public void testPop() {
498          LinkedList q = populatedQueue(SIZE);
499          for (int i = 0; i < SIZE; ++i) {
500 <            assertEquals(i, ((Integer)q.pop()).intValue());
500 >            assertEquals(i, q.pop());
501          }
502          try {
503              q.pop();
504              shouldThrow();
505 <        } catch (NoSuchElementException success){
526 <        }
505 >        } catch (NoSuchElementException success) {}
506      }
507  
508      /**
# Line 545 | Line 524 | public class LinkedListTest extends JSR1
524      }
525  
526      /**
527 <     *  pollLast succeeds unless empty
527 >     * pollLast succeeds unless empty
528       */
529      public void testPollLast() {
530          LinkedList q = populatedQueue(SIZE);
531          for (int i = SIZE-1; i >= 0; --i) {
532 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
532 >            assertEquals(i, q.pollLast());
533          }
534 <        assertNull(q.pollLast());
534 >        assertNull(q.pollLast());
535      }
536  
537      /**
538 <     *  peekFirst returns next element, or null if empty
538 >     * peekFirst returns next element, or null if empty
539       */
540      public void testPeekFirst() {
541          LinkedList q = populatedQueue(SIZE);
542          for (int i = 0; i < SIZE; ++i) {
543 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
544 <            q.pollFirst();
543 >            assertEquals(i, q.peekFirst());
544 >            assertEquals(i, q.pollFirst());
545              assertTrue(q.peekFirst() == null ||
546 <                       i != ((Integer)q.peekFirst()).intValue());
546 >                       !q.peekFirst().equals(i));
547          }
548 <        assertNull(q.peekFirst());
548 >        assertNull(q.peekFirst());
549      }
550  
551  
552      /**
553 <     *  peekLast returns next element, or null if empty
553 >     * peekLast returns next element, or null if empty
554       */
555      public void testPeekLast() {
556          LinkedList q = populatedQueue(SIZE);
557          for (int i = SIZE-1; i >= 0; --i) {
558 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
559 <            q.pollLast();
558 >            assertEquals(i, q.peekLast());
559 >            assertEquals(i, q.pollLast());
560              assertTrue(q.peekLast() == null ||
561 <                       i != ((Integer)q.peekLast()).intValue());
561 >                       !q.peekLast().equals(i));
562          }
563 <        assertNull(q.peekLast());
563 >        assertNull(q.peekLast());
564      }
565  
566      public void testFirstElement() {
567          LinkedList q = populatedQueue(SIZE);
568          for (int i = 0; i < SIZE; ++i) {
569 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
570 <            q.pollFirst();
569 >            assertEquals(i, q.getFirst());
570 >            assertEquals(i, q.pollFirst());
571          }
572          try {
573              q.getFirst();
574              shouldThrow();
575 <        }
597 <        catch (NoSuchElementException success) {}
575 >        } catch (NoSuchElementException success) {}
576      }
577  
578      /**
579 <     *  getLast returns next element, or throws NSEE if empty
579 >     * getLast returns next element, or throws NSEE if empty
580       */
581      public void testLastElement() {
582          LinkedList q = populatedQueue(SIZE);
583          for (int i = SIZE-1; i >= 0; --i) {
584 <            assertEquals(i, ((Integer)q.getLast()).intValue());
585 <            q.pollLast();
584 >            assertEquals(i, q.getLast());
585 >            assertEquals(i, q.pollLast());
586          }
587          try {
588              q.getLast();
589              shouldThrow();
590 <        }
591 <        catch (NoSuchElementException success) {}
614 <        assertNull(q.peekLast());
590 >        } catch (NoSuchElementException success) {}
591 >        assertNull(q.peekLast());
592      }
593  
594      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines