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