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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines