001: /*
002: * $Header: /home/cvs/jakarta-commons/primitives/src/test/org/apache/commons/collections/primitives/TestLongList.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.ListLongList;
067: import org.apache.commons.collections.primitives.adapters.LongListList;
068:
069: /**
070: * @version $Revision: 1.3 $ $Date: 2003/10/27 18:50:31 $
071: * @author Rodney Waldhoff
072: */
073: public abstract class TestLongList extends BaseTestList {
074:
075: // conventional
076: // ------------------------------------------------------------------------
077:
078: public TestLongList(String testName) {
079: super (testName);
080: }
081:
082: // collections testing framework
083: // ------------------------------------------------------------------------
084:
085: // collections testing framework: long list
086: // ------------------------------------------------------------------------
087:
088: protected abstract LongList makeEmptyLongList();
089:
090: protected LongList makeFullLongList() {
091: LongList list = makeEmptyLongList();
092: long[] values = getFullLongs();
093: for (int i = 0; i < values.length; i++) {
094: list.add(values[i]);
095: }
096: return list;
097: }
098:
099: protected long[] getFullLongs() {
100: long[] result = new long[19];
101: for (int i = 0; i < result.length; i++) {
102: result[i] = (long) i
103: + ((long) Integer.MAX_VALUE - (long) 10);
104: }
105: return result;
106: }
107:
108: protected long[] getOtherLongs() {
109: long[] result = new long[16];
110: for (int i = 0; i < result.length; i++) {
111: result[i] = (long) i + (long) 43;
112: }
113: return result;
114: }
115:
116: // collections testing framework: inherited
117: // ------------------------------------------------------------------------
118:
119: protected List makeEmptyList() {
120: return new LongListList(makeEmptyLongList());
121: }
122:
123: protected Object[] getFullElements() {
124: return wrapArray(getFullLongs());
125: }
126:
127: protected Object[] getOtherElements() {
128: return wrapArray(getOtherLongs());
129: }
130:
131: // private utils
132: // ------------------------------------------------------------------------
133:
134: private Long[] wrapArray(long[] primitives) {
135: Long[] result = new Long[primitives.length];
136: for (int i = 0; i < result.length; i++) {
137: result[i] = new Long(primitives[i]);
138: }
139: return result;
140: }
141:
142: // tests
143: // ------------------------------------------------------------------------
144:
145: public void testExceptionOnConcurrentModification() {
146: LongList list = makeFullLongList();
147: LongIterator iter = list.iterator();
148: iter.next();
149: list.add((long) 3);
150: try {
151: iter.next();
152: fail("Expected ConcurrentModificationException");
153: } catch (ConcurrentModificationException e) {
154: // expected
155: }
156: }
157:
158: public void testAddAllLongListAtIndex() {
159: LongList source = makeFullLongList();
160: LongList dest = makeFullLongList();
161: dest.addAll(1, source);
162:
163: LongIterator iter = dest.iterator();
164: assertTrue(iter.hasNext());
165: assertEquals(source.get(0), iter.next());
166: for (int i = 0; i < source.size(); i++) {
167: assertTrue(iter.hasNext());
168: assertEquals(source.get(i), iter.next());
169: }
170: for (int i = 1; i < source.size(); i++) {
171: assertTrue(iter.hasNext());
172: assertEquals(source.get(i), iter.next());
173: }
174: assertFalse(iter.hasNext());
175: }
176:
177: public void testToJustBigEnoughLongArray() {
178: LongList list = makeFullLongList();
179: long[] dest = new long[list.size()];
180: assertSame(dest, list.toArray(dest));
181: int i = 0;
182: for (LongIterator iter = list.iterator(); iter.hasNext(); i++) {
183: assertEquals(iter.next(), dest[i]);
184: }
185: }
186:
187: public void testToLargerThanNeededLongArray() {
188: LongList list = makeFullLongList();
189: long[] dest = new long[list.size() * 2];
190: for (int i = 0; i < dest.length; i++) {
191: dest[i] = Long.MAX_VALUE;
192: }
193: assertSame(dest, list.toArray(dest));
194: int i = 0;
195: for (LongIterator iter = list.iterator(); iter.hasNext(); i++) {
196: assertEquals(iter.next(), dest[i]);
197: }
198: for (; i < dest.length; i++) {
199: assertEquals(Long.MAX_VALUE, dest[i]);
200: }
201: }
202:
203: public void testToSmallerThanNeededLongArray() {
204: LongList list = makeFullLongList();
205: long[] dest = new long[list.size() / 2];
206: long[] dest2 = list.toArray(dest);
207: assertTrue(dest != dest2);
208: int i = 0;
209: for (LongIterator iter = list.iterator(); iter.hasNext(); i++) {
210: assertEquals(iter.next(), dest2[i]);
211: }
212: }
213:
214: public void testHashCodeSpecification() {
215: LongList list = makeFullLongList();
216: int hash = 1;
217: for (LongIterator iter = list.iterator(); iter.hasNext();) {
218: long val = iter.next();
219: hash = 31 * hash + ((int) (val ^ (val >>> 32)));
220: }
221: assertEquals(hash, list.hashCode());
222: }
223:
224: public void testEqualsWithTwoLongLists() {
225: LongList one = makeEmptyLongList();
226: assertEquals("Equals is reflexive on empty list", one, one);
227: LongList two = makeEmptyLongList();
228: assertEquals("Empty lists are equal", one, two);
229: assertEquals("Equals is symmetric on empty lists", two, one);
230:
231: one.add((long) 1);
232: assertEquals("Equals is reflexive on non empty list", one, one);
233: assertTrue(!one.equals(two));
234: assertTrue(!two.equals(one));
235:
236: two.add((long) 1);
237: assertEquals("Non empty lists are equal", one, two);
238: assertEquals("Equals is symmetric on non empty list", one, two);
239:
240: one.add((long) 1);
241: one.add((long) 2);
242: one.add((long) 3);
243: one.add((long) 5);
244: one.add((long) 8);
245: assertEquals("Equals is reflexive on larger non empty list",
246: one, one);
247: assertTrue(!one.equals(two));
248: assertTrue(!two.equals(one));
249:
250: two.add((long) 1);
251: two.add((long) 2);
252: two.add((long) 3);
253: two.add((long) 5);
254: two.add((long) 8);
255: assertEquals("Larger non empty lists are equal", one, two);
256: assertEquals("Equals is symmetric on larger non empty list",
257: two, one);
258:
259: one.add((long) 9);
260: two.add((long) 10);
261: assertTrue(!one.equals(two));
262: assertTrue(!two.equals(one));
263:
264: }
265:
266: public void testLongSubListEquals() {
267: LongList one = makeEmptyLongList();
268: assertEquals(one, one.subList(0, 0));
269: assertEquals(one.subList(0, 0), one);
270:
271: one.add((long) 1);
272: assertEquals(one, one.subList(0, 1));
273: assertEquals(one.subList(0, 1), one);
274:
275: one.add((long) 1);
276: one.add((long) 2);
277: one.add((long) 3);
278: one.add((long) 5);
279: one.add((long) 8);
280: assertEquals(one.subList(0, 4), one.subList(0, 4));
281: assertEquals(one.subList(3, 5), one.subList(3, 5));
282: }
283:
284: public void testEqualsWithLongListAndList() {
285: LongList ilist = makeEmptyLongList();
286: List list = new ArrayList();
287:
288: assertTrue(
289: "Unwrapped, empty List should not be equal to empty LongList.",
290: !ilist.equals(list));
291: assertTrue(
292: "Unwrapped, empty LongList should not be equal to empty List.",
293: !list.equals(ilist));
294:
295: assertEquals(new ListLongList(list), ilist);
296: assertEquals(ilist, new ListLongList(list));
297: assertEquals(new LongListList(ilist), list);
298: assertEquals(list, new LongListList(ilist));
299:
300: ilist.add((long) 1);
301: list.add(new Long(1));
302:
303: assertTrue(
304: "Unwrapped, non-empty List is not equal to non-empty LongList.",
305: !ilist.equals(list));
306: assertTrue(
307: "Unwrapped, non-empty LongList is not equal to non-empty List.",
308: !list.equals(ilist));
309:
310: assertEquals(new ListLongList(list), ilist);
311: assertEquals(ilist, new ListLongList(list));
312: assertEquals(new LongListList(ilist), list);
313: assertEquals(list, new LongListList(ilist));
314:
315: ilist.add(1);
316: ilist.add(2);
317: ilist.add(3);
318: ilist.add(5);
319: ilist.add(8);
320: list.add(new Long(1));
321: list.add(new Long(2));
322: list.add(new Long(3));
323: list.add(new Long(5));
324: list.add(new Long(8));
325:
326: assertTrue(
327: "Unwrapped, non-empty List is not equal to non-empty LongList.",
328: !ilist.equals(list));
329: assertTrue(
330: "Unwrapped, non-empty LongList is not equal to non-empty List.",
331: !list.equals(ilist));
332:
333: assertEquals(new ListLongList(list), ilist);
334: assertEquals(ilist, new ListLongList(list));
335: assertEquals(new LongListList(ilist), list);
336: assertEquals(list, new LongListList(ilist));
337:
338: }
339:
340: public void testClearAndSize() {
341: LongList list = makeEmptyLongList();
342: assertEquals(0, list.size());
343: for (int i = 0; i < 100; i++) {
344: list.add((long) i);
345: }
346: assertEquals(100, list.size());
347: list.clear();
348: assertEquals(0, list.size());
349: }
350:
351: public void testRemoveViaSubList() {
352: LongList list = makeEmptyLongList();
353: for (int i = 0; i < 100; i++) {
354: list.add((long) i);
355: }
356: LongList sub = list.subList(25, 75);
357: assertEquals(50, sub.size());
358: for (int i = 0; i < 50; i++) {
359: assertEquals(100 - i, list.size());
360: assertEquals(50 - i, sub.size());
361: assertEquals(25 + i, sub.removeElementAt(0));
362: assertEquals(50 - i - 1, sub.size());
363: assertEquals(100 - i - 1, list.size());
364: }
365: assertEquals(0, sub.size());
366: assertEquals(50, list.size());
367: }
368:
369: public void testAddGet() {
370: LongList list = makeEmptyLongList();
371: for (int i = 0; i < 1000; i++) {
372: list.add((long) i);
373: }
374: for (int i = 0; i < 1000; i++) {
375: assertEquals((long) i, list.get(i));
376: }
377: }
378:
379: public void testAddAndShift() {
380: LongList list = makeEmptyLongList();
381: list.add(0, (long) 1);
382: assertEquals("Should have one entry", 1, list.size());
383: list.add((long) 3);
384: list.add((long) 4);
385: list.add(1, (long) 2);
386: for (int i = 0; i < 4; i++) {
387: assertEquals("Should get entry back", (long) (i + 1), list
388: .get(i));
389: }
390: list.add(0, (long) 0);
391: for (int i = 0; i < 5; i++) {
392: assertEquals("Should get entry back", (long) i, list.get(i));
393: }
394: }
395:
396: public void testIsSerializable() throws Exception {
397: LongList list = makeFullLongList();
398: assertTrue(list instanceof Serializable);
399: byte[] ser = writeExternalFormToBytes((Serializable) list);
400: LongList deser = (LongList) (readExternalFormFromBytes(ser));
401: assertEquals(list, deser);
402: assertEquals(deser, list);
403: }
404:
405: public void testLongListSerializeDeserializeThenCompare()
406: throws Exception {
407: LongList list = makeFullLongList();
408: if (list instanceof Serializable) {
409: byte[] ser = writeExternalFormToBytes((Serializable) list);
410: LongList deser = (LongList) (readExternalFormFromBytes(ser));
411: assertEquals("obj != deserialize(serialize(obj))", list,
412: deser);
413: }
414: }
415:
416: public void testSubListsAreNotSerializable() throws Exception {
417: LongList list = makeFullLongList().subList(2, 3);
418: assertTrue(!(list instanceof Serializable));
419: }
420:
421: public void testSubListOutOfBounds() throws Exception {
422: try {
423: makeEmptyLongList().subList(2, 3);
424: fail("Expected IndexOutOfBoundsException");
425: } catch (IndexOutOfBoundsException e) {
426: // expected
427: }
428:
429: try {
430: makeFullLongList().subList(-1, 3);
431: fail("Expected IndexOutOfBoundsException");
432: } catch (IndexOutOfBoundsException e) {
433: // expected
434: }
435:
436: try {
437: makeFullLongList().subList(5, 2);
438: fail("Expected IllegalArgumentException");
439: } catch (IllegalArgumentException e) {
440: // expected
441: }
442:
443: try {
444: makeFullLongList()
445: .subList(2, makeFullLongList().size() + 2);
446: fail("Expected IndexOutOfBoundsException");
447: } catch (IndexOutOfBoundsException e) {
448: // expected
449: }
450: }
451:
452: public void testListIteratorOutOfBounds() throws Exception {
453: try {
454: makeEmptyLongList().listIterator(2);
455: fail("Expected IndexOutOfBoundsException");
456: } catch (IndexOutOfBoundsException e) {
457: // expected
458: }
459:
460: try {
461: makeFullLongList().listIterator(-1);
462: fail("Expected IndexOutOfBoundsException");
463: } catch (IndexOutOfBoundsException e) {
464: // expected
465: }
466:
467: try {
468: makeFullLongList().listIterator(
469: makeFullLongList().size() + 2);
470: fail("Expected IndexOutOfBoundsException");
471: } catch (IndexOutOfBoundsException e) {
472: // expected
473: }
474: }
475:
476: public void testListIteratorSetWithoutNext() throws Exception {
477: LongListIterator iter = makeFullLongList().listIterator();
478: try {
479: iter.set(3);
480: fail("Expected IllegalStateException");
481: } catch (IllegalStateException e) {
482: // expected
483: }
484: }
485:
486: public void testListIteratorSetAfterRemove() throws Exception {
487: LongListIterator iter = makeFullLongList().listIterator();
488: iter.next();
489: iter.remove();
490: try {
491: iter.set(3);
492: fail("Expected IllegalStateException");
493: } catch (IllegalStateException e) {
494: // expected
495: }
496: }
497:
498: }
|