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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.8 by dl, Tue Dec 28 16:15:59 2004 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/licenses/publicdomain
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12  
13 < public class LinkedListTest extends TestCase {
13 <
14 <    private static final int N = 10;
15 <    private static final long SHORT_DELAY_MS = 100;
16 <    private static final long MEDIUM_DELAY_MS = 1000;
17 <    private static final long LONG_DELAY_MS = 10000;
18 <
13 > public class LinkedListTest extends JSR166TestCase {
14      public static void main(String[] args) {
15          junit.textui.TestRunner.run (suite());  
16      }
# Line 28 | Line 23 | public class LinkedListTest extends Test
23       * Create a queue of given size containing consecutive
24       * Integers 0 ... n.
25       */
26 <    private LinkedList fullQueue(int n) {
26 >    private LinkedList populatedQueue(int n) {
27          LinkedList q = new LinkedList();
28          assertTrue(q.isEmpty());
29          for(int i = 0; i < n; ++i)
# Line 38 | Line 33 | public class LinkedListTest extends Test
33          return q;
34      }
35  
36 <    public void testConstructor1(){
36 >    /**
37 >     * new queue is empty
38 >     */
39 >    public void testConstructor1() {
40          assertEquals(0, new LinkedList().size());
41      }
42  
43 +    /**
44 +     * Initializing from null Collection throws NPE
45 +     */
46      public void testConstructor3() {
47          try {
48              LinkedList q = new LinkedList((Collection)null);
49 <            fail("Cannot make from null collection");
49 >            shouldThrow();
50          }
51          catch (NullPointerException success) {}
52      }
53  
54 <    public void testConstructor6(){
54 >    /**
55 >     * Queue contains all elements of collection used to initialize
56 >
57 >     */
58 >    public void testConstructor6() {
59          try {
60 <            Integer[] ints = new Integer[N];
61 <            for (int i = 0; i < N; ++i)
60 >            Integer[] ints = new Integer[SIZE];
61 >            for (int i = 0; i < SIZE; ++i)
62                  ints[i] = new Integer(i);
63              LinkedList q = new LinkedList(Arrays.asList(ints));
64 <            for (int i = 0; i < N; ++i)
64 >            for (int i = 0; i < SIZE; ++i)
65                  assertEquals(ints[i], q.poll());
66          }
67          finally {}
68      }
69  
70 +    /**
71 +     * isEmpty is true before add, false after
72 +     */
73      public void testEmpty() {
74          LinkedList q = new LinkedList();
75          assertTrue(q.isEmpty());
# Line 73 | Line 81 | public class LinkedListTest extends Test
81          assertTrue(q.isEmpty());
82      }
83  
84 +    /**
85 +     * size changes when elements added and removed
86 +     */
87      public void testSize() {
88 <        LinkedList q = fullQueue(N);
89 <        for (int i = 0; i < N; ++i) {
90 <            assertEquals(N-i, q.size());
88 >        LinkedList q = populatedQueue(SIZE);
89 >        for (int i = 0; i < SIZE; ++i) {
90 >            assertEquals(SIZE-i, q.size());
91              q.remove();
92          }
93 <        for (int i = 0; i < N; ++i) {
93 >        for (int i = 0; i < SIZE; ++i) {
94              assertEquals(i, q.size());
95              q.add(new Integer(i));
96          }
97      }
98  
99 <    public void testOfferNull(){
99 >    /**
100 >     * offer(null) succeeds
101 >     */
102 >    public void testOfferNull() {
103          try {
104              LinkedList q = new LinkedList();
105              q.offer(null);
106          } catch (NullPointerException ie) {
107 <            fail("should not throw NPE");
107 >            unexpectedException();
108          }  
109      }
110  
111 +    /**
112 +     * Offer succeeds
113 +     */
114      public void testOffer() {
115          LinkedList q = new LinkedList();
116          assertTrue(q.offer(new Integer(0)));
117          assertTrue(q.offer(new Integer(1)));
118      }
119  
120 <    public void testAdd(){
120 >    /**
121 >     * add succeeds
122 >     */
123 >    public void testAdd() {
124          LinkedList q = new LinkedList();
125 <        for (int i = 0; i < N; ++i) {
125 >        for (int i = 0; i < SIZE; ++i) {
126              assertEquals(i, q.size());
127              assertTrue(q.add(new Integer(i)));
128          }
129      }
130  
131 <    public void testAddAll1(){
131 >    /**
132 >     * addAll(null) throws NPE
133 >     */
134 >    public void testAddAll1() {
135          try {
136              LinkedList q = new LinkedList();
137              q.addAll(null);
138 <            fail("Cannot add null collection");
138 >            shouldThrow();
139          }
140          catch (NullPointerException success) {}
141      }
142  
143 <    public void testAddAll5(){
143 >    /**
144 >     * Queue contains all elements, in traversal order, of successful addAll
145 >     */
146 >    public void testAddAll5() {
147          try {
148              Integer[] empty = new Integer[0];
149 <            Integer[] ints = new Integer[N];
150 <            for (int i = 0; i < N; ++i)
149 >            Integer[] ints = new Integer[SIZE];
150 >            for (int i = 0; i < SIZE; ++i)
151                  ints[i] = new Integer(i);
152              LinkedList q = new LinkedList();
153              assertFalse(q.addAll(Arrays.asList(empty)));
154              assertTrue(q.addAll(Arrays.asList(ints)));
155 <            for (int i = 0; i < N; ++i)
155 >            for (int i = 0; i < SIZE; ++i)
156                  assertEquals(ints[i], q.poll());
157          }
158          finally {}
159      }
160  
161 <    public void testPoll(){
162 <        LinkedList q = fullQueue(N);
163 <        for (int i = 0; i < N; ++i) {
161 >    /**
162 >     * addAll with too large an index throws IOOBE
163 >     */
164 >    public void testAddAll2_IndexOutOfBoundsException() {
165 >        try {
166 >            LinkedList l = new LinkedList();
167 >            l.add(new Object());
168 >            LinkedList m = new LinkedList();
169 >            m.add(new Object());
170 >            l.addAll(4,m);
171 >            shouldThrow();
172 >        } catch(IndexOutOfBoundsException  success) {}
173 >    }
174 >
175 >    /**
176 >     * addAll with negative index throws IOOBE
177 >     */
178 >    public void testAddAll4_BadIndex() {
179 >        try {
180 >            LinkedList l = new LinkedList();
181 >            l.add(new Object());
182 >            LinkedList m = new LinkedList();
183 >            m.add(new Object());
184 >            l.addAll(-1,m);
185 >            shouldThrow();
186 >        } catch(IndexOutOfBoundsException  success){}
187 >    }
188 >
189 >    /**
190 >     *  poll succeeds unless empty
191 >     */
192 >    public void testPoll() {
193 >        LinkedList q = populatedQueue(SIZE);
194 >        for (int i = 0; i < SIZE; ++i) {
195              assertEquals(i, ((Integer)q.poll()).intValue());
196          }
197          assertNull(q.poll());
198      }
199  
200 <    public void testPeek(){
201 <        LinkedList q = fullQueue(N);
202 <        for (int i = 0; i < N; ++i) {
200 >    /**
201 >     *  peek returns next element, or null if empty
202 >     */
203 >    public void testPeek() {
204 >        LinkedList q = populatedQueue(SIZE);
205 >        for (int i = 0; i < SIZE; ++i) {
206              assertEquals(i, ((Integer)q.peek()).intValue());
207              q.poll();
208              assertTrue(q.peek() == null ||
# Line 151 | Line 211 | public class LinkedListTest extends Test
211          assertNull(q.peek());
212      }
213  
214 <    public void testElement(){
215 <        LinkedList q = fullQueue(N);
216 <        for (int i = 0; i < N; ++i) {
214 >    /**
215 >     * element returns next element, or throws NSEE if empty
216 >     */
217 >    public void testElement() {
218 >        LinkedList q = populatedQueue(SIZE);
219 >        for (int i = 0; i < SIZE; ++i) {
220              assertEquals(i, ((Integer)q.element()).intValue());
221              q.poll();
222          }
223          try {
224              q.element();
225 <            fail("no such element");
225 >            shouldThrow();
226          }
227          catch (NoSuchElementException success) {}
228      }
229  
230 <    public void testRemove(){
231 <        LinkedList q = fullQueue(N);
232 <        for (int i = 0; i < N; ++i) {
230 >    /**
231 >     *  remove removes next element, or throws NSEE if empty
232 >     */
233 >    public void testRemove() {
234 >        LinkedList q = populatedQueue(SIZE);
235 >        for (int i = 0; i < SIZE; ++i) {
236              assertEquals(i, ((Integer)q.remove()).intValue());
237          }
238          try {
239              q.remove();
240 <            fail("remove should throw");
240 >            shouldThrow();
241          } catch (NoSuchElementException success){
242          }  
243      }
244  
245 <    public void testRemoveElement(){
246 <        LinkedList q = fullQueue(N);
247 <        for (int i = 1; i < N; i+=2) {
245 >    /**
246 >     * remove(x) removes x and returns true if present
247 >     */
248 >    public void testRemoveElement() {
249 >        LinkedList q = populatedQueue(SIZE);
250 >        for (int i = 1; i < SIZE; i+=2) {
251              assertTrue(q.remove(new Integer(i)));
252          }
253 <        for (int i = 0; i < N; i+=2) {
253 >        for (int i = 0; i < SIZE; i+=2) {
254              assertTrue(q.remove(new Integer(i)));
255              assertFalse(q.remove(new Integer(i+1)));
256          }
257          assertTrue(q.isEmpty());
258      }
259          
260 <    public void testContains(){
261 <        LinkedList q = fullQueue(N);
262 <        for (int i = 0; i < N; ++i) {
260 >    /**
261 >     * contains(x) reports true when elements added but not yet removed
262 >     */
263 >    public void testContains() {
264 >        LinkedList q = populatedQueue(SIZE);
265 >        for (int i = 0; i < SIZE; ++i) {
266              assertTrue(q.contains(new Integer(i)));
267              q.poll();
268              assertFalse(q.contains(new Integer(i)));
269          }
270      }
271  
272 <    public void testClear(){
273 <        LinkedList q = fullQueue(N);
272 >    /**
273 >     * clear removes all elements
274 >     */
275 >    public void testClear() {
276 >        LinkedList q = populatedQueue(SIZE);
277          q.clear();
278          assertTrue(q.isEmpty());
279          assertEquals(0, q.size());
# Line 208 | Line 283 | public class LinkedListTest extends Test
283          assertTrue(q.isEmpty());
284      }
285  
286 <    public void testContainsAll(){
287 <        LinkedList q = fullQueue(N);
286 >    /**
287 >     * containsAll(c) is true when c contains a subset of elements
288 >     */
289 >    public void testContainsAll() {
290 >        LinkedList q = populatedQueue(SIZE);
291          LinkedList p = new LinkedList();
292 <        for (int i = 0; i < N; ++i) {
292 >        for (int i = 0; i < SIZE; ++i) {
293              assertTrue(q.containsAll(p));
294              assertFalse(p.containsAll(q));
295              p.add(new Integer(i));
# Line 219 | Line 297 | public class LinkedListTest extends Test
297          assertTrue(p.containsAll(q));
298      }
299  
300 <    public void testRetainAll(){
301 <        LinkedList q = fullQueue(N);
302 <        LinkedList p = fullQueue(N);
303 <        for (int i = 0; i < N; ++i) {
300 >    /**
301 >     * retainAll(c) retains only those elements of c and reports true if changed
302 >     */
303 >    public void testRetainAll() {
304 >        LinkedList q = populatedQueue(SIZE);
305 >        LinkedList p = populatedQueue(SIZE);
306 >        for (int i = 0; i < SIZE; ++i) {
307              boolean changed = q.retainAll(p);
308              if (i == 0)
309                  assertFalse(changed);
# Line 230 | Line 311 | public class LinkedListTest extends Test
311                  assertTrue(changed);
312  
313              assertTrue(q.containsAll(p));
314 <            assertEquals(N-i, q.size());
314 >            assertEquals(SIZE-i, q.size());
315              p.remove();
316          }
317      }
318  
319 <    public void testRemoveAll(){
320 <        for (int i = 1; i < N; ++i) {
321 <            LinkedList q = fullQueue(N);
322 <            LinkedList p = fullQueue(i);
319 >    /**
320 >     * removeAll(c) removes only those elements of c and reports true if changed
321 >     */
322 >    public void testRemoveAll() {
323 >        for (int i = 1; i < SIZE; ++i) {
324 >            LinkedList q = populatedQueue(SIZE);
325 >            LinkedList p = populatedQueue(i);
326              assertTrue(q.removeAll(p));
327 <            assertEquals(N-i, q.size());
327 >            assertEquals(SIZE-i, q.size());
328              for (int j = 0; j < i; ++j) {
329                  Integer I = (Integer)(p.remove());
330                  assertFalse(q.contains(I));
# Line 248 | Line 332 | public class LinkedListTest extends Test
332          }
333      }
334  
335 <    public void testToArray(){
336 <        LinkedList q = fullQueue(N);
335 >    /**
336 >     *  toArray contains all elements
337 >     */
338 >    public void testToArray() {
339 >        LinkedList q = populatedQueue(SIZE);
340          Object[] o = q.toArray();
341          Arrays.sort(o);
342          for(int i = 0; i < o.length; i++)
343              assertEquals(o[i], q.poll());
344      }
345  
346 <    public void testToArray2(){
347 <        LinkedList q = fullQueue(N);
348 <        Integer[] ints = new Integer[N];
346 >    /**
347 >     *  toArray(a) contains all elements
348 >     */
349 >    public void testToArray2() {
350 >        LinkedList q = populatedQueue(SIZE);
351 >        Integer[] ints = new Integer[SIZE];
352          ints = (Integer[])q.toArray(ints);
353          Arrays.sort(ints);
354          for(int i = 0; i < ints.length; i++)
355              assertEquals(ints[i], q.poll());
356      }
357 +
358 +    /**
359 +     * toArray(null) throws NPE
360 +     */
361 +    public void testToArray_BadArg() {
362 +        try {
363 +            LinkedList l = new LinkedList();
364 +            l.add(new Object());
365 +            Object o[] = l.toArray(null);
366 +            shouldThrow();
367 +        } catch(NullPointerException success){}
368 +    }
369 +
370 +    /**
371 +     * toArray with incompatable aray type throws CCE
372 +     */
373 +    public void testToArray1_BadArg() {
374 +        try {
375 +            LinkedList l = new LinkedList();
376 +            l.add(new Integer(5));
377 +            Object o[] = l.toArray(new String[10] );
378 +            shouldThrow();
379 +        } catch(ArrayStoreException  success){}
380 +    }
381      
382 <    public void testIterator(){
383 <        LinkedList q = fullQueue(N);
382 >    /**
383 >     *  iterator iterates through all elements
384 >     */
385 >    public void testIterator() {
386 >        LinkedList q = populatedQueue(SIZE);
387          int i = 0;
388          Iterator it = q.iterator();
389          while(it.hasNext()) {
390              assertTrue(q.contains(it.next()));
391              ++i;
392          }
393 <        assertEquals(i, N);
393 >        assertEquals(i, SIZE);
394      }
395  
396 +    /**
397 +     *  iterator ordering is FIFO
398 +     */
399      public void testIteratorOrdering() {
280
400          final LinkedList q = new LinkedList();
282
401          q.add(new Integer(1));
402          q.add(new Integer(2));
403          q.add(new Integer(3));
286
404          int k = 0;
405          for (Iterator it = q.iterator(); it.hasNext();) {
406              int i = ((Integer)(it.next())).intValue();
407 <            assertEquals("items should come out in order", ++k, i);
407 >            assertEquals(++k, i);
408          }
409  
410 <        assertEquals("should go through 3 elements", 3, k);
410 >        assertEquals(3, k);
411      }
412  
413 +    /**
414 +     * iterator.remove removes current element
415 +     */
416      public void testIteratorRemove () {
417          final LinkedList q = new LinkedList();
298
418          q.add(new Integer(1));
419          q.add(new Integer(2));
420          q.add(new Integer(3));
302
421          Iterator it = q.iterator();
422          it.next();
423          it.remove();
306
424          it = q.iterator();
425          assertEquals(it.next(), new Integer(2));
426          assertEquals(it.next(), new Integer(3));
# Line 311 | Line 428 | public class LinkedListTest extends Test
428      }
429  
430  
431 <    public void testToString(){
432 <        LinkedList q = fullQueue(N);
431 >    /**
432 >     * toString contains toStrings of elements
433 >     */
434 >    public void testToString() {
435 >        LinkedList q = populatedQueue(SIZE);
436          String s = q.toString();
437 <        for (int i = 0; i < N; ++i) {
437 >        for (int i = 0; i < SIZE; ++i) {
438              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
439          }
440      }        
441  
442 <    public void testAddFirst(){
443 <        LinkedList q = fullQueue(3);
444 <        q.addFirst(new Integer(4));
445 <        assertEquals(new Integer(4),q.get(0));
442 >    /**
443 >     * peek returns element inserted with addFirst
444 >     */
445 >    public void testAddFirst() {
446 >        LinkedList q = populatedQueue(3);
447 >        q.addFirst(four);
448 >        assertEquals(four,q.peek());
449      }  
450  
451 <    public void testAddLast(){
452 <        LinkedList q = fullQueue(3);
453 <        q.addLast(new Integer(3));
454 <        assertEquals(new Integer(3),q.get(3));
455 <    }
456 <    
457 <    public void testGetFirst() {
458 <        LinkedList q = fullQueue(3);
336 <        assertEquals(new Integer(0),q.getFirst());
451 >    /**
452 >     * peekFirst returns element inserted with push
453 >     */
454 >    public void testPush() {
455 >        LinkedList q = populatedQueue(3);
456 >        q.pollLast();
457 >        q.push(four);
458 >        assertEquals(four,q.peekFirst());
459      }  
338    
339    public void testGetLast() {
340        LinkedList q = fullQueue(3);
341        assertEquals(new Integer(2),q.getLast());
342    }
343    
344    public void testIndexOf(){
345        LinkedList q = fullQueue(3);
346        assertEquals(0,q.indexOf(new Integer(0)));
347        assertEquals(1,q.indexOf(new Integer(1)));
348        assertEquals(2,q.indexOf(new Integer(2)));
349        assertEquals(-1, q.indexOf("not there"));
350    }
460  
461 <    public void testLastIndexOf(){
462 <        LinkedList q = fullQueue(3);
463 <        q.add(new Integer(2));
464 <        assertEquals(3,q.lastIndexOf(new Integer(2)));
465 <        assertEquals(-1, q.lastIndexOf("not there"));
466 <    }
467 <    
468 <    public void testSet(){
469 <        LinkedList q = fullQueue(3);
470 <        q.set(0,(new Integer(1)));
471 <        assertFalse(q.contains(new Integer(0)));
472 <        assertEquals(new Integer(1), q.get(0));
473 <    }
365 <    
366 <
367 <    public void testGetFirst_NoSuchElementException(){
368 <        try {
369 <            LinkedList l = new LinkedList();
370 <            l.getFirst();
371 <            fail("First Element");
372 <        }
373 <        catch(NoSuchElementException success) {}
374 <    }
375 <    
376 <    public void testRemoveFirst() {
377 <        try {
378 <            LinkedList l = new LinkedList();
379 <            l.removeFirst();
380 <            fail("R: First Element");
381 <        }
382 <        catch(NoSuchElementException success) {}
383 <    }
384 <    
385 <    public void testRemoveLast() {
386 <        try {
387 <            LinkedList l = new LinkedList();
388 <            l.removeLast();
389 <            fail("R: Last Element");  
390 <        }
391 <        catch(NoSuchElementException success) {}
392 <    }
393 <    
394 <    public void testGetLast_NoSuchElementException(){
395 <        try {
396 <            LinkedList l = new LinkedList();
397 <            l.getLast();
398 <            fail("Last Element");  
399 <        }
400 <        catch(NoSuchElementException success) {}
401 <    }
402 <
403 <
404 <    public void testAddAll_NullPointerException(){
405 <        try {
406 <            LinkedList l = new LinkedList();
407 <            l.addAll((Collection)null);
408 <            fail("Add All Failed");
409 <        }
410 <        catch(NullPointerException success){}
411 <    }
412 <    
413 <    
414 <    public void testAddAll1_OutOfBounds() {
415 <        try {
416 <            LinkedList l = new LinkedList();
417 <            l.addAll(4,new LinkedList());
418 <            fail("boolean addAll(int, Collection) should throw IndexOutOfBoundsException");
419 <        }
420 <        catch(IndexOutOfBoundsException  success) {}
421 <    }
422 <    
423 <    
424 <    public void testAddAll2_IndexOutOfBoundsException() {
425 <        try {
426 <            LinkedList l = new LinkedList();
427 <            l.add(new Object());
428 <            LinkedList m = new LinkedList();
429 <            m.add(new Object());
430 <            l.addAll(4,m);
431 <            fail("Add All Failed " + l.size());
432 <        }catch(IndexOutOfBoundsException  success) {}
433 <    }
434 <
435 <    public void testAddAll4_BadIndex() {
436 <        try {
437 <            LinkedList l = new LinkedList();
438 <            l.add(new Object());
439 <            LinkedList m = new LinkedList();
440 <            m.add(new Object());
441 <            l.addAll(-1,m);
442 <            fail("Add All Failed " + l.size());
443 <        }catch(IndexOutOfBoundsException  success){}
444 <    }
445 <
446 <    public void testget1() {
447 <        try {
448 <            LinkedList l = new LinkedList();
449 <            l.add(new Object());
450 <            l.get(-1);
451 <            fail("get Failed - l.get(-1)");
452 <        }catch(IndexOutOfBoundsException  success) {}  
453 <    }
454 <
455 <    public void testget2() {
456 <        try {
457 <            LinkedList l = new LinkedList();
458 <            l.add(new Object());
459 <            l.get(5);
460 <            fail("get Failed - l.get(5) l.size(): " + l.size());
461 <        }catch(IndexOutOfBoundsException  success){}    
461 >    /**
462 >     *  pop removes next element, or throws NSEE if empty
463 >     */
464 >    public void testPop() {
465 >        LinkedList q = populatedQueue(SIZE);
466 >        for (int i = 0; i < SIZE; ++i) {
467 >            assertEquals(i, ((Integer)q.pop()).intValue());
468 >        }
469 >        try {
470 >            q.pop();
471 >            shouldThrow();
472 >        } catch (NoSuchElementException success){
473 >        }  
474      }
475  
476 <    public void testset1() {
477 <        try {
478 <            LinkedList l = new LinkedList();
479 <            l.add(new Object());
480 <            l.set(-1,new Object());
481 <            fail("set failed - l.set(-1,...)" + l.size());
482 <        }catch(IndexOutOfBoundsException  success){}    
476 >    /**
477 >     * OfferFirst succeeds
478 >     */
479 >    public void testOfferFirst() {
480 >        LinkedList q = new LinkedList();
481 >        assertTrue(q.offerFirst(new Integer(0)));
482 >        assertTrue(q.offerFirst(new Integer(1)));
483      }
484  
485 <    public void testset2() {
486 <        try {
487 <            LinkedList l = new LinkedList();
488 <            l.add(new Object());
489 <            l.set(5,new Object());
490 <            fail("set failed = l.set(5,..) l.size():" + l.size());
491 <        }catch(IndexOutOfBoundsException  success){}    
485 >    /**
486 >     * OfferLast succeeds
487 >     */
488 >    public void testOfferLast() {
489 >        LinkedList q = new LinkedList();
490 >        assertTrue(q.offerLast(new Integer(0)));
491 >        assertTrue(q.offerLast(new Integer(1)));
492      }
493  
494 <    public void testadd1() {
495 <        try {
496 <            LinkedList l = new LinkedList();
497 <            l.add(new Object());
498 <            l.add(-1,new Object());
499 <            fail("Add Failed - l.add(-1) l.size(): " + l.size());
500 <        }catch(IndexOutOfBoundsException  success){}    
494 >    /**
495 >     *  pollLast succeeds unless empty
496 >     */
497 >    public void testPollLast() {
498 >        LinkedList q = populatedQueue(SIZE);
499 >        for (int i = SIZE-1; i >= 0; --i) {
500 >            assertEquals(i, ((Integer)q.pollLast()).intValue());
501 >        }
502 >        assertNull(q.pollLast());
503      }
504  
505 <    public void add2(){
506 <        try {
507 <            LinkedList l = new LinkedList();
508 <            l.add(new Object());
509 <            l.add(5,new Object());
510 <            fail("Add Failed  l.add(f,...)");
511 <        }catch(IndexOutOfBoundsException  success) {}  
505 >    /**
506 >     *  peekFirst returns next element, or null if empty
507 >     */
508 >    public void testPeekFirst() {
509 >        LinkedList q = populatedQueue(SIZE);
510 >        for (int i = 0; i < SIZE; ++i) {
511 >            assertEquals(i, ((Integer)q.peekFirst()).intValue());
512 >            q.pollFirst();
513 >            assertTrue(q.peekFirst() == null ||
514 >                       i != ((Integer)q.peekFirst()).intValue());
515 >        }
516 >        assertNull(q.peekFirst());
517      }
518  
500    public void testremove(){
501        try {
502            LinkedList l = new LinkedList();
503            l.add(new Object());
504            l.remove(-1);
505            fail("Remove Failed l.remove(-1); l.size():" + l.size());
506        }catch(IndexOutOfBoundsException  success){}
507    }
508    
509    public void testremove1(){
510        try {
511            LinkedList l = new LinkedList();
512            l.add(new Object());
513            l.remove(5);
514            fail("Remove Failed l.remove(5); l.size():" + l.size());
515        }catch(IndexOutOfBoundsException  success){}
516    }
519  
520 <    
521 <    public void testremove2(){
522 <        try{
523 <            LinkedList l = new LinkedList();
524 <            l.remove();
525 <            fail("LinkedList - Object remove() should throw a NoSuchElementException");
526 <        }catch(NoSuchElementException e){}      
520 >    /**
521 >     *  peekLast returns next element, or null if empty
522 >     */
523 >    public void testPeekLast() {
524 >        LinkedList q = populatedQueue(SIZE);
525 >        for (int i = SIZE-1; i >= 0; --i) {
526 >            assertEquals(i, ((Integer)q.peekLast()).intValue());
527 >            q.pollLast();
528 >            assertTrue(q.peekLast() == null ||
529 >                       i != ((Integer)q.peekLast()).intValue());
530 >        }
531 >        assertNull(q.peekLast());
532 >    }
533 >
534 >    public void testFirstElement() {
535 >        LinkedList q = populatedQueue(SIZE);
536 >        for (int i = 0; i < SIZE; ++i) {
537 >            assertEquals(i, ((Integer)q.getFirst()).intValue());
538 >            q.pollFirst();
539 >        }
540 >        try {
541 >            q.getFirst();
542 >            shouldThrow();
543 >        }
544 >        catch (NoSuchElementException success) {}
545      }
546  
547 <    public void testlistIt1() {
548 <        try {
549 <            LinkedList l = new LinkedList();
550 <            l.add(new Object());
551 <            l.listIterator(5);
552 <            fail("l.listIterator(5) l.size():" + l.size());
553 <        }catch(IndexOutOfBoundsException  success){}
554 <    }
555 <    
556 <    public void testlistIt2() {
557 <        try {
558 <            LinkedList l = new LinkedList();
559 <            l.add(new Object());
560 <            l.listIterator(-1);
561 <            fail("l.listIterator(-1) l.size():" + l.size());
542 <        }catch(IndexOutOfBoundsException  success){}
543 <    }
544 <    
545 <    public void testlistIt3() {
546 <        try {
547 <            LinkedList l = new LinkedList();
548 <            l.add(new Object());
549 <            ListIterator a = l.listIterator(0);
550 <            l.removeFirst();
551 <            a.next();
552 <            fail("l.listIterator(-1) l.size():" + l.size());
553 <        }catch(ConcurrentModificationException success){}
547 >    /**
548 >     *  getLast returns next element, or throws NSEE if empty
549 >     */
550 >    public void testLastElement() {
551 >        LinkedList q = populatedQueue(SIZE);
552 >        for (int i = SIZE-1; i >= 0; --i) {
553 >            assertEquals(i, ((Integer)q.getLast()).intValue());
554 >            q.pollLast();
555 >        }
556 >        try {
557 >            q.getLast();
558 >            shouldThrow();
559 >        }
560 >        catch (NoSuchElementException success) {}
561 >        assertNull(q.peekLast());
562      }
563  
564 <    public void testToArray_BadArg() {
565 <        try {
566 <            LinkedList l = new LinkedList();
567 <            l.add(new Object());
568 <            Object o[] = l.toArray(null);
569 <            fail("l.toArray(null) did not throw an exception");
570 <        }catch(NullPointerException success){}
564 >    /**
565 >     * removeFirstOccurrence(x) removes x and returns true if present
566 >     */
567 >    public void testRemoveFirstOccurrence() {
568 >        LinkedList q = populatedQueue(SIZE);
569 >        for (int i = 1; i < SIZE; i+=2) {
570 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
571 >        }
572 >        for (int i = 0; i < SIZE; i+=2) {
573 >            assertTrue(q.removeFirstOccurrence(new Integer(i)));
574 >            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
575 >        }
576 >        assertTrue(q.isEmpty());
577      }
578  
579 <    public void testToArray1_BadArg() {
580 <        try {
581 <            LinkedList l = new LinkedList();
582 <            l.add(new Integer(5));
583 <            Object o[] = l.toArray(new String[10] );
584 <            fail("l.toArray(String[] f) did not throw an exception, an Integer was added");
585 <        }catch(ArrayStoreException  success){}
579 >    /**
580 >     * removeLastOccurrence(x) removes x and returns true if present
581 >     */
582 >    public void testRemoveLastOccurrence() {
583 >        LinkedList q = populatedQueue(SIZE);
584 >        for (int i = 1; i < SIZE; i+=2) {
585 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
586 >        }
587 >        for (int i = 0; i < SIZE; i+=2) {
588 >            assertTrue(q.removeLastOccurrence(new Integer(i)));
589 >            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
590 >        }
591 >        assertTrue(q.isEmpty());
592      }
593  
594   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines