0001: /*
0002: * Copyright 2001-2004 The Apache Software Foundation
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.apache.commons.collections;
0017:
0018: import java.util.ArrayList;
0019: import java.util.HashSet;
0020: import java.util.Iterator;
0021: import java.util.List;
0022: import java.util.ListIterator;
0023:
0024: import junit.framework.Test;
0025:
0026: import org.apache.commons.collections.list.AbstractTestList;
0027:
0028: /**
0029: * Test class.
0030: *
0031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
0032: *
0033: * @author Rodney Waldhoff
0034: * @author Simon Kitching
0035: */
0036: public class TestCursorableLinkedList extends AbstractTestList {
0037: public TestCursorableLinkedList(String testName) {
0038: super (testName);
0039: }
0040:
0041: public static Test suite() {
0042: return BulkTest.makeSuite(TestCursorableLinkedList.class);
0043: }
0044:
0045: public static void main(String args[]) {
0046: String[] testCaseName = { TestCursorableLinkedList.class
0047: .getName() };
0048: junit.textui.TestRunner.main(testCaseName);
0049: }
0050:
0051: private CursorableLinkedList list = null;
0052:
0053: public void setUp() {
0054: list = new CursorableLinkedList();
0055: }
0056:
0057: public List makeEmptyList() {
0058: return new CursorableLinkedList();
0059: }
0060:
0061: public void testAdd() {
0062: assertEquals("[]", list.toString());
0063: assertTrue(list.add(new Integer(1)));
0064: assertEquals("[1]", list.toString());
0065: assertTrue(list.add(new Integer(2)));
0066: assertEquals("[1, 2]", list.toString());
0067: assertTrue(list.add(new Integer(3)));
0068: assertEquals("[1, 2, 3]", list.toString());
0069: assertTrue(list.addFirst(new Integer(0)));
0070: assertEquals("[0, 1, 2, 3]", list.toString());
0071: assertTrue(list.addLast(new Integer(4)));
0072: assertEquals("[0, 1, 2, 3, 4]", list.toString());
0073: list.add(0, new Integer(-2));
0074: assertEquals("[-2, 0, 1, 2, 3, 4]", list.toString());
0075: list.add(1, new Integer(-1));
0076: assertEquals("[-2, -1, 0, 1, 2, 3, 4]", list.toString());
0077: list.add(7, new Integer(5));
0078: assertEquals("[-2, -1, 0, 1, 2, 3, 4, 5]", list.toString());
0079:
0080: java.util.List list2 = new java.util.LinkedList();
0081: list2.add("A");
0082: list2.add("B");
0083: list2.add("C");
0084:
0085: assertTrue(list.addAll(list2));
0086: assertEquals("[-2, -1, 0, 1, 2, 3, 4, 5, A, B, C]", list
0087: .toString());
0088: assertTrue(list.addAll(3, list2));
0089: assertEquals("[-2, -1, 0, A, B, C, 1, 2, 3, 4, 5, A, B, C]",
0090: list.toString());
0091: }
0092:
0093: public void testClear() {
0094: assertEquals(0, list.size());
0095: assertTrue(list.isEmpty());
0096: list.clear();
0097: assertEquals(0, list.size());
0098: assertTrue(list.isEmpty());
0099:
0100: list.add("element");
0101: assertEquals(1, list.size());
0102: assertTrue(!list.isEmpty());
0103:
0104: list.clear();
0105: assertEquals(0, list.size());
0106: assertTrue(list.isEmpty());
0107:
0108: list.add("element1");
0109: list.add("element2");
0110: assertEquals(2, list.size());
0111: assertTrue(!list.isEmpty());
0112:
0113: list.clear();
0114: assertEquals(0, list.size());
0115: assertTrue(list.isEmpty());
0116:
0117: for (int i = 0; i < 1000; i++) {
0118: list.add(new Integer(i));
0119: }
0120: assertEquals(1000, list.size());
0121: assertTrue(!list.isEmpty());
0122:
0123: list.clear();
0124: assertEquals(0, list.size());
0125: assertTrue(list.isEmpty());
0126: }
0127:
0128: public void testContains() {
0129: assertTrue(!list.contains("A"));
0130: assertTrue(list.add("A"));
0131: assertTrue(list.contains("A"));
0132: assertTrue(list.add("B"));
0133: assertTrue(list.contains("A"));
0134: assertTrue(list.addFirst("a"));
0135: assertTrue(list.contains("A"));
0136: assertTrue(list.remove("a"));
0137: assertTrue(list.contains("A"));
0138: assertTrue(list.remove("A"));
0139: assertTrue(!list.contains("A"));
0140: }
0141:
0142: public void testContainsAll() {
0143: assertTrue(list.containsAll(list));
0144: java.util.List list2 = new java.util.LinkedList();
0145: assertTrue(list.containsAll(list2));
0146: list2.add("A");
0147: assertTrue(!list.containsAll(list2));
0148: list.add("B");
0149: list.add("A");
0150: assertTrue(list.containsAll(list2));
0151: list2.add("B");
0152: assertTrue(list.containsAll(list2));
0153: list2.add("C");
0154: assertTrue(!list.containsAll(list2));
0155: list.add("C");
0156: assertTrue(list.containsAll(list2));
0157: list2.add("C");
0158: assertTrue(list.containsAll(list2));
0159: assertTrue(list.containsAll(list));
0160: }
0161:
0162: public void testCursorNavigation() {
0163: list.add("1");
0164: list.add("2");
0165: list.add("3");
0166: list.add("4");
0167: list.add("5");
0168: CursorableLinkedList.Cursor it = list.cursor();
0169: assertTrue(it.hasNext());
0170: assertTrue(!it.hasPrevious());
0171: assertEquals("1", it.next());
0172: assertTrue(it.hasNext());
0173: assertTrue(it.hasPrevious());
0174: assertEquals("1", it.previous());
0175: assertTrue(it.hasNext());
0176: assertTrue(!it.hasPrevious());
0177: assertEquals("1", it.next());
0178: assertTrue(it.hasNext());
0179: assertTrue(it.hasPrevious());
0180: assertEquals("2", it.next());
0181: assertTrue(it.hasNext());
0182: assertTrue(it.hasPrevious());
0183: assertEquals("2", it.previous());
0184: assertTrue(it.hasNext());
0185: assertTrue(it.hasPrevious());
0186: assertEquals("2", it.next());
0187: assertTrue(it.hasNext());
0188: assertTrue(it.hasPrevious());
0189: assertEquals("3", it.next());
0190: assertTrue(it.hasNext());
0191: assertTrue(it.hasPrevious());
0192: assertEquals("4", it.next());
0193: assertTrue(it.hasNext());
0194: assertTrue(it.hasPrevious());
0195: assertEquals("5", it.next());
0196: assertTrue(!it.hasNext());
0197: assertTrue(it.hasPrevious());
0198: assertEquals("5", it.previous());
0199: assertTrue(it.hasNext());
0200: assertTrue(it.hasPrevious());
0201: assertEquals("4", it.previous());
0202: assertTrue(it.hasNext());
0203: assertTrue(it.hasPrevious());
0204: assertEquals("3", it.previous());
0205: assertTrue(it.hasNext());
0206: assertTrue(it.hasPrevious());
0207: assertEquals("2", it.previous());
0208: assertTrue(it.hasNext());
0209: assertTrue(it.hasPrevious());
0210: assertEquals("1", it.previous());
0211: assertTrue(it.hasNext());
0212: assertTrue(!it.hasPrevious());
0213: it.close();
0214: }
0215:
0216: public void testCursorSet() {
0217: list.add("1");
0218: list.add("2");
0219: list.add("3");
0220: list.add("4");
0221: list.add("5");
0222:
0223: CursorableLinkedList.Cursor it = list.cursor();
0224: assertEquals("1", it.next());
0225: it.set("a");
0226: assertEquals("a", it.previous());
0227: it.set("A");
0228: assertEquals("A", it.next());
0229: assertEquals("2", it.next());
0230: it.set("B");
0231: assertEquals("3", it.next());
0232: assertEquals("4", it.next());
0233: it.set("D");
0234: assertEquals("5", it.next());
0235: it.set("E");
0236: assertEquals("[A, B, 3, D, E]", list.toString());
0237: it.close();
0238: }
0239:
0240: public void testCursorRemove() {
0241: list.add("1");
0242: list.add("2");
0243: list.add("3");
0244: list.add("4");
0245: list.add("5");
0246:
0247: CursorableLinkedList.Cursor it = list.cursor();
0248: try {
0249: it.remove();
0250: } catch (IllegalStateException e) {
0251: // expected
0252: }
0253: assertEquals("1", it.next());
0254: assertEquals("2", it.next());
0255: assertEquals("[1, 2, 3, 4, 5]", list.toString());
0256: it.remove();
0257: assertEquals("[1, 3, 4, 5]", list.toString());
0258: assertEquals("3", it.next());
0259: assertEquals("3", it.previous());
0260: assertEquals("1", it.previous());
0261: it.remove();
0262: assertEquals("[3, 4, 5]", list.toString());
0263: assertTrue(!it.hasPrevious());
0264: assertEquals("3", it.next());
0265: it.remove();
0266: assertEquals("[4, 5]", list.toString());
0267: try {
0268: it.remove();
0269: } catch (IllegalStateException e) {
0270: // expected
0271: }
0272: assertEquals("4", it.next());
0273: assertEquals("5", it.next());
0274: it.remove();
0275: assertEquals("[4]", list.toString());
0276: assertEquals("4", it.previous());
0277: it.remove();
0278: assertEquals("[]", list.toString());
0279: it.close();
0280: }
0281:
0282: public void testCursorAdd() {
0283: CursorableLinkedList.Cursor it = list.cursor();
0284: it.add("1");
0285: assertEquals("[1]", list.toString());
0286: it.add("3");
0287: assertEquals("[1, 3]", list.toString());
0288: it.add("5");
0289: assertEquals("[1, 3, 5]", list.toString());
0290: assertEquals("5", it.previous());
0291: it.add("4");
0292: assertEquals("[1, 3, 4, 5]", list.toString());
0293: assertEquals("4", it.previous());
0294: assertEquals("3", it.previous());
0295: it.add("2");
0296: assertEquals("[1, 2, 3, 4, 5]", list.toString());
0297: it.close();
0298: }
0299:
0300: public void testCursorConcurrentModification() {
0301: // this test verifies that cursors remain valid when the list
0302: // is modified via other means.
0303: list.add("1");
0304: list.add("2");
0305: list.add("3");
0306: list.add("5");
0307: list.add("7");
0308: list.add("9");
0309:
0310: CursorableLinkedList.Cursor c1 = list.cursor();
0311: CursorableLinkedList.Cursor c2 = list.cursor();
0312: ListIterator li = list.listIterator();
0313:
0314: // test cursors remain valid when list modified by std ListIterator
0315: // test cursors skip elements removed via ListIterator
0316: assertEquals("1", li.next());
0317: assertEquals("2", li.next());
0318: li.remove();
0319: assertEquals("3", li.next());
0320: assertEquals("1", c1.next());
0321: assertEquals("3", c1.next());
0322: assertEquals("1", c2.next());
0323:
0324: // test cursor c1 can remove elements from previously modified list
0325: // test cursor c2 skips elements removed via different cursor
0326: c1.remove();
0327: assertEquals("5", c2.next());
0328: c2.add("6");
0329: assertEquals("5", c1.next());
0330: assertEquals("6", c1.next());
0331: assertEquals("7", c1.next());
0332:
0333: // test cursors remain valid when list mod via CursorableLinkedList
0334: // test cursor remains valid when elements inserted into list before
0335: // the current position of the cursor.
0336: list.add(0, "0");
0337:
0338: // test cursor remains valid when element inserted immediately after
0339: // current element of a cursor, and the element is seen on the
0340: // next call to the next method of that cursor.
0341: list.add(5, "8");
0342:
0343: assertEquals("8", c1.next());
0344: assertEquals("9", c1.next());
0345: c1.add("10");
0346: assertEquals("7", c2.next());
0347: assertEquals("8", c2.next());
0348: assertEquals("9", c2.next());
0349: assertEquals("10", c2.next());
0350:
0351: boolean nosuch = false;
0352: try {
0353: c2.next();
0354: } catch (java.util.NoSuchElementException nse) {
0355: nosuch = true; // expected
0356: }
0357: assertTrue(nosuch);
0358:
0359: boolean listIteratorInvalid = false;
0360: try {
0361: li.next();
0362: } catch (java.util.ConcurrentModificationException cme) {
0363: listIteratorInvalid = true; // expected
0364: }
0365: assertTrue(listIteratorInvalid);
0366:
0367: c1.close(); // not necessary
0368: c2.close(); // not necessary
0369: }
0370:
0371: public void testEqualsAndHashCode() {
0372: assertTrue(list.equals(list));
0373: assertEquals(list.hashCode(), list.hashCode());
0374: list.add("A");
0375: assertTrue(list.equals(list));
0376: assertEquals(list.hashCode(), list.hashCode());
0377:
0378: CursorableLinkedList list2 = new CursorableLinkedList();
0379: assertTrue(!list.equals(list2));
0380: assertTrue(!list2.equals(list));
0381:
0382: java.util.List list3 = new java.util.LinkedList();
0383: assertTrue(!list.equals(list3));
0384: assertTrue(!list3.equals(list));
0385: assertTrue(list2.equals(list3));
0386: assertTrue(list3.equals(list2));
0387: assertEquals(list2.hashCode(), list3.hashCode());
0388:
0389: list2.add("A");
0390: assertTrue(list.equals(list2));
0391: assertTrue(list2.equals(list));
0392: assertTrue(!list2.equals(list3));
0393: assertTrue(!list3.equals(list2));
0394:
0395: list3.add("A");
0396: assertTrue(list2.equals(list3));
0397: assertTrue(list3.equals(list2));
0398: assertEquals(list2.hashCode(), list3.hashCode());
0399:
0400: list.add("B");
0401: assertTrue(list.equals(list));
0402: assertTrue(!list.equals(list2));
0403: assertTrue(!list2.equals(list));
0404: assertTrue(!list.equals(list3));
0405: assertTrue(!list3.equals(list));
0406:
0407: list2.add("B");
0408: list3.add("B");
0409: assertTrue(list.equals(list));
0410: assertTrue(list.equals(list2));
0411: assertTrue(list2.equals(list));
0412: assertTrue(list2.equals(list3));
0413: assertTrue(list3.equals(list2));
0414: assertEquals(list2.hashCode(), list3.hashCode());
0415:
0416: list.add("C");
0417: list2.add("C");
0418: list3.add("C");
0419: assertTrue(list.equals(list));
0420: assertTrue(list.equals(list2));
0421: assertTrue(list2.equals(list));
0422: assertTrue(list2.equals(list3));
0423: assertTrue(list3.equals(list2));
0424: assertEquals(list.hashCode(), list2.hashCode());
0425: assertEquals(list2.hashCode(), list3.hashCode());
0426:
0427: list.add("D");
0428: list2.addFirst("D");
0429: assertTrue(list.equals(list));
0430: assertTrue(!list.equals(list2));
0431: assertTrue(!list2.equals(list));
0432: }
0433:
0434: public void testGet() {
0435: try {
0436: list.get(0);
0437: fail("shouldn't get here");
0438: } catch (IndexOutOfBoundsException e) {
0439: // expected
0440: }
0441:
0442: assertTrue(list.add("A"));
0443: assertEquals("A", list.get(0));
0444: assertTrue(list.add("B"));
0445: assertEquals("A", list.get(0));
0446: assertEquals("B", list.get(1));
0447:
0448: try {
0449: list.get(-1);
0450: fail("shouldn't get here");
0451: } catch (IndexOutOfBoundsException e) {
0452: // expected
0453: }
0454:
0455: try {
0456: list.get(2);
0457: fail("shouldn't get here");
0458: } catch (IndexOutOfBoundsException e) {
0459: // expected
0460: }
0461: }
0462:
0463: public void testIndexOf() {
0464: assertEquals(-1, list.indexOf("A"));
0465: assertEquals(-1, list.lastIndexOf("A"));
0466: list.add("A");
0467: assertEquals(0, list.indexOf("A"));
0468: assertEquals(0, list.lastIndexOf("A"));
0469: assertEquals(-1, list.indexOf("B"));
0470: assertEquals(-1, list.lastIndexOf("B"));
0471: list.add("B");
0472: assertEquals(0, list.indexOf("A"));
0473: assertEquals(0, list.lastIndexOf("A"));
0474: assertEquals(1, list.indexOf("B"));
0475: assertEquals(1, list.lastIndexOf("B"));
0476: list.addFirst("B");
0477: assertEquals(1, list.indexOf("A"));
0478: assertEquals(1, list.lastIndexOf("A"));
0479: assertEquals(0, list.indexOf("B"));
0480: assertEquals(2, list.lastIndexOf("B"));
0481: }
0482:
0483: public void testIsEmpty() {
0484: assertTrue(list.isEmpty());
0485: list.add("element");
0486: assertTrue(!list.isEmpty());
0487: list.remove("element");
0488: assertTrue(list.isEmpty());
0489: list.add("element");
0490: assertTrue(!list.isEmpty());
0491: list.clear();
0492: assertTrue(list.isEmpty());
0493: }
0494:
0495: public void testIterator() {
0496: list.add("1");
0497: list.add("2");
0498: list.add("3");
0499: list.add("4");
0500: list.add("5");
0501: Iterator it = list.iterator();
0502: assertTrue(it.hasNext());
0503: assertEquals("1", it.next());
0504: assertTrue(it.hasNext());
0505: assertEquals("2", it.next());
0506: assertTrue(it.hasNext());
0507: assertEquals("3", it.next());
0508: assertTrue(it.hasNext());
0509: assertEquals("4", it.next());
0510: assertTrue(it.hasNext());
0511: assertEquals("5", it.next());
0512: assertTrue(!it.hasNext());
0513:
0514: it = list.iterator();
0515: assertTrue(it.hasNext());
0516: assertEquals("1", it.next());
0517: it.remove();
0518: assertEquals("[2, 3, 4, 5]", list.toString());
0519: assertTrue(it.hasNext());
0520: assertEquals("2", it.next());
0521: it.remove();
0522: assertEquals("[3, 4, 5]", list.toString());
0523: assertTrue(it.hasNext());
0524: assertEquals("3", it.next());
0525: it.remove();
0526: assertEquals("[4, 5]", list.toString());
0527: assertTrue(it.hasNext());
0528: assertEquals("4", it.next());
0529: it.remove();
0530: assertEquals("[5]", list.toString());
0531: assertTrue(it.hasNext());
0532: assertEquals("5", it.next());
0533: it.remove();
0534: assertEquals("[]", list.toString());
0535: assertTrue(!it.hasNext());
0536: }
0537:
0538: public void testListIteratorNavigation() {
0539: list.add("1");
0540: list.add("2");
0541: list.add("3");
0542: list.add("4");
0543: list.add("5");
0544: ListIterator it = list.listIterator();
0545: assertTrue(it.hasNext());
0546: assertTrue(!it.hasPrevious());
0547: assertEquals(-1, it.previousIndex());
0548: assertEquals(0, it.nextIndex());
0549: assertEquals("1", it.next());
0550: assertTrue(it.hasNext());
0551: assertTrue(it.hasPrevious());
0552: assertEquals(0, it.previousIndex());
0553: assertEquals(1, it.nextIndex());
0554: assertEquals("1", it.previous());
0555: assertTrue(it.hasNext());
0556: assertTrue(!it.hasPrevious());
0557: assertEquals(-1, it.previousIndex());
0558: assertEquals(0, it.nextIndex());
0559: assertEquals("1", it.next());
0560: assertTrue(it.hasNext());
0561: assertTrue(it.hasPrevious());
0562: assertEquals(0, it.previousIndex());
0563: assertEquals(1, it.nextIndex());
0564: assertEquals("2", it.next());
0565: assertTrue(it.hasNext());
0566: assertTrue(it.hasPrevious());
0567: assertEquals(1, it.previousIndex());
0568: assertEquals(2, it.nextIndex());
0569: assertEquals("2", it.previous());
0570: assertTrue(it.hasNext());
0571: assertTrue(it.hasPrevious());
0572: assertEquals(0, it.previousIndex());
0573: assertEquals(1, it.nextIndex());
0574: assertEquals("2", it.next());
0575: assertTrue(it.hasNext());
0576: assertTrue(it.hasPrevious());
0577: assertEquals(1, it.previousIndex());
0578: assertEquals(2, it.nextIndex());
0579: assertEquals("3", it.next());
0580: assertTrue(it.hasNext());
0581: assertTrue(it.hasPrevious());
0582: assertEquals(2, it.previousIndex());
0583: assertEquals(3, it.nextIndex());
0584: assertEquals("4", it.next());
0585: assertTrue(it.hasNext());
0586: assertTrue(it.hasPrevious());
0587: assertEquals(3, it.previousIndex());
0588: assertEquals(4, it.nextIndex());
0589: assertEquals("5", it.next());
0590: assertTrue(!it.hasNext());
0591: assertTrue(it.hasPrevious());
0592: assertEquals(4, it.previousIndex());
0593: assertEquals(5, it.nextIndex());
0594: assertEquals("5", it.previous());
0595: assertTrue(it.hasNext());
0596: assertTrue(it.hasPrevious());
0597: assertEquals(3, it.previousIndex());
0598: assertEquals(4, it.nextIndex());
0599: assertEquals("4", it.previous());
0600: assertTrue(it.hasNext());
0601: assertTrue(it.hasPrevious());
0602: assertEquals(2, it.previousIndex());
0603: assertEquals(3, it.nextIndex());
0604: assertEquals("3", it.previous());
0605: assertTrue(it.hasNext());
0606: assertTrue(it.hasPrevious());
0607: assertEquals(1, it.previousIndex());
0608: assertEquals(2, it.nextIndex());
0609: assertEquals("2", it.previous());
0610: assertTrue(it.hasNext());
0611: assertTrue(it.hasPrevious());
0612: assertEquals(0, it.previousIndex());
0613: assertEquals(1, it.nextIndex());
0614: assertEquals("1", it.previous());
0615: assertTrue(it.hasNext());
0616: assertTrue(!it.hasPrevious());
0617: assertEquals(-1, it.previousIndex());
0618: assertEquals(0, it.nextIndex());
0619: }
0620:
0621: public void testListIteratorSet() {
0622: list.add("1");
0623: list.add("2");
0624: list.add("3");
0625: list.add("4");
0626: list.add("5");
0627:
0628: ListIterator it = list.listIterator();
0629: assertEquals("1", it.next());
0630: it.set("a");
0631: assertEquals("a", it.previous());
0632: it.set("A");
0633: assertEquals("A", it.next());
0634: assertEquals("2", it.next());
0635: it.set("B");
0636: assertEquals("3", it.next());
0637: assertEquals("4", it.next());
0638: it.set("D");
0639: assertEquals("5", it.next());
0640: it.set("E");
0641: assertEquals("[A, B, 3, D, E]", list.toString());
0642: }
0643:
0644: public void testListIteratorRemove() {
0645: list.add("1");
0646: list.add("2");
0647: list.add("3");
0648: list.add("4");
0649: list.add("5");
0650:
0651: ListIterator it = list.listIterator();
0652: try {
0653: it.remove();
0654: } catch (IllegalStateException e) {
0655: // expected
0656: }
0657: assertEquals("1", it.next());
0658: assertEquals("2", it.next());
0659: assertEquals("[1, 2, 3, 4, 5]", list.toString());
0660: it.remove();
0661: assertEquals("[1, 3, 4, 5]", list.toString());
0662: assertEquals("3", it.next());
0663: assertEquals("3", it.previous());
0664: assertEquals("1", it.previous());
0665: it.remove();
0666: assertEquals("[3, 4, 5]", list.toString());
0667: assertTrue(!it.hasPrevious());
0668: assertEquals("3", it.next());
0669: it.remove();
0670: assertEquals("[4, 5]", list.toString());
0671: try {
0672: it.remove();
0673: } catch (IllegalStateException e) {
0674: // expected
0675: }
0676: assertEquals("4", it.next());
0677: assertEquals("5", it.next());
0678: it.remove();
0679: assertEquals("[4]", list.toString());
0680: assertEquals("4", it.previous());
0681: it.remove();
0682: assertEquals("[]", list.toString());
0683: }
0684:
0685: public void testListIteratorAdd() {
0686: ListIterator it = list.listIterator();
0687: it.add("1");
0688: assertEquals("[1]", list.toString());
0689: it.add("3");
0690: assertEquals("[1, 3]", list.toString());
0691: it.add("5");
0692: assertEquals("[1, 3, 5]", list.toString());
0693: assertEquals("5", it.previous());
0694: it.add("4");
0695: assertEquals("[1, 3, 4, 5]", list.toString());
0696: assertEquals("4", it.previous());
0697: assertEquals("3", it.previous());
0698: it.add("2");
0699: assertEquals("[1, 2, 3, 4, 5]", list.toString());
0700: }
0701:
0702: public void testRemoveAll() {
0703: list.add("1");
0704: list.add("2");
0705: list.add("3");
0706: list.add("4");
0707: list.add("5");
0708:
0709: HashSet set = new HashSet();
0710: set.add("A");
0711: set.add("2");
0712: set.add("C");
0713: set.add("4");
0714: set.add("D");
0715:
0716: assertTrue(list.removeAll(set));
0717: assertEquals("[1, 3, 5]", list.toString());
0718: assertTrue(!list.removeAll(set));
0719: }
0720:
0721: public void testRemoveByIndex() {
0722: list.add("1");
0723: list.add("2");
0724: list.add("3");
0725: list.add("4");
0726: list.add("5");
0727: assertEquals("[1, 2, 3, 4, 5]", list.toString());
0728: assertEquals("1", list.remove(0));
0729: assertEquals("[2, 3, 4, 5]", list.toString());
0730: assertEquals("3", list.remove(1));
0731: assertEquals("[2, 4, 5]", list.toString());
0732: assertEquals("4", list.remove(1));
0733: assertEquals("[2, 5]", list.toString());
0734: assertEquals("5", list.remove(1));
0735: assertEquals("[2]", list.toString());
0736: assertEquals("2", list.remove(0));
0737: assertEquals("[]", list.toString());
0738: }
0739:
0740: public void testRemove() {
0741: list.add("1");
0742: list.add("1");
0743: list.add("2");
0744: list.add("3");
0745: list.add("4");
0746: list.add("5");
0747: list.add("2");
0748: list.add("3");
0749: list.add("4");
0750: list.add("5");
0751: assertEquals("[1, 1, 2, 3, 4, 5, 2, 3, 4, 5]", list.toString());
0752: assertTrue(!list.remove("6"));
0753: assertTrue(list.remove("5"));
0754: assertEquals("[1, 1, 2, 3, 4, 2, 3, 4, 5]", list.toString());
0755: assertTrue(list.remove("5"));
0756: assertEquals("[1, 1, 2, 3, 4, 2, 3, 4]", list.toString());
0757: assertTrue(!list.remove("5"));
0758: assertTrue(list.remove("1"));
0759: assertEquals("[1, 2, 3, 4, 2, 3, 4]", list.toString());
0760: assertTrue(list.remove("1"));
0761: assertEquals("[2, 3, 4, 2, 3, 4]", list.toString());
0762: assertTrue(list.remove("2"));
0763: assertEquals("[3, 4, 2, 3, 4]", list.toString());
0764: assertTrue(list.remove("2"));
0765: assertEquals("[3, 4, 3, 4]", list.toString());
0766: assertTrue(list.remove("3"));
0767: assertEquals("[4, 3, 4]", list.toString());
0768: assertTrue(list.remove("3"));
0769: assertEquals("[4, 4]", list.toString());
0770: assertTrue(list.remove("4"));
0771: assertEquals("[4]", list.toString());
0772: assertTrue(list.remove("4"));
0773: assertEquals("[]", list.toString());
0774: }
0775:
0776: public void testRetainAll() {
0777: list.add("1");
0778: list.add("1");
0779: list.add("2");
0780: list.add("2");
0781: list.add("3");
0782: list.add("3");
0783: list.add("4");
0784: list.add("4");
0785: list.add("5");
0786: list.add("5");
0787:
0788: HashSet set = new HashSet();
0789: set.add("A");
0790: set.add("2");
0791: set.add("C");
0792: set.add("4");
0793: set.add("D");
0794:
0795: assertTrue(list.retainAll(set));
0796: assertEquals("[2, 2, 4, 4]", list.toString());
0797: assertTrue(!list.retainAll(set));
0798: }
0799:
0800: public void testSet() {
0801: list.add("1");
0802: list.add("2");
0803: list.add("3");
0804: list.add("4");
0805: list.add("5");
0806: assertEquals("[1, 2, 3, 4, 5]", list.toString());
0807: list.set(0, "A");
0808: assertEquals("[A, 2, 3, 4, 5]", list.toString());
0809: list.set(1, "B");
0810: assertEquals("[A, B, 3, 4, 5]", list.toString());
0811: list.set(2, "C");
0812: assertEquals("[A, B, C, 4, 5]", list.toString());
0813: list.set(3, "D");
0814: assertEquals("[A, B, C, D, 5]", list.toString());
0815: list.set(4, "E");
0816: assertEquals("[A, B, C, D, E]", list.toString());
0817: }
0818:
0819: public void testSubList() {
0820: list.add("A");
0821: list.add("B");
0822: list.add("C");
0823: list.add("D");
0824: list.add("E");
0825:
0826: assertEquals("[A, B, C, D, E]", list.toString());
0827: assertEquals("[A, B, C, D, E]", list.subList(0, 5).toString());
0828: assertEquals("[B, C, D, E]", list.subList(1, 5).toString());
0829: assertEquals("[C, D, E]", list.subList(2, 5).toString());
0830: assertEquals("[D, E]", list.subList(3, 5).toString());
0831: assertEquals("[E]", list.subList(4, 5).toString());
0832: assertEquals("[]", list.subList(5, 5).toString());
0833: }
0834:
0835: public void testSubListAddEnd() {
0836: list.add("A");
0837: list.add("B");
0838: list.add("C");
0839: list.add("D");
0840: list.add("E");
0841:
0842: List sublist = list.subList(5, 5);
0843: sublist.add("F");
0844: assertEquals("[A, B, C, D, E, F]", list.toString());
0845: assertEquals("[F]", sublist.toString());
0846: sublist.add("G");
0847: assertEquals("[A, B, C, D, E, F, G]", list.toString());
0848: assertEquals("[F, G]", sublist.toString());
0849: }
0850:
0851: public void testSubListAddBegin() {
0852: list.add("A");
0853: list.add("B");
0854: list.add("C");
0855: list.add("D");
0856: list.add("E");
0857:
0858: List sublist = list.subList(0, 0);
0859: sublist.add("a");
0860: assertEquals("[a, A, B, C, D, E]", list.toString());
0861: assertEquals("[a]", sublist.toString());
0862: sublist.add("b");
0863: assertEquals("[a, b, A, B, C, D, E]", list.toString());
0864: assertEquals("[a, b]", sublist.toString());
0865: }
0866:
0867: public void testSubListAddMiddle() {
0868: list.add("A");
0869: list.add("B");
0870: list.add("C");
0871: list.add("D");
0872: list.add("E");
0873:
0874: List sublist = list.subList(1, 3);
0875: sublist.add("a");
0876: assertEquals("[A, B, C, a, D, E]", list.toString());
0877: assertEquals("[B, C, a]", sublist.toString());
0878: sublist.add("b");
0879: assertEquals("[A, B, C, a, b, D, E]", list.toString());
0880: assertEquals("[B, C, a, b]", sublist.toString());
0881: }
0882:
0883: public void testSubListRemove() {
0884: list.add("A");
0885: list.add("B");
0886: list.add("C");
0887: list.add("D");
0888: list.add("E");
0889:
0890: List sublist = list.subList(1, 4);
0891: assertEquals("[B, C, D]", sublist.toString());
0892: assertEquals("[A, B, C, D, E]", list.toString());
0893: sublist.remove("C");
0894: assertEquals("[B, D]", sublist.toString());
0895: assertEquals("[A, B, D, E]", list.toString());
0896: sublist.remove(1);
0897: assertEquals("[B]", sublist.toString());
0898: assertEquals("[A, B, E]", list.toString());
0899: sublist.clear();
0900: assertEquals("[]", sublist.toString());
0901: assertEquals("[A, E]", list.toString());
0902: }
0903:
0904: public void testToArray() {
0905: list.add("1");
0906: list.add("2");
0907: list.add("3");
0908: list.add("4");
0909: list.add("5");
0910:
0911: Object[] elts = list.toArray();
0912: assertEquals("1", elts[0]);
0913: assertEquals("2", elts[1]);
0914: assertEquals("3", elts[2]);
0915: assertEquals("4", elts[3]);
0916: assertEquals("5", elts[4]);
0917: assertEquals(5, elts.length);
0918:
0919: String[] elts2 = (String[]) (list.toArray(new String[0]));
0920: assertEquals("1", elts2[0]);
0921: assertEquals("2", elts2[1]);
0922: assertEquals("3", elts2[2]);
0923: assertEquals("4", elts2[3]);
0924: assertEquals("5", elts2[4]);
0925: assertEquals(5, elts2.length);
0926:
0927: String[] elts3 = new String[5];
0928: assertSame(elts3, list.toArray(elts3));
0929: assertEquals("1", elts3[0]);
0930: assertEquals("2", elts3[1]);
0931: assertEquals("3", elts3[2]);
0932: assertEquals("4", elts3[3]);
0933: assertEquals("5", elts3[4]);
0934: assertEquals(5, elts3.length);
0935:
0936: String[] elts4 = new String[3];
0937: String[] elts4b = (String[]) (list.toArray(elts4));
0938: assertTrue(elts4 != elts4b);
0939: assertEquals("1", elts4b[0]);
0940: assertEquals("2", elts4b[1]);
0941: assertEquals("3", elts4b[2]);
0942: assertEquals("4", elts4b[3]);
0943: assertEquals("5", elts4b[4]);
0944: assertEquals(5, elts4b.length);
0945: }
0946:
0947: public void testSerialization() throws Exception {
0948: list.add("A");
0949: list.add("B");
0950: list.add("C");
0951: list.add("D");
0952: list.add("E");
0953:
0954: java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
0955: java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(
0956: buf);
0957: out.writeObject(list);
0958: out.flush();
0959: out.close();
0960:
0961: java.io.ByteArrayInputStream bufin = new java.io.ByteArrayInputStream(
0962: buf.toByteArray());
0963: java.io.ObjectInputStream in = new java.io.ObjectInputStream(
0964: bufin);
0965: Object list2 = in.readObject();
0966:
0967: assertTrue(list != list2);
0968: assertTrue(list2.equals(list));
0969: assertTrue(list.equals(list2));
0970: }
0971:
0972: public void testSerializationWithOpenCursor() throws Exception {
0973: list.add("A");
0974: list.add("B");
0975: list.add("C");
0976: list.add("D");
0977: list.add("E");
0978: CursorableLinkedList.Cursor cursor = list.cursor();
0979:
0980: java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
0981: java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(
0982: buf);
0983: out.writeObject(list);
0984: out.flush();
0985: out.close();
0986:
0987: java.io.ByteArrayInputStream bufin = new java.io.ByteArrayInputStream(
0988: buf.toByteArray());
0989: java.io.ObjectInputStream in = new java.io.ObjectInputStream(
0990: bufin);
0991: Object list2 = in.readObject();
0992:
0993: assertTrue(list != list2);
0994: assertTrue(list2.equals(list));
0995: assertTrue(list.equals(list2));
0996: }
0997:
0998: public void testLongSerialization() throws Exception {
0999: // recursive serialization will cause a stack
1000: // overflow exception with long lists
1001: for (int i = 0; i < 10000; i++) {
1002: list.add(new Integer(i));
1003: }
1004:
1005: java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
1006: java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(
1007: buf);
1008: out.writeObject(list);
1009: out.flush();
1010: out.close();
1011:
1012: java.io.ByteArrayInputStream bufin = new java.io.ByteArrayInputStream(
1013: buf.toByteArray());
1014: java.io.ObjectInputStream in = new java.io.ObjectInputStream(
1015: bufin);
1016: Object list2 = in.readObject();
1017:
1018: assertTrue(list != list2);
1019: assertTrue(list2.equals(list));
1020: assertTrue(list.equals(list2));
1021: }
1022:
1023: /**
1024: * Ignore the serialization tests for sublists and sub-sublists.
1025: *
1026: * @return an array of sublist serialization test names
1027: */
1028: public String[] ignoredTests() {
1029: ArrayList list = new ArrayList();
1030: String prefix = "TestCursorableLinkedList";
1031: String bulk = ".bulkTestSubList";
1032: String[] ignored = new String[] {
1033: ".testEmptyListSerialization",
1034: ".testFullListSerialization",
1035: ".testEmptyListCompatibility",
1036: ".testFullListCompatibility",
1037: ".testSimpleSerialization",
1038: ".testCanonicalEmptyCollectionExists",
1039: ".testCanonicalFullCollectionExists",
1040: ".testSerializeDeserializeThenCompare" };
1041: for (int i = 0; i < ignored.length; i++) {
1042: list.add(prefix + bulk + ignored[i]);
1043: list.add(prefix + bulk + bulk + ignored[i]);
1044: }
1045: return (String[]) list.toArray(new String[0]);
1046: }
1047:
1048: }
|