1 9 package test.ozoneDB.DxLib; 10 11 import org.ozoneDB.DxLib.*; 12 13 19 public class CollectionTest extends AbstractTest { 20 21 final static int SIZE = 2000; 22 23 public CollectionTest() { 24 super("testCollections"); 25 } 26 27 public CollectionTest(String name) { 28 super(name); 29 } 30 31 public void testCollections() throws Exception { 32 Class [] factories = 33 {DxListBag.class, DxArrayBag.class, DxHashSet.class, DxTreeSet.class, DxHashMap.class, DxTreeMap.class}; 34 35 CollectionTest collTest = new CollectionTest(); 36 System.out.println("\n*** CollectionTest ***\n"); 37 38 for (int i = 0; i < factories.length; i++) { 40 collTest.add_iterate((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings()); 41 } 42 collTest.add_iterate(new DxTreeSet(new DxStringComparator()), CollectionTest.newStrings()); 43 collTest.add_iterate(new DxTreeMap(new DxStringComparator()), CollectionTest.newStrings()); 44 45 for (int i = 0; i < factories.length; i++) { 47 collTest.contains((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings()); 48 } 49 collTest.contains(new DxTreeSet(new DxStringComparator()), CollectionTest.newStrings()); 50 collTest.contains(new DxTreeMap(new DxStringComparator()), CollectionTest.newStrings()); 51 52 for (int i = 0; i < factories.length; i++) { 54 collTest.containsAll((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings()); 55 } 56 57 for (int i = 0; i < factories.length; i++) { 59 collTest.remove_count_isEmpty((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings()); 60 } 61 62 for (int i = 0; i < factories.length; i++) { 64 collTest.removeAll((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings()); 65 } 66 67 for (int i = 0; i < factories.length; i++) { 69 collTest.equals((DxCollection) factories[i].newInstance(), (DxCollection) factories[i].newInstance(), 70 CollectionTest.newDxStrings()); 71 } 72 73 for (int i = 0; i < factories.length; i++) { 75 collTest.toArray_clone_clear((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings()); 76 } 77 } 78 79 81 public static Object [] newStrings() { 82 Object [] objs = new Object [SIZE]; 83 for (int i = 0; i < SIZE; i++) { 84 objs[i] = String.valueOf(i); 85 } 86 return objs; 87 } 88 89 90 92 public static Object [] newIntegers() { 93 Object [] objs = new Object [SIZE]; 94 for (int i = 0; i < SIZE; i++) { 95 objs[i] = new Integer (i); 96 } 97 return objs; 98 } 99 100 101 103 public static Object [] newDxStrings() { 104 Object [] objs = new Object [SIZE]; 105 for (int i = 0; i < SIZE; i++) { 106 objs[i] = new DxString(String.valueOf(i)); 107 } 108 return objs; 109 } 110 111 112 114 public void add_iterate(DxCollection coll, Object [] objs) throws Exception { 115 startTimer(coll.getClass().getName(), "add_iterate"); 116 for (int i = 0; i < objs.length; i++) { 117 coll.add(objs[i]); 118 } 119 stopTimer(); 120 121 DxIterator it = coll.iterator(); 122 while (it.next() != null) { 123 boolean found = false; 124 Object obj = it.object(); 125 for (int j = 0; j < SIZE; j++) { 126 if (obj == objs[j]) { 127 found = true; 128 } 129 } 130 assertTrue("iterating through collection looking for added object", found); 131 } 132 stopTimer(); 133 } 134 135 136 138 public void contains(DxCollection coll, Object [] objs) throws Exception { 139 for (int i = 0; i < objs.length; i++) { 140 coll.add(objs[i]); 141 } 142 startTimer(coll.getClass().getName(), "contains"); 143 144 for (int i = 0; i < SIZE; i++) { 145 assertTrue("contains should report existance of added object", coll.contains(objs[i])); 146 } 147 stopTimer(); 148 } 149 150 151 153 public void containsAll(DxCollection coll, Object [] objs) throws Exception { 154 for (int i = 0; i < objs.length; i++) { 155 coll.add(objs[i]); 156 } 157 DxBag bag = new DxArrayBag(); 158 bag.addAll(objs); 159 startTimer(coll.getClass().getName(), "containsAll"); 160 161 assertTrue("contains all should report true for an added collection", coll.containsAll(bag)); 162 163 assertTrue("contains all should report true for an empty collection", coll.containsAll(new DxArrayBag())); 164 165 DxBag emptyBag = new DxArrayBag(); 166 167 assertFalse("contains all should report false for an empty collection coparing with a populated one", emptyBag.containsAll(coll)); 168 169 stopTimer(); 170 } 171 172 173 175 public void removeAll(DxCollection coll, Object [] objs) throws Exception { 176 coll.addAll(objs); 177 startTimer(coll.getClass().getName(), "removeAll"); 178 179 DxBag bag = new DxArrayBag(); 180 bag.addAll(objs); 181 182 coll.removeAll(bag); 183 184 assertFalse("after removeAll there should be no objects", coll.contains(objs[0])); 185 assertTrue("after removeAll collection should be empty", coll.isEmpty()); 186 assertEquals("after removeAll number of elements should be 0", coll.count(), 0); 187 188 stopTimer(); 189 } 190 191 192 194 public void remove_count_isEmpty(DxCollection coll, Object [] objs) throws Exception { 195 startTimer(coll.getClass().getName(), "remove_count_isEmpty"); 196 assertTrue(coll.isEmpty()); 197 assertEquals(coll.count(),0); 198 199 coll.addAll(objs); 200 201 coll.remove(objs[0]); 202 coll.remove(objs[2]); 203 coll.remove (objs[50]); 204 205 206 assertEquals(coll.count(),(SIZE - 3)); 207 208 assertFalse(coll.isEmpty()); 209 210 assertFalse(coll.contains(objs[0])); 211 212 assertFalse(coll.contains(objs[2])); 213 214 assertFalse(coll.contains(objs[50])); 215 216 assertTrue(coll.contains(objs[1])); 217 218 stopTimer(); 219 } 220 221 222 224 public void equals(DxCollection coll1, DxCollection coll2, Object [] objs) throws Exception { 225 coll1.addAll(objs); 226 assertFalse(coll1.equals(coll2)); 227 228 coll2.addAll(objs); 229 startTimer(coll1.getClass().getName(), "equals"); 230 231 assertEquals(coll1,coll2); 232 233 coll2.remove(objs[0]); 234 235 assertFalse(coll1.equals(coll2)); 236 237 stopTimer(); 238 } 239 240 241 243 public void toArray_clone_clear(DxCollection coll, Object [] objs) { 244 coll.addAll(objs); 245 DxCollection clone = (DxCollection) coll.clone(); 246 assertEquals(coll, clone); 247 248 startTimer(coll.getClass().getName(), "toArray"); 249 250 Object [] array = coll.toArray(); 251 stopTimer(); 252 253 coll.clear(); 254 assertFalse(coll.equals(clone)); 255 256 coll.addAll(array); 257 assertEquals(coll, clone); 258 259 } 260 261 } 262 | Popular Tags |