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.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.22 by jsr166, Wed Nov 3 16:46:34 2010 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 11 | 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 25 | 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 <
36 <    public void testConstructor1(){
35 >
36 >    /**
37 >     * new queue is empty
38 >     */
39 >    public void testConstructor1() {
40          assertEquals(0, new LinkedList().size());
41      }
42  
43 +    /**
44 +     * Initializing from null Collection throws NPE
45 +     */
46      public void testConstructor3() {
47          try {
48              LinkedList q = new LinkedList((Collection)null);
49 <            fail("Cannot make from null collection");
50 <        }
44 <        catch (NullPointerException success) {}
49 >            shouldThrow();
50 >        } catch (NullPointerException success) {}
51      }
52  
53 <    public void testConstructor6(){
54 <        try {
55 <            Integer[] ints = new Integer[SIZE];
56 <            for (int i = 0; i < SIZE; ++i)
57 <                ints[i] = new Integer(i);
58 <            LinkedList q = new LinkedList(Arrays.asList(ints));
59 <            for (int i = 0; i < SIZE; ++i)
60 <                assertEquals(ints[i], q.poll());
61 <        }
62 <        finally {}
53 >    /**
54 >     * Queue contains all elements of collection used to initialize
55 >     */
56 >    public void testConstructor6() {
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 +    /**
66 +     * isEmpty is true before add, false after
67 +     */
68      public void testEmpty() {
69          LinkedList q = new LinkedList();
70          assertTrue(q.isEmpty());
# Line 67 | Line 76 | public class LinkedListTest extends JSR1
76          assertTrue(q.isEmpty());
77      }
78  
79 +    /**
80 +     * size changes when elements added and removed
81 +     */
82      public void testSize() {
83          LinkedList q = populatedQueue(SIZE);
84          for (int i = 0; i < SIZE; ++i) {
# Line 79 | Line 91 | public class LinkedListTest extends JSR1
91          }
92      }
93  
94 <    public void testOfferNull(){
95 <        try {
96 <            LinkedList q = new LinkedList();
97 <            q.offer(null);
98 <        } catch (NullPointerException ie) {
99 <            fail("should not throw NPE");
88 <        }  
94 >    /**
95 >     * offer(null) succeeds
96 >     */
97 >    public void testOfferNull() {
98 >        LinkedList q = new LinkedList();
99 >        q.offer(null);
100      }
101  
102 +    /**
103 +     * Offer succeeds
104 +     */
105      public void testOffer() {
106          LinkedList q = new LinkedList();
107          assertTrue(q.offer(new Integer(0)));
108          assertTrue(q.offer(new Integer(1)));
109      }
110  
111 <    public void testAdd(){
111 >    /**
112 >     * add succeeds
113 >     */
114 >    public void testAdd() {
115          LinkedList q = new LinkedList();
116          for (int i = 0; i < SIZE; ++i) {
117              assertEquals(i, q.size());
# Line 102 | Line 119 | public class LinkedListTest extends JSR1
119          }
120      }
121  
122 <    public void testAddAll1(){
122 >    /**
123 >     * addAll(null) throws NPE
124 >     */
125 >    public void testAddAll1() {
126          try {
127              LinkedList q = new LinkedList();
128              q.addAll(null);
129 <            fail("Cannot add null collection");
130 <        }
111 <        catch (NullPointerException success) {}
129 >            shouldThrow();
130 >        } catch (NullPointerException success) {}
131      }
132  
133 <    public void testAddAll5(){
133 >    /**
134 >     * Queue contains all elements, in traversal order, of successful addAll
135 >     */
136 >    public void testAddAll5() {
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 >        LinkedList l = new LinkedList();
153 >        l.add(new Object());
154 >        LinkedList m = new LinkedList();
155 >        m.add(new Object());
156          try {
157 <            Integer[] empty = new Integer[0];
158 <            Integer[] ints = new Integer[SIZE];
159 <            for (int i = 0; i < SIZE; ++i)
119 <                ints[i] = new Integer(i);
120 <            LinkedList q = new LinkedList();
121 <            assertFalse(q.addAll(Arrays.asList(empty)));
122 <            assertTrue(q.addAll(Arrays.asList(ints)));
123 <            for (int i = 0; i < SIZE; ++i)
124 <                assertEquals(ints[i], q.poll());
125 <        }
126 <        finally {}
157 >            l.addAll(4,m);
158 >            shouldThrow();
159 >        } catch (IndexOutOfBoundsException success) {}
160      }
161  
162 <    public void testPoll(){
162 >    /**
163 >     * addAll with negative index throws IOOBE
164 >     */
165 >    public void testAddAll4_BadIndex() {
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
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 <    public void testPeek(){
187 >    /**
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 <    public void testElement(){
201 >    /**
202 >     * element returns next element, or throws NSEE if empty
203 >     */
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 <            fail("no such element");
213 <        }
158 <        catch (NoSuchElementException success) {}
212 >            shouldThrow();
213 >        } catch (NoSuchElementException success) {}
214      }
215  
216 <    public void testRemove(){
216 >    /**
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 <            fail("remove should throw");
227 <        } catch (NoSuchElementException success){
170 <        }  
226 >            shouldThrow();
227 >        } catch (NoSuchElementException success) {}
228      }
229  
230 <    public void testRemoveElement(){
230 >    /**
231 >     * remove(x) removes x and returns true if present
232 >     */
233 >    public void testRemoveElement() {
234          LinkedList q = populatedQueue(SIZE);
235          for (int i = 1; i < SIZE; i+=2) {
236              assertTrue(q.remove(new Integer(i)));
# Line 181 | Line 241 | public class LinkedListTest extends JSR1
241          }
242          assertTrue(q.isEmpty());
243      }
244 <        
245 <    public void testContains(){
244 >
245 >    /**
246 >     * contains(x) reports true when elements added but not yet removed
247 >     */
248 >    public void testContains() {
249          LinkedList q = populatedQueue(SIZE);
250          for (int i = 0; i < SIZE; ++i) {
251              assertTrue(q.contains(new Integer(i)));
# Line 191 | Line 254 | public class LinkedListTest extends JSR1
254          }
255      }
256  
257 <    public void testClear(){
257 >    /**
258 >     * clear removes all elements
259 >     */
260 >    public void testClear() {
261          LinkedList q = populatedQueue(SIZE);
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());
269      }
270  
271 <    public void testContainsAll(){
271 >    /**
272 >     * containsAll(c) is true when c contains a subset of elements
273 >     */
274 >    public void testContainsAll() {
275          LinkedList q = populatedQueue(SIZE);
276          LinkedList p = new LinkedList();
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      }
284  
285 <    public void testRetainAll(){
285 >    /**
286 >     * retainAll(c) retains only those elements of c and reports true if changed
287 >     */
288 >    public void testRetainAll() {
289          LinkedList q = populatedQueue(SIZE);
290          LinkedList p = populatedQueue(SIZE);
291          for (int i = 0; i < SIZE; ++i) {
# Line 229 | Line 301 | public class LinkedListTest extends JSR1
301          }
302      }
303  
304 <    public void testRemoveAll(){
304 >    /**
305 >     * removeAll(c) removes only those elements of c and reports true if changed
306 >     */
307 >    public void testRemoveAll() {
308          for (int i = 1; i < SIZE; ++i) {
309              LinkedList q = populatedQueue(SIZE);
310              LinkedList p = populatedQueue(i);
# Line 242 | Line 317 | public class LinkedListTest extends JSR1
317          }
318      }
319  
320 <    public void testToArray(){
320 >    /**
321 >     * toArray contains all elements
322 >     */
323 >    public void testToArray() {
324          LinkedList q = populatedQueue(SIZE);
325 <        Object[] o = q.toArray();
325 >        Object[] o = q.toArray();
326          Arrays.sort(o);
327 <        for(int i = 0; i < o.length; i++)
328 <            assertEquals(o[i], q.poll());
327 >        for (int i = 0; i < o.length; i++)
328 >            assertEquals(o[i], q.poll());
329      }
330  
331 <    public void testToArray2(){
331 >    /**
332 >     * toArray(a) contains all elements
333 >     */
334 >    public void testToArray2() {
335          LinkedList q = populatedQueue(SIZE);
336 <        Integer[] ints = new Integer[SIZE];
337 <        ints = (Integer[])q.toArray(ints);
336 >        Integer[] ints = new Integer[SIZE];
337 >        ints = (Integer[])q.toArray(ints);
338          Arrays.sort(ints);
339 <        for(int i = 0; i < ints.length; i++)
339 >        for (int i = 0; i < ints.length; i++)
340              assertEquals(ints[i], q.poll());
341      }
342 <    
343 <    public void testIterator(){
342 >
343 >    /**
344 >     * toArray(null) throws NullPointerException
345 >     */
346 >    public void testToArray_NullArg() {
347 >        LinkedList l = new LinkedList();
348 >        l.add(new Object());
349 >        try {
350 >            l.toArray(null);
351 >            shouldThrow();
352 >        } catch (NullPointerException success) {}
353 >    }
354 >
355 >    /**
356 >     * toArray(incompatible array type) throws ArrayStoreException
357 >     */
358 >    public void testToArray1_BadArg() {
359 >        LinkedList l = new LinkedList();
360 >        l.add(new Integer(5));
361 >        try {
362 >            l.toArray(new String[10]);
363 >            shouldThrow();
364 >        } catch (ArrayStoreException success) {}
365 >    }
366 >
367 >    /**
368 >     * iterator iterates through all elements
369 >     */
370 >    public void testIterator() {
371          LinkedList q = populatedQueue(SIZE);
372          int i = 0;
373 <        Iterator it = q.iterator();
374 <        while(it.hasNext()) {
373 >        Iterator it = q.iterator();
374 >        while (it.hasNext()) {
375              assertTrue(q.contains(it.next()));
376              ++i;
377          }
378          assertEquals(i, SIZE);
379      }
380  
381 +    /**
382 +     * iterator ordering is FIFO
383 +     */
384      public void testIteratorOrdering() {
274
385          final LinkedList q = new LinkedList();
276
386          q.add(new Integer(1));
387          q.add(new Integer(2));
388          q.add(new Integer(3));
280
389          int k = 0;
390          for (Iterator it = q.iterator(); it.hasNext();) {
391 <            int i = ((Integer)(it.next())).intValue();
284 <            assertEquals("items should come out in order", ++k, i);
391 >            assertEquals(++k, it.next());
392          }
393  
394 <        assertEquals("should go through 3 elements", 3, k);
394 >        assertEquals(3, k);
395      }
396  
397 <    public void testIteratorRemove () {
397 >    /**
398 >     * iterator.remove removes current element
399 >     */
400 >    public void testIteratorRemove() {
401          final LinkedList q = new LinkedList();
292
402          q.add(new Integer(1));
403          q.add(new Integer(2));
404          q.add(new Integer(3));
296
405          Iterator it = q.iterator();
406 <        it.next();
406 >        assertEquals(it.next(), 1);
407          it.remove();
300
408          it = q.iterator();
409 <        assertEquals(it.next(), new Integer(2));
410 <        assertEquals(it.next(), new Integer(3));
409 >        assertEquals(it.next(), 2);
410 >        assertEquals(it.next(), 3);
411 >        assertFalse(it.hasNext());
412 >    }
413 >
414 >    /**
415 >     * Descending iterator iterates through all elements
416 >     */
417 >    public void testDescendingIterator() {
418 >        LinkedList q = populatedQueue(SIZE);
419 >        int i = 0;
420 >        Iterator it = q.descendingIterator();
421 >        while (it.hasNext()) {
422 >            assertTrue(q.contains(it.next()));
423 >            ++i;
424 >        }
425 >        assertEquals(i, SIZE);
426          assertFalse(it.hasNext());
427 +        try {
428 +            it.next();
429 +            shouldThrow();
430 +        } catch (NoSuchElementException success) {}
431 +    }
432 +
433 +    /**
434 +     * Descending iterator ordering is reverse FIFO
435 +     */
436 +    public void testDescendingIteratorOrdering() {
437 +        final LinkedList q = new LinkedList();
438 +        q.add(new Integer(3));
439 +        q.add(new Integer(2));
440 +        q.add(new Integer(1));
441 +        int k = 0;
442 +        for (Iterator it = q.descendingIterator(); it.hasNext();) {
443 +            assertEquals(++k, it.next());
444 +        }
445 +
446 +        assertEquals(3, k);
447      }
448  
449 +    /**
450 +     * descendingIterator.remove removes current element
451 +     */
452 +    public void testDescendingIteratorRemove() {
453 +        final LinkedList q = new LinkedList();
454 +        q.add(three);
455 +        q.add(two);
456 +        q.add(one);
457 +        Iterator it = q.descendingIterator();
458 +        it.next();
459 +        it.remove();
460 +        it = q.descendingIterator();
461 +        assertSame(it.next(), two);
462 +        assertSame(it.next(), three);
463 +        assertFalse(it.hasNext());
464 +    }
465  
466 <    public void testToString(){
466 >
467 >    /**
468 >     * toString contains toStrings of elements
469 >     */
470 >    public void testToString() {
471          LinkedList q = populatedQueue(SIZE);
472          String s = q.toString();
473          for (int i = 0; i < SIZE; ++i) {
474              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
475          }
476 <    }        
476 >    }
477  
478 <    public void testAddFirst(){
478 >    /**
479 >     * peek returns element inserted with addFirst
480 >     */
481 >    public void testAddFirst() {
482          LinkedList q = populatedQueue(3);
483 <        q.addFirst(new Integer(4));
484 <        assertEquals(new Integer(4),q.get(0));
485 <    }  
483 >        q.addFirst(four);
484 >        assertSame(four, q.peek());
485 >    }
486  
487 <    public void testAddLast(){
487 >    /**
488 >     * peekFirst returns element inserted with push
489 >     */
490 >    public void testPush() {
491          LinkedList q = populatedQueue(3);
492 <        q.addLast(new Integer(3));
493 <        assertEquals(new Integer(3),q.get(3));
492 >        q.push(four);
493 >        assertSame(four, q.peekFirst());
494      }
495 <    
496 <    public void testGetFirst() {
497 <        LinkedList q = populatedQueue(3);
498 <        assertEquals(new Integer(0),q.getFirst());
499 <    }  
500 <    
501 <    public void testGetLast() {
502 <        LinkedList q = populatedQueue(3);
503 <        assertEquals(new Integer(2),q.getLast());
495 >
496 >    /**
497 >     * pop removes next element, or throws NSEE if empty
498 >     */
499 >    public void testPop() {
500 >        LinkedList q = populatedQueue(SIZE);
501 >        for (int i = 0; i < SIZE; ++i) {
502 >            assertEquals(i, q.pop());
503 >        }
504 >        try {
505 >            q.pop();
506 >            shouldThrow();
507 >        } catch (NoSuchElementException success) {}
508      }
509 <    
510 <    public void testIndexOf(){
511 <        LinkedList q = populatedQueue(3);
512 <        assertEquals(0,q.indexOf(new Integer(0)));
513 <        assertEquals(1,q.indexOf(new Integer(1)));
514 <        assertEquals(2,q.indexOf(new Integer(2)));
515 <        assertEquals(-1, q.indexOf("not there"));
509 >
510 >    /**
511 >     * OfferFirst succeeds
512 >     */
513 >    public void testOfferFirst() {
514 >        LinkedList q = new LinkedList();
515 >        assertTrue(q.offerFirst(new Integer(0)));
516 >        assertTrue(q.offerFirst(new Integer(1)));
517      }
518  
519 <    public void testLastIndexOf(){
520 <        LinkedList q = populatedQueue(3);
521 <        q.add(new Integer(2));
522 <        assertEquals(3,q.lastIndexOf(new Integer(2)));
523 <        assertEquals(-1, q.lastIndexOf("not there"));
519 >    /**
520 >     * OfferLast succeeds
521 >     */
522 >    public void testOfferLast() {
523 >        LinkedList q = new LinkedList();
524 >        assertTrue(q.offerLast(new Integer(0)));
525 >        assertTrue(q.offerLast(new Integer(1)));
526      }
527 <    
528 <    public void testSet(){
529 <        LinkedList q = populatedQueue(3);
530 <        q.set(0,(new Integer(1)));
531 <        assertFalse(q.contains(new Integer(0)));
532 <        assertEquals(new Integer(1), q.get(0));
533 <    }
534 <    
535 <
536 <    public void testGetFirst_NoSuchElementException(){
537 <        try {
538 <            LinkedList l = new LinkedList();
539 <            l.getFirst();
540 <            fail("First Element");
541 <        }
542 <        catch(NoSuchElementException success) {}
543 <    }
544 <    
545 <    public void testRemoveFirst() {
546 <        try {
547 <            LinkedList l = new LinkedList();
548 <            l.removeFirst();
549 <            fail("R: First Element");
550 <        }
376 <        catch(NoSuchElementException success) {}
377 <    }
378 <    
379 <    public void testRemoveLast() {
380 <        try {
381 <            LinkedList l = new LinkedList();
382 <            l.removeLast();
383 <            fail("R: Last Element");  
384 <        }
385 <        catch(NoSuchElementException success) {}
386 <    }
387 <    
388 <    public void testGetLast_NoSuchElementException(){
389 <        try {
390 <            LinkedList l = new LinkedList();
391 <            l.getLast();
392 <            fail("Last Element");  
393 <        }
394 <        catch(NoSuchElementException success) {}
527 >
528 >    /**
529 >     * pollLast succeeds unless empty
530 >     */
531 >    public void testPollLast() {
532 >        LinkedList q = populatedQueue(SIZE);
533 >        for (int i = SIZE-1; i >= 0; --i) {
534 >            assertEquals(i, q.pollLast());
535 >        }
536 >        assertNull(q.pollLast());
537 >    }
538 >
539 >    /**
540 >     * peekFirst returns next element, or null if empty
541 >     */
542 >    public void testPeekFirst() {
543 >        LinkedList q = populatedQueue(SIZE);
544 >        for (int i = 0; i < SIZE; ++i) {
545 >            assertEquals(i, q.peekFirst());
546 >            assertEquals(i, q.pollFirst());
547 >            assertTrue(q.peekFirst() == null ||
548 >                       !q.peekFirst().equals(i));
549 >        }
550 >        assertNull(q.peekFirst());
551      }
552  
553  
554 <    public void testAddAll_NullPointerException(){
555 <        try {
556 <            LinkedList l = new LinkedList();
557 <            l.addAll((Collection)null);
558 <            fail("Add All Failed");
559 <        }
560 <        catch(NullPointerException success){}
561 <    }
562 <    
563 <    
564 <    public void testAddAll1_OutOfBounds() {
565 <        try {
410 <            LinkedList l = new LinkedList();
411 <            l.addAll(4,new LinkedList());
412 <            fail("boolean addAll(int, Collection) should throw IndexOutOfBoundsException");
413 <        }
414 <        catch(IndexOutOfBoundsException  success) {}
554 >    /**
555 >     * peekLast returns next element, or null if empty
556 >     */
557 >    public void testPeekLast() {
558 >        LinkedList q = populatedQueue(SIZE);
559 >        for (int i = SIZE-1; i >= 0; --i) {
560 >            assertEquals(i, q.peekLast());
561 >            assertEquals(i, q.pollLast());
562 >            assertTrue(q.peekLast() == null ||
563 >                       !q.peekLast().equals(i));
564 >        }
565 >        assertNull(q.peekLast());
566      }
567 <    
568 <    
569 <    public void testAddAll2_IndexOutOfBoundsException() {
570 <        try {
571 <            LinkedList l = new LinkedList();
572 <            l.add(new Object());
573 <            LinkedList m = new LinkedList();
574 <            m.add(new Object());
575 <            l.addAll(4,m);
576 <            fail("Add All Failed " + l.size());
577 <        }catch(IndexOutOfBoundsException  success) {}
567 >
568 >    public void testFirstElement() {
569 >        LinkedList q = populatedQueue(SIZE);
570 >        for (int i = 0; i < SIZE; ++i) {
571 >            assertEquals(i, q.getFirst());
572 >            assertEquals(i, q.pollFirst());
573 >        }
574 >        try {
575 >            q.getFirst();
576 >            shouldThrow();
577 >        } catch (NoSuchElementException success) {}
578      }
579  
580 <    public void testAddAll4_BadIndex() {
581 <        try {
582 <            LinkedList l = new LinkedList();
583 <            l.add(new Object());
584 <            LinkedList m = new LinkedList();
585 <            m.add(new Object());
586 <            l.addAll(-1,m);
587 <            fail("Add All Failed " + l.size());
588 <        }catch(IndexOutOfBoundsException  success){}
589 <    }
590 <
591 <    public void testget1() {
592 <        try {
593 <            LinkedList l = new LinkedList();
443 <            l.add(new Object());
444 <            l.get(-1);
445 <            fail("get Failed - l.get(-1)");
446 <        }catch(IndexOutOfBoundsException  success) {}  
447 <    }
448 <
449 <    public void testget2() {
450 <        try {
451 <            LinkedList l = new LinkedList();
452 <            l.add(new Object());
453 <            l.get(5);
454 <            fail("get Failed - l.get(5) l.size(): " + l.size());
455 <        }catch(IndexOutOfBoundsException  success){}    
456 <    }
457 <
458 <    public void testset1() {
459 <        try {
460 <            LinkedList l = new LinkedList();
461 <            l.add(new Object());
462 <            l.set(-1,new Object());
463 <            fail("set failed - l.set(-1,...)" + l.size());
464 <        }catch(IndexOutOfBoundsException  success){}    
465 <    }
466 <
467 <    public void testset2() {
468 <        try {
469 <            LinkedList l = new LinkedList();
470 <            l.add(new Object());
471 <            l.set(5,new Object());
472 <            fail("set failed = l.set(5,..) l.size():" + l.size());
473 <        }catch(IndexOutOfBoundsException  success){}    
474 <    }
475 <
476 <    public void testadd1() {
477 <        try {
478 <            LinkedList l = new LinkedList();
479 <            l.add(new Object());
480 <            l.add(-1,new Object());
481 <            fail("Add Failed - l.add(-1) l.size(): " + l.size());
482 <        }catch(IndexOutOfBoundsException  success){}    
483 <    }
484 <
485 <    public void add2(){
486 <        try {
487 <            LinkedList l = new LinkedList();
488 <            l.add(new Object());
489 <            l.add(5,new Object());
490 <            fail("Add Failed  l.add(f,...)");
491 <        }catch(IndexOutOfBoundsException  success) {}  
492 <    }
493 <
494 <    public void testremove(){
495 <        try {
496 <            LinkedList l = new LinkedList();
497 <            l.add(new Object());
498 <            l.remove(-1);
499 <            fail("Remove Failed l.remove(-1); l.size():" + l.size());
500 <        }catch(IndexOutOfBoundsException  success){}
501 <    }
502 <    
503 <    public void testremove1(){
504 <        try {
505 <            LinkedList l = new LinkedList();
506 <            l.add(new Object());
507 <            l.remove(5);
508 <            fail("Remove Failed l.remove(5); l.size():" + l.size());
509 <        }catch(IndexOutOfBoundsException  success){}
510 <    }
511 <
512 <    
513 <    public void testremove2(){
514 <        try{
515 <            LinkedList l = new LinkedList();
516 <            l.remove();
517 <            fail("LinkedList - Object remove() should throw a NoSuchElementException");
518 <        }catch(NoSuchElementException e){}      
519 <    }
520 <
521 <    public void testlistIt1() {
522 <        try {
523 <            LinkedList l = new LinkedList();
524 <            l.add(new Object());
525 <            l.listIterator(5);
526 <            fail("l.listIterator(5) l.size():" + l.size());
527 <        }catch(IndexOutOfBoundsException  success){}
528 <    }
529 <    
530 <    public void testlistIt2() {
531 <        try {
532 <            LinkedList l = new LinkedList();
533 <            l.add(new Object());
534 <            l.listIterator(-1);
535 <            fail("l.listIterator(-1) l.size():" + l.size());
536 <        }catch(IndexOutOfBoundsException  success){}
537 <    }
538 <    
539 <    public void testlistIt3() {
540 <        try {
541 <            LinkedList l = new LinkedList();
542 <            l.add(new Object());
543 <            ListIterator a = l.listIterator(0);
544 <            l.removeFirst();
545 <            a.next();
546 <            fail("l.listIterator(-1) l.size():" + l.size());
547 <        }catch(ConcurrentModificationException success){}
548 <    }
549 <
550 <    public void testToArray_BadArg() {
551 <        try {
552 <            LinkedList l = new LinkedList();
553 <            l.add(new Object());
554 <            Object o[] = l.toArray(null);
555 <            fail("l.toArray(null) did not throw an exception");
556 <        }catch(NullPointerException success){}
580 >    /**
581 >     * getLast returns next element, or throws NSEE if empty
582 >     */
583 >    public void testLastElement() {
584 >        LinkedList q = populatedQueue(SIZE);
585 >        for (int i = SIZE-1; i >= 0; --i) {
586 >            assertEquals(i, q.getLast());
587 >            assertEquals(i, q.pollLast());
588 >        }
589 >        try {
590 >            q.getLast();
591 >            shouldThrow();
592 >        } catch (NoSuchElementException success) {}
593 >        assertNull(q.peekLast());
594      }
595  
596 <    public void testToArray1_BadArg() {
597 <        try {
598 <            LinkedList l = new LinkedList();
599 <            l.add(new Integer(5));
600 <            Object o[] = l.toArray(new String[10] );
601 <            fail("l.toArray(String[] f) did not throw an exception, an Integer was added");
602 <        }catch(ArrayStoreException  success){}
596 >    /**
597 >     * removeFirstOccurrence(x) removes x and returns true if present
598 >     */
599 >    public void testRemoveFirstOccurrence() {
600 >        LinkedList q = populatedQueue(SIZE);
601 >        for (int i = 1; i < SIZE; i+=2) {
602 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
603 >        }
604 >        for (int i = 0; i < SIZE; i+=2) {
605 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
606 >            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
607 >        }
608 >        assertTrue(q.isEmpty());
609 >    }
610 >
611 >    /**
612 >     * removeLastOccurrence(x) removes x and returns true if present
613 >     */
614 >    public void testRemoveLastOccurrence() {
615 >        LinkedList q = populatedQueue(SIZE);
616 >        for (int i = 1; i < SIZE; i+=2) {
617 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
618 >        }
619 >        for (int i = 0; i < SIZE; i+=2) {
620 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
621 >            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
622 >        }
623 >        assertTrue(q.isEmpty());
624      }
625  
626   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines