KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > list > TestTreeList


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.list;
17
18 import java.util.List JavaDoc;
19
20 import junit.framework.Test;
21
22 import org.apache.commons.collections.BulkTest;
23
24 /**
25  * JUnit tests
26  *
27  * @since Commons Collections 3.1
28  * @version $Revision: 1.2 $ $Date: 2004/05/12 23:24:44 $
29  *
30  * @author Joerg Schmuecker
31  */

32 public class TestTreeList extends AbstractTestList {
33     
34     public TestTreeList(String JavaDoc name) {
35         super(name);
36     }
37
38     public static void main(String JavaDoc[] args) {
39         junit.textui.TestRunner.run(suite());
40 // System.out.println(" add; toArray; iterator; insert; get; indexOf; remove");
41
// System.out.print(" TreeList = ");
42
// benchmark(new TreeList());
43
// System.out.print("\n ArrayList = ");
44
// benchmark(new java.util.ArrayList());
45
// System.out.print("\n LinkedList = ");
46
// benchmark(new java.util.LinkedList());
47
// benchmark(new NodeCachingLinkedList());
48
}
49
50     public static Test suite() {
51         return BulkTest.makeSuite(TestTreeList.class);
52     }
53
54     public static void benchmark(List JavaDoc l) {
55         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
56         long start = System.currentTimeMillis();
57         for (int i = 0; i < 100000; i++) {
58             l.add(new Integer JavaDoc(i));
59         }
60         System.out.print(System.currentTimeMillis() - start + ";");
61         
62         start = System.currentTimeMillis();
63         for (int i = 0; i < 200; i++) {
64             l.toArray();
65         }
66         System.out.print(System.currentTimeMillis() - start + ";");
67         
68         start = System.currentTimeMillis();
69         for (int i = 0; i < 100; i++) {
70             java.util.Iterator JavaDoc it = l.iterator();
71             while (it.hasNext()) {
72                 it.next();
73             }
74         }
75         System.out.print(System.currentTimeMillis() - start + ";");
76         
77         start = System.currentTimeMillis();
78         for (int i = 0; i < 10000; i++) {
79             int j = (int) (Math.random() * 100000);
80             l.add(j, new Integer JavaDoc(-j));
81         }
82         System.out.print(System.currentTimeMillis() - start + ";");
83         
84         start = System.currentTimeMillis();
85         for (int i = 0; i < 50000; i++) {
86             int j = (int) (Math.random() * 110000);
87             l.get(j);
88         }
89         System.out.print(System.currentTimeMillis() - start + ";");
90         
91         start = System.currentTimeMillis();
92         for (int i = 0; i < 200; i++) {
93             int j = (int) (Math.random() * 100000);
94             l.indexOf(new Integer JavaDoc(j));
95         }
96         System.out.print(System.currentTimeMillis() - start + ";");
97         
98         start = System.currentTimeMillis();
99         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 JavaDoc makeEmptyList() {
108         return new TreeList();
109     }
110
111     //-----------------------------------------------------------------------
112
public void testAddMultiple() {
113         List JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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
Popular Tags