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