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