001: /*
002: * Copyright 2004,2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.list;
017:
018: import java.util.List;
019: import java.util.ListIterator;
020:
021: import junit.framework.Test;
022:
023: import org.apache.commons.collections.BulkTest;
024:
025: /**
026: * JUnit tests
027: *
028: * @since Commons Collections 3.1
029: * @version $Revision: 370952 $ $Date: 2006-01-21 01:49:21 +0000 (Sat, 21 Jan 2006) $
030: *
031: * @author Joerg Schmuecker
032: */
033: public class TestTreeList extends AbstractTestList {
034:
035: public TestTreeList(String name) {
036: super (name);
037: }
038:
039: public static void main(String[] args) {
040: junit.textui.TestRunner.run(suite());
041: // System.out.println(" add; toArray; iterator; insert; get; indexOf; remove");
042: // System.out.print(" TreeList = ");
043: // benchmark(new TreeList());
044: // System.out.print("\n ArrayList = ");
045: // benchmark(new java.util.ArrayList());
046: // System.out.print("\n LinkedList = ");
047: // benchmark(new java.util.LinkedList());
048: // benchmark(new NodeCachingLinkedList());
049: }
050:
051: public static Test suite() {
052: return BulkTest.makeSuite(TestTreeList.class);
053: }
054:
055: public static void benchmark(List l) {
056: long start = System.currentTimeMillis();
057: for (int i = 0; i < 100000; i++) {
058: l.add(new Integer(i));
059: }
060: System.out.print(System.currentTimeMillis() - start + ";");
061:
062: start = System.currentTimeMillis();
063: for (int i = 0; i < 200; i++) {
064: l.toArray();
065: }
066: System.out.print(System.currentTimeMillis() - start + ";");
067:
068: start = System.currentTimeMillis();
069: for (int i = 0; i < 100; i++) {
070: java.util.Iterator it = l.iterator();
071: while (it.hasNext()) {
072: it.next();
073: }
074: }
075: System.out.print(System.currentTimeMillis() - start + ";");
076:
077: start = System.currentTimeMillis();
078: for (int i = 0; i < 10000; i++) {
079: int j = (int) (Math.random() * 100000);
080: l.add(j, new Integer(-j));
081: }
082: System.out.print(System.currentTimeMillis() - start + ";");
083:
084: start = System.currentTimeMillis();
085: for (int i = 0; i < 50000; i++) {
086: int j = (int) (Math.random() * 110000);
087: l.get(j);
088: }
089: System.out.print(System.currentTimeMillis() - start + ";");
090:
091: start = System.currentTimeMillis();
092: for (int i = 0; i < 200; i++) {
093: int j = (int) (Math.random() * 100000);
094: l.indexOf(new Integer(j));
095: }
096: System.out.print(System.currentTimeMillis() - start + ";");
097:
098: start = System.currentTimeMillis();
099: for (int i = 0; i < 10000; i++) {
100: int j = (int) (Math.random() * 100000);
101: l.remove(j);
102: }
103: System.out.print(System.currentTimeMillis() - start + ";");
104: }
105:
106: //-----------------------------------------------------------------------
107: public List makeEmptyList() {
108: return new TreeList();
109: }
110:
111: //-----------------------------------------------------------------------
112: public void testAddMultiple() {
113: List l = makeEmptyList();
114: l.add("hugo");
115: l.add("erna");
116: l.add("daniel");
117: l.add("andres");
118: l.add("harald");
119: l.add(0, null);
120: assertEquals(null, l.get(0));
121: assertEquals("hugo", l.get(1));
122: assertEquals("erna", l.get(2));
123: assertEquals("daniel", l.get(3));
124: assertEquals("andres", l.get(4));
125: assertEquals("harald", l.get(5));
126: }
127:
128: public void testRemove() {
129: List l = makeEmptyList();
130: l.add("hugo");
131: l.add("erna");
132: l.add("daniel");
133: l.add("andres");
134: l.add("harald");
135: l.add(0, null);
136: int i = 0;
137: assertEquals(null, l.get(i++));
138: assertEquals("hugo", l.get(i++));
139: assertEquals("erna", l.get(i++));
140: assertEquals("daniel", l.get(i++));
141: assertEquals("andres", l.get(i++));
142: assertEquals("harald", l.get(i++));
143:
144: l.remove(0);
145: i = 0;
146: assertEquals("hugo", l.get(i++));
147: assertEquals("erna", l.get(i++));
148: assertEquals("daniel", l.get(i++));
149: assertEquals("andres", l.get(i++));
150: assertEquals("harald", l.get(i++));
151:
152: i = 0;
153: l.remove(1);
154: assertEquals("hugo", l.get(i++));
155: assertEquals("daniel", l.get(i++));
156: assertEquals("andres", l.get(i++));
157: assertEquals("harald", l.get(i++));
158:
159: i = 0;
160: l.remove(2);
161: assertEquals("hugo", l.get(i++));
162: assertEquals("daniel", l.get(i++));
163: assertEquals("harald", l.get(i++));
164: }
165:
166: public void testInsertBefore() {
167: List l = makeEmptyList();
168: l.add("erna");
169: l.add(0, "hugo");
170: assertEquals("hugo", l.get(0));
171: assertEquals("erna", l.get(1));
172: }
173:
174: public void testIndexOf() {
175: List l = makeEmptyList();
176: l.add("0");
177: l.add("1");
178: l.add("2");
179: l.add("3");
180: l.add("4");
181: l.add("5");
182: l.add("6");
183: assertEquals(0, l.indexOf("0"));
184: assertEquals(1, l.indexOf("1"));
185: assertEquals(2, l.indexOf("2"));
186: assertEquals(3, l.indexOf("3"));
187: assertEquals(4, l.indexOf("4"));
188: assertEquals(5, l.indexOf("5"));
189: assertEquals(6, l.indexOf("6"));
190:
191: l.set(1, "0");
192: assertEquals(0, l.indexOf("0"));
193:
194: l.set(3, "3");
195: assertEquals(3, l.indexOf("3"));
196: l.set(2, "3");
197: assertEquals(2, l.indexOf("3"));
198: l.set(1, "3");
199: assertEquals(1, l.indexOf("3"));
200: l.set(0, "3");
201: assertEquals(0, l.indexOf("3"));
202: }
203:
204: // public void testCheck() {
205: // List l = makeEmptyList();
206: // l.add("A1");
207: // l.add("A2");
208: // l.add("A3");
209: // l.add("A4");
210: // l.add("A5");
211: // l.add("A6");
212: // }
213:
214: public void testBug35258() {
215: Object objectToRemove = new Integer(3);
216:
217: List treelist = new TreeList();
218: treelist.add(new Integer(0));
219: treelist.add(new Integer(1));
220: treelist.add(new Integer(2));
221: treelist.add(new Integer(3));
222: treelist.add(new Integer(4));
223:
224: // this cause inconsistence of ListIterator()
225: treelist.remove(objectToRemove);
226:
227: ListIterator li = treelist.listIterator();
228: assertEquals(new Integer(0), li.next());
229: assertEquals(new Integer(0), li.previous());
230: assertEquals(new Integer(0), li.next());
231: assertEquals(new Integer(1), li.next());
232: // this caused error in bug 35258
233: assertEquals(new Integer(1), li.previous());
234: assertEquals(new Integer(1), li.next());
235: assertEquals(new Integer(2), li.next());
236: assertEquals(new Integer(2), li.previous());
237: assertEquals(new Integer(2), li.next());
238: assertEquals(new Integer(4), li.next());
239: assertEquals(new Integer(4), li.previous());
240: assertEquals(new Integer(4), li.next());
241: assertEquals(false, li.hasNext());
242: }
243:
244: }
|