001: /*
002: * $Header: /home/cvs/jakarta-commons/primitives/src/test/org/apache/commons/collections/primitives/TestIntList.java,v 1.3 2003/10/27 18:50:31 rwaldhoff Exp $
003: * ====================================================================
004: * The Apache Software License, Version 1.1
005: *
006: * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowledgement:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgement may appear in the software itself,
026: * if and wherever such third-party acknowledgements normally appear.
027: *
028: * 4. The names "The Jakarta Project", "Commons", and "Apache Software
029: * Foundation" must not be used to endorse or promote products derived
030: * from this software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache"
034: * nor may "Apache" appear in their names without prior written
035: * permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation. For more
053: * information on the Apache Software Foundation, please see
054: * <http://www.apache.org/>.
055: *
056: */
057:
058: package org.apache.commons.collections.primitives;
059:
060: import java.io.Serializable;
061: import java.util.ArrayList;
062: import java.util.ConcurrentModificationException;
063: import java.util.List;
064:
065: import org.apache.commons.collections.primitives.adapters.BaseTestList;
066: import org.apache.commons.collections.primitives.adapters.IntListList;
067: import org.apache.commons.collections.primitives.adapters.ListIntList;
068:
069: /**
070: * @version $Revision: 1.3 $ $Date: 2003/10/27 18:50:31 $
071: * @author Rodney Waldhoff
072: */
073: public abstract class TestIntList extends BaseTestList {
074:
075: // conventional
076: // ------------------------------------------------------------------------
077:
078: public TestIntList(String testName) {
079: super (testName);
080: }
081:
082: // collections testing framework
083: // ------------------------------------------------------------------------
084:
085: // collections testing framework: int list
086: // ------------------------------------------------------------------------
087:
088: protected abstract IntList makeEmptyIntList();
089:
090: protected IntList makeFullIntList() {
091: IntList list = makeEmptyIntList();
092: int[] values = getFullIntegers();
093: for (int i = 0; i < values.length; i++) {
094: list.add(values[i]);
095: }
096: return list;
097: }
098:
099: protected int[] getFullIntegers() {
100: int[] result = new int[19];
101: for (int i = 0; i < result.length; i++) {
102: result[i] = i + 19;
103: }
104: return result;
105: }
106:
107: protected int[] getOtherIntegers() {
108: int[] result = new int[16];
109: for (int i = 0; i < result.length; i++) {
110: result[i] = i + 43;
111: }
112: return result;
113: }
114:
115: // collections testing framework: inherited
116: // ------------------------------------------------------------------------
117:
118: protected List makeEmptyList() {
119: return new IntListList(makeEmptyIntList());
120: }
121:
122: protected Object[] getFullElements() {
123: return wrapArray(getFullIntegers());
124: }
125:
126: protected Object[] getOtherElements() {
127: return wrapArray(getOtherIntegers());
128: }
129:
130: // private utils
131: // ------------------------------------------------------------------------
132:
133: private Integer[] wrapArray(int[] primitives) {
134: Integer[] result = new Integer[primitives.length];
135: for (int i = 0; i < result.length; i++) {
136: result[i] = new Integer(primitives[i]);
137: }
138: return result;
139: }
140:
141: // tests
142: // ------------------------------------------------------------------------
143:
144: public void testExceptionOnConcurrentModification() {
145: IntList list = makeFullIntList();
146: IntIterator iter = list.iterator();
147: iter.next();
148: list.add((int) 3);
149: try {
150: iter.next();
151: fail("Expected ConcurrentModificationException");
152: } catch (ConcurrentModificationException e) {
153: // expected
154: }
155: }
156:
157: public void testAddAllIntListAtIndex() {
158: IntList source = makeFullIntList();
159: IntList dest = makeFullIntList();
160: dest.addAll(1, source);
161:
162: IntIterator iter = dest.iterator();
163: assertTrue(iter.hasNext());
164: assertEquals(source.get(0), iter.next());
165: for (int i = 0; i < source.size(); i++) {
166: assertTrue(iter.hasNext());
167: assertEquals(source.get(i), iter.next());
168: }
169: for (int i = 1; i < source.size(); i++) {
170: assertTrue(iter.hasNext());
171: assertEquals(source.get(i), iter.next());
172: }
173: assertFalse(iter.hasNext());
174: }
175:
176: public void testToJustBigEnoughIntArray() {
177: IntList list = makeFullIntList();
178: int[] dest = new int[list.size()];
179: assertSame(dest, list.toArray(dest));
180: int i = 0;
181: for (IntIterator iter = list.iterator(); iter.hasNext(); i++) {
182: assertEquals(iter.next(), dest[i]);
183: }
184: }
185:
186: public void testToLargerThanNeededIntArray() {
187: IntList list = makeFullIntList();
188: int[] dest = new int[list.size() * 2];
189: for (int i = 0; i < dest.length; i++) {
190: dest[i] = Integer.MAX_VALUE;
191: }
192: assertSame(dest, list.toArray(dest));
193: int i = 0;
194: for (IntIterator iter = list.iterator(); iter.hasNext(); i++) {
195: assertEquals(iter.next(), dest[i]);
196: }
197: for (; i < dest.length; i++) {
198: assertEquals(Integer.MAX_VALUE, dest[i]);
199: }
200: }
201:
202: public void testToSmallerThanNeededIntArray() {
203: IntList list = makeFullIntList();
204: int[] dest = new int[list.size() / 2];
205: int[] dest2 = list.toArray(dest);
206: assertTrue(dest != dest2);
207: int i = 0;
208: for (IntIterator iter = list.iterator(); iter.hasNext(); i++) {
209: assertEquals(iter.next(), dest2[i]);
210: }
211: }
212:
213: public void testHashCodeSpecification() {
214: IntList list = makeFullIntList();
215: int hash = 1;
216: for (IntIterator iter = list.iterator(); iter.hasNext();) {
217: hash = 31 * hash + iter.next();
218: }
219: assertEquals(hash, list.hashCode());
220: }
221:
222: public void testEqualsWithTwoIntLists() {
223: IntList one = makeEmptyIntList();
224: assertEquals("Equals is reflexive on empty list", one, one);
225: IntList two = makeEmptyIntList();
226: assertEquals("Empty lists are equal", one, two);
227: assertEquals("Equals is symmetric on empty lists", two, one);
228:
229: one.add(1);
230: assertEquals("Equals is reflexive on non empty list", one, one);
231: assertTrue(!one.equals(two));
232: assertTrue(!two.equals(one));
233:
234: two.add(1);
235: assertEquals("Non empty lists are equal", one, two);
236: assertEquals("Equals is symmetric on non empty list", one, two);
237:
238: one.add(1);
239: one.add(2);
240: one.add(3);
241: one.add(5);
242: one.add(8);
243: assertEquals("Equals is reflexive on larger non empty list",
244: one, one);
245: assertTrue(!one.equals(two));
246: assertTrue(!two.equals(one));
247:
248: two.add(1);
249: two.add(2);
250: two.add(3);
251: two.add(5);
252: two.add(8);
253: assertEquals("Larger non empty lists are equal", one, two);
254: assertEquals("Equals is symmetric on larger non empty list",
255: two, one);
256:
257: one.add(9);
258: two.add(10);
259: assertTrue(!one.equals(two));
260: assertTrue(!two.equals(one));
261:
262: }
263:
264: public void testIntSubListEquals() {
265: IntList one = makeEmptyIntList();
266: assertEquals(one, one.subList(0, 0));
267: assertEquals(one.subList(0, 0), one);
268:
269: one.add(1);
270: assertEquals(one, one.subList(0, 1));
271: assertEquals(one.subList(0, 1), one);
272:
273: one.add(1);
274: one.add(2);
275: one.add(3);
276: one.add(5);
277: one.add(8);
278: assertEquals(one.subList(0, 4), one.subList(0, 4));
279: assertEquals(one.subList(3, 5), one.subList(3, 5));
280: }
281:
282: public void testEqualsWithIntListAndList() {
283: IntList ilist = makeEmptyIntList();
284: List list = new ArrayList();
285:
286: assertTrue(
287: "Unwrapped, empty List should not be equal to empty IntList.",
288: !ilist.equals(list));
289: assertTrue(
290: "Unwrapped, empty IntList should not be equal to empty List.",
291: !list.equals(ilist));
292:
293: assertEquals(new ListIntList(list), ilist);
294: assertEquals(ilist, new ListIntList(list));
295: assertEquals(new IntListList(ilist), list);
296: assertEquals(list, new IntListList(ilist));
297:
298: ilist.add(1);
299: list.add(new Integer(1));
300:
301: assertTrue(
302: "Unwrapped, non-empty List is not equal to non-empty IntList.",
303: !ilist.equals(list));
304: assertTrue(
305: "Unwrapped, non-empty IntList is not equal to non-empty List.",
306: !list.equals(ilist));
307:
308: assertEquals(new ListIntList(list), ilist);
309: assertEquals(ilist, new ListIntList(list));
310: assertEquals(new IntListList(ilist), list);
311: assertEquals(list, new IntListList(ilist));
312:
313: ilist.add(1);
314: ilist.add(2);
315: ilist.add(3);
316: ilist.add(5);
317: ilist.add(8);
318: list.add(new Integer(1));
319: list.add(new Integer(2));
320: list.add(new Integer(3));
321: list.add(new Integer(5));
322: list.add(new Integer(8));
323:
324: assertTrue(
325: "Unwrapped, non-empty List is not equal to non-empty IntList.",
326: !ilist.equals(list));
327: assertTrue(
328: "Unwrapped, non-empty IntList is not equal to non-empty List.",
329: !list.equals(ilist));
330:
331: assertEquals(new ListIntList(list), ilist);
332: assertEquals(ilist, new ListIntList(list));
333: assertEquals(new IntListList(ilist), list);
334: assertEquals(list, new IntListList(ilist));
335:
336: }
337:
338: public void testClearAndSize() {
339: IntList list = makeEmptyIntList();
340: assertEquals(0, list.size());
341: for (int i = 0; i < 100; i++) {
342: list.add(i);
343: }
344: assertEquals(100, list.size());
345: list.clear();
346: assertEquals(0, list.size());
347: }
348:
349: public void testRemoveViaSubList() {
350: IntList list = makeEmptyIntList();
351: for (int i = 0; i < 100; i++) {
352: list.add(i);
353: }
354: IntList sub = list.subList(25, 75);
355: assertEquals(50, sub.size());
356: for (int i = 0; i < 50; i++) {
357: assertEquals(100 - i, list.size());
358: assertEquals(50 - i, sub.size());
359: assertEquals(25 + i, sub.removeElementAt(0));
360: assertEquals(50 - i - 1, sub.size());
361: assertEquals(100 - i - 1, list.size());
362: }
363: assertEquals(0, sub.size());
364: assertEquals(50, list.size());
365: }
366:
367: public void testAddGet() {
368: IntList list = makeEmptyIntList();
369: for (int i = 0; i < 1000; i++) {
370: list.add(i);
371: }
372: for (int i = 0; i < 1000; i++) {
373: assertEquals(i, list.get(i));
374: }
375: }
376:
377: public void testAddAndShift() {
378: IntList list = makeEmptyIntList();
379: list.add(0, 1);
380: assertEquals("Should have one entry", 1, list.size());
381: list.add(3);
382: list.add(4);
383: list.add(1, 2);
384: for (int i = 0; i < 4; i++) {
385: assertEquals("Should get entry back", i + 1, list.get(i));
386: }
387: list.add(0, 0);
388: for (int i = 0; i < 5; i++) {
389: assertEquals("Should get entry back", i, list.get(i));
390: }
391: }
392:
393: public void testIsSerializable() throws Exception {
394: IntList list = makeFullIntList();
395: assertTrue(list instanceof Serializable);
396: byte[] ser = writeExternalFormToBytes((Serializable) list);
397: IntList deser = (IntList) (readExternalFormFromBytes(ser));
398: assertEquals(list, deser);
399: assertEquals(deser, list);
400: }
401:
402: public void testIntListSerializeDeserializeThenCompare()
403: throws Exception {
404: IntList list = makeFullIntList();
405: if (list instanceof Serializable) {
406: byte[] ser = writeExternalFormToBytes((Serializable) list);
407: IntList deser = (IntList) (readExternalFormFromBytes(ser));
408: assertEquals("obj != deserialize(serialize(obj))", list,
409: deser);
410: }
411: }
412:
413: public void testSubListsAreNotSerializable() throws Exception {
414: IntList list = makeFullIntList().subList(2, 3);
415: assertTrue(!(list instanceof Serializable));
416: }
417:
418: public void testSubListOutOfBounds() throws Exception {
419: try {
420: makeEmptyIntList().subList(2, 3);
421: fail("Expected IndexOutOfBoundsException");
422: } catch (IndexOutOfBoundsException e) {
423: // expected
424: }
425:
426: try {
427: makeFullIntList().subList(-1, 3);
428: fail("Expected IndexOutOfBoundsException");
429: } catch (IndexOutOfBoundsException e) {
430: // expected
431: }
432:
433: try {
434: makeFullIntList().subList(5, 2);
435: fail("Expected IllegalArgumentException");
436: } catch (IllegalArgumentException e) {
437: // expected
438: }
439:
440: try {
441: makeFullIntList().subList(2, makeFullIntList().size() + 2);
442: fail("Expected IndexOutOfBoundsException");
443: } catch (IndexOutOfBoundsException e) {
444: // expected
445: }
446: }
447:
448: public void testListIteratorOutOfBounds() throws Exception {
449: try {
450: makeEmptyIntList().listIterator(2);
451: fail("Expected IndexOutOfBoundsException");
452: } catch (IndexOutOfBoundsException e) {
453: // expected
454: }
455:
456: try {
457: makeFullIntList().listIterator(-1);
458: fail("Expected IndexOutOfBoundsException");
459: } catch (IndexOutOfBoundsException e) {
460: // expected
461: }
462:
463: try {
464: makeFullIntList()
465: .listIterator(makeFullIntList().size() + 2);
466: fail("Expected IndexOutOfBoundsException");
467: } catch (IndexOutOfBoundsException e) {
468: // expected
469: }
470: }
471:
472: public void testListIteratorSetWithoutNext() throws Exception {
473: IntListIterator iter = makeFullIntList().listIterator();
474: try {
475: iter.set(3);
476: fail("Expected IllegalStateException");
477: } catch (IllegalStateException e) {
478: // expected
479: }
480: }
481:
482: public void testListIteratorSetAfterRemove() throws Exception {
483: IntListIterator iter = makeFullIntList().listIterator();
484: iter.next();
485: iter.remove();
486: try {
487: iter.set(3);
488: fail("Expected IllegalStateException");
489: } catch (IllegalStateException e) {
490: // expected
491: }
492: }
493:
494: }
|