make ArrayIntList serializable

add additional tests


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130979 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Rodney Waldhoff 2003-02-26 15:45:19 +00:00
parent cec79200d7
commit d10b146e18
4 changed files with 194 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/Attic/ArrayIntList.java,v 1.5 2003/01/13 21:52:28 rwaldhoff Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/Attic/ArrayIntList.java,v 1.6 2003/02/26 15:45:19 rwaldhoff Exp $
* ==================================================================== * ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
@ -57,16 +57,21 @@
package org.apache.commons.collections.primitives; package org.apache.commons.collections.primitives;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/** /**
* An {@link IntList} backed by an array of <code>int</code>s. * An {@link IntList} backed by an array of <code>int</code>s.
* This implementation supports all optional methods. * This implementation supports all optional methods.
* *
* @since Commons Collections 2.2 * @since Commons Collections 2.2
* @version $Revision: 1.5 $ $Date: 2003/01/13 21:52:28 $ * @version $Revision: 1.6 $ $Date: 2003/02/26 15:45:19 $
* *
* @author Rodney Waldhoff * @author Rodney Waldhoff
*/ */
public class ArrayIntList extends AbstractRandomAccessIntList implements IntList { public class ArrayIntList extends AbstractRandomAccessIntList implements IntList, Serializable {
// constructors // constructors
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
@ -223,6 +228,22 @@ public class ArrayIntList extends AbstractRandomAccessIntList implements IntList
// private methods // private methods
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
private void writeObject(ObjectOutputStream out) throws IOException{
out.defaultWriteObject();
out.writeInt(_data.length);
for(int i=0;i<_size;i++) {
out.writeInt(_data[i]);
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
_data = new int[in.readInt()];
for(int i=0;i<_size;i++) {
_data[i] = in.readInt();
}
}
private final void checkRange(int index) { private final void checkRange(int index) {
if(index < 0 || index >= _size) { if(index < 0 || index >= _size) {
throw new IndexOutOfBoundsException("Should be at least 0 and less than " + _size + ", found " + index); throw new IndexOutOfBoundsException("Should be at least 0 and less than " + _size + ", found " + index);

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/Attic/TestArrayIntList.java,v 1.6 2003/01/12 15:23:20 rwaldhoff Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/Attic/TestArrayIntList.java,v 1.7 2003/02/26 15:45:19 rwaldhoff Exp $
* ==================================================================== * ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
@ -63,7 +63,7 @@ import junit.framework.TestSuite;
import org.apache.commons.collections.BulkTest; import org.apache.commons.collections.BulkTest;
/** /**
* @version $Revision: 1.6 $ $Date: 2003/01/12 15:23:20 $ * @version $Revision: 1.7 $ $Date: 2003/02/26 15:45:19 $
* @author Rodney Waldhoff * @author Rodney Waldhoff
*/ */
public class TestArrayIntList extends TestIntList { public class TestArrayIntList extends TestIntList {
@ -127,4 +127,81 @@ public class TestArrayIntList extends TestIntList {
public void testZeroInitialCapacityIsValid() { public void testZeroInitialCapacityIsValid() {
ArrayIntList list = new ArrayIntList(0); ArrayIntList list = new ArrayIntList(0);
} }
public void testNegativeInitialCapacityIsInvalid() {
try {
ArrayIntList list = new ArrayIntList(-1);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
}
public void testCopyConstructor() {
ArrayIntList expected = new ArrayIntList();
for(int i=0;i<10;i++) {
expected.add(i);
}
ArrayIntList list = new ArrayIntList(expected);
assertEquals(10,list.size());
assertEquals(expected,list);
}
public void testCopyConstructorWithNull() {
try {
ArrayIntList list = new ArrayIntList(null);
fail("Expected NullPointerException");
} catch(NullPointerException e) {
// expected
}
}
public void testTrimToSize() {
ArrayIntList list = new ArrayIntList();
for(int j=0;j<3;j++) {
assertTrue(list.isEmpty());
list.trimToSize();
assertTrue(list.isEmpty());
for(int i=0;i<10;i++) {
list.add(i);
}
for(int i=0;i<10;i++) {
assertEquals(i,list.get(i));
}
list.trimToSize();
for(int i=0;i<10;i++) {
assertEquals(i,list.get(i));
}
for(int i=0;i<10;i+=2) {
list.removeElement(i);
}
for(int i=0;i<5;i++) {
assertEquals((2*i)+1,list.get(i));
}
list.trimToSize();
for(int i=0;i<5;i++) {
assertEquals((2*i)+1,list.get(i));
}
list.trimToSize();
for(int i=0;i<5;i++) {
assertEquals((2*i)+1,list.get(i));
}
list.clear();
}
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/Attic/TestArrayUnsignedShortList.java,v 1.6 2003/01/13 21:52:28 rwaldhoff Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/Attic/TestArrayUnsignedShortList.java,v 1.7 2003/02/26 15:45:19 rwaldhoff Exp $
* ==================================================================== * ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
@ -63,7 +63,7 @@ import junit.framework.TestSuite;
import org.apache.commons.collections.BulkTest; import org.apache.commons.collections.BulkTest;
/** /**
* @version $Revision: 1.6 $ $Date: 2003/01/13 21:52:28 $ * @version $Revision: 1.7 $ $Date: 2003/02/26 15:45:19 $
* @author Rodney Waldhoff * @author Rodney Waldhoff
*/ */
public class TestArrayUnsignedShortList extends TestIntList { public class TestArrayUnsignedShortList extends TestIntList {
@ -132,4 +132,80 @@ public class TestArrayUnsignedShortList extends TestIntList {
} }
} }
public void testNegativeInitialCapacityIsInvalid() {
try {
ArrayUnsignedShortList list = new ArrayUnsignedShortList(-1);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
}
public void testCopyConstructor() {
ArrayUnsignedShortList expected = new ArrayUnsignedShortList();
for(int i=0;i<10;i++) {
expected.add(i);
}
ArrayUnsignedShortList list = new ArrayUnsignedShortList(expected);
assertEquals(10,list.size());
assertEquals(expected,list);
}
public void testCopyConstructorWithNull() {
try {
ArrayUnsignedShortList list = new ArrayUnsignedShortList(null);
fail("Expected NullPointerException");
} catch(NullPointerException e) {
// expected
}
}
public void testTrimToSize() {
ArrayUnsignedShortList list = new ArrayUnsignedShortList();
for(int j=0;j<3;j++) {
assertTrue(list.isEmpty());
list.trimToSize();
assertTrue(list.isEmpty());
for(int i=0;i<10;i++) {
list.add(i);
}
for(int i=0;i<10;i++) {
assertEquals(i,list.get(i));
}
list.trimToSize();
for(int i=0;i<10;i++) {
assertEquals(i,list.get(i));
}
for(int i=0;i<10;i+=2) {
list.removeElement(i);
}
for(int i=0;i<5;i++) {
assertEquals((2*i)+1,list.get(i));
}
list.trimToSize();
for(int i=0;i<5;i++) {
assertEquals((2*i)+1,list.get(i));
}
list.trimToSize();
for(int i=0;i<5;i++) {
assertEquals((2*i)+1,list.get(i));
}
list.clear();
}
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/Attic/TestIntList.java,v 1.3 2003/01/12 15:23:20 rwaldhoff Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/Attic/TestIntList.java,v 1.4 2003/02/26 15:45:19 rwaldhoff Exp $
* ==================================================================== * ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
@ -57,6 +57,7 @@
package org.apache.commons.collections.primitives; package org.apache.commons.collections.primitives;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -65,7 +66,7 @@ import org.apache.commons.collections.primitives.adapters.IntListList;
import org.apache.commons.collections.primitives.adapters.ListIntList; import org.apache.commons.collections.primitives.adapters.ListIntList;
/** /**
* @version $Revision: 1.3 $ $Date: 2003/01/12 15:23:20 $ * @version $Revision: 1.4 $ $Date: 2003/02/26 15:45:19 $
* @author Rodney Waldhoff * @author Rodney Waldhoff
*/ */
public abstract class TestIntList extends TestList { public abstract class TestIntList extends TestList {
@ -280,4 +281,14 @@ public abstract class TestIntList extends TestList {
} }
} }
public void testIsSerializable() throws Exception {
IntList list = makeFullIntList();
assertTrue(list instanceof Serializable);
byte[] ser = writeExternalFormToBytes((Serializable)list);
IntList deser = (IntList)(readExternalFormFromBytes(ser));
assertEquals(list,deser);
assertEquals(deser,list);
}
} }