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