001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.jre12.collections.facades;
022:
023: import java.util.*;
024:
025: import com.db4o.collections.facades.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class FastListTestCaseBase extends AbstractDb4oTestCase {
031:
032: public FastList _list;
033:
034: private static int CAPACITY = 100;
035:
036: protected void init(FastList list) {
037: _list = list;
038: for (int i = 0; i < CAPACITY; i++) {
039: _list.add(new Integer(i));
040: }
041: }
042:
043: public void testAdd() throws Exception {
044: for (int i = 0; i < CAPACITY; ++i) {
045: _list.add(new Integer(CAPACITY + i));
046: }
047:
048: for (int i = 0; i < CAPACITY * 2; ++i) {
049: Assert.areEqual(new Integer(i), _list.get(i));
050: }
051: }
052:
053: public void testAdd_LObject() throws Exception {
054: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
055: public void run() throws Throwable {
056: _list.add(-1, new Integer(0));
057: }
058: });
059:
060: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
061: public void run() throws Throwable {
062: _list.add(CAPACITY + 1, new Integer(0));
063: }
064: });
065:
066: Integer i1 = new Integer(0);
067: _list.add(0, i1);
068: // elements: 0, 0,1 - 100
069: // index: 0, 1,2 - 101
070: Assert.areSame(i1, _list.get(0));
071:
072: for (int i = 1; i < CAPACITY + 1; ++i) {
073: Assert.areEqual(new Integer(i - 1), _list.get(i));
074: }
075:
076: Integer i2 = new Integer(42);
077: _list.add(42, i2);
078: // elements: 0, 0,1 - 42, 42, 43 - 100
079: // index: 0, 1,2 - 43, 44, 45 - 102
080: for (int i = 1; i < 42; ++i) {
081: Assert.areEqual(new Integer(i - 1), _list.get(i));
082: }
083:
084: Assert.areSame(i2, _list.get(42));
085: Assert.areEqual(new Integer(41), _list.get(43));
086:
087: for (int i = 44; i < CAPACITY + 2; ++i) {
088: Assert.areEqual(new Integer(i - 2), _list.get(i));
089: }
090: }
091:
092: public void testAddAll_LCollection() throws Exception {
093: final Vector v = new Vector();
094: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
095: public void run() throws Throwable {
096: _list.addAll(-1, v);
097: }
098: });
099:
100: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
101: public void run() throws Throwable {
102: _list.addAll(CAPACITY + 1, v);
103: }
104: });
105:
106: for (int i = 0; i < CAPACITY; ++i) {
107: v.add(new Integer(CAPACITY + i));
108: }
109:
110: _list.addAll(v);
111:
112: for (int i = 0; i < CAPACITY * 2; ++i) {
113: Assert.areEqual(new Integer(i), _list.get(i));
114: }
115: }
116:
117: public void testAddAll_ILCollection() throws Exception {
118: final Vector v = new Vector();
119: final int INDEX = 42;
120:
121: for (int i = 0; i < CAPACITY; ++i) {
122: v.add(new Integer(CAPACITY + i));
123: }
124:
125: _list.addAll(INDEX, v);
126: // elements: 0 - 41, 100 - 199, 42 - 100
127: // index: 0 - 41, 42 - 141, 142 - 200
128: for (int i = 0; i < INDEX; ++i) {
129: Assert.areEqual(new Integer(i), _list.get(i));
130: }
131:
132: for (int i = INDEX, j = 0; j < CAPACITY; ++i, ++j) {
133: Assert.areEqual(new Integer(CAPACITY + j), _list.get(i));
134: }
135:
136: for (int i = INDEX + CAPACITY; i < CAPACITY * 2; ++i) {
137: Assert.areEqual(new Integer(i - CAPACITY), _list.get(i));
138: }
139:
140: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
141: public void run() throws Throwable {
142: _list.addAll(-1, v);
143: }
144: });
145: }
146:
147: public void testClear() throws Exception {
148: _list.clear();
149: Assert.areEqual(0, _list.size());
150: }
151:
152: public void testContains() throws Exception {
153: Assert.isTrue(_list.contains(new Integer(0)));
154: Assert.isTrue(_list.contains(new Integer(CAPACITY / 2)));
155: Assert.isTrue(_list.contains(new Integer(CAPACITY / 3)));
156: Assert.isTrue(_list.contains(new Integer(CAPACITY / 4)));
157:
158: Assert.isFalse(_list.contains(new Integer(-1)));
159: Assert.isFalse(_list.contains(new Integer(CAPACITY)));
160:
161: // returns false because current data doesn't contain null.
162: // Quotes from j.u.List spec: More formally, returns true if and only if
163: // this list contains at least one element e such that (o==null ?
164: // e==null : o.equals(e)).
165: Assert.isFalse(_list.contains(null));
166: }
167:
168: public void testContainsAll() throws Exception {
169: Vector v = new Vector();
170:
171: v.add(new Integer(0));
172: Assert.isTrue(_list.containsAll(v));
173:
174: v.add(new Integer(0));
175: Assert.isTrue(_list.containsAll(v));
176:
177: v.add(new Integer(CAPACITY / 2));
178: Assert.isTrue(_list.containsAll(v));
179:
180: v.add(new Integer(CAPACITY / 3));
181: Assert.isTrue(_list.containsAll(v));
182:
183: v.add(new Integer(CAPACITY / 4));
184: Assert.isTrue(_list.containsAll(v));
185:
186: v.add(new Integer(CAPACITY));
187: Assert.isFalse(_list.containsAll(v));
188: }
189:
190: public void testGet() throws Exception {
191: for (int i = 0; i < CAPACITY; ++i) {
192: Assert.areEqual(new Integer(i), _list.get(i));
193: }
194:
195: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
196: public void run() throws Throwable {
197: _list.get(-1);
198: }
199: });
200:
201: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
202: public void run() throws Throwable {
203: _list.get(CAPACITY);
204: }
205: });
206: }
207:
208: public void testIndexOf() throws Exception {
209: Assert.areEqual(0, _list.indexOf(new Integer(0)));
210: Assert.areEqual(CAPACITY / 2, _list.indexOf(new Integer(
211: CAPACITY / 2)));
212: Assert.areEqual(CAPACITY / 3, _list.indexOf(new Integer(
213: CAPACITY / 3)));
214: Assert.areEqual(CAPACITY / 4, _list.indexOf(new Integer(
215: CAPACITY / 4)));
216:
217: Assert.areEqual(-1, _list.indexOf(new Integer(-1)));
218: Assert.areEqual(-1, _list.indexOf(new Integer(CAPACITY)));
219:
220: // returns false because current data doesn't contain null.
221: // Quotes from j.u.List spec: More formally, returns the lowest index i
222: // such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there
223: // is no such index.
224: Assert.areEqual(-1, _list.indexOf(null));
225: }
226:
227: public void testIsEmpty() throws Exception {
228: Assert.isFalse(_list.isEmpty());
229: _list.clear();
230: Assert.isTrue(_list.isEmpty());
231: }
232:
233: public void testIterator() throws Exception {
234: Iterator iter = _list.iterator();
235: int count = 0;
236: while (iter.hasNext()) {
237: Integer i = (Integer) iter.next();
238: Assert.areEqual(count, i.intValue());
239: ++count;
240: }
241: Assert.areEqual(CAPACITY, count);
242:
243: _list.clear();
244: iter = _list.iterator();
245: Assert.isFalse(iter.hasNext());
246: }
247:
248: public void testLastIndexOf() throws Exception {
249: Assert.areEqual(0, _list.indexOf(new Integer(0)));
250: Assert.areEqual(CAPACITY / 2, _list.lastIndexOf(new Integer(
251: CAPACITY / 2)));
252: Assert.areEqual(CAPACITY / 3, _list.lastIndexOf(new Integer(
253: CAPACITY / 3)));
254: Assert.areEqual(CAPACITY / 4, _list.lastIndexOf(new Integer(
255: CAPACITY / 4)));
256:
257: Assert.areEqual(-1, _list.lastIndexOf(new Integer(-1)));
258: Assert.areEqual(-1, _list.lastIndexOf(new Integer(CAPACITY)));
259:
260: // returns false because current data doesn't contain null.
261: // Quotes from j.u.List spec: More formally, returns the lowest index i
262: // such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there
263: // is no such index.
264: Assert.areEqual(-1, _list.lastIndexOf(null));
265:
266: _list.add(new Integer(0));
267: _list.add(new Integer(CAPACITY / 2));
268: _list.add(new Integer(CAPACITY / 3));
269: _list.add(new Integer(CAPACITY / 4));
270:
271: Assert.areEqual(CAPACITY, _list.lastIndexOf(new Integer(0)));
272: Assert.areEqual(CAPACITY + 1, _list.lastIndexOf(new Integer(
273: CAPACITY / 2)));
274: Assert.areEqual(CAPACITY + 2, _list.lastIndexOf(new Integer(
275: CAPACITY / 3)));
276: Assert.areEqual(CAPACITY + 3, _list.lastIndexOf(new Integer(
277: CAPACITY / 4)));
278:
279: Assert.areEqual(-1, _list.lastIndexOf(new Integer(-1)));
280: Assert.areEqual(-1, _list.lastIndexOf(new Integer(CAPACITY)));
281:
282: // returns false because current data doesn't contain null.
283: // Quotes from j.u.List spec: More formally, returns the lowest index i
284: // such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there
285: // is no such index.
286: Assert.areEqual(-1, _list.lastIndexOf(null));
287: }
288:
289: public void testRemove_Object() throws Exception {
290: _list.remove(new Integer(0));
291: Assert.areEqual(new Integer(1), _list.get(0));
292:
293: Assert.areEqual(CAPACITY - 1, _list.size());
294:
295: _list.remove(new Integer(43));
296: Assert.areEqual(new Integer(44), _list.get(42));
297: Assert.areEqual(new Integer(42), _list.get(41));
298: Assert.areEqual(CAPACITY - 2, _list.size());
299:
300: for (int i = 0; i < CAPACITY - 2; ++i) {
301: _list.remove(_list.get(0));
302: Assert.areEqual(CAPACITY - 3 - i, _list.size());
303: }
304: Assert.isTrue(_list.isEmpty());
305: }
306:
307: public void testRemove_I() throws Exception {
308: _list.remove(0);
309: Assert.areEqual(new Integer(1), _list.get(0));
310: Assert.isFalse(_list.contains(new Integer(0)));
311: Assert.areEqual(CAPACITY - 1, _list.size());
312:
313: _list.remove(42);
314: Assert.areEqual(new Integer(44), _list.get(42));
315: Assert.areEqual(new Integer(42), _list.get(41));
316: Assert.isFalse(_list.contains(new Integer(43)));
317: Assert.areEqual(CAPACITY - 2, _list.size());
318:
319: for (int i = 0; i < CAPACITY - 2; ++i) {
320: _list.remove(0);
321: Assert.areEqual(CAPACITY - 3 - i, _list.size());
322: }
323: Assert.isTrue(_list.isEmpty());
324: }
325:
326: public void testRemoveAll() throws Exception {
327: Vector v = new Vector();
328:
329: _list.removeAll(v);
330: Assert.areEqual(CAPACITY, _list.size());
331:
332: v.add(new Integer(0));
333: v.add(new Integer(42));
334: _list.removeAll(v);
335: Assert.isFalse(_list.contains(new Integer(0)));
336: Assert.isFalse(_list.contains(new Integer(42)));
337: Assert.areEqual(CAPACITY - 2, _list.size());
338:
339: v.add(new Integer(1));
340: v.add(new Integer(2));
341: _list.removeAll(v);
342: Assert.isFalse(_list.contains(new Integer(1)));
343: Assert.isFalse(_list.contains(new Integer(2)));
344: Assert.areEqual(CAPACITY - 4, _list.size());
345:
346: for (int i = 0; i < CAPACITY; ++i) {
347: v.add(new Integer(i));
348: }
349: _list.removeAll(v);
350: Assert.isTrue(_list.isEmpty());
351: }
352:
353: public void testRetainAll() throws Exception {
354: Vector v = new Vector();
355: v.add(new Integer(0));
356: v.add(new Integer(42));
357:
358: boolean ret = _list.retainAll(_list);
359: Assert.isFalse(ret);
360: Assert.areEqual(100, _list.size());
361: for (int i = 0; i < CAPACITY; ++i) {
362: Assert.isTrue(_list.contains(new Integer(i)));
363: }
364:
365: ret = _list.retainAll(v);
366: Assert.isTrue(ret);
367: Assert.areEqual(2, _list.size());
368: _list.contains(new Integer(0));
369: _list.contains(new Integer(42));
370:
371: ret = _list.retainAll(v);
372: Assert.isFalse(ret);
373: _list.contains(new Integer(0));
374: _list.contains(new Integer(42));
375: }
376:
377: public void testSet() throws Exception {
378: Integer element = new Integer(1);
379: _list.set(0, element);
380: Assert.areSame(element, _list.get(0));
381:
382: _list.set(42, element);
383: Assert.areSame(element, _list.get(42));
384:
385: for (int i = 0; i < CAPACITY; ++i) {
386: element = new Integer(i);
387: _list.set(i, element);
388: Assert.areSame(element, _list.get(i));
389: }
390:
391: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
392: public void run() throws Throwable {
393: _list.set(-1, new Integer(0));
394: }
395: });
396:
397: Assert.expect(IndexOutOfBoundsException.class, new CodeBlock() {
398: public void run() throws Throwable {
399: _list.set(CAPACITY, new Integer(0));
400: }
401: });
402: }
403:
404: public void testSize() throws Exception {
405: Assert.areEqual(CAPACITY, _list.size());
406: for (int i = 0; i < CAPACITY; ++i) {
407: _list.remove(0);
408: Assert.areEqual(CAPACITY - 1 - i, _list.size());
409: }
410: for (int i = 0; i < CAPACITY; ++i) {
411: _list.add(new Integer(i));
412: Assert.areEqual(i + 1, _list.size());
413: }
414: }
415:
416: public void testToArray() throws Exception {
417: Object[] array = _list.toArray();
418: Assert.areEqual(CAPACITY, array.length);
419: for (int i = 0; i < CAPACITY; ++i) {
420: Integer element = (Integer) array[i];
421: Assert.areEqual(new Integer(i), element);
422: }
423:
424: _list.clear();
425: array = _list.toArray();
426: Assert.areEqual(0, array.length);
427: }
428:
429: public void testToArray_LObject() throws Exception {
430: Object[] array1;
431: Object[] array2 = new Object[CAPACITY];
432: array1 = _list.toArray(array2);
433: Assert.areSame(array1, array2);
434: Assert.areEqual(CAPACITY, array2.length);
435: for (int i = 0; i < CAPACITY; ++i) {
436: Integer element = (Integer) array2[i];
437: Assert.areEqual(new Integer(i), element);
438: }
439:
440: _list.clear();
441:
442: array1 = new Object[0];
443: array2 = new Object[CAPACITY];
444: array1 = _list.toArray(array2);
445: Assert.areSame(array1, array2);
446: Assert.areEqual(CAPACITY, array1.length);
447:
448: array2 = new Object[0];
449: array1 = _list.toArray(array2);
450: Assert.areEqual(0, array1.length);
451:
452: }
453: }
|