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.26 by jsr166, Tue Mar 15 19:47:06 2011 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/publicdomain/zero/1.0/
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      /**
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)));
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 <
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 {
48              LinkedList q = new LinkedList((Collection)null);
49              shouldThrow();
50 <        }
50 <        catch (NullPointerException success) {}
50 >        } catch (NullPointerException success) {}
51      }
52  
53      /**
54 <     *
54 >     * Queue contains all elements of collection used to initialize
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)
63 <                assertEquals(ints[i], q.poll());
64 <        }
65 <        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      /**
66 <     *
66 >     * isEmpty is true before add, false after
67       */
68      public void testEmpty() {
69          LinkedList q = new LinkedList();
# Line 80 | Line 77 | public class LinkedListTest extends JSR1
77      }
78  
79      /**
80 <     *
80 >     * size changes when elements added and removed
81       */
82      public void testSize() {
83          LinkedList q = populatedQueue(SIZE);
# Line 95 | Line 92 | public class LinkedListTest extends JSR1
92      }
93  
94      /**
95 <     *
95 >     * offer(null) succeeds
96       */
97      public void testOfferNull() {
98 <        try {
99 <            LinkedList q = new LinkedList();
103 <            q.offer(null);
104 <        } catch (NullPointerException ie) {
105 <            unexpectedException();
106 <        }  
98 >        LinkedList q = new LinkedList();
99 >        q.offer(null);
100      }
101  
102      /**
103 <     *
103 >     * Offer succeeds
104       */
105      public void testOffer() {
106          LinkedList q = new LinkedList();
# Line 116 | Line 109 | public class LinkedListTest extends JSR1
109      }
110  
111      /**
112 <     *
112 >     * add succeeds
113       */
114      public void testAdd() {
115          LinkedList q = new LinkedList();
# Line 127 | Line 120 | public class LinkedListTest extends JSR1
120      }
121  
122      /**
123 <     *
123 >     * addAll(null) throws NPE
124       */
125      public void testAddAll1() {
126          try {
127              LinkedList q = new LinkedList();
128              q.addAll(null);
129              shouldThrow();
130 <        }
138 <        catch (NullPointerException success) {}
130 >        } catch (NullPointerException success) {}
131      }
132  
133      /**
134 <     *
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)
160 <                ints[i] = new Integer(i);
161 <            LinkedList q = new LinkedList();
162 <            assertFalse(q.addAll(Arrays.asList(empty)));
163 <            assertTrue(q.addAll(Arrays.asList(ints)));
164 <            for (int i = 0; i < SIZE; ++i)
165 <                assertEquals(ints[i], q.poll());
166 <        }
167 <        finally {}
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 >        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 <     *
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 <     *
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      /**
202 <     *
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              shouldThrow();
213 <        }
197 <        catch (NoSuchElementException success) {}
213 >        } catch (NoSuchElementException success) {}
214      }
215  
216      /**
217 <     *
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){
212 <        }  
227 >        } catch (NoSuchElementException success) {}
228      }
229  
230      /**
231 <     *
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)));
236 >            assertTrue(q.contains(i));
237 >            assertTrue(q.remove((Integer)i));
238 >            assertFalse(q.contains(i));
239 >            assertTrue(q.contains(i-1));
240          }
241          for (int i = 0; i < SIZE; i+=2) {
242 <            assertTrue(q.remove(new Integer(i)));
243 <            assertFalse(q.remove(new Integer(i+1)));
242 >            assertTrue(q.contains(i));
243 >            assertTrue(q.remove((Integer)i));
244 >            assertFalse(q.contains(i));
245 >            assertFalse(q.remove((Integer)(i+1)));
246 >            assertFalse(q.contains(i+1));
247          }
248          assertTrue(q.isEmpty());
249      }
250 <        
250 >
251      /**
252 <     *
252 >     * contains(x) reports true when elements added but not yet removed
253       */
254      public void testContains() {
255          LinkedList q = populatedQueue(SIZE);
# Line 240 | Line 261 | public class LinkedListTest extends JSR1
261      }
262  
263      /**
264 <     *
264 >     * clear removes all elements
265       */
266      public void testClear() {
267          LinkedList q = populatedQueue(SIZE);
268          q.clear();
269          assertTrue(q.isEmpty());
270          assertEquals(0, q.size());
271 <        q.add(new Integer(1));
271 >        assertTrue(q.add(new Integer(1)));
272          assertFalse(q.isEmpty());
273          q.clear();
274          assertTrue(q.isEmpty());
275      }
276  
277      /**
278 <     *
278 >     * containsAll(c) is true when c contains a subset of elements
279       */
280      public void testContainsAll() {
281          LinkedList q = populatedQueue(SIZE);
# Line 262 | Line 283 | public class LinkedListTest extends JSR1
283          for (int i = 0; i < SIZE; ++i) {
284              assertTrue(q.containsAll(p));
285              assertFalse(p.containsAll(q));
286 <            p.add(new Integer(i));
286 >            assertTrue(p.add(new Integer(i)));
287          }
288          assertTrue(p.containsAll(q));
289      }
290  
291      /**
292 <     *
292 >     * retainAll(c) retains only those elements of c and reports true if changed
293       */
294      public void testRetainAll() {
295          LinkedList q = populatedQueue(SIZE);
# Line 287 | Line 308 | public class LinkedListTest extends JSR1
308      }
309  
310      /**
311 <     *
311 >     * removeAll(c) removes only those elements of c and reports true if changed
312       */
313      public void testRemoveAll() {
314          for (int i = 1; i < SIZE; ++i) {
# Line 303 | Line 324 | public class LinkedListTest extends JSR1
324      }
325  
326      /**
327 <     *
327 >     * toArray contains all elements in FIFO order
328       */
329      public void testToArray() {
330          LinkedList q = populatedQueue(SIZE);
331 <        Object[] o = q.toArray();
332 <        Arrays.sort(o);
333 <        for(int i = 0; i < o.length; i++)
313 <            assertEquals(o[i], q.poll());
331 >        Object[] o = q.toArray();
332 >        for (int i = 0; i < o.length; i++)
333 >            assertSame(o[i], q.poll());
334      }
335  
336      /**
337 <     *
337 >     * toArray(a) contains all elements in FIFO order
338       */
339      public void testToArray2() {
340 <        LinkedList q = populatedQueue(SIZE);
341 <        Integer[] ints = new Integer[SIZE];
342 <        ints = (Integer[])q.toArray(ints);
343 <        Arrays.sort(ints);
344 <        for(int i = 0; i < ints.length; i++)
345 <            assertEquals(ints[i], q.poll());
340 >        LinkedList<Integer> q = populatedQueue(SIZE);
341 >        Integer[] ints = new Integer[SIZE];
342 >        Integer[] array = q.toArray(ints);
343 >        assertSame(ints, array);
344 >        for (int i = 0; i < ints.length; i++)
345 >            assertSame(ints[i], q.poll());
346      }
347 <    
347 >
348      /**
349 <     *
349 >     * toArray(null) throws NullPointerException
350 >     */
351 >    public void testToArray_NullArg() {
352 >        LinkedList l = new LinkedList();
353 >        l.add(new Object());
354 >        try {
355 >            l.toArray(null);
356 >            shouldThrow();
357 >        } catch (NullPointerException success) {}
358 >    }
359 >
360 >    /**
361 >     * toArray(incompatible array type) throws ArrayStoreException
362 >     */
363 >    public void testToArray1_BadArg() {
364 >        LinkedList l = new LinkedList();
365 >        l.add(new Integer(5));
366 >        try {
367 >            l.toArray(new String[10]);
368 >            shouldThrow();
369 >        } catch (ArrayStoreException success) {}
370 >    }
371 >
372 >    /**
373 >     * iterator iterates through all elements
374       */
375      public void testIterator() {
376          LinkedList q = populatedQueue(SIZE);
377          int i = 0;
378 <        Iterator it = q.iterator();
379 <        while(it.hasNext()) {
378 >        Iterator it = q.iterator();
379 >        while (it.hasNext()) {
380              assertTrue(q.contains(it.next()));
381              ++i;
382          }
# Line 340 | Line 384 | public class LinkedListTest extends JSR1
384      }
385  
386      /**
387 <     *
387 >     * iterator ordering is FIFO
388       */
389      public void testIteratorOrdering() {
346
390          final LinkedList q = new LinkedList();
348
391          q.add(new Integer(1));
392          q.add(new Integer(2));
393          q.add(new Integer(3));
352
394          int k = 0;
395          for (Iterator it = q.iterator(); it.hasNext();) {
396 <            int i = ((Integer)(it.next())).intValue();
356 <            assertEquals(++k, i);
396 >            assertEquals(++k, it.next());
397          }
398  
399          assertEquals(3, k);
400      }
401  
402      /**
403 <     *
403 >     * iterator.remove removes current element
404       */
405 <    public void testIteratorRemove () {
405 >    public void testIteratorRemove() {
406          final LinkedList q = new LinkedList();
367
407          q.add(new Integer(1));
408          q.add(new Integer(2));
409          q.add(new Integer(3));
371
410          Iterator it = q.iterator();
411 <        it.next();
411 >        assertEquals(it.next(), 1);
412          it.remove();
375
413          it = q.iterator();
414 <        assertEquals(it.next(), new Integer(2));
415 <        assertEquals(it.next(), new Integer(3));
414 >        assertEquals(it.next(), 2);
415 >        assertEquals(it.next(), 3);
416          assertFalse(it.hasNext());
417      }
418  
382
419      /**
420 <     *
420 >     * Descending iterator iterates through all elements
421       */
422 <    public void testToString() {
422 >    public void testDescendingIterator() {
423          LinkedList q = populatedQueue(SIZE);
424 <        String s = q.toString();
425 <        for (int i = 0; i < SIZE; ++i) {
426 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
424 >        int i = 0;
425 >        Iterator it = q.descendingIterator();
426 >        while (it.hasNext()) {
427 >            assertTrue(q.contains(it.next()));
428 >            ++i;
429          }
430 <    }        
431 <
432 <    /**
433 <     *
434 <     */
435 <    public void testAddFirst() {
398 <        LinkedList q = populatedQueue(3);
399 <        q.addFirst(new Integer(4));
400 <        assertEquals(new Integer(4),q.get(0));
401 <    }  
402 <
403 <    /**
404 <     *
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 <     *
414 <     */
415 <    public void testGetFirst() {
416 <        LinkedList q = populatedQueue(3);
417 <        assertEquals(new Integer(0),q.getFirst());
418 <    }  
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"));
430 >        assertEquals(i, SIZE);
431 >        assertFalse(it.hasNext());
432 >        try {
433 >            it.next();
434 >            shouldThrow();
435 >        } catch (NoSuchElementException success) {}
436      }
437  
438      /**
439 <     *
439 >     * Descending iterator ordering is reverse FIFO
440       */
441 <    public void testLastIndexOf() {
442 <        LinkedList q = populatedQueue(3);
441 >    public void testDescendingIteratorOrdering() {
442 >        final LinkedList q = new LinkedList();
443 >        q.add(new Integer(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 <    
445 >        q.add(new Integer(1));
446 >        int k = 0;
447 >        for (Iterator it = q.descendingIterator(); it.hasNext();) {
448 >            assertEquals(++k, it.next());
449 >        }
450  
451 <    /**
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) {}
451 >        assertEquals(3, k);
452      }
453 <    
453 >
454      /**
455 <     *
455 >     * descendingIterator.remove removes current element
456       */
457 <    public void testGetLast_NoSuchElementException() {
458 <        try {
459 <            LinkedList l = new LinkedList();
460 <            l.getLast();
461 <            shouldThrow();
462 <        }
463 <        catch(NoSuchElementException success) {}
457 >    public void testDescendingIteratorRemove() {
458 >        final LinkedList q = new LinkedList();
459 >        q.add(three);
460 >        q.add(two);
461 >        q.add(one);
462 >        Iterator it = q.descendingIterator();
463 >        it.next();
464 >        it.remove();
465 >        it = q.descendingIterator();
466 >        assertSame(it.next(), two);
467 >        assertSame(it.next(), three);
468 >        assertFalse(it.hasNext());
469      }
470  
471  
472      /**
473 <     *
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 <     *
473 >     * toString contains toStrings of elements
474       */
475 <    public void testAddAll1_OutOfBounds() {
476 <        try {
477 <            LinkedList l = new LinkedList();
478 <            l.addAll(4,new LinkedList());
479 <            shouldThrow();
480 <        }
531 <        catch(IndexOutOfBoundsException  success) {}
475 >    public void testToString() {
476 >        LinkedList q = populatedQueue(SIZE);
477 >        String s = q.toString();
478 >        for (int i = 0; i < SIZE; ++i) {
479 >            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
480 >        }
481      }
482 <    
534 <    
482 >
483      /**
484 <     *
484 >     * peek returns element inserted with addFirst
485       */
486 <    public void testAddAll2_IndexOutOfBoundsException() {
487 <        try {
488 <            LinkedList l = new LinkedList();
489 <            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) {}
486 >    public void testAddFirst() {
487 >        LinkedList q = populatedQueue(3);
488 >        q.addFirst(four);
489 >        assertSame(four, q.peek());
490      }
491  
492      /**
493 <     *
493 >     * peekFirst returns element inserted with push
494       */
495 <    public void testAddAll4_BadIndex() {
496 <        try {
497 <            LinkedList l = new LinkedList();
498 <            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){}
495 >    public void testPush() {
496 >        LinkedList q = populatedQueue(3);
497 >        q.push(four);
498 >        assertSame(four, q.peekFirst());
499      }
500  
501      /**
502 <     *
502 >     * pop removes next element, or throws NSEE if empty
503       */
504 <    public void testget1() {
505 <        try {
506 <            LinkedList l = new LinkedList();
507 <            l.add(new Object());
508 <            l.get(-1);
509 <            shouldThrow();
510 <        } catch(IndexOutOfBoundsException  success) {}  
504 >    public void testPop() {
505 >        LinkedList q = populatedQueue(SIZE);
506 >        for (int i = 0; i < SIZE; ++i) {
507 >            assertEquals(i, q.pop());
508 >        }
509 >        try {
510 >            q.pop();
511 >            shouldThrow();
512 >        } catch (NoSuchElementException success) {}
513      }
514  
515      /**
516 <     *
516 >     * OfferFirst succeeds
517       */
518 <    public void testget2() {
519 <        try {
520 <            LinkedList l = new LinkedList();
521 <            l.add(new Object());
582 <            l.get(5);
583 <            shouldThrow();
584 <        } catch(IndexOutOfBoundsException  success){}  
518 >    public void testOfferFirst() {
519 >        LinkedList q = new LinkedList();
520 >        assertTrue(q.offerFirst(new Integer(0)));
521 >        assertTrue(q.offerFirst(new Integer(1)));
522      }
523  
524      /**
525 <     *
525 >     * OfferLast succeeds
526       */
527 <    public void testset1() {
528 <        try {
529 <            LinkedList l = new LinkedList();
530 <            l.add(new Object());
594 <            l.set(-1,new Object());
595 <            shouldThrow();
596 <        } catch(IndexOutOfBoundsException  success){}  
527 >    public void testOfferLast() {
528 >        LinkedList q = new LinkedList();
529 >        assertTrue(q.offerLast(new Integer(0)));
530 >        assertTrue(q.offerLast(new Integer(1)));
531      }
532  
533      /**
534 <     *
534 >     * pollLast succeeds unless empty
535       */
536 <    public void testset2() {
537 <        try {
538 <            LinkedList l = new LinkedList();
539 <            l.add(new Object());
540 <            l.set(5,new Object());
541 <            shouldThrow();
608 <        } catch(IndexOutOfBoundsException  success){}  
536 >    public void testPollLast() {
537 >        LinkedList q = populatedQueue(SIZE);
538 >        for (int i = SIZE-1; i >= 0; --i) {
539 >            assertEquals(i, q.pollLast());
540 >        }
541 >        assertNull(q.pollLast());
542      }
543  
544      /**
545 <     *
545 >     * peekFirst returns next element, or null if empty
546       */
547 <    public void testadd1() {
548 <        try {
549 <            LinkedList l = new LinkedList();
550 <            l.add(new Object());
551 <            l.add(-1,new Object());
552 <            shouldThrow();
553 <        } catch(IndexOutOfBoundsException  success){}  
547 >    public void testPeekFirst() {
548 >        LinkedList q = populatedQueue(SIZE);
549 >        for (int i = 0; i < SIZE; ++i) {
550 >            assertEquals(i, q.peekFirst());
551 >            assertEquals(i, q.pollFirst());
552 >            assertTrue(q.peekFirst() == null ||
553 >                       !q.peekFirst().equals(i));
554 >        }
555 >        assertNull(q.peekFirst());
556      }
557  
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    }
558  
559      /**
560 <     *
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 <     *
560 >     * peekLast returns next element, or null if empty
561       */
562 <    public void testremove1() {
563 <        try {
564 <            LinkedList l = new LinkedList();
565 <            l.add(new Object());
566 <            l.remove(5);
567 <            shouldThrow();
568 <        } catch(IndexOutOfBoundsException  success){}
562 >    public void testPeekLast() {
563 >        LinkedList q = populatedQueue(SIZE);
564 >        for (int i = SIZE-1; i >= 0; --i) {
565 >            assertEquals(i, q.peekLast());
566 >            assertEquals(i, q.pollLast());
567 >            assertTrue(q.peekLast() == null ||
568 >                       !q.peekLast().equals(i));
569 >        }
570 >        assertNull(q.peekLast());
571      }
572  
573 <    
574 <    /**
575 <     *
576 <     */
577 <    public void testremove2() {
573 >    public void testFirstElement() {
574 >        LinkedList q = populatedQueue(SIZE);
575 >        for (int i = 0; i < SIZE; ++i) {
576 >            assertEquals(i, q.getFirst());
577 >            assertEquals(i, q.pollFirst());
578 >        }
579          try {
580 <            LinkedList l = new LinkedList();
663 <            l.remove();
580 >            q.getFirst();
581              shouldThrow();
582 <        } catch(NoSuchElementException e){}    
582 >        } catch (NoSuchElementException success) {}
583      }
584  
585      /**
586 <     *
586 >     * getLast returns next element, or throws NSEE if empty
587       */
588 <    public void testlistIt1() {
589 <        try {
590 <            LinkedList l = new LinkedList();
591 <            l.add(new Object());
592 <            l.listIterator(5);
593 <            shouldThrow();
594 <        } catch(IndexOutOfBoundsException  success){}
595 <    }
596 <    
597 <    /**
598 <     *
682 <     */
683 <    public void testlistIt2() {
684 <        try {
685 <            LinkedList l = new LinkedList();
686 <            l.add(new Object());
687 <            l.listIterator(-1);
688 <            shouldThrow();
689 <        } catch(IndexOutOfBoundsException  success){}
690 <    }
691 <    
692 <    /**
693 <     *
694 <     */
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){}
588 >    public void testLastElement() {
589 >        LinkedList q = populatedQueue(SIZE);
590 >        for (int i = SIZE-1; i >= 0; --i) {
591 >            assertEquals(i, q.getLast());
592 >            assertEquals(i, q.pollLast());
593 >        }
594 >        try {
595 >            q.getLast();
596 >            shouldThrow();
597 >        } catch (NoSuchElementException success) {}
598 >        assertNull(q.peekLast());
599      }
600  
601      /**
602 <     *
602 >     * removeFirstOccurrence(x) removes x and returns true if present
603       */
604 <    public void testToArray_BadArg() {
605 <        try {
606 <            LinkedList l = new LinkedList();
607 <            l.add(new Object());
608 <            Object o[] = l.toArray(null);
609 <            shouldThrow();
610 <        } catch(NullPointerException success){}
604 >    public void testRemoveFirstOccurrence() {
605 >        LinkedList q = populatedQueue(SIZE);
606 >        for (int i = 1; i < SIZE; i+=2) {
607 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
608 >        }
609 >        for (int i = 0; i < SIZE; i+=2) {
610 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
611 >            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
612 >        }
613 >        assertTrue(q.isEmpty());
614      }
615  
616      /**
617 <     *
617 >     * removeLastOccurrence(x) removes x and returns true if present
618       */
619 <    public void testToArray1_BadArg() {
620 <        try {
621 <            LinkedList l = new LinkedList();
622 <            l.add(new Integer(5));
623 <            Object o[] = l.toArray(new String[10] );
624 <            shouldThrow();
625 <        } catch(ArrayStoreException  success){}
619 >    public void testRemoveLastOccurrence() {
620 >        LinkedList q = populatedQueue(SIZE);
621 >        for (int i = 1; i < SIZE; i+=2) {
622 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
623 >        }
624 >        for (int i = 0; i < SIZE; i+=2) {
625 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
626 >            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
627 >        }
628 >        assertTrue(q.isEmpty());
629      }
630  
631   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines