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