1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.util;
19
20import java.io.Serializable;
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.Collection;
24import java.util.ConcurrentModificationException;
25import java.util.Iterator;
26import java.util.LinkedList;
27import java.util.List;
28import java.util.ListIterator;
29import java.util.NoSuchElementException;
30
31import org.apache.harmony.testframework.serialization.SerializationTest;
32import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
33
34import tests.support.Support_ListTest;
35
36public class LinkedListTest extends junit.framework.TestCase {
37
38    LinkedList ll;
39
40    LinkedList<Object> testList;
41
42    private Object testObjOne;
43
44    private Object testObjTwo;
45
46    private Object testObjThree;
47
48    private Object testObjFour;
49
50    private Object testObjLast;
51
52    Object[] objArray;
53
54    /**
55     * java.util.LinkedList#LinkedList()
56     */
57    public void test_Constructor() {
58        // Test for method java.util.LinkedList()
59        new Support_ListTest("", ll).runTest();
60
61        LinkedList subList = new LinkedList();
62        for (int i = -50; i < 150; i++)
63            subList.add(new Integer(i));
64        new Support_ListTest("", subList.subList(50, 150)).runTest();
65    }
66
67    /**
68     * java.util.LinkedList#LinkedList(java.util.Collection)
69     */
70    public void test_ConstructorLjava_util_Collection() {
71        // Test for method java.util.LinkedList(java.util.Collection)
72        assertTrue("Incorrect LinkedList constructed", new LinkedList(ll)
73                .equals(ll));
74
75        try {
76            new LinkedList(null);
77            fail("NullPointerException expected");
78        } catch (NullPointerException e) {
79            //expected
80        }
81    }
82
83    /**
84     * java.util.LinkedList#add(int, java.lang.Object)
85     */
86    public void test_addILjava_lang_Object() {
87        // Test for method void java.util.LinkedList.add(int, java.lang.Object)
88        Object o;
89        ll.add(50, o = "Test");
90        assertTrue("Failed to add Object>: " + ll.get(50).toString(), ll
91                .get(50) == o);
92        assertTrue("Failed to fix up list after insert",
93                ll.get(51) == objArray[50] && (ll.get(52) == objArray[51]));
94        ll.add(50, null);
95        assertNull("Did not add null correctly", ll.get(50));
96
97        try {
98            ll.add(-1, "Test");
99            fail("Should throw IndexOutOfBoundsException");
100        } catch (IndexOutOfBoundsException e) {
101            // Excepted
102        }
103
104        try {
105            ll.add(-1, null);
106            fail("Should throw IndexOutOfBoundsException");
107        } catch (IndexOutOfBoundsException e) {
108            // Excepted
109        }
110
111        try {
112            ll.add(ll.size() + 1, "Test");
113            fail("Should throw IndexOutOfBoundsException");
114        } catch (IndexOutOfBoundsException e) {
115            // Excepted
116        }
117
118        try {
119            ll.add(ll.size() + 1, null);
120            fail("Should throw IndexOutOfBoundsException");
121        } catch (IndexOutOfBoundsException e) {
122            // Excepted
123        }
124    }
125
126    /**
127     * java.util.LinkedList#add(java.lang.Object)
128     */
129    public void test_addLjava_lang_Object() {
130        // Test for method boolean java.util.LinkedList.add(java.lang.Object)
131        Object o;
132        ll.add(o = new Object());
133        assertTrue("Failed to add Object", ll.getLast() == o);
134        ll.add(null);
135        assertNull("Did not add null correctly", ll.get(ll.size() - 1));
136    }
137
138    /**
139     * java.util.LinkedList#addAll(int, java.util.Collection)
140     */
141    public void test_addAllILjava_util_Collection() {
142        // Test for method boolean java.util.LinkedList.addAll(int,
143        // java.util.Collection)
144        ll.addAll(50, (Collection) ll.clone());
145        assertEquals("Returned incorrect size after adding to existing list", 200, ll
146                .size());
147        for (int i = 0; i < 50; i++)
148            assertTrue("Manipulated elements < index", ll.get(i) == objArray[i]);
149        for (int i = 0; i >= 50 && (i < 150); i++)
150            assertTrue("Failed to ad elements properly",
151                    ll.get(i) == objArray[i - 50]);
152        for (int i = 0; i >= 150 && (i < 200); i++)
153            assertTrue("Failed to ad elements properly",
154                    ll.get(i) == objArray[i - 100]);
155        List myList = new LinkedList();
156        myList.add(null);
157        myList.add("Blah");
158        myList.add(null);
159        myList.add("Booga");
160        myList.add(null);
161        ll.addAll(50, myList);
162        assertNull("a) List w/nulls not added correctly", ll.get(50));
163        assertEquals("b) List w/nulls not added correctly",
164                "Blah", ll.get(51));
165        assertNull("c) List w/nulls not added correctly", ll.get(52));
166        assertEquals("d) List w/nulls not added correctly",
167                "Booga", ll.get(53));
168        assertNull("e) List w/nulls not added correctly", ll.get(54));
169
170        try {
171            ll.addAll(-1, (Collection) null);
172            fail("IndexOutOfBoundsException expected");
173        } catch (IndexOutOfBoundsException e) {
174            //expected
175        }
176
177        try {
178            ll.addAll(ll.size() + 1, (Collection) null);
179            fail("IndexOutOfBoundsException expected");
180        } catch (IndexOutOfBoundsException e) {
181            //expected
182        }
183
184        try {
185            ll.addAll(0, null);
186            fail("NullPointerException expected");
187        } catch (NullPointerException e) {
188            //expected
189        }
190    }
191
192    /**
193     * java.util.LinkedList#addAll(int, java.util.Collection)
194     */
195    public void test_addAllILjava_util_Collection_2() {
196        // Regression for HARMONY-467
197        LinkedList obj = new LinkedList();
198        try {
199            obj.addAll(-1, (Collection) null);
200            fail("IndexOutOfBoundsException expected");
201        } catch (IndexOutOfBoundsException e) {
202        }
203    }
204
205    /**
206     * java.util.LinkedList#addAll(java.util.Collection)
207     */
208    public void test_addAllLjava_util_Collection() {
209        // Test for method boolean
210        // java.util.LinkedList.addAll(java.util.Collection)
211        List l = new ArrayList();
212        l.addAll((Collection) ll.clone());
213        for (int i = 0; i < ll.size(); i++)
214            assertTrue("Failed to add elements properly", l.get(i).equals(
215                    ll.get(i)));
216        ll.addAll((Collection) ll.clone());
217        assertEquals("Returned incorrect siZe after adding to existing list", 200, ll
218                .size());
219        for (int i = 0; i < 100; i++) {
220            assertTrue("Added to list in incorrect order", ll.get(i).equals(
221                    l.get(i)));
222            assertTrue("Failed to add to existing list", ll.get(i + 100)
223                    .equals(l.get(i)));
224        }
225        List myList = new LinkedList();
226        myList.add(null);
227        myList.add("Blah");
228        myList.add(null);
229        myList.add("Booga");
230        myList.add(null);
231        ll.addAll(myList);
232        assertNull("a) List w/nulls not added correctly", ll.get(200));
233        assertEquals("b) List w/nulls not added correctly",
234                "Blah", ll.get(201));
235        assertNull("c) List w/nulls not added correctly", ll.get(202));
236        assertEquals("d) List w/nulls not added correctly",
237                "Booga", ll.get(203));
238        assertNull("e) List w/nulls not added correctly", ll.get(204));
239
240        try {
241            ll.addAll(null);
242            fail("Should throw NullPointerException");
243        } catch (NullPointerException e) {
244            // Excepted
245        }
246    }
247
248    public void test_addAll_Self_Ljava_util_Collection() {
249        LinkedList linkedList = new LinkedList();
250        linkedList.addLast(1);
251        assertEquals(1, linkedList.size());
252        assertTrue(linkedList.addAll(linkedList));
253        assertEquals(2, linkedList.size());
254    }
255
256    public void test_addAll_Self_ILjava_util_Collection() {
257        LinkedList linkedList = new LinkedList();
258        linkedList.addLast(1);
259        assertEquals(1, linkedList.size());
260        assertTrue(linkedList.addAll(1, linkedList));
261        assertEquals(2, linkedList.size());
262    }
263
264    /**
265     * java.util.LinkedList#addFirst(java.lang.Object)
266     */
267    public void test_addFirstLjava_lang_Object() {
268        // Test for method void java.util.LinkedList.addFirst(java.lang.Object)
269        Object o;
270        ll.addFirst(o = new Object());
271        assertTrue("Failed to add Object", ll.getFirst() == o);
272        ll.addFirst(null);
273        assertNull("Failed to add null", ll.getFirst());
274    }
275
276    /**
277     * java.util.LinkedList#addLast(java.lang.Object)
278     */
279    public void test_addLastLjava_lang_Object() {
280        // Test for method void java.util.LinkedList.addLast(java.lang.Object)
281        Object o;
282        ll.addLast(o = new Object());
283        assertTrue("Failed to add Object", ll.getLast() == o);
284        ll.addLast(null);
285        assertNull("Failed to add null", ll.getLast());
286    }
287
288    /**
289     * java.util.LinkedList#clear()
290     */
291    public void test_clear() {
292        // Test for method void java.util.LinkedList.clear()
293        ll.clear();
294        for (int i = 0; i < ll.size(); i++)
295            assertNull("Failed to clear list", ll.get(i));
296    }
297
298    /**
299     * java.util.LinkedList#clone()
300     */
301    public void test_clone() {
302        // Test for method java.lang.Object java.util.LinkedList.clone()
303        Object x = ll.clone();
304        assertTrue("Cloned list was inequal to cloned", x.equals(ll));
305        for (int i = 0; i < ll.size(); i++)
306            assertTrue("Cloned list contains incorrect elements", ll.get(i)
307                    .equals(((LinkedList) x).get(i)));
308        ll.addFirst(null);
309        x = ll.clone();
310        assertTrue("List with a null did not clone properly", ll.equals(x));
311    }
312
313    /**
314     * java.util.LinkedList#contains(java.lang.Object)
315     */
316    public void test_containsLjava_lang_Object() {
317        // Test for method boolean
318        // java.util.LinkedList.contains(java.lang.Object)
319        assertTrue("Returned false for valid element", ll
320                .contains(objArray[99]));
321        assertTrue("Returned false for equal element", ll.contains(new Integer(
322                8)));
323        assertTrue("Returned true for invalid element", !ll
324                .contains(new Object()));
325        assertTrue("Should not contain null", !ll.contains(null));
326        ll.add(25, null);
327        assertTrue("Should contain null", ll.contains(null));
328    }
329
330    /**
331     * java.util.LinkedList#get(int)
332     */
333    public void test_getI() {
334        // Test for method java.lang.Object java.util.LinkedList.get(int)
335        assertTrue("Returned incorrect element", ll.get(22) == objArray[22]);
336        try {
337            ll.get(8765);
338            fail("Failed to throw expected exception for index > size");
339        } catch (IndexOutOfBoundsException e) {
340        }
341    }
342
343    /**
344     * {@link java.util.LinkedList#peek()}
345     */
346    public void test_peek() {
347        LinkedList list = new LinkedList();
348
349        assertNull("Should return null if this list is empty", list.peek());
350
351        assertEquals("Returned incorrect first element", ll.peek(), objArray[0]);
352        assertEquals("Peek remove the head (first element) of this list", ll.getFirst(), objArray[0]);
353    }
354
355    /**
356     * java.util.LinkedList#getFirst()
357     */
358    public void test_getFirst() {
359        // Test for method java.lang.Object java.util.LinkedList.getFirst()
360        assertTrue("Returned incorrect first element", ll.getFirst().equals(
361                objArray[0]));
362
363        LinkedList list = new LinkedList();
364        try {
365            list.getFirst();
366            fail("Should throw NoSuchElementException");
367        } catch (NoSuchElementException e) {
368            // Excepted
369        }
370    }
371
372    /**
373     * java.util.LinkedList#getLast()
374     */
375    public void test_getLast() {
376        // Test for method java.lang.Object java.util.LinkedList.getLast()
377        assertTrue("Returned incorrect first element", ll.getLast().equals(
378                objArray[objArray.length - 1]));
379
380        LinkedList list = new LinkedList();
381        try {
382            list.getLast();
383            fail("Should throw NoSuchElementException");
384        } catch (NoSuchElementException e) {
385            // Excepted
386        }
387    }
388
389    /**
390     * java.util.LinkedList#indexOf(java.lang.Object)
391     */
392    public void test_indexOfLjava_lang_Object() {
393        // Test for method int java.util.LinkedList.indexOf(java.lang.Object)
394        assertEquals("Returned incorrect index", 87, ll.indexOf(objArray[87]));
395        assertEquals("Returned index for invalid Object", -1, ll
396                .indexOf(new Object()));
397        ll.add(20, null);
398        ll.add(24, null);
399        assertTrue("Index of null should be 20, but got: " + ll.indexOf(null),
400                ll.indexOf(null) == 20);
401    }
402
403    /**
404     * java.util.LinkedList#lastIndexOf(java.lang.Object)
405     */
406    public void test_lastIndexOfLjava_lang_Object() {
407        // Test for method int
408        // java.util.LinkedList.lastIndexOf(java.lang.Object)
409        ll.add(new Integer(99));
410        assertEquals("Returned incorrect index",
411                100, ll.lastIndexOf(objArray[99]));
412        assertEquals("Returned index for invalid Object", -1, ll
413                .lastIndexOf(new Object()));
414        ll.add(20, null);
415        ll.add(24, null);
416        assertTrue("Last index of null should be 20, but got: "
417                + ll.lastIndexOf(null), ll.lastIndexOf(null) == 24);
418    }
419
420    /**
421     * java.util.LinkedList#listIterator(int)
422     */
423    public void test_listIteratorI() {
424        // Test for method java.util.ListIterator
425        // java.util.LinkedList.listIterator(int)
426        ListIterator i1 = ll.listIterator();
427        ListIterator i2 = ll.listIterator(0);
428        Object elm;
429        int n = 0;
430        while (i2.hasNext()) {
431            if (n == 0 || n == objArray.length - 1) {
432                if (n == 0)
433                    assertTrue("First element claimed to have a previous", !i2
434                            .hasPrevious());
435                if (n == objArray.length)
436                    assertTrue("Last element claimed to have next", !i2
437                            .hasNext());
438            }
439            elm = i2.next();
440            assertTrue("Iterator returned elements in wrong order",
441                    elm == objArray[n]);
442            if (n > 0 && n < objArray.length - 1) {
443                assertTrue("Next index returned incorrect value",
444                        i2.nextIndex() == n + 1);
445                assertTrue("previousIndex returned incorrect value : "
446                        + i2.previousIndex() + ", n val: " + n, i2
447                        .previousIndex() == n);
448            }
449            elm = i1.next();
450            assertTrue("Iterator returned elements in wrong order",
451                    elm == objArray[n]);
452            ++n;
453        }
454
455        i2 = ll.listIterator(ll.size()/2);
456        assertTrue((Integer)i2.next() == ll.size()/2);
457        List myList = new LinkedList();
458        myList.add(null);
459        myList.add("Blah");
460        myList.add(null);
461        myList.add("Booga");
462        myList.add(null);
463        ListIterator li = myList.listIterator();
464        assertTrue("li.hasPrevious() should be false", !li.hasPrevious());
465        assertNull("li.next() should be null", li.next());
466        assertTrue("li.hasPrevious() should be true", li.hasPrevious());
467        assertNull("li.prev() should be null", li.previous());
468        assertNull("li.next() should be null", li.next());
469        assertEquals("li.next() should be Blah", "Blah", li.next());
470        assertNull("li.next() should be null", li.next());
471        assertEquals("li.next() should be Booga", "Booga", li.next());
472        assertTrue("li.hasNext() should be true", li.hasNext());
473        assertNull("li.next() should be null", li.next());
474        assertTrue("li.hasNext() should be false", !li.hasNext());
475
476        try {
477            ll.listIterator(-1);
478            fail("IndexOutOfBoundsException expected");
479        } catch (IndexOutOfBoundsException e) {
480            //expected
481        }
482
483        try {
484            ll.listIterator(ll.size() + 1);
485            fail("IndexOutOfBoundsException expected");
486        } catch (IndexOutOfBoundsException e) {
487            //expected
488        }
489    }
490
491    /**
492     * java.util.LinkedList#remove(int)
493     */
494    public void test_removeI() {
495        // Test for method java.lang.Object java.util.LinkedList.remove(int)
496        ll.remove(10);
497        assertEquals("Failed to remove element", -1, ll.indexOf(objArray[10]));
498        try {
499            ll.remove(999);
500            fail("Failed to throw expected exception when index out of range");
501        } catch (IndexOutOfBoundsException e) {
502            // Correct
503        }
504
505        ll.add(20, null);
506        ll.remove(20);
507        assertNotNull("Should have removed null", ll.get(20));
508    }
509
510    /**
511     * java.util.LinkedList#remove(java.lang.Object)
512     */
513    public void test_removeLjava_lang_Object() {
514        // Test for method boolean java.util.LinkedList.remove(java.lang.Object)
515        assertTrue("Failed to remove valid Object", ll.remove(objArray[87]));
516        assertTrue("Removed invalid object", !ll.remove(new Object()));
517        assertEquals("Found Object after removal", -1, ll.indexOf(objArray[87]));
518        ll.add(null);
519        ll.remove(null);
520        assertTrue("Should not contain null afrer removal", !ll.contains(null));
521    }
522
523    /**
524     * java.util.LinkedList#removeFirst()
525     */
526    public void test_removeFirst() {
527        // Test for method java.lang.Object java.util.LinkedList.removeFirst()
528        ll.removeFirst();
529        assertTrue("Failed to remove first element",
530                ll.getFirst() != objArray[0]);
531
532        LinkedList list = new LinkedList();
533        try {
534            list.removeFirst();
535            fail("Should throw NoSuchElementException");
536        } catch (NoSuchElementException e) {
537            // Excepted
538        }
539    }
540
541    /**
542     * java.util.LinkedList#removeLast()
543     */
544    public void test_removeLast() {
545        // Test for method java.lang.Object java.util.LinkedList.removeLast()
546        ll.removeLast();
547        assertTrue("Failed to remove last element",
548                ll.getLast() != objArray[objArray.length - 1]);
549
550        LinkedList list = new LinkedList();
551        try {
552            list.removeLast();
553            fail("Should throw NoSuchElementException");
554        } catch (NoSuchElementException e) {
555            // Excepted
556        }
557    }
558
559    /**
560     * java.util.LinkedList#set(int, java.lang.Object)
561     */
562    public void test_setILjava_lang_Object() {
563        // Test for method java.lang.Object java.util.LinkedList.set(int,
564        // java.lang.Object)
565        Object obj;
566        ll.set(65, obj = new Object());
567        assertTrue("Failed to set object", ll.get(65) == obj);
568
569        try {
570            ll.set(-1, obj = new Object());
571            fail("IndexOutOfBoundsException expected");
572        } catch (IndexOutOfBoundsException e) {
573            //expected
574        }
575
576        try {
577            ll.set(ll.size() + 1, obj = new Object());
578            fail("IndexOutOfBoundsException expected");
579        } catch (IndexOutOfBoundsException e) {
580            //expected
581        }
582    }
583
584    /**
585     * java.util.LinkedList#size()
586     */
587    public void test_size() {
588        // Test for method int java.util.LinkedList.size()
589        assertTrue("Returned incorrect size", ll.size() == objArray.length);
590        ll.removeFirst();
591        assertTrue("Returned incorrect size", ll.size() == objArray.length - 1);
592    }
593
594    /**
595     * java.util.LinkedList#toArray()
596     */
597    public void test_toArray() {
598        // Test for method java.lang.Object [] java.util.LinkedList.toArray()
599        ll.add(null);
600        Object[] obj = ll.toArray();
601        assertEquals("Returned array of incorrect size", objArray.length + 1, obj.length);
602
603        for (int i = 0; i < obj.length - 1; i++)
604            assertTrue("Returned incorrect array: " + i, obj[i] == objArray[i]);
605        assertNull("Returned incorrect array--end isn't null",
606                obj[obj.length - 1]);
607    }
608
609    /**
610     * java.util.LinkedList#toArray(java.lang.Object[])
611     */
612    public void test_toArray$Ljava_lang_Object() {
613        // Test for method java.lang.Object []
614        // java.util.LinkedList.toArray(java.lang.Object [])
615        Integer[] argArray = new Integer[100];
616        Object[] retArray;
617        retArray = ll.toArray(argArray);
618        assertTrue("Returned different array than passed", retArray == argArray);
619        List retList = new LinkedList(Arrays.asList(retArray));
620        Iterator li = ll.iterator();
621        Iterator ri = retList.iterator();
622        while (li.hasNext())
623            assertTrue("Lists are not equal", li.next() == ri.next());
624        argArray = new Integer[1000];
625        retArray = ll.toArray(argArray);
626        assertNull("Failed to set first extra element to null", argArray[ll
627                .size()]);
628        for (int i = 0; i < ll.size(); i++)
629            assertTrue("Returned incorrect array: " + i,
630                    retArray[i] == objArray[i]);
631        ll.add(50, null);
632        argArray = new Integer[101];
633        retArray = ll.toArray(argArray);
634        assertTrue("Returned different array than passed", retArray == argArray);
635        retArray = ll.toArray(argArray);
636        assertTrue("Returned different array than passed", retArray == argArray);
637        retList = new LinkedList(Arrays.asList(retArray));
638        li = ll.iterator();
639        ri = retList.iterator();
640        while (li.hasNext())
641            assertTrue("Lists are not equal", li.next() == ri.next());
642
643        try {
644            ll.toArray(null);
645            fail("NullPointerException expected");
646        } catch (NullPointerException e) {
647            //expected
648        }
649
650        LinkedList<String> lls = new LinkedList<String>();
651        lls.add("First");
652        lls.add("Second");
653
654        try {
655            lls.toArray(argArray);
656            fail("ArrayStoreException expected");
657        } catch (ArrayStoreException e) {
658            //expected
659        }
660    }
661
662    public void test_offer() {
663        int origSize = ll.size();
664        assertTrue("offer() should return true'", ll.offer(objArray[0]));
665        assertEquals("offer() should add an element as the last one", origSize, ll.lastIndexOf(objArray[0]));
666    }
667
668    public void test_poll() {
669        for (int i = 0; i < objArray.length; i++) {
670            assertEquals("should remove the head", objArray[i], ll.poll());
671        }
672        assertEquals("should be empty", 0, ll.size());
673        assertNull("should return 'null' if list is empty", ll.poll());
674    }
675
676    public void test_remove() {
677        for (int i = 0; i < objArray.length; i++) {
678            assertEquals("should remove the head", objArray[i], ll.remove());
679        }
680        assertEquals("should be empty", 0, ll.size());
681        try {
682            ll.remove();
683            fail("NoSuchElementException is expected when removing from the empty list");
684        } catch (NoSuchElementException e) {
685            //-- expected
686        }
687    }
688
689    public void test_element() {
690        assertEquals("should return the head", objArray[0], ll.element());
691        assertEquals("element() should remove nothing", objArray.length, ll.size());
692        try {
693            new LinkedList().remove();
694            fail("NoSuchElementException is expected when the list is empty");
695        } catch (NoSuchElementException e) {
696            //-- expected
697        }
698    }
699
700    /**
701     * {@link java.util.LinkedList#removeFirstOccurrence(Object)}
702     */
703    public void test_removeFirstOccurrence() throws Exception {
704        assertTrue(testList.offerLast(testObjOne));
705        assertTrue(testList.offerLast(testObjTwo));
706        assertTrue(testList.offerLast(testObjOne));
707        assertTrue(testList.offerLast(testObjThree));
708        assertTrue(testList.offerLast(testObjOne));
709        assertEquals(5, testList.size());
710        assertTrue(testList.removeFirstOccurrence(testObjOne));
711        assertFalse(testList.removeFirstOccurrence(testObjFour));
712        assertEquals(testObjTwo, testList.peekFirst());
713        assertEquals(testObjOne, testList.peekLast());
714        assertEquals(4, testList.size());
715        assertTrue(testList.removeFirstOccurrence(testObjOne));
716        assertEquals(3, testList.size());
717        assertEquals(testObjOne, testList.peekLast());
718        assertTrue(testList.removeFirstOccurrence(testObjOne));
719        assertEquals(2, testList.size());
720        assertEquals(testObjThree, testList.peekLast());
721        assertFalse(testList.removeFirstOccurrence(testObjOne));
722    }
723
724    /**
725     * {@link java.util.LinkedList#removeLastOccurrence(Object)}
726     */
727    public void test_removeLastOccurrence() throws Exception {
728        assertTrue(testList.offerLast(testObjOne));
729        assertTrue(testList.offerLast(testObjTwo));
730        assertTrue(testList.offerLast(testObjOne));
731        assertTrue(testList.offerLast(testObjThree));
732        assertTrue(testList.offerLast(testObjOne));
733        assertEquals(5, testList.size());
734        assertTrue(testList.removeLastOccurrence(testObjOne));
735        assertFalse(testList.removeLastOccurrence(testObjFour));
736        assertEquals(testObjOne, testList.peekFirst());
737        assertEquals(testObjThree, testList.peekLast());
738        assertEquals(4, testList.size());
739        assertTrue(testList.removeLastOccurrence(testObjOne));
740        assertEquals(3, testList.size());
741        assertEquals(testObjOne, testList.peekFirst());
742        assertEquals(testObjThree, testList.peekLast());
743        assertTrue(testList.removeLastOccurrence(testObjOne));
744        assertEquals(2, testList.size());
745        assertEquals(testObjThree, testList.peekLast());
746        assertFalse(testList.removeLastOccurrence(testObjOne));
747    }
748
749    /**
750     * {@link java.util.LinkedList#offerFirst(Object)}
751     */
752    public void test_offerFirst() throws Exception {
753        assertTrue(testList.offerFirst(testObjOne));
754        assertEquals(1, testList.size());
755        assertEquals(testObjOne, testList.peek());
756        assertTrue(testList.offerFirst(testObjOne));
757        assertEquals(2, testList.size());
758        assertEquals(testObjOne, testList.peek());
759        assertTrue(testList.offerFirst(testObjTwo));
760        assertEquals(3, testList.size());
761        assertEquals(testObjTwo, testList.peek());
762        assertEquals(testObjOne, testList.getLast());
763        assertTrue(testList.offerFirst(null));
764        assertEquals(4, testList.size());
765    }
766
767    /**
768     * {@link java.util.LinkedList#offerLast(Object)}
769     */
770    public void test_offerLast() throws Exception {
771        assertTrue(testList.offerLast(testObjOne));
772        assertEquals(1, testList.size());
773        assertEquals(testObjOne, testList.peek());
774        assertTrue(testList.offerLast(testObjOne));
775        assertEquals(2, testList.size());
776        assertEquals(testObjOne, testList.peek());
777        assertTrue(testList.offerLast(testObjTwo));
778        assertEquals(3, testList.size());
779        assertEquals(testObjOne, testList.peek());
780        assertEquals(testObjTwo, testList.getLast());
781        assertTrue(testList.offerLast(null));
782        assertEquals(4, testList.size());
783    }
784
785    /**
786     * {@link java.util.LinkedList#push(Object)}
787     */
788    public void test_push() throws Exception {
789        testList.push(testObjOne);
790        assertEquals(1, testList.size());
791        assertEquals(testObjOne, testList.peek());
792        testList.push(testObjOne);
793        assertEquals(2, testList.size());
794        assertEquals(testObjOne, testList.peek());
795        testList.push(testObjTwo);
796        assertEquals(3, testList.size());
797        assertEquals(testObjTwo, testList.peek());
798        assertEquals(testObjOne, testList.getLast());
799        testList.push(null);
800        assertEquals(4, testList.size());
801    }
802
803    /**
804     * {@link java.util.LinkedList#pop()}
805     */
806    public void test_pop() throws Exception {
807        assertTrue(testList.offerLast(testObjOne));
808        assertTrue(testList.offerLast(testObjTwo));
809        assertTrue(testList.offerLast(testObjThree));
810        assertEquals(3, testList.size());
811        assertEquals(testObjOne, testList.pop());
812        assertEquals(2, testList.size());
813        assertEquals(testObjTwo, testList.pop());
814        assertEquals(testObjThree, testList.pop());
815        assertEquals(0, testList.size());
816        testList.push(null);
817        assertEquals(1, testList.size());
818        assertNull(testList.pop());
819        try {
820            testList.pop();
821            fail("should throw NoSuchElementException ");
822        } catch (NoSuchElementException e) {
823            // expected
824        }
825    }
826
827    /**
828     * {@link java.util.LinkedList#descendingIterator()}
829     */
830    public void test_descendingIterator() throws Exception {
831        assertFalse(testList.descendingIterator().hasNext());
832        assertTrue(testList.add(testObjOne));
833        assertTrue(testList.add(testObjTwo));
834        assertTrue(testList.add(testObjOne));
835        assertTrue(testList.add(testObjThree));
836        assertTrue(testList.add(testObjLast));
837        Iterator result = testList.descendingIterator();
838        assertEquals(5, testList.size());
839        try {
840            result.remove();
841            fail("should throw IllegalStateException");
842        } catch (IllegalStateException e) {
843            // expected
844        }
845        assertTrue(testList.add(testObjFour));
846
847        try {
848            assertEquals(testObjLast, result.next());
849            fail("should throw ConcurrentModificationException");
850        } catch (ConcurrentModificationException e) {
851            // expected
852        }
853
854        result = testList.descendingIterator();
855        assertEquals(testObjFour, result.next());
856        assertEquals(testObjLast, result.next());
857        assertEquals(testObjThree, result.next());
858        assertEquals(testObjOne, result.next());
859        assertEquals(testObjTwo, result.next());
860        assertTrue(result.hasNext());
861        result.remove();
862        assertEquals(testObjOne, result.next());
863        assertFalse(result.hasNext());
864        try {
865            result.next();
866            fail("should throw NoSuchElementException");
867        } catch (NoSuchElementException e) {
868            // expected
869        }
870    }
871
872    /**
873     * {@link java.util.LinkedList#pollFirst()}
874     */
875    public void test_pollFirst() throws Exception {
876        assertTrue(testList.offerLast(testObjOne));
877        assertTrue(testList.offerLast(testObjTwo));
878        assertTrue(testList.offerLast(testObjThree));
879        assertEquals(3, testList.size());
880        assertEquals(testObjOne, testList.pollFirst());
881        assertEquals(2, testList.size());
882        assertEquals(testObjTwo, testList.pollFirst());
883        assertEquals(testObjThree, testList.pollFirst());
884        assertEquals(0, testList.size());
885        assertNull(testList.pollFirst());
886    }
887
888    /**
889     * {@link java.util.LinkedList#pollLast()}
890     */
891    public void test_pollLast() throws Exception {
892        assertTrue(testList.offerLast(testObjOne));
893        assertTrue(testList.offerLast(testObjTwo));
894        assertTrue(testList.offerLast(testObjThree));
895        assertEquals(3, testList.size());
896        assertEquals(testObjThree, testList.pollLast());
897        assertEquals(2, testList.size());
898        assertEquals(testObjTwo, testList.pollLast());
899        assertEquals(testObjOne, testList.pollLast());
900        assertEquals(0, testList.size());
901        assertNull(testList.pollFirst());
902    }
903
904    /**
905     * {@link java.util.LinkedList#peekFirst()}
906     */
907    public void test_peekFirst() throws Exception {
908        assertTrue(testList.offerLast(testObjOne));
909        assertTrue(testList.offerLast(testObjTwo));
910        assertTrue(testList.offerLast(testObjThree));
911        assertEquals(3, testList.size());
912        assertEquals(testObjOne, testList.peekFirst());
913        assertEquals(3, testList.size());
914        assertEquals(testObjOne, testList.pollFirst());
915        assertEquals(testObjTwo, testList.peekFirst());
916        assertEquals(testObjTwo, testList.pollFirst());
917        assertEquals(testObjThree, testList.pollFirst());
918        assertEquals(0, testList.size());
919        assertEquals(null, testList.peekFirst());
920    }
921
922    /**
923     * {@link java.util.LinkedList#peek()}
924     */
925    public void test_peekLast() throws Exception {
926        assertTrue(testList.offerLast(testObjOne));
927        assertTrue(testList.offerLast(testObjTwo));
928        assertTrue(testList.offerLast(testObjThree));
929        assertEquals(3, testList.size());
930        assertEquals(testObjThree, testList.peekLast());
931        assertEquals(3, testList.size());
932        assertEquals(testObjThree, testList.pollLast());
933        assertEquals(testObjTwo, testList.peekLast());
934        assertEquals(testObjTwo, testList.pollLast());
935        assertEquals(testObjOne, testList.pollLast());
936        assertEquals(0, testList.size());
937        assertNull(testList.peekLast());
938    }
939
940    /**
941     * java.util.LinkedList#Serialization()
942     */
943    public void test_serialization() throws Exception {
944        assertTrue(ll.add(new Integer(1)));
945        assertTrue(ll.add(new Integer(2)));
946        assertTrue(ll.add(new Integer(3)));
947        assertTrue(ll.add(new Integer(4)));
948        assertTrue(ll.add(new Integer(5)));
949        SerializationTest.verifySelf(ll, new SerializableAssert() {
950            public void assertDeserialized(Serializable initial,
951                    Serializable deserialized) {
952                LinkedList<Object> formerQue = (LinkedList) initial;
953                LinkedList<Object> deserializedQue = (LinkedList) deserialized;
954                assertEquals(formerQue.remove(), deserializedQue.remove());
955            }
956        });
957    }
958
959    /**
960     * Sets up the fixture, for example, open a network connection. This method
961     * is called before a test is executed.
962     */
963    protected void setUp() throws Exception {
964        super.setUp();
965
966        objArray = new Object[100];
967        for (int i = 0; i < objArray.length; i++) {
968            objArray[i] = new Integer(i);
969        }
970
971        ll = new LinkedList();
972        for (int i = 0; i < objArray.length; i++) {
973            ll.add(objArray[i]);
974        }
975
976        testList = new LinkedList<Object>();
977        testObjOne = new Object();
978        testObjTwo = new Object();
979        testObjThree = new Object();
980        testObjFour = new Object();
981        testObjLast = new Object();
982    }
983}
984