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.42 by jsr166, Mon Oct 17 00:56:11 2016 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.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
9 > import java.util.Arrays;
10 > import java.util.Collection;
11 > import java.util.Iterator;
12 > import java.util.LinkedList;
13 > import java.util.NoSuchElementException;
14 >
15 > import junit.framework.Test;
16 > import junit.framework.TestSuite;
17  
18   public class LinkedListTest extends JSR166TestCase {
19      public static void main(String[] args) {
20 <        junit.textui.TestRunner.run (suite());  
20 >        main(suite(), args);
21      }
22  
23      public static Test suite() {
24 <        return new TestSuite(LinkedListTest.class);
24 >        class Implementation implements CollectionImplementation {
25 >            public Class<?> klazz() { return LinkedList.class; }
26 >            public Collection emptyCollection() { return new LinkedList(); }
27 >            public Object makeElement(int i) { return i; }
28 >            public boolean isConcurrent() { return false; }
29 >            public boolean permitsNulls() { return true; }
30 >        }
31 >        return newTestSuite(LinkedListTest.class,
32 >                            CollectionTest.testSuite(new Implementation()));
33      }
34  
35      /**
36 <     * Create a queue of given size containing consecutive
37 <     * Integers 0 ... n.
36 >     * Returns a new queue of given size containing consecutive
37 >     * Integers 0 ... n - 1.
38       */
39 <    private LinkedList populatedQueue(int n) {
40 <        LinkedList q = new LinkedList();
39 >    private LinkedList<Integer> populatedQueue(int n) {
40 >        LinkedList<Integer> q = new LinkedList<Integer>();
41          assertTrue(q.isEmpty());
42 <        for(int i = 0; i < n; ++i)
43 <            assertTrue(q.offer(new Integer(i)));
42 >        for (int i = 0; i < n; ++i)
43 >            assertTrue(q.offer(new Integer(i)));
44          assertFalse(q.isEmpty());
45 <        assertEquals(n, q.size());
45 >        assertEquals(n, q.size());
46 >        assertEquals((Integer) 0, q.peekFirst());
47 >        assertEquals((Integer) (n - 1), q.peekLast());
48          return q;
49      }
50 <
50 >
51      /**
52 <     *
52 >     * new queue is empty
53       */
54      public void testConstructor1() {
55          assertEquals(0, new LinkedList().size());
56      }
57  
58      /**
59 <     *
59 >     * Initializing from null Collection throws NPE
60       */
61      public void testConstructor3() {
62          try {
63 <            LinkedList q = new LinkedList((Collection)null);
63 >            new LinkedList((Collection)null);
64              shouldThrow();
65 <        }
50 <        catch (NullPointerException success) {}
65 >        } catch (NullPointerException success) {}
66      }
67  
68      /**
69 <     *
69 >     * Queue contains all elements of collection used to initialize
70       */
71      public void testConstructor6() {
72 <        try {
73 <            Integer[] ints = new Integer[SIZE];
74 <            for (int i = 0; i < SIZE; ++i)
75 <                ints[i] = new Integer(i);
76 <            LinkedList q = new LinkedList(Arrays.asList(ints));
77 <            for (int i = 0; i < SIZE; ++i)
63 <                assertEquals(ints[i], q.poll());
64 <        }
65 <        finally {}
72 >        Integer[] ints = new Integer[SIZE];
73 >        for (int i = 0; i < SIZE; ++i)
74 >            ints[i] = i;
75 >        LinkedList q = new LinkedList(Arrays.asList(ints));
76 >        for (int i = 0; i < SIZE; ++i)
77 >            assertEquals(ints[i], q.poll());
78      }
79  
80      /**
81 <     *
81 >     * isEmpty is true before add, false after
82       */
83      public void testEmpty() {
84          LinkedList q = new LinkedList();
# Line 80 | Line 92 | public class LinkedListTest extends JSR1
92      }
93  
94      /**
95 <     *
95 >     * size changes when elements added and removed
96       */
97      public void testSize() {
98          LinkedList q = populatedQueue(SIZE);
99          for (int i = 0; i < SIZE; ++i) {
100 <            assertEquals(SIZE-i, q.size());
100 >            assertEquals(SIZE - i, q.size());
101              q.remove();
102          }
103          for (int i = 0; i < SIZE; ++i) {
# Line 95 | Line 107 | public class LinkedListTest extends JSR1
107      }
108  
109      /**
110 <     *
110 >     * offer(null) succeeds
111       */
112      public void testOfferNull() {
113 <        try {
114 <            LinkedList q = new LinkedList();
115 <            q.offer(null);
116 <        } catch (NullPointerException ie) {
105 <            unexpectedException();
106 <        }  
113 >        LinkedList q = new LinkedList();
114 >        q.offer(null);
115 >        assertNull(q.get(0));
116 >        assertTrue(q.contains(null));
117      }
118  
119      /**
120 <     *
120 >     * Offer succeeds
121       */
122      public void testOffer() {
123          LinkedList q = new LinkedList();
# Line 116 | Line 126 | public class LinkedListTest extends JSR1
126      }
127  
128      /**
129 <     *
129 >     * add succeeds
130       */
131      public void testAdd() {
132          LinkedList q = new LinkedList();
# Line 127 | Line 137 | public class LinkedListTest extends JSR1
137      }
138  
139      /**
140 <     *
140 >     * addAll(null) throws NPE
141       */
142      public void testAddAll1() {
143 +        LinkedList q = new LinkedList();
144          try {
134            LinkedList q = new LinkedList();
145              q.addAll(null);
146              shouldThrow();
147 <        }
138 <        catch (NullPointerException success) {}
147 >        } catch (NullPointerException success) {}
148      }
149  
150      /**
151 <     *
151 >     * Queue contains all elements, in traversal order, of successful addAll
152       */
153      public void testAddAll5() {
154 +        Integer[] empty = new Integer[0];
155 +        Integer[] ints = new Integer[SIZE];
156 +        for (int i = 0; i < SIZE; ++i)
157 +            ints[i] = i;
158 +        LinkedList q = new LinkedList();
159 +        assertFalse(q.addAll(Arrays.asList(empty)));
160 +        assertTrue(q.addAll(Arrays.asList(ints)));
161 +        for (int i = 0; i < SIZE; ++i)
162 +            assertEquals(ints[i], q.poll());
163 +    }
164 +
165 +    /**
166 +     * addAll with too large an index throws IOOBE
167 +     */
168 +    public void testAddAll2_IndexOutOfBoundsException() {
169 +        LinkedList l = new LinkedList();
170 +        l.add(new Object());
171 +        LinkedList m = new LinkedList();
172 +        m.add(new Object());
173          try {
174 <            Integer[] empty = new Integer[0];
175 <            Integer[] ints = new Integer[SIZE];
176 <            for (int i = 0; i < SIZE; ++i)
149 <                ints[i] = new Integer(i);
150 <            LinkedList q = new LinkedList();
151 <            assertFalse(q.addAll(Arrays.asList(empty)));
152 <            assertTrue(q.addAll(Arrays.asList(ints)));
153 <            for (int i = 0; i < SIZE; ++i)
154 <                assertEquals(ints[i], q.poll());
155 <        }
156 <        finally {}
174 >            l.addAll(4,m);
175 >            shouldThrow();
176 >        } catch (IndexOutOfBoundsException success) {}
177      }
178  
179      /**
180 <     *
180 >     * addAll with negative index throws IOOBE
181 >     */
182 >    public void testAddAll4_BadIndex() {
183 >        LinkedList l = new LinkedList();
184 >        l.add(new Object());
185 >        LinkedList m = new LinkedList();
186 >        m.add(new Object());
187 >        try {
188 >            l.addAll(-1,m);
189 >            shouldThrow();
190 >        } catch (IndexOutOfBoundsException success) {}
191 >    }
192 >
193 >    /**
194 >     * poll succeeds unless empty
195       */
196      public void testPoll() {
197          LinkedList q = populatedQueue(SIZE);
198          for (int i = 0; i < SIZE; ++i) {
199 <            assertEquals(i, ((Integer)q.poll()).intValue());
199 >            assertEquals(i, q.poll());
200          }
201 <        assertNull(q.poll());
201 >        assertNull(q.poll());
202      }
203  
204      /**
205 <     *
205 >     * peek returns next element, or null if empty
206       */
207      public void testPeek() {
208          LinkedList q = populatedQueue(SIZE);
209          for (int i = 0; i < SIZE; ++i) {
210 <            assertEquals(i, ((Integer)q.peek()).intValue());
211 <            q.poll();
210 >            assertEquals(i, q.peek());
211 >            assertEquals(i, q.poll());
212              assertTrue(q.peek() == null ||
213 <                       i != ((Integer)q.peek()).intValue());
213 >                       !q.peek().equals(i));
214          }
215 <        assertNull(q.peek());
215 >        assertNull(q.peek());
216      }
217  
218      /**
219 <     *
219 >     * element returns next element, or throws NSEE if empty
220       */
221      public void testElement() {
222          LinkedList q = populatedQueue(SIZE);
223          for (int i = 0; i < SIZE; ++i) {
224 <            assertEquals(i, ((Integer)q.element()).intValue());
225 <            q.poll();
224 >            assertEquals(i, q.element());
225 >            assertEquals(i, q.poll());
226          }
227          try {
228              q.element();
229              shouldThrow();
230 <        }
197 <        catch (NoSuchElementException success) {}
230 >        } catch (NoSuchElementException success) {}
231      }
232  
233      /**
234 <     *
234 >     * remove removes next element, or throws NSEE if empty
235       */
236      public void testRemove() {
237          LinkedList q = populatedQueue(SIZE);
238          for (int i = 0; i < SIZE; ++i) {
239 <            assertEquals(i, ((Integer)q.remove()).intValue());
239 >            assertEquals(i, q.remove());
240          }
241          try {
242              q.remove();
243              shouldThrow();
244 <        } catch (NoSuchElementException success){
212 <        }  
244 >        } catch (NoSuchElementException success) {}
245      }
246  
247      /**
248 <     *
248 >     * remove(x) removes x and returns true if present
249       */
250      public void testRemoveElement() {
251          LinkedList q = populatedQueue(SIZE);
252 <        for (int i = 1; i < SIZE; i+=2) {
253 <            assertTrue(q.remove(new Integer(i)));
254 <        }
255 <        for (int i = 0; i < SIZE; i+=2) {
256 <            assertTrue(q.remove(new Integer(i)));
257 <            assertFalse(q.remove(new Integer(i+1)));
252 >        for (int i = 1; i < SIZE; i += 2) {
253 >            assertTrue(q.contains(i));
254 >            assertTrue(q.remove((Integer)i));
255 >            assertFalse(q.contains(i));
256 >            assertTrue(q.contains(i - 1));
257 >        }
258 >        for (int i = 0; i < SIZE; i += 2) {
259 >            assertTrue(q.contains(i));
260 >            assertTrue(q.remove((Integer)i));
261 >            assertFalse(q.contains(i));
262 >            assertFalse(q.remove((Integer)(i + 1)));
263 >            assertFalse(q.contains(i + 1));
264          }
265          assertTrue(q.isEmpty());
266      }
267 <        
267 >
268      /**
269 <     *
269 >     * contains(x) reports true when elements added but not yet removed
270       */
271      public void testContains() {
272          LinkedList q = populatedQueue(SIZE);
# Line 240 | Line 278 | public class LinkedListTest extends JSR1
278      }
279  
280      /**
281 <     *
281 >     * clear removes all elements
282       */
283      public void testClear() {
284          LinkedList q = populatedQueue(SIZE);
285          q.clear();
286          assertTrue(q.isEmpty());
287          assertEquals(0, q.size());
288 <        q.add(new Integer(1));
288 >        assertTrue(q.add(new Integer(1)));
289          assertFalse(q.isEmpty());
290          q.clear();
291          assertTrue(q.isEmpty());
292      }
293  
294      /**
295 <     *
295 >     * containsAll(c) is true when c contains a subset of elements
296       */
297      public void testContainsAll() {
298          LinkedList q = populatedQueue(SIZE);
# Line 262 | Line 300 | public class LinkedListTest extends JSR1
300          for (int i = 0; i < SIZE; ++i) {
301              assertTrue(q.containsAll(p));
302              assertFalse(p.containsAll(q));
303 <            p.add(new Integer(i));
303 >            assertTrue(p.add(new Integer(i)));
304          }
305          assertTrue(p.containsAll(q));
306      }
307  
308      /**
309 <     *
309 >     * retainAll(c) retains only those elements of c and reports true if changed
310       */
311      public void testRetainAll() {
312          LinkedList q = populatedQueue(SIZE);
# Line 281 | Line 319 | public class LinkedListTest extends JSR1
319                  assertTrue(changed);
320  
321              assertTrue(q.containsAll(p));
322 <            assertEquals(SIZE-i, q.size());
322 >            assertEquals(SIZE - i, q.size());
323              p.remove();
324          }
325      }
326  
327      /**
328 <     *
328 >     * removeAll(c) removes only those elements of c and reports true if changed
329       */
330      public void testRemoveAll() {
331          for (int i = 1; i < SIZE; ++i) {
332              LinkedList q = populatedQueue(SIZE);
333              LinkedList p = populatedQueue(i);
334              assertTrue(q.removeAll(p));
335 <            assertEquals(SIZE-i, q.size());
335 >            assertEquals(SIZE - i, q.size());
336              for (int j = 0; j < i; ++j) {
337 <                Integer I = (Integer)(p.remove());
338 <                assertFalse(q.contains(I));
337 >                Integer x = (Integer)(p.remove());
338 >                assertFalse(q.contains(x));
339              }
340          }
341      }
342  
343      /**
344 <     *
344 >     * toArray contains all elements in FIFO order
345       */
346      public void testToArray() {
347          LinkedList q = populatedQueue(SIZE);
348 <        Object[] o = q.toArray();
349 <        Arrays.sort(o);
350 <        for(int i = 0; i < o.length; i++)
313 <            assertEquals(o[i], q.poll());
348 >        Object[] o = q.toArray();
349 >        for (int i = 0; i < o.length; i++)
350 >            assertSame(o[i], q.poll());
351      }
352  
353      /**
354 <     *
354 >     * toArray(a) contains all elements in FIFO order
355       */
356      public void testToArray2() {
357 <        LinkedList q = populatedQueue(SIZE);
358 <        Integer[] ints = new Integer[SIZE];
359 <        ints = (Integer[])q.toArray(ints);
360 <        Arrays.sort(ints);
361 <        for(int i = 0; i < ints.length; i++)
362 <            assertEquals(ints[i], q.poll());
357 >        LinkedList<Integer> q = populatedQueue(SIZE);
358 >        Integer[] ints = new Integer[SIZE];
359 >        Integer[] array = q.toArray(ints);
360 >        assertSame(ints, array);
361 >        for (int i = 0; i < ints.length; i++)
362 >            assertSame(ints[i], q.poll());
363      }
364 <    
364 >
365      /**
366 <     *
366 >     * toArray(null) throws NullPointerException
367 >     */
368 >    public void testToArray_NullArg() {
369 >        LinkedList l = new LinkedList();
370 >        l.add(new Object());
371 >        try {
372 >            l.toArray(null);
373 >            shouldThrow();
374 >        } catch (NullPointerException success) {}
375 >    }
376 >
377 >    /**
378 >     * toArray(incompatible array type) throws ArrayStoreException
379 >     */
380 >    public void testToArray1_BadArg() {
381 >        LinkedList l = new LinkedList();
382 >        l.add(new Integer(5));
383 >        try {
384 >            l.toArray(new String[10]);
385 >            shouldThrow();
386 >        } catch (ArrayStoreException success) {}
387 >    }
388 >
389 >    /**
390 >     * iterator iterates through all elements
391       */
392      public void testIterator() {
393          LinkedList q = populatedQueue(SIZE);
394 <        int i = 0;
395 <        Iterator it = q.iterator();
396 <        while(it.hasNext()) {
394 >        Iterator it = q.iterator();
395 >        int i;
396 >        for (i = 0; it.hasNext(); i++)
397              assertTrue(q.contains(it.next()));
337            ++i;
338        }
398          assertEquals(i, SIZE);
399 +        assertIteratorExhausted(it);
400      }
401  
402      /**
403 <     *
403 >     * iterator of empty collection has no elements
404       */
405 <    public void testIteratorOrdering() {
405 >    public void testEmptyIterator() {
406 >        assertIteratorExhausted(new LinkedList().iterator());
407 >    }
408  
409 +    /**
410 +     * iterator ordering is FIFO
411 +     */
412 +    public void testIteratorOrdering() {
413          final LinkedList q = new LinkedList();
348
414          q.add(new Integer(1));
415          q.add(new Integer(2));
416          q.add(new Integer(3));
352
417          int k = 0;
418          for (Iterator it = q.iterator(); it.hasNext();) {
419 <            int i = ((Integer)(it.next())).intValue();
356 <            assertEquals(++k, i);
419 >            assertEquals(++k, it.next());
420          }
421  
422          assertEquals(3, k);
423      }
424  
425      /**
426 <     *
426 >     * iterator.remove removes current element
427       */
428 <    public void testIteratorRemove () {
428 >    public void testIteratorRemove() {
429          final LinkedList q = new LinkedList();
367
430          q.add(new Integer(1));
431          q.add(new Integer(2));
432          q.add(new Integer(3));
371
433          Iterator it = q.iterator();
434 <        it.next();
434 >        assertEquals(1, it.next());
435          it.remove();
375
436          it = q.iterator();
437 <        assertEquals(it.next(), new Integer(2));
438 <        assertEquals(it.next(), new Integer(3));
437 >        assertEquals(2, it.next());
438 >        assertEquals(3, it.next());
439          assertFalse(it.hasNext());
440      }
441  
382
442      /**
443 <     *
443 >     * Descending iterator iterates through all elements
444       */
445 <    public void testToString() {
445 >    public void testDescendingIterator() {
446          LinkedList q = populatedQueue(SIZE);
447 <        String s = q.toString();
448 <        for (int i = 0; i < SIZE; ++i) {
449 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
447 >        int i = 0;
448 >        Iterator it = q.descendingIterator();
449 >        while (it.hasNext()) {
450 >            assertTrue(q.contains(it.next()));
451 >            ++i;
452          }
453 <    }        
454 <
455 <    /**
456 <     *
457 <     */
458 <    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"));
453 >        assertEquals(i, SIZE);
454 >        assertFalse(it.hasNext());
455 >        try {
456 >            it.next();
457 >            shouldThrow();
458 >        } catch (NoSuchElementException success) {}
459      }
460  
461      /**
462 <     *
462 >     * Descending iterator ordering is reverse FIFO
463       */
464 <    public void testLastIndexOf() {
465 <        LinkedList q = populatedQueue(3);
464 >    public void testDescendingIteratorOrdering() {
465 >        final LinkedList q = new LinkedList();
466 >        q.add(new Integer(3));
467          q.add(new Integer(2));
468 <        assertEquals(3,q.lastIndexOf(new Integer(2)));
469 <        assertEquals(-1, q.lastIndexOf("not there"));
470 <    }
471 <    
472 <    /**
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 <    
468 >        q.add(new Integer(1));
469 >        int k = 0;
470 >        for (Iterator it = q.descendingIterator(); it.hasNext();) {
471 >            assertEquals(++k, it.next());
472 >        }
473  
474 <    /**
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) {}
474 >        assertEquals(3, k);
475      }
476  
508
477      /**
478 <     *
478 >     * descendingIterator.remove removes current element
479       */
480 <    public void testAddAll_NullPointerException() {
481 <        try {
482 <            LinkedList l = new LinkedList();
483 <            l.addAll((Collection)null);
484 <            shouldThrow();
485 <        }
486 <        catch(NullPointerException success){}
487 <    }
488 <    
489 <    
490 <    /**
491 <     *
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) {}
480 >    public void testDescendingIteratorRemove() {
481 >        final LinkedList q = new LinkedList();
482 >        q.add(three);
483 >        q.add(two);
484 >        q.add(one);
485 >        Iterator it = q.descendingIterator();
486 >        it.next();
487 >        it.remove();
488 >        it = q.descendingIterator();
489 >        assertSame(it.next(), two);
490 >        assertSame(it.next(), three);
491 >        assertFalse(it.hasNext());
492      }
493 <    
534 <    
493 >
494      /**
495 <     *
495 >     * toString contains toStrings of elements
496       */
497 <    public void testAddAll2_IndexOutOfBoundsException() {
498 <        try {
499 <            LinkedList l = new LinkedList();
500 <            l.add(new Object());
501 <            LinkedList m = new LinkedList();
502 <            m.add(new Object());
544 <            l.addAll(4,m);
545 <            shouldThrow();
546 <        } catch(IndexOutOfBoundsException  success) {}
497 >    public void testToString() {
498 >        LinkedList q = populatedQueue(SIZE);
499 >        String s = q.toString();
500 >        for (int i = 0; i < SIZE; ++i) {
501 >            assertTrue(s.contains(String.valueOf(i)));
502 >        }
503      }
504  
505      /**
506 <     *
506 >     * peek returns element inserted with addFirst
507       */
508 <    public void testAddAll4_BadIndex() {
509 <        try {
510 <            LinkedList l = new LinkedList();
511 <            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){}
508 >    public void testAddFirst() {
509 >        LinkedList q = populatedQueue(3);
510 >        q.addFirst(four);
511 >        assertSame(four, q.peek());
512      }
513  
514      /**
515 <     *
515 >     * peekFirst returns element inserted with push
516       */
517 <    public void testget1() {
518 <        try {
519 <            LinkedList l = new LinkedList();
520 <            l.add(new Object());
570 <            l.get(-1);
571 <            shouldThrow();
572 <        } catch(IndexOutOfBoundsException  success) {}  
517 >    public void testPush() {
518 >        LinkedList q = populatedQueue(3);
519 >        q.push(four);
520 >        assertSame(four, q.peekFirst());
521      }
522  
523      /**
524 <     *
524 >     * pop removes next element, or throws NSEE if empty
525       */
526 <    public void testget2() {
527 <        try {
528 <            LinkedList l = new LinkedList();
529 <            l.add(new Object());
530 <            l.get(5);
531 <            shouldThrow();
532 <        } catch(IndexOutOfBoundsException  success){}  
526 >    public void testPop() {
527 >        LinkedList q = populatedQueue(SIZE);
528 >        for (int i = 0; i < SIZE; ++i) {
529 >            assertEquals(i, q.pop());
530 >        }
531 >        try {
532 >            q.pop();
533 >            shouldThrow();
534 >        } catch (NoSuchElementException success) {}
535      }
536  
537      /**
538 <     *
538 >     * OfferFirst succeeds
539       */
540 <    public void testset1() {
541 <        try {
542 <            LinkedList l = new LinkedList();
543 <            l.add(new Object());
594 <            l.set(-1,new Object());
595 <            shouldThrow();
596 <        } catch(IndexOutOfBoundsException  success){}  
540 >    public void testOfferFirst() {
541 >        LinkedList q = new LinkedList();
542 >        assertTrue(q.offerFirst(new Integer(0)));
543 >        assertTrue(q.offerFirst(new Integer(1)));
544      }
545  
546      /**
547 <     *
547 >     * OfferLast succeeds
548       */
549 <    public void testset2() {
550 <        try {
551 <            LinkedList l = new LinkedList();
552 <            l.add(new Object());
606 <            l.set(5,new Object());
607 <            shouldThrow();
608 <        } catch(IndexOutOfBoundsException  success){}  
549 >    public void testOfferLast() {
550 >        LinkedList q = new LinkedList();
551 >        assertTrue(q.offerLast(new Integer(0)));
552 >        assertTrue(q.offerLast(new Integer(1)));
553      }
554  
555      /**
556 <     *
556 >     * pollLast succeeds unless empty
557       */
558 <    public void testadd1() {
559 <        try {
560 <            LinkedList l = new LinkedList();
561 <            l.add(new Object());
562 <            l.add(-1,new Object());
563 <            shouldThrow();
620 <        } catch(IndexOutOfBoundsException  success){}  
621 <    }
622 <
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) {}  
558 >    public void testPollLast() {
559 >        LinkedList q = populatedQueue(SIZE);
560 >        for (int i = SIZE - 1; i >= 0; --i) {
561 >            assertEquals(i, q.pollLast());
562 >        }
563 >        assertNull(q.pollLast());
564      }
565  
566      /**
567 <     *
567 >     * peekFirst returns next element, or null if empty
568       */
569 <    public void testremove() {
570 <        try {
571 <            LinkedList l = new LinkedList();
572 <            l.add(new Object());
573 <            l.remove(-1);
574 <            shouldThrow();
575 <        } catch(IndexOutOfBoundsException  success){}
569 >    public void testPeekFirst() {
570 >        LinkedList q = populatedQueue(SIZE);
571 >        for (int i = 0; i < SIZE; ++i) {
572 >            assertEquals(i, q.peekFirst());
573 >            assertEquals(i, q.pollFirst());
574 >            assertTrue(q.peekFirst() == null ||
575 >                       !q.peekFirst().equals(i));
576 >        }
577 >        assertNull(q.peekFirst());
578      }
579 <    
579 >
580      /**
581 <     *
581 >     * peekLast returns next element, or null if empty
582       */
583 <    public void testremove1() {
584 <        try {
585 <            LinkedList l = new LinkedList();
586 <            l.add(new Object());
587 <            l.remove(5);
588 <            shouldThrow();
589 <        } catch(IndexOutOfBoundsException  success){}
583 >    public void testPeekLast() {
584 >        LinkedList q = populatedQueue(SIZE);
585 >        for (int i = SIZE - 1; i >= 0; --i) {
586 >            assertEquals(i, q.peekLast());
587 >            assertEquals(i, q.pollLast());
588 >            assertTrue(q.peekLast() == null ||
589 >                       !q.peekLast().equals(i));
590 >        }
591 >        assertNull(q.peekLast());
592      }
593  
594 <    
595 <    /**
596 <     *
597 <     */
598 <    public void testremove2() {
594 >    public void testFirstElement() {
595 >        LinkedList q = populatedQueue(SIZE);
596 >        for (int i = 0; i < SIZE; ++i) {
597 >            assertEquals(i, q.getFirst());
598 >            assertEquals(i, q.pollFirst());
599 >        }
600          try {
601 <            LinkedList l = new LinkedList();
663 <            l.remove();
601 >            q.getFirst();
602              shouldThrow();
603 <        } catch(NoSuchElementException e){}    
603 >        } catch (NoSuchElementException success) {}
604      }
605  
606      /**
607 <     *
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 <     *
607 >     * getLast returns next element, or throws NSEE if empty
608       */
609 <    public void testlistIt2() {
610 <        try {
611 <            LinkedList l = new LinkedList();
612 <            l.add(new Object());
613 <            l.listIterator(-1);
614 <            shouldThrow();
615 <        } catch(IndexOutOfBoundsException  success){}
616 <    }
617 <    
618 <    /**
619 <     *
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){}
609 >    public void testLastElement() {
610 >        LinkedList q = populatedQueue(SIZE);
611 >        for (int i = SIZE - 1; i >= 0; --i) {
612 >            assertEquals(i, q.getLast());
613 >            assertEquals(i, q.pollLast());
614 >        }
615 >        try {
616 >            q.getLast();
617 >            shouldThrow();
618 >        } catch (NoSuchElementException success) {}
619 >        assertNull(q.peekLast());
620      }
621  
622      /**
623 <     *
623 >     * removeFirstOccurrence(x) removes x and returns true if present
624       */
625 <    public void testToArray_BadArg() {
626 <        try {
627 <            LinkedList l = new LinkedList();
628 <            l.add(new Object());
629 <            Object o[] = l.toArray(null);
630 <            shouldThrow();
631 <        } catch(NullPointerException success){}
625 >    public void testRemoveFirstOccurrence() {
626 >        LinkedList q = populatedQueue(SIZE);
627 >        for (int i = 1; i < SIZE; i += 2) {
628 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
629 >        }
630 >        for (int i = 0; i < SIZE; i += 2) {
631 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
632 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
633 >        }
634 >        assertTrue(q.isEmpty());
635      }
636  
637      /**
638 <     *
638 >     * removeLastOccurrence(x) removes x and returns true if present
639       */
640 <    public void testToArray1_BadArg() {
641 <        try {
642 <            LinkedList l = new LinkedList();
643 <            l.add(new Integer(5));
644 <            Object o[] = l.toArray(new String[10] );
645 <            shouldThrow();
646 <        } catch(ArrayStoreException  success){}
640 >    public void testRemoveLastOccurrence() {
641 >        LinkedList q = populatedQueue(SIZE);
642 >        for (int i = 1; i < SIZE; i += 2) {
643 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
644 >        }
645 >        for (int i = 0; i < SIZE; i += 2) {
646 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
647 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
648 >        }
649 >        assertTrue(q.isEmpty());
650      }
651  
652   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines