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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines