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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines