KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > ozoneDB > DxLib > CollectionTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8

9 package test.ozoneDB.DxLib;
10
11 import org.ozoneDB.DxLib.*;
12
13 /**
14  *
15  *
16  * @author <a HREF="http://www.softwarebuero.de/">SMB</a>
17  * @version $Revision$Date$
18  */

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 JavaDoc name) {
28         super(name);
29     }
30
31     public void testCollections() throws Exception JavaDoc {
32         Class JavaDoc[] 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         // add_iterate
39
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         // contains
46
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         // containsAll
53
for (int i = 0; i < factories.length; i++) {
54             collTest.containsAll((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings());
55         }
56
57         // remove_count_isEmpty
58
for (int i = 0; i < factories.length; i++) {
59             collTest.remove_count_isEmpty((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings());
60         }
61
62         // removeAll
63
for (int i = 0; i < factories.length; i++) {
64             collTest.removeAll((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings());
65         }
66
67         // equals
68
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         // toArray_clone_clear
74
for (int i = 0; i < factories.length; i++) {
75             collTest.toArray_clone_clear((DxCollection) factories[i].newInstance(), CollectionTest.newDxStrings());
76         }
77     }
78
79     /**
80      */

81     public static Object JavaDoc[] newStrings() {
82         Object JavaDoc[] objs = new Object JavaDoc[SIZE];
83         for (int i = 0; i < SIZE; i++) {
84             objs[i] = String.valueOf(i);
85         }
86         return objs;
87     }
88
89
90     /**
91      */

92     public static Object JavaDoc[] newIntegers() {
93         Object JavaDoc[] objs = new Object JavaDoc[SIZE];
94         for (int i = 0; i < SIZE; i++) {
95             objs[i] = new Integer JavaDoc(i);
96         }
97         return objs;
98     }
99
100
101     /**
102      */

103     public static Object JavaDoc[] newDxStrings() {
104         Object JavaDoc[] objs = new Object JavaDoc[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     /**
113      */

114     public void add_iterate(DxCollection coll, Object JavaDoc[] objs) throws Exception JavaDoc {
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 JavaDoc 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     /**
137      */

138     public void contains(DxCollection coll, Object JavaDoc[] objs) throws Exception JavaDoc {
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     /**
152      */

153     public void containsAll(DxCollection coll, Object JavaDoc[] objs) throws Exception JavaDoc {
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     /**
174      */

175     public void removeAll(DxCollection coll, Object JavaDoc[] objs) throws Exception JavaDoc {
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     /**
193      */

194     public void remove_count_isEmpty(DxCollection coll, Object JavaDoc[] objs) throws Exception JavaDoc {
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     /**
223      */

224     public void equals(DxCollection coll1, DxCollection coll2, Object JavaDoc[] objs) throws Exception JavaDoc {
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     /**
242      */

243     public void toArray_clone_clear(DxCollection coll, Object JavaDoc[] 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 JavaDoc[] 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