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.4 by dl, Sat Sep 20 18:20:08 2003 UTC vs.
Revision 1.8 by dl, Tue Dec 28 16:15:59 2004 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
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
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
# Line 33 | Line 34 | public class LinkedListTest extends JSR1
34      }
35  
36      /**
37 <     *
37 >     * new queue is empty
38       */
39      public void testConstructor1() {
40          assertEquals(0, new LinkedList().size());
41      }
42  
43      /**
44 <     *
44 >     * Initializing from null Collection throws NPE
45       */
46      public void testConstructor3() {
47          try {
# Line 51 | Line 52 | public class LinkedListTest extends JSR1
52      }
53  
54      /**
55 <     *
55 >     * Queue contains all elements of collection used to initialize
56 >
57       */
58      public void testConstructor6() {
59          try {
# Line 66 | Line 68 | public class LinkedListTest extends JSR1
68      }
69  
70      /**
71 <     *
71 >     * isEmpty is true before add, false after
72       */
73      public void testEmpty() {
74          LinkedList q = new LinkedList();
# Line 80 | Line 82 | public class LinkedListTest extends JSR1
82      }
83  
84      /**
85 <     *
85 >     * size changes when elements added and removed
86       */
87      public void testSize() {
88          LinkedList q = populatedQueue(SIZE);
# Line 95 | Line 97 | public class LinkedListTest extends JSR1
97      }
98  
99      /**
100 <     *
100 >     * offer(null) succeeds
101       */
102      public void testOfferNull() {
103          try {
# Line 107 | Line 109 | public class LinkedListTest extends JSR1
109      }
110  
111      /**
112 <     *
112 >     * Offer succeeds
113       */
114      public void testOffer() {
115          LinkedList q = new LinkedList();
# Line 116 | Line 118 | public class LinkedListTest extends JSR1
118      }
119  
120      /**
121 <     *
121 >     * add succeeds
122       */
123      public void testAdd() {
124          LinkedList q = new LinkedList();
# Line 127 | Line 129 | public class LinkedListTest extends JSR1
129      }
130  
131      /**
132 <     *
132 >     * addAll(null) throws NPE
133       */
134      public void testAddAll1() {
135          try {
# Line 139 | Line 141 | public class LinkedListTest extends JSR1
141      }
142  
143      /**
144 <     *
144 >     * Queue contains all elements, in traversal order, of successful addAll
145       */
146      public void testAddAll5() {
147          try {
# Line 157 | Line 159 | public class LinkedListTest extends JSR1
159      }
160  
161      /**
162 <     *
162 >     * addAll with too large an index throws IOOBE
163 >     */
164 >    public void testAddAll2_IndexOutOfBoundsException() {
165 >        try {
166 >            LinkedList l = new LinkedList();
167 >            l.add(new Object());
168 >            LinkedList m = new LinkedList();
169 >            m.add(new Object());
170 >            l.addAll(4,m);
171 >            shouldThrow();
172 >        } catch(IndexOutOfBoundsException  success) {}
173 >    }
174 >
175 >    /**
176 >     * addAll with negative index throws IOOBE
177 >     */
178 >    public void testAddAll4_BadIndex() {
179 >        try {
180 >            LinkedList l = new LinkedList();
181 >            l.add(new Object());
182 >            LinkedList m = new LinkedList();
183 >            m.add(new Object());
184 >            l.addAll(-1,m);
185 >            shouldThrow();
186 >        } catch(IndexOutOfBoundsException  success){}
187 >    }
188 >
189 >    /**
190 >     *  poll succeeds unless empty
191       */
192      public void testPoll() {
193          LinkedList q = populatedQueue(SIZE);
# Line 168 | Line 198 | public class LinkedListTest extends JSR1
198      }
199  
200      /**
201 <     *
201 >     *  peek returns next element, or null if empty
202       */
203      public void testPeek() {
204          LinkedList q = populatedQueue(SIZE);
# Line 182 | Line 212 | public class LinkedListTest extends JSR1
212      }
213  
214      /**
215 <     *
215 >     * element returns next element, or throws NSEE if empty
216       */
217      public void testElement() {
218          LinkedList q = populatedQueue(SIZE);
# Line 198 | Line 228 | public class LinkedListTest extends JSR1
228      }
229  
230      /**
231 <     *
231 >     *  remove removes next element, or throws NSEE if empty
232       */
233      public void testRemove() {
234          LinkedList q = populatedQueue(SIZE);
# Line 213 | Line 243 | public class LinkedListTest extends JSR1
243      }
244  
245      /**
246 <     *
246 >     * remove(x) removes x and returns true if present
247       */
248      public void testRemoveElement() {
249          LinkedList q = populatedQueue(SIZE);
# Line 228 | Line 258 | public class LinkedListTest extends JSR1
258      }
259          
260      /**
261 <     *
261 >     * contains(x) reports true when elements added but not yet removed
262       */
263      public void testContains() {
264          LinkedList q = populatedQueue(SIZE);
# Line 240 | Line 270 | public class LinkedListTest extends JSR1
270      }
271  
272      /**
273 <     *
273 >     * clear removes all elements
274       */
275      public void testClear() {
276          LinkedList q = populatedQueue(SIZE);
# Line 254 | Line 284 | public class LinkedListTest extends JSR1
284      }
285  
286      /**
287 <     *
287 >     * containsAll(c) is true when c contains a subset of elements
288       */
289      public void testContainsAll() {
290          LinkedList q = populatedQueue(SIZE);
# Line 268 | Line 298 | public class LinkedListTest extends JSR1
298      }
299  
300      /**
301 <     *
301 >     * retainAll(c) retains only those elements of c and reports true if changed
302       */
303      public void testRetainAll() {
304          LinkedList q = populatedQueue(SIZE);
# Line 287 | Line 317 | public class LinkedListTest extends JSR1
317      }
318  
319      /**
320 <     *
320 >     * removeAll(c) removes only those elements of c and reports true if changed
321       */
322      public void testRemoveAll() {
323          for (int i = 1; i < SIZE; ++i) {
# Line 303 | Line 333 | public class LinkedListTest extends JSR1
333      }
334  
335      /**
336 <     *
336 >     *  toArray contains all elements
337       */
338      public void testToArray() {
339          LinkedList q = populatedQueue(SIZE);
# Line 314 | Line 344 | public class LinkedListTest extends JSR1
344      }
345  
346      /**
347 <     *
347 >     *  toArray(a) contains all elements
348       */
349      public void testToArray2() {
350          LinkedList q = populatedQueue(SIZE);
# Line 324 | Line 354 | public class LinkedListTest extends JSR1
354          for(int i = 0; i < ints.length; i++)
355              assertEquals(ints[i], q.poll());
356      }
357 +
358 +    /**
359 +     * toArray(null) throws NPE
360 +     */
361 +    public void testToArray_BadArg() {
362 +        try {
363 +            LinkedList l = new LinkedList();
364 +            l.add(new Object());
365 +            Object o[] = l.toArray(null);
366 +            shouldThrow();
367 +        } catch(NullPointerException success){}
368 +    }
369 +
370 +    /**
371 +     * toArray with incompatable aray type throws CCE
372 +     */
373 +    public void testToArray1_BadArg() {
374 +        try {
375 +            LinkedList l = new LinkedList();
376 +            l.add(new Integer(5));
377 +            Object o[] = l.toArray(new String[10] );
378 +            shouldThrow();
379 +        } catch(ArrayStoreException  success){}
380 +    }
381      
382      /**
383 <     *
383 >     *  iterator iterates through all elements
384       */
385      public void testIterator() {
386          LinkedList q = populatedQueue(SIZE);
# Line 340 | Line 394 | public class LinkedListTest extends JSR1
394      }
395  
396      /**
397 <     *
397 >     *  iterator ordering is FIFO
398       */
399      public void testIteratorOrdering() {
346
400          final LinkedList q = new LinkedList();
348
401          q.add(new Integer(1));
402          q.add(new Integer(2));
403          q.add(new Integer(3));
352
404          int k = 0;
405          for (Iterator it = q.iterator(); it.hasNext();) {
406              int i = ((Integer)(it.next())).intValue();
# Line 360 | Line 411 | public class LinkedListTest extends JSR1
411      }
412  
413      /**
414 <     *
414 >     * iterator.remove removes current element
415       */
416      public void testIteratorRemove () {
417          final LinkedList q = new LinkedList();
367
418          q.add(new Integer(1));
419          q.add(new Integer(2));
420          q.add(new Integer(3));
371
421          Iterator it = q.iterator();
422          it.next();
423          it.remove();
375
424          it = q.iterator();
425          assertEquals(it.next(), new Integer(2));
426          assertEquals(it.next(), new Integer(3));
# Line 381 | Line 429 | public class LinkedListTest extends JSR1
429  
430  
431      /**
432 <     *
432 >     * toString contains toStrings of elements
433       */
434      public void testToString() {
435          LinkedList q = populatedQueue(SIZE);
# Line 392 | Line 440 | public class LinkedListTest extends JSR1
440      }        
441  
442      /**
443 <     *
443 >     * peek returns element inserted with addFirst
444       */
445      public void testAddFirst() {
446          LinkedList q = populatedQueue(3);
447 <        q.addFirst(new Integer(4));
448 <        assertEquals(new Integer(4),q.get(0));
447 >        q.addFirst(four);
448 >        assertEquals(four,q.peek());
449      }  
450  
451      /**
452 <     *
405 <     */
406 <    public void testAddLast() {
407 <        LinkedList q = populatedQueue(3);
408 <        q.addLast(new Integer(3));
409 <        assertEquals(new Integer(3),q.get(3));
410 <    }
411 <    
412 <    /**
413 <     *
452 >     * peekFirst returns element inserted with push
453       */
454 <    public void testGetFirst() {
454 >    public void testPush() {
455          LinkedList q = populatedQueue(3);
456 <        assertEquals(new Integer(0),q.getFirst());
456 >        q.pollLast();
457 >        q.push(four);
458 >        assertEquals(four,q.peekFirst());
459      }  
419    
420    /**
421     *
422     */
423    public void testGetLast() {
424        LinkedList q = populatedQueue(3);
425        assertEquals(new Integer(2),q.getLast());
426    }
427    
428    /**
429     *
430     */
431    public void testIndexOf() {
432        LinkedList q = populatedQueue(3);
433        assertEquals(0,q.indexOf(new Integer(0)));
434        assertEquals(1,q.indexOf(new Integer(1)));
435        assertEquals(2,q.indexOf(new Integer(2)));
436        assertEquals(-1, q.indexOf("not there"));
437    }
438
439    /**
440     *
441     */
442    public void testLastIndexOf() {
443        LinkedList q = populatedQueue(3);
444        q.add(new Integer(2));
445        assertEquals(3,q.lastIndexOf(new Integer(2)));
446        assertEquals(-1, q.lastIndexOf("not there"));
447    }
448    
449    /**
450     *
451     */
452    public void testSet() {
453        LinkedList q = populatedQueue(3);
454        q.set(0,(new Integer(1)));
455        assertFalse(q.contains(new Integer(0)));
456        assertEquals(new Integer(1), q.get(0));
457    }
458    
459
460    /**
461     *
462     */
463    public void testGetFirst_NoSuchElementException() {
464        try {
465            LinkedList l = new LinkedList();
466            l.getFirst();
467            shouldThrow();
468        }
469        catch(NoSuchElementException success) {}
470    }
471    
472    /**
473     *
474     */
475    public void testRemoveFirst() {
476        try {
477            LinkedList l = new LinkedList();
478            l.removeFirst();
479            shouldThrow();
480        }
481        catch(NoSuchElementException success) {}
482    }
483    
484    /**
485     *
486     */
487    public void testRemoveLast() {
488        try {
489            LinkedList l = new LinkedList();
490            l.removeLast();
491            shouldThrow();
492        }
493        catch(NoSuchElementException success) {}
494    }
495    
496    /**
497     *
498     */
499    public void testGetLast_NoSuchElementException() {
500        try {
501            LinkedList l = new LinkedList();
502            l.getLast();
503            shouldThrow();
504        }
505        catch(NoSuchElementException success) {}
506    }
507
508
509    /**
510     *
511     */
512    public void testAddAll_NullPointerException() {
513        try {
514            LinkedList l = new LinkedList();
515            l.addAll((Collection)null);
516            shouldThrow();
517        }
518        catch(NullPointerException success){}
519    }
520    
521    
522    /**
523     *
524     */
525    public void testAddAll1_OutOfBounds() {
526        try {
527            LinkedList l = new LinkedList();
528            l.addAll(4,new LinkedList());
529            shouldThrow();
530        }
531        catch(IndexOutOfBoundsException  success) {}
532    }
533    
534    
535    /**
536     *
537     */
538    public void testAddAll2_IndexOutOfBoundsException() {
539        try {
540            LinkedList l = new LinkedList();
541            l.add(new Object());
542            LinkedList m = new LinkedList();
543            m.add(new Object());
544            l.addAll(4,m);
545            shouldThrow();
546        } catch(IndexOutOfBoundsException  success) {}
547    }
548
549    /**
550     *
551     */
552    public void testAddAll4_BadIndex() {
553        try {
554            LinkedList l = new LinkedList();
555            l.add(new Object());
556            LinkedList m = new LinkedList();
557            m.add(new Object());
558            l.addAll(-1,m);
559            shouldThrow();
560        } catch(IndexOutOfBoundsException  success){}
561    }
460  
461      /**
462 <     *
462 >     *  pop removes next element, or throws NSEE if empty
463       */
464 <    public void testget1() {
465 <        try {
466 <            LinkedList l = new LinkedList();
467 <            l.add(new Object());
468 <            l.get(-1);
469 <            shouldThrow();
470 <        } catch(IndexOutOfBoundsException  success) {}  
464 >    public void testPop() {
465 >        LinkedList q = populatedQueue(SIZE);
466 >        for (int i = 0; i < SIZE; ++i) {
467 >            assertEquals(i, ((Integer)q.pop()).intValue());
468 >        }
469 >        try {
470 >            q.pop();
471 >            shouldThrow();
472 >        } catch (NoSuchElementException success){
473 >        }  
474      }
475  
476      /**
477 <     *
477 >     * OfferFirst succeeds
478       */
479 <    public void testget2() {
480 <        try {
481 <            LinkedList l = new LinkedList();
482 <            l.add(new Object());
582 <            l.get(5);
583 <            shouldThrow();
584 <        } catch(IndexOutOfBoundsException  success){}  
479 >    public void testOfferFirst() {
480 >        LinkedList q = new LinkedList();
481 >        assertTrue(q.offerFirst(new Integer(0)));
482 >        assertTrue(q.offerFirst(new Integer(1)));
483      }
484  
485      /**
486 <     *
486 >     * OfferLast succeeds
487       */
488 <    public void testset1() {
489 <        try {
490 <            LinkedList l = new LinkedList();
491 <            l.add(new Object());
594 <            l.set(-1,new Object());
595 <            shouldThrow();
596 <        } catch(IndexOutOfBoundsException  success){}  
488 >    public void testOfferLast() {
489 >        LinkedList q = new LinkedList();
490 >        assertTrue(q.offerLast(new Integer(0)));
491 >        assertTrue(q.offerLast(new Integer(1)));
492      }
493  
494      /**
495 <     *
495 >     *  pollLast succeeds unless empty
496       */
497 <    public void testset2() {
498 <        try {
499 <            LinkedList l = new LinkedList();
500 <            l.add(new Object());
501 <            l.set(5,new Object());
502 <            shouldThrow();
608 <        } catch(IndexOutOfBoundsException  success){}  
497 >    public void testPollLast() {
498 >        LinkedList q = populatedQueue(SIZE);
499 >        for (int i = SIZE-1; i >= 0; --i) {
500 >            assertEquals(i, ((Integer)q.pollLast()).intValue());
501 >        }
502 >        assertNull(q.pollLast());
503      }
504  
505      /**
506 <     *
506 >     *  peekFirst returns next element, or null if empty
507       */
508 <    public void testadd1() {
509 <        try {
510 <            LinkedList l = new LinkedList();
511 <            l.add(new Object());
512 <            l.add(-1,new Object());
513 <            shouldThrow();
514 <        } catch(IndexOutOfBoundsException  success){}  
508 >    public void testPeekFirst() {
509 >        LinkedList q = populatedQueue(SIZE);
510 >        for (int i = 0; i < SIZE; ++i) {
511 >            assertEquals(i, ((Integer)q.peekFirst()).intValue());
512 >            q.pollFirst();
513 >            assertTrue(q.peekFirst() == null ||
514 >                       i != ((Integer)q.peekFirst()).intValue());
515 >        }
516 >        assertNull(q.peekFirst());
517      }
518  
623    public void add2() {
624        try {
625            LinkedList l = new LinkedList();
626            l.add(new Object());
627            l.add(5,new Object());
628            shouldThrow();
629        } catch(IndexOutOfBoundsException  success) {}  
630    }
519  
520      /**
521 <     *
634 <     */
635 <    public void testremove() {
636 <        try {
637 <            LinkedList l = new LinkedList();
638 <            l.add(new Object());
639 <            l.remove(-1);
640 <            shouldThrow();
641 <        } catch(IndexOutOfBoundsException  success){}
642 <    }
643 <    
644 <    /**
645 <     *
521 >     *  peekLast returns next element, or null if empty
522       */
523 <    public void testremove1() {
524 <        try {
525 <            LinkedList l = new LinkedList();
526 <            l.add(new Object());
527 <            l.remove(5);
528 <            shouldThrow();
529 <        } catch(IndexOutOfBoundsException  success){}
523 >    public void testPeekLast() {
524 >        LinkedList q = populatedQueue(SIZE);
525 >        for (int i = SIZE-1; i >= 0; --i) {
526 >            assertEquals(i, ((Integer)q.peekLast()).intValue());
527 >            q.pollLast();
528 >            assertTrue(q.peekLast() == null ||
529 >                       i != ((Integer)q.peekLast()).intValue());
530 >        }
531 >        assertNull(q.peekLast());
532      }
533  
534 <    
535 <    /**
536 <     *
537 <     */
538 <    public void testremove2() {
534 >    public void testFirstElement() {
535 >        LinkedList q = populatedQueue(SIZE);
536 >        for (int i = 0; i < SIZE; ++i) {
537 >            assertEquals(i, ((Integer)q.getFirst()).intValue());
538 >            q.pollFirst();
539 >        }
540          try {
541 <            LinkedList l = new LinkedList();
663 <            l.remove();
541 >            q.getFirst();
542              shouldThrow();
543 <        } catch(NoSuchElementException e){}    
543 >        }
544 >        catch (NoSuchElementException success) {}
545      }
546  
547      /**
548 <     *
670 <     */
671 <    public void testlistIt1() {
672 <        try {
673 <            LinkedList l = new LinkedList();
674 <            l.add(new Object());
675 <            l.listIterator(5);
676 <            shouldThrow();
677 <        } catch(IndexOutOfBoundsException  success){}
678 <    }
679 <    
680 <    /**
681 <     *
548 >     *  getLast returns next element, or throws NSEE if empty
549       */
550 <    public void testlistIt2() {
551 <        try {
552 <            LinkedList l = new LinkedList();
553 <            l.add(new Object());
554 <            l.listIterator(-1);
555 <            shouldThrow();
556 <        } catch(IndexOutOfBoundsException  success){}
557 <    }
558 <    
559 <    /**
560 <     *
561 <     */
695 <    public void testlistIt3() {
696 <        try {
697 <            LinkedList l = new LinkedList();
698 <            l.add(new Object());
699 <            ListIterator a = l.listIterator(0);
700 <            l.removeFirst();
701 <            a.next();
702 <            shouldThrow();
703 <        } catch(ConcurrentModificationException success){}
550 >    public void testLastElement() {
551 >        LinkedList q = populatedQueue(SIZE);
552 >        for (int i = SIZE-1; i >= 0; --i) {
553 >            assertEquals(i, ((Integer)q.getLast()).intValue());
554 >            q.pollLast();
555 >        }
556 >        try {
557 >            q.getLast();
558 >            shouldThrow();
559 >        }
560 >        catch (NoSuchElementException success) {}
561 >        assertNull(q.peekLast());
562      }
563  
564      /**
565 <     *
565 >     * removeFirstOccurrence(x) removes x and returns true if present
566       */
567 <    public void testToArray_BadArg() {
568 <        try {
569 <            LinkedList l = new LinkedList();
570 <            l.add(new Object());
571 <            Object o[] = l.toArray(null);
572 <            shouldThrow();
573 <        } catch(NullPointerException success){}
567 >    public void testRemoveFirstOccurrence() {
568 >        LinkedList q = populatedQueue(SIZE);
569 >        for (int i = 1; i < SIZE; i+=2) {
570 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
571 >        }
572 >        for (int i = 0; i < SIZE; i+=2) {
573 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
574 >            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
575 >        }
576 >        assertTrue(q.isEmpty());
577      }
578  
579      /**
580 <     *
580 >     * removeLastOccurrence(x) removes x and returns true if present
581       */
582 <    public void testToArray1_BadArg() {
583 <        try {
584 <            LinkedList l = new LinkedList();
585 <            l.add(new Integer(5));
586 <            Object o[] = l.toArray(new String[10] );
587 <            shouldThrow();
588 <        } catch(ArrayStoreException  success){}
582 >    public void testRemoveLastOccurrence() {
583 >        LinkedList q = populatedQueue(SIZE);
584 >        for (int i = 1; i < SIZE; i+=2) {
585 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
586 >        }
587 >        for (int i = 0; i < SIZE; i+=2) {
588 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
589 >            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
590 >        }
591 >        assertTrue(q.isEmpty());
592      }
593  
594   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines