1 16 package org.apache.commons.collections.list; 17 18 import java.util.List ; 19 20 import junit.framework.Test; 21 22 import org.apache.commons.collections.BulkTest; 23 24 32 public class TestTreeList extends AbstractTestList { 33 34 public TestTreeList(String name) { 35 super(name); 36 } 37 38 public static void main(String [] args) { 39 junit.textui.TestRunner.run(suite()); 40 } 49 50 public static Test suite() { 51 return BulkTest.makeSuite(TestTreeList.class); 52 } 53 54 public static void benchmark(List l) { 55 StringBuffer sb = new StringBuffer (); 56 long start = System.currentTimeMillis(); 57 for (int i = 0; i < 100000; i++) { 58 l.add(new Integer (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 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 (-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 (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 public List makeEmptyList() { 108 return new TreeList(); 109 } 110 111 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 } 214 | Popular Tags |