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.14 by jsr166, Sat Nov 21 02:39:41 2009 UTC vs.
Revision 1.24 by jsr166, Fri Nov 5 00:17:22 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() {
# Line 23 | Line 23 | public class LinkedListTest extends JSR1
23       * Create a queue of given size containing consecutive
24       * Integers 0 ... n.
25       */
26 <    private LinkedList populatedQueue(int n) {
27 <        LinkedList q = new LinkedList();
26 >    private LinkedList<Integer> populatedQueue(int n) {
27 >        LinkedList<Integer> q = new LinkedList<Integer>();
28          assertTrue(q.isEmpty());
29          for (int i = 0; i < n; ++i)
30              assertTrue(q.offer(new Integer(i)));
# Line 56 | Line 56 | public class LinkedListTest extends JSR1
56      public void testConstructor6() {
57          Integer[] ints = new Integer[SIZE];
58          for (int i = 0; i < SIZE; ++i)
59 <            ints[i] = new Integer(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());
# Line 137 | Line 137 | public class LinkedListTest extends JSR1
137          Integer[] empty = new Integer[0];
138          Integer[] ints = new Integer[SIZE];
139          for (int i = 0; i < SIZE; ++i)
140 <            ints[i] = new Integer(i);
140 >            ints[i] = i;
141          LinkedList q = new LinkedList();
142          assertFalse(q.addAll(Arrays.asList(empty)));
143          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 156 | Line 156 | public class LinkedListTest extends JSR1
156          try {
157              l.addAll(4,m);
158              shouldThrow();
159 <        } catch (IndexOutOfBoundsException  success) {}
159 >        } catch (IndexOutOfBoundsException success) {}
160      }
161  
162      /**
# Line 170 | Line 170 | public class LinkedListTest extends JSR1
170          try {
171              l.addAll(-1,m);
172              shouldThrow();
173 <        } catch (IndexOutOfBoundsException  success) {}
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());
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());
199      }
# Line 204 | 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();
# Line 214 | Line 214 | public class LinkedListTest extends JSR1
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();
# Line 262 | 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 277 | 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 318 | 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);
326          for (int i = 0; i < o.length; i++)
327 <            assertEquals(o[i], q.poll());
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);
334 >        LinkedList<Integer> q = populatedQueue(SIZE);
335          Integer[] ints = new Integer[SIZE];
336 <        ints = (Integer[])q.toArray(ints);
337 <        Arrays.sort(ints);
336 >        Integer[] array = q.toArray(ints);
337 >        assertSame(ints, array);
338          for (int i = 0; i < ints.length; i++)
339 <            assertEquals(ints[i], q.poll());
339 >            assertSame(ints[i], q.poll());
340      }
341  
342      /**
343 <     * toArray(null) throws NPE
343 >     * toArray(null) throws NullPointerException
344       */
345 <    public void testToArray_BadArg() {
345 >    public void testToArray_NullArg() {
346 >        LinkedList l = new LinkedList();
347 >        l.add(new Object());
348          try {
349 <            LinkedList l = new LinkedList();
349 <            l.add(new Object());
350 <            Object o[] = l.toArray(null);
349 >            l.toArray(null);
350              shouldThrow();
351          } catch (NullPointerException success) {}
352      }
353  
354      /**
355 <     * toArray with incompatable aray type throws CCE
355 >     * toArray(incompatible array type) throws ArrayStoreException
356       */
357      public void testToArray1_BadArg() {
358 +        LinkedList l = new LinkedList();
359 +        l.add(new Integer(5));
360          try {
361 <            LinkedList l = new LinkedList();
361 <            l.add(new Integer(5));
362 <            Object o[] = l.toArray(new String[10] );
361 >            l.toArray(new String[10]);
362              shouldThrow();
363          } catch (ArrayStoreException success) {}
364      }
365  
366      /**
367 <     *  iterator iterates through all elements
367 >     * iterator iterates through all elements
368       */
369      public void testIterator() {
370          LinkedList q = populatedQueue(SIZE);
# Line 379 | Line 378 | public class LinkedListTest extends JSR1
378      }
379  
380      /**
381 <     *  iterator ordering is FIFO
381 >     * iterator ordering is FIFO
382       */
383      public void testIteratorOrdering() {
384          final LinkedList q = new LinkedList();
# Line 388 | Line 387 | public class LinkedListTest extends JSR1
387          q.add(new Integer(3));
388          int k = 0;
389          for (Iterator it = q.iterator(); it.hasNext();) {
390 <            int i = ((Integer)(it.next())).intValue();
392 <            assertEquals(++k, i);
390 >            assertEquals(++k, it.next());
391          }
392  
393          assertEquals(3, k);
# Line 398 | Line 396 | public class LinkedListTest extends JSR1
396      /**
397       * iterator.remove removes current element
398       */
399 <    public void testIteratorRemove () {
399 >    public void testIteratorRemove() {
400          final LinkedList q = new LinkedList();
401          q.add(new Integer(1));
402          q.add(new Integer(2));
403          q.add(new Integer(3));
404          Iterator it = q.iterator();
405 <        it.next();
405 >        assertEquals(it.next(), 1);
406          it.remove();
407          it = q.iterator();
408 <        assertEquals(it.next(), new Integer(2));
409 <        assertEquals(it.next(), new Integer(3));
408 >        assertEquals(it.next(), 2);
409 >        assertEquals(it.next(), 3);
410          assertFalse(it.hasNext());
411      }
412  
413      /**
414 <     *  Descending iterator iterates through all elements
414 >     * Descending iterator iterates through all elements
415       */
416      public void testDescendingIterator() {
417          LinkedList q = populatedQueue(SIZE);
# Line 432 | Line 430 | public class LinkedListTest extends JSR1
430      }
431  
432      /**
433 <     *  Descending iterator ordering is reverse FIFO
433 >     * Descending iterator ordering is reverse FIFO
434       */
435      public void testDescendingIteratorOrdering() {
436          final LinkedList q = new LinkedList();
# Line 441 | Line 439 | public class LinkedListTest extends JSR1
439          q.add(new Integer(1));
440          int k = 0;
441          for (Iterator it = q.descendingIterator(); it.hasNext();) {
442 <            int i = ((Integer)(it.next())).intValue();
445 <            assertEquals(++k, i);
442 >            assertEquals(++k, it.next());
443          }
444  
445          assertEquals(3, k);
# Line 451 | Line 448 | public class LinkedListTest extends JSR1
448      /**
449       * descendingIterator.remove removes current element
450       */
451 <    public void testDescendingIteratorRemove () {
451 >    public void testDescendingIteratorRemove() {
452          final LinkedList q = new LinkedList();
453 <        q.add(new Integer(3));
454 <        q.add(new Integer(2));
455 <        q.add(new Integer(1));
453 >        q.add(three);
454 >        q.add(two);
455 >        q.add(one);
456          Iterator it = q.descendingIterator();
457          it.next();
458          it.remove();
459          it = q.descendingIterator();
460 <        assertEquals(it.next(), new Integer(2));
461 <        assertEquals(it.next(), new Integer(3));
460 >        assertSame(it.next(), two);
461 >        assertSame(it.next(), three);
462          assertFalse(it.hasNext());
463      }
464  
# Line 483 | Line 480 | public class LinkedListTest extends JSR1
480      public void testAddFirst() {
481          LinkedList q = populatedQueue(3);
482          q.addFirst(four);
483 <        assertEquals(four,q.peek());
483 >        assertSame(four, q.peek());
484      }
485  
486      /**
# Line 491 | Line 488 | public class LinkedListTest extends JSR1
488       */
489      public void testPush() {
490          LinkedList q = populatedQueue(3);
494        q.pollLast();
491          q.push(four);
492 <        assertEquals(four,q.peekFirst());
492 >        assertSame(four, q.peekFirst());
493      }
494  
495      /**
496 <     *  pop removes next element, or throws NSEE if empty
496 >     * pop removes next element, or throws NSEE if empty
497       */
498      public void testPop() {
499          LinkedList q = populatedQueue(SIZE);
500          for (int i = 0; i < SIZE; ++i) {
501 <            assertEquals(i, ((Integer)q.pop()).intValue());
501 >            assertEquals(i, q.pop());
502          }
503          try {
504              q.pop();
# Line 529 | Line 525 | public class LinkedListTest extends JSR1
525      }
526  
527      /**
528 <     *  pollLast succeeds unless empty
528 >     * pollLast succeeds unless empty
529       */
530      public void testPollLast() {
531          LinkedList q = populatedQueue(SIZE);
532          for (int i = SIZE-1; i >= 0; --i) {
533 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
533 >            assertEquals(i, q.pollLast());
534          }
535          assertNull(q.pollLast());
536      }
537  
538      /**
539 <     *  peekFirst returns next element, or null if empty
539 >     * peekFirst returns next element, or null if empty
540       */
541      public void testPeekFirst() {
542          LinkedList q = populatedQueue(SIZE);
543          for (int i = 0; i < SIZE; ++i) {
544 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
545 <            q.pollFirst();
544 >            assertEquals(i, q.peekFirst());
545 >            assertEquals(i, q.pollFirst());
546              assertTrue(q.peekFirst() == null ||
547 <                       i != ((Integer)q.peekFirst()).intValue());
547 >                       !q.peekFirst().equals(i));
548          }
549          assertNull(q.peekFirst());
550      }
551  
552  
553      /**
554 <     *  peekLast returns next element, or null if empty
554 >     * peekLast returns next element, or null if empty
555       */
556      public void testPeekLast() {
557          LinkedList q = populatedQueue(SIZE);
558          for (int i = SIZE-1; i >= 0; --i) {
559 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
560 <            q.pollLast();
559 >            assertEquals(i, q.peekLast());
560 >            assertEquals(i, q.pollLast());
561              assertTrue(q.peekLast() == null ||
562 <                       i != ((Integer)q.peekLast()).intValue());
562 >                       !q.peekLast().equals(i));
563          }
564          assertNull(q.peekLast());
565      }
# Line 571 | Line 567 | public class LinkedListTest extends JSR1
567      public void testFirstElement() {
568          LinkedList q = populatedQueue(SIZE);
569          for (int i = 0; i < SIZE; ++i) {
570 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
571 <            q.pollFirst();
570 >            assertEquals(i, q.getFirst());
571 >            assertEquals(i, q.pollFirst());
572          }
573          try {
574              q.getFirst();
# Line 581 | Line 577 | public class LinkedListTest extends JSR1
577      }
578  
579      /**
580 <     *  getLast returns next element, or throws NSEE if empty
580 >     * getLast returns next element, or throws NSEE if empty
581       */
582      public void testLastElement() {
583          LinkedList q = populatedQueue(SIZE);
584          for (int i = SIZE-1; i >= 0; --i) {
585 <            assertEquals(i, ((Integer)q.getLast()).intValue());
586 <            q.pollLast();
585 >            assertEquals(i, q.getLast());
586 >            assertEquals(i, q.pollLast());
587          }
588          try {
589              q.getLast();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines