From 8f77a02066d791c25454a5c19d24e471c4678c20 Mon Sep 17 00:00:00 2001 From: Rodney Waldhoff Date: Tue, 7 Jan 2003 13:24:52 +0000 Subject: [PATCH] * fix problems with IntListList and ListIntList equals methods * throw IndexOutOfBounds instead of IllegalArgument, where necessary * re-enable TestArrayIntList suite * re-enable primitives.TestAll suite from root (not clear why this would have been disabled) git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130913 13f79535-47bb-0310-9956-ffa450edef68 --- .../AbstractRandomAccessIntList.java | 12 +- .../collections/primitives/IntListList.java | 18 ++- .../collections/primitives/ListIntList.java | 18 +-- .../apache/commons/collections/TestAll.java | 10 +- .../collections/primitives/TestAll.java | 10 +- .../primitives/TestArrayIntList.java | 106 +++++++++++++++++- 6 files changed, 135 insertions(+), 39 deletions(-) diff --git a/src/java/org/apache/commons/collections/primitives/AbstractRandomAccessIntList.java b/src/java/org/apache/commons/collections/primitives/AbstractRandomAccessIntList.java index 75f2d7a71..60a49dbac 100644 --- a/src/java/org/apache/commons/collections/primitives/AbstractRandomAccessIntList.java +++ b/src/java/org/apache/commons/collections/primitives/AbstractRandomAccessIntList.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/Attic/AbstractRandomAccessIntList.java,v 1.3 2003/01/07 01:29:28 rwaldhoff Exp $ - * $Revision: 1.3 $ - * $Date: 2003/01/07 01:29:28 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/Attic/AbstractRandomAccessIntList.java,v 1.4 2003/01/07 13:24:52 rwaldhoff Exp $ + * $Revision: 1.4 $ + * $Date: 2003/01/07 13:24:52 $ * * ==================================================================== * @@ -331,7 +331,7 @@ public abstract class AbstractRandomAccessIntList extends AbstractIntCollection protected static class RandomAccessIntSubList extends AbstractRandomAccessIntList implements IntList { RandomAccessIntSubList(AbstractRandomAccessIntList list, int fromIndex, int toIndex) { if(fromIndex < 0 || toIndex > list.size() || fromIndex > toIndex) { - throw new IllegalArgumentException(); + throw new IndexOutOfBoundsException(); } else { _list = list; _offset = fromIndex; @@ -382,13 +382,13 @@ public abstract class AbstractRandomAccessIntList extends AbstractIntCollection private void checkRange(int index) { if(index < 0 || index >= size()) { - throw new IllegalArgumentException("index " + index + " not in [0," + size() + ")"); + throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + ")"); } } private void checkRangeIncludingEndpoint(int index) { if(index < 0 || index > size()) { - throw new IllegalArgumentException("index " + index + " not in [0," + size() + "]"); + throw new IndexOutOfBoundsException("index " + index + " not in [0," + size() + "]"); } } diff --git a/src/java/org/apache/commons/collections/primitives/IntListList.java b/src/java/org/apache/commons/collections/primitives/IntListList.java index 84d75cb2b..2f6130318 100644 --- a/src/java/org/apache/commons/collections/primitives/IntListList.java +++ b/src/java/org/apache/commons/collections/primitives/IntListList.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/Attic/IntListList.java,v 1.2 2003/01/07 00:59:51 rwaldhoff Exp $ - * $Revision: 1.2 $ - * $Date: 2003/01/07 00:59:51 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/Attic/IntListList.java,v 1.3 2003/01/07 13:24:52 rwaldhoff Exp $ + * $Revision: 1.3 $ + * $Date: 2003/01/07 13:24:52 $ * * ==================================================================== * @@ -69,7 +69,7 @@ import java.util.ListIterator; * Adapts an {@link IntList} to the * {@link java.util.List List} interface. * - * @version $Revision: 1.2 $ $Date: 2003/01/07 00:59:51 $ + * @version $Revision: 1.3 $ $Date: 2003/01/07 13:24:52 $ * @author Rodney Waldhoff */ public class IntListList extends IntCollectionCollection implements List { @@ -120,8 +120,14 @@ public class IntListList extends IntCollectionCollection implements List { } public boolean equals(Object that) { - if(that instanceof IntList) { - return _list.equals(ListIntList.wrap((List)that)); + if(that instanceof List) { + try { + return _list.equals(ListIntList.wrap((List)that)); + } catch(NullPointerException e) { + return false; + } catch(ClassCastException e) { + return false; + } } else { return super.equals(that); } diff --git a/src/java/org/apache/commons/collections/primitives/ListIntList.java b/src/java/org/apache/commons/collections/primitives/ListIntList.java index b3532a35e..e8fe750d7 100644 --- a/src/java/org/apache/commons/collections/primitives/ListIntList.java +++ b/src/java/org/apache/commons/collections/primitives/ListIntList.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/Attic/ListIntList.java,v 1.1 2003/01/07 00:59:51 rwaldhoff Exp $ - * $Revision: 1.1 $ - * $Date: 2003/01/07 00:59:51 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/primitives/Attic/ListIntList.java,v 1.2 2003/01/07 13:24:52 rwaldhoff Exp $ + * $Revision: 1.2 $ + * $Date: 2003/01/07 13:24:52 $ * * ==================================================================== * @@ -67,7 +67,7 @@ import java.util.List; * Adapts a {@link Number}-valued {@link java.util.List List} * to the {@link IntList} interface. * - * @version $Revision: 1.1 $ $Date: 2003/01/07 00:59:51 $ + * @version $Revision: 1.2 $ $Date: 2003/01/07 13:24:52 $ * @author Rodney Waldhoff */ public class ListIntList extends CollectionIntCollection implements IntList { @@ -118,14 +118,8 @@ public class ListIntList extends CollectionIntCollection implements IntList { } public boolean equals(Object that) { - if(that instanceof List) { - try { - return _list.equals(ListIntList.wrap((List)that)); - } catch(ClassCastException e) { - return false; - } catch(NullPointerException e) { - return false; - } + if(that instanceof IntList) { + return _list.equals(IntListList.wrap((IntList)that)); } else { return super.equals(that); } diff --git a/src/test/org/apache/commons/collections/TestAll.java b/src/test/org/apache/commons/collections/TestAll.java index a7e32766f..c694781f5 100644 --- a/src/test/org/apache/commons/collections/TestAll.java +++ b/src/test/org/apache/commons/collections/TestAll.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.37 2002/11/18 23:58:46 scolebourne Exp $ - * $Revision: 1.37 $ - * $Date: 2002/11/18 23:58:46 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.38 2003/01/07 13:24:52 rwaldhoff Exp $ + * $Revision: 1.38 $ + * $Date: 2003/01/07 13:24:52 $ * * ==================================================================== * @@ -66,7 +66,7 @@ import junit.framework.*; /** * Entry point for all Collections tests. * @author Rodney Waldhoff - * @version $Id: TestAll.java,v 1.37 2002/11/18 23:58:46 scolebourne Exp $ + * @version $Id: TestAll.java,v 1.38 2003/01/07 13:24:52 rwaldhoff Exp $ */ public class TestAll extends TestCase { public TestAll(String testName) { @@ -106,7 +106,7 @@ public class TestAll extends TestCase { suite.addTest(TestIteratorUtils.suite()); suite.addTest(org.apache.commons.collections.comparators.TestAll.suite()); suite.addTest(org.apache.commons.collections.iterators.TestAll.suite()); -// suite.addTest(org.apache.commons.collections.primitives.TestAll.suite()); + suite.addTest(org.apache.commons.collections.primitives.TestAll.suite()); return suite; } diff --git a/src/test/org/apache/commons/collections/primitives/TestAll.java b/src/test/org/apache/commons/collections/primitives/TestAll.java index ddfc9c2fd..c1e82d6d1 100644 --- a/src/test/org/apache/commons/collections/primitives/TestAll.java +++ b/src/test/org/apache/commons/collections/primitives/TestAll.java @@ -1,7 +1,7 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/Attic/TestAll.java,v 1.4 2003/01/07 01:29:28 rwaldhoff Exp $ - * $Revision: 1.4 $ - * $Date: 2003/01/07 01:29:28 $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/primitives/Attic/TestAll.java,v 1.5 2003/01/07 13:24:52 rwaldhoff Exp $ + * $Revision: 1.5 $ + * $Date: 2003/01/07 13:24:52 $ * * ==================================================================== * @@ -66,7 +66,7 @@ import junit.framework.TestCase; import junit.framework.TestSuite; /** - * @version $Revision: 1.4 $ $Date: 2003/01/07 01:29:28 $ + * @version $Revision: 1.5 $ $Date: 2003/01/07 13:24:52 $ * @author Rodney Waldhoff */ public class TestAll extends TestCase { @@ -82,7 +82,7 @@ public class TestAll extends TestCase { public static Test suite() { TestSuite suite = new TestSuite(); - //suite.addTest(TestArrayIntList.suite()); + suite.addTest(TestArrayIntList.suite()); //suite.addTest(TestArrayUnsignedShortList.suite()); suite.addTest(TestUnsignedByteArrayList.suite()); diff --git a/src/test/org/apache/commons/collections/primitives/TestArrayIntList.java b/src/test/org/apache/commons/collections/primitives/TestArrayIntList.java index 13b3c5646..b3bd579e2 100644 --- a/src/test/org/apache/commons/collections/primitives/TestArrayIntList.java +++ b/src/test/org/apache/commons/collections/primitives/TestArrayIntList.java @@ -1,7 +1,7 @@ /* - * $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.2 2003/01/07 01:29:28 rwaldhoff Exp $ - * $Revision: 1.2 $ - * $Date: 2003/01/07 01:29:28 $ + * $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.3 2003/01/07 13:24:52 rwaldhoff Exp $ + * $Revision: 1.3 $ + * $Date: 2003/01/07 13:24:52 $ * * ==================================================================== * @@ -61,6 +61,7 @@ package org.apache.commons.collections.primitives; +import java.util.ArrayList; import java.util.List; import junit.framework.Test; @@ -70,7 +71,7 @@ import org.apache.commons.collections.BulkTest; import org.apache.commons.collections.TestList; /** - * @version $Revision: 1.2 $ $Date: 2003/01/07 01:29:28 $ + * @version $Revision: 1.3 $ $Date: 2003/01/07 13:24:52 $ * @author Rodney Waldhoff */ public class TestArrayIntList extends TestList { @@ -131,10 +132,86 @@ public class TestArrayIntList extends TestList { //------------------------------------------------------------------- Tests + public void testEqualsWithTwoIntLists() { + IntList one = new ArrayIntList(); + assertEquals("Equals is reflexive on empty list",one,one); + IntList two = new ArrayIntList(); + assertEquals("Empty lists are equal",one,two); + assertEquals("Equals is symmetric on empty lists",two,one); + + one.add(1); + assertEquals("Equals is reflexive on non empty list",one,one); + assertTrue(!one.equals(two)); + assertTrue(!two.equals(one)); + + two.add(1); + assertEquals("Non empty lists are equal",one,two); + assertEquals("Equals is symmetric on non empty list",one,two); + + one.add(1); one.add(2); one.add(3); one.add(5); one.add(8); + assertEquals("Equals is reflexive on larger non empty list",one,one); + assertTrue(!one.equals(two)); + assertTrue(!two.equals(one)); + + two.add(1); two.add(2); two.add(3); two.add(5); two.add(8); + assertEquals("Larger non empty lists are equal",one,two); + assertEquals("Equals is symmetric on larger non empty list",two,one); + } + + public void testIntSubListEquals() { + IntList one = new ArrayIntList(); + assertEquals(one,one.subList(0,0)); + assertEquals(one.subList(0,0),one); + + one.add(1); + assertEquals(one,one.subList(0,1)); + assertEquals(one.subList(0,1),one); + + one.add(1); one.add(2); one.add(3); one.add(5); one.add(8); + assertEquals(one.subList(0,4),one.subList(0,4)); + assertEquals(one.subList(3,5),one.subList(3,5)); + } + + public void testEqualsWithIntListAndList() { + IntList ilist = new ArrayIntList(); + List list = new ArrayList(); + + assertTrue("Unwrapped, empty List is not equal to empty IntList.",!ilist.equals(list)); + assertTrue("Unwrapped, empty IntList is not equal to empty List.",!list.equals(ilist)); + + assertEquals(new ListIntList(list),ilist); + assertEquals(ilist,new ListIntList(list)); + assertEquals(new IntListList(ilist),list); + assertEquals(list,new IntListList(ilist)); + + ilist.add(1); + list.add(new Integer(1)); + + assertTrue("Unwrapped, non-empty List is not equal to non-empty IntList.",!ilist.equals(list)); + assertTrue("Unwrapped, non-empty IntList is not equal to non-empty List.",!list.equals(ilist)); + + assertEquals(new ListIntList(list),ilist); + assertEquals(ilist,new ListIntList(list)); + assertEquals(new IntListList(ilist),list); + assertEquals(list,new IntListList(ilist)); + + ilist.add(1); ilist.add(2); ilist.add(3); ilist.add(5); ilist.add(8); + list.add(new Integer(1)); list.add(new Integer(2)); list.add(new Integer(3)); list.add(new Integer(5)); list.add(new Integer(8)); + + assertTrue("Unwrapped, non-empty List is not equal to non-empty IntList.",!ilist.equals(list)); + assertTrue("Unwrapped, non-empty IntList is not equal to non-empty List.",!list.equals(ilist)); + + assertEquals(new ListIntList(list),ilist); + assertEquals(ilist,new ListIntList(list)); + assertEquals(new IntListList(ilist),list); + assertEquals(list,new IntListList(ilist)); + + } + public void testClearAndSize() { IntList list = new ArrayIntList(); assertEquals(0, list.size()); - for (int i = 0; i < 100; i++) { + for(int i = 0; i < 100; i++) { list.add(i); } assertEquals(100, list.size()); @@ -142,6 +219,25 @@ public class TestArrayIntList extends TestList { assertEquals(0, list.size()); } + public void testRemoveViaSubList() { + IntList list = new ArrayIntList(); + for(int i = 0; i < 100; i++) { + list.add(i); + } + IntList sub = list.subList(25,75); + assertEquals(50,sub.size()); + for(int i = 0; i < 50; i++) { + assertEquals(100-i,list.size()); + assertEquals(50-i,sub.size()); + assertEquals(25+i,sub.removeElementAt(0)); + assertEquals(50-i-1,sub.size()); + assertEquals(100-i-1,list.size()); + } + assertEquals(0,sub.size()); + assertEquals(50,list.size()); + } + + public void testAddGet() { IntList list = new ArrayIntList(); for (int i = 0; i < 1000; i++) {