Improve testing for SortedSet

includes some code from Dieter Wimberger


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131109 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-12 15:47:53 +00:00
parent f51f08a1fd
commit 3439cce207
5 changed files with 387 additions and 129 deletions

View File

@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestCollection.java,v 1.12 2003/07/12 15:11:25 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestCollection.java,v 1.13 2003/07/12 15:47:53 scolebourne Exp $
* $Revision: 1.12 $ * $Revision: 1.13 $
* $Date: 2003/07/12 15:11:25 $ * $Date: 2003/07/12 15:47:53 $
* *
* ==================================================================== * ====================================================================
* *
@ -157,7 +157,7 @@ import java.util.NoSuchElementException;
* @author <a href="mailto:mas@apache.org">Michael A. Smith</a> * @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
* @author Neil O'Toole * @author Neil O'Toole
* @author Stephen Colebourne * @author Stephen Colebourne
* @version $Id: TestCollection.java,v 1.12 2003/07/12 15:11:25 scolebourne Exp $ * @version $Id: TestCollection.java,v 1.13 2003/07/12 15:47:53 scolebourne Exp $
*/ */
public abstract class TestCollection extends TestObject { public abstract class TestCollection extends TestObject {
@ -442,6 +442,84 @@ public abstract class TestCollection extends TestObject {
return getOtherNonNullElements(); return getOtherNonNullElements();
} }
//-----------------------------------------------------------------------
/**
* Returns a list of elements suitable for return by
* {@link getFullElements()}. The array returned by this method
* does not include null, but does include a variety of objects
* of different types. Override getFullElements to return
* the results of this method if your collection does not support
* the null element.
*/
protected Object[] getFullNonNullElements() {
return new Object[] {
new String(""),
new String("One"),
new Integer(2),
"Three",
new Integer(4),
"One",
new Double(5),
new Float(6),
"Seven",
"Eight",
new String("Nine"),
new Integer(10),
new Short((short)11),
new Long(12),
"Thirteen",
"14",
"15",
new Byte((byte)16)
};
}
/**
* Returns the default list of objects returned by
* {@link getOtherElements()}. Includes many objects
* of different types.
*/
protected Object[] getOtherNonNullElements() {
return new Object[] {
new Integer(0),
new Float(0),
new Double(0),
"Zero",
new Short((short)0),
new Byte((byte)0),
new Long(0),
new Character('\u0000'),
"0"
};
}
/**
* Returns a list of string elements suitable for return by
* {@link getFullElements()}. Override getFullElements to return
* the results of this method if your collection does not support
* heterogenous elements or the null element.
*/
protected Object[] getFullNonNullStringElements() {
return new Object[] {
"If","the","dull","substance","of","my","flesh","were","thought",
"Injurious","distance","could","not","stop","my","way",
};
}
/**
* Returns a list of string elements suitable for return by
* {@link getOtherElements()}. Override getOtherElements to return
* the results of this method if your collection does not support
* heterogenous elements or the null element.
*/
protected Object[] getOtherNonNullStringElements() {
return new Object[] {
"For","then","despite",/* of */"space","I","would","be","brought",
"From","limits","far","remote","where","thou","dost","stay"
};
}
// Tests
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Tests {@link Collection#add(Object)}. * Tests {@link Collection#add(Object)}.
@ -1230,84 +1308,4 @@ public abstract class TestCollection extends TestObject {
} }
} }
/**
* Returns a list of elements suitable for return by
* {@link getFullElements()}. The array returned by this method
* does not include null, but does include a variety of objects
* of different types. Override getFullElements to return
* the results of this method if your collection does not support
* the null element.
*/
public static Object[] getFullNonNullElements() {
return new Object[] {
new String(""),
new String("One"),
new Integer(2),
"Three",
new Integer(4),
"One",
new Double(5),
new Float(6),
"Seven",
"Eight",
new String("Nine"),
new Integer(10),
new Short((short)11),
new Long(12),
"Thirteen",
"14",
"15",
new Byte((byte)16)
};
}
/**
* Returns the default list of objects returned by
* {@link getOtherElements()}. Includes many objects
* of different types.
*/
public static Object[] getOtherNonNullElements() {
return new Object[] {
new Integer(0),
new Float(0),
new Double(0),
"Zero",
new Short((short)0),
new Byte((byte)0),
new Long(0),
new Character('\u0000'),
"0"
};
}
/**
* Returns a list of string elements suitable for return by
* {@link getFullElements()}. Override getFullElements to return
* the results of this method if your collection does not support
* heterogenous elements or the null element.
*/
public static Object[] getFullNonNullStringElements() {
return new Object[] {
"If","the","dull","substance","of","my","flesh","were","thought",
"Injurious","distance","could","not","stop","my","way",
};
}
/**
* Returns a list of string elements suitable for return by
* {@link getOtherElements()}. Override getOtherElements to return
* the results of this method if your collection does not support
* heterogenous elements or the null element.
*/
public static Object[] getOtherNonNullStringElements() {
return new Object[] {
"For","then","despite",/* of */"space","I","would","be","brought",
"From","limits","far","remote","where","thou","dost","stay"
};
}
} }

View File

@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestMap.java,v 1.23 2003/02/26 01:33:22 rwaldhoff Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestMap.java,v 1.24 2003/07/12 15:47:53 scolebourne Exp $
* $Revision: 1.23 $ * $Revision: 1.24 $
* $Date: 2003/02/26 01:33:22 $ * $Date: 2003/07/12 15:47:53 $
* *
* ==================================================================== * ====================================================================
* *
@ -152,7 +152,7 @@ import java.util.Set;
* @author Michael Smith * @author Michael Smith
* @author Rodney Waldhoff * @author Rodney Waldhoff
* @author Paul Jack * @author Paul Jack
* @version $Revision: 1.23 $ $Date: 2003/02/26 01:33:22 $ * @version $Revision: 1.24 $ $Date: 2003/07/12 15:47:53 $
*/ */
public abstract class TestMap extends TestObject { public abstract class TestMap extends TestObject {
@ -246,11 +246,24 @@ public abstract class TestMap extends TestObject {
protected Object[] getOtherKeys() { protected Object[] getOtherKeys() {
return TestCollection.getOtherNonNullStringElements(); return getOtherNonNullStringElements();
} }
protected Object[] getOtherValues() { protected Object[] getOtherValues() {
return TestCollection.getOtherNonNullStringElements(); return getOtherNonNullStringElements();
}
/**
* Returns a list of string elements suitable for return by
* {@link getOtherElements()}. Override getOtherElements to return
* the results of this method if your collection does not support
* heterogenous elements or the null element.
*/
protected Object[] getOtherNonNullStringElements() {
return new Object[] {
"For","then","despite",/* of */"space","I","would","be","brought",
"From","limits","far","remote","where","thou","dost","stay"
};
} }
/** /**

View File

@ -1,7 +1,7 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestSet.java,v 1.3 2003/07/12 15:11:25 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestSet.java,v 1.4 2003/07/12 15:47:53 scolebourne Exp $
* $Revision: 1.3 $ * $Revision: 1.4 $
* $Date: 2003/07/12 15:11:25 $ * $Date: 2003/07/12 15:47:53 $
* *
* ==================================================================== * ====================================================================
* *
@ -81,7 +81,7 @@ import java.util.Set;
* elements may be added; see {@link TestCollection} for more details.<P> * elements may be added; see {@link TestCollection} for more details.<P>
* *
* @author Paul Jack * @author Paul Jack
* @version $Id: TestSet.java,v 1.3 2003/07/12 15:11:25 scolebourne Exp $ * @version $Id: TestSet.java,v 1.4 2003/07/12 15:47:53 scolebourne Exp $
*/ */
public abstract class TestSet extends TestCollection { public abstract class TestSet extends TestCollection {
@ -103,7 +103,7 @@ public abstract class TestSet extends TestCollection {
assertEquals("Sets should be equal", confirmed, collection); assertEquals("Sets should be equal", confirmed, collection);
assertEquals("Sets should have equal hashCodes", assertEquals("Sets should have equal hashCodes",
confirmed.hashCode(), collection.hashCode()); confirmed.hashCode(), collection.hashCode());
HashSet set = new HashSet(); Collection set = makeConfirmedCollection();
Iterator iterator = collection.iterator(); Iterator iterator = collection.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
assertTrue("Set.iterator should only return unique elements", assertTrue("Set.iterator should only return unique elements",
@ -113,7 +113,7 @@ public abstract class TestSet extends TestCollection {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Returns an empty {@link HashSet} for use in modification testing. * Returns an empty Set for use in modification testing.
* *
* @return a confirmed empty collection * @return a confirmed empty collection
*/ */
@ -122,12 +122,12 @@ public abstract class TestSet extends TestCollection {
} }
/** /**
* Returns a full {@link HashSet} for use in modification testing. * Returns a full Set for use in modification testing.
* *
* @return a confirmed full collection * @return a confirmed full collection
*/ */
protected Collection makeConfirmedFullCollection() { protected Collection makeConfirmedFullCollection() {
HashSet set = new HashSet(); Collection set = makeConfirmedCollection();
set.addAll(Arrays.asList(getFullElements())); set.addAll(Arrays.asList(getFullElements()));
return set; return set;
} }
@ -198,7 +198,7 @@ public abstract class TestSet extends TestCollection {
getSet(), getConfirmedSet()); getSet(), getConfirmedSet());
verify(); verify();
HashSet set2 = new HashSet(); Collection set2 = makeConfirmedCollection();
set2.add("foo"); set2.add("foo");
assertTrue("Empty set shouldn't equal nonempty set", assertTrue("Empty set shouldn't equal nonempty set",
!getSet().equals(set2)); !getSet().equals(set2));

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/Attic/TestSortedSet.java,v 1.1 2003/05/11 13:11:37 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestSortedSet.java,v 1.2 2003/07/12 15:47:53 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -57,27 +57,283 @@
*/ */
package org.apache.commons.collections; package org.apache.commons.collections;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
/** /**
* Tests base {@link java.util.SortedSet} methods and contracts. * Tests base {@link SortedSet} methods and contracts.
* <p>
* To use, subclass and override the {@link #makeEmptySet()}
* method. You may have to override other protected methods if your
* set is not modifiable, or if your set restricts what kinds of
* elements may be added; see {@link TestCollection} for more details.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:11:37 $ * @version $Revision: 1.2 $ $Date: 2003/07/12 15:47:53 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Dieter Wimberger
*/ */
public abstract class TestSortedSet extends TestSet { public abstract class TestSortedSet extends TestSet {
public TestSortedSet(String testName) { /**
super(testName); * Constructor.
} *
* @param name name for test
protected Object[] getFullElements() { */
return new Object[] {"1", "3", "5", "7", "2", "4", "6"}; public TestSortedSet(String name) {
super(name);
} }
protected Object[] getOtherElements() { //-----------------------------------------------------------------------
return new Object[] {"9", "88", "678", "87", "98", "78", "99"}; /**
* Verification extension, will check the order of elements,
* the sets should already be verified equal.
*/
protected void verify() {
super.verify();
//Sorted sets should return in-order iterators by contract
Iterator colliter = collection.iterator();
Iterator confiter = confirmed.iterator();
while (colliter.hasNext()) {
assertEquals("Element appears to be out of order.", colliter.next(), confiter.next());
}
}
//-----------------------------------------------------------------------
/**
* Overridden because UnboundedFifoBuffer doesn't allow null elements.
* @return false
*/
protected boolean isNullSupported() {
return false;
}
//-----------------------------------------------------------------------
/**
* Returns an empty {@link TreeSet} for use in modification testing.
*
* @return a confirmed empty collection
*/
protected Collection makeConfirmedCollection() {
return new TreeSet();
}
//-----------------------------------------------------------------------
/**
* Return the {@link TestCollection#confirmed} fixture, but cast as a
* SortedSet.
*/
protected SortedSet getConfirmedSortedSet() {
return (SortedSet) confirmed;
}
//-----------------------------------------------------------------------
/**
* Override to return comparable objects.
*/
protected Object[] getFullNonNullElements() {
Object[] elements = new Object[30];
for (int i = 0; i < 30; i++) {
elements[i] = new Integer(i + i + 1);
}
return elements;
}
/**
* Override to return comparable objects.
*/
protected Object[] getOtherNonNullElements() {
Object[] elements = new Object[30];
for (int i = 0; i < 30; i++) {
elements[i] = new Integer(i + i + 2);
}
return elements;
}
//-----------------------------------------------------------------------
/**
* Bulk test {@link SortedSet#subSet(Object, Object)}. This method runs through all of
* the tests in {@link TestSortedSet}.
* After modification operations, {@link #verify()} is invoked to ensure
* that the set and the other collection views are still valid.
*
* @return a {@link TestSet} instance for testing a subset.
*/
public BulkTest bulkTestSortedSetSubSet() {
int length = getFullElements().length;
int lobound = length / 3;
int hibound = lobound * 2;
return new TestSortedSetSubSet(lobound, hibound);
}
/**
* Bulk test {@link SortedSet#headSet(Object)}. This method runs through all of
* the tests in {@link TestSortedSet}.
* After modification operations, {@link #verify()} is invoked to ensure
* that the set and the other collection views are still valid.
*
* @return a {@link TestSet} instance for testing a headset.
*/
public BulkTest bulkTestSortedSetHeadSet() {
int length = getFullElements().length;
int lobound = length / 3;
int hibound = lobound * 2;
return new TestSortedSetSubSet(hibound, true);
}
/**
* Bulk test {@link SortedSet#tailSet(Object)}. This method runs through all of
* the tests in {@link TestSortedSet}.
* After modification operations, {@link #verify()} is invoked to ensure
* that the set and the other collection views are still valid.
*
* @return a {@link TestSet} instance for testing a tailset.
*/
public BulkTest bulkTestSortedSetTailSet() {
int length = getFullElements().length;
int lobound = length / 3;
return new TestSortedSetSubSet(lobound, false);
}
class TestSortedSetSubSet extends TestSortedSet {
private int m_Type;
private int m_LowBound;
private int m_HighBound;
private Object[] m_FullElements;
private Object[] m_OtherElements;
public TestSortedSetSubSet(int bound, boolean head) {
super("TestSortedSetSubSet");
if (head) {
//System.out.println("HEADSET");
m_Type = TYPE_HEADSET;
m_HighBound = bound;
m_FullElements = new Object[bound];
System.arraycopy(TestSortedSet.this.getFullElements(), 0, m_FullElements, 0, bound);
m_OtherElements = new Object[bound - 1];
System.arraycopy(//src src_pos dst dst_pos length
TestSortedSet.this.getOtherElements(), 0, m_OtherElements, 0, bound - 1);
//System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
//System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
} else {
//System.out.println("TAILSET");
m_Type = TYPE_TAILSET;
m_LowBound = bound;
Object[] allelements = TestSortedSet.this.getFullElements();
//System.out.println("bound = "+bound +"::length="+allelements.length);
m_FullElements = new Object[allelements.length - bound];
System.arraycopy(allelements, bound, m_FullElements, 0, allelements.length - bound);
m_OtherElements = new Object[allelements.length - bound - 1];
System.arraycopy(//src src_pos dst dst_pos length
TestSortedSet.this.getOtherElements(), bound, m_OtherElements, 0, allelements.length - bound - 1);
//System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
//System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
//resetFull();
//System.out.println(collection);
//System.out.println(confirmed);
}
} //type
public TestSortedSetSubSet(int lobound, int hibound) {
super("TestSortedSetSubSet");
//System.out.println("SUBSET");
m_Type = TYPE_SUBSET;
m_LowBound = lobound;
m_HighBound = hibound;
int length = hibound - lobound;
//System.out.println("Low=" + lobound + "::High=" + hibound + "::Length=" + length);
m_FullElements = new Object[length];
System.arraycopy(TestSortedSet.this.getFullElements(), lobound, m_FullElements, 0, length);
m_OtherElements = new Object[length - 1];
System.arraycopy(//src src_pos dst dst_pos length
TestSortedSet.this.getOtherElements(), lobound, m_OtherElements, 0, length - 1);
//System.out.println(new TreeSet(Arrays.asList(m_FullElements)));
//System.out.println(new TreeSet(Arrays.asList(m_OtherElements)));
} //TestSortedSetSubSet
public boolean isNullSupported() {
return TestSortedSet.this.isNullSupported();
} //useNullValue
protected Object[] getFullElements() {
//System.out.println("getFullElements()");
return m_FullElements;
}
protected Object[] getOtherElements() {
return m_OtherElements;
}
private SortedSet getSubSet(SortedSet set) {
Object[] elements = TestSortedSet.this.getFullElements();
switch (m_Type) {
case TYPE_SUBSET :
return set.subSet(elements[m_LowBound], elements[m_HighBound]);
case TYPE_HEADSET :
return set.headSet(elements[m_HighBound]);
case TYPE_TAILSET :
return set.tailSet(elements[m_LowBound]);
default :
return null;
}
} //getSubSet
protected Set makeEmptySet() {
SortedSet s = (SortedSet) TestSortedSet.this.makeFullSet();
s = getSubSet(s);
s.clear();
return s;
} //makeEmptySet
protected Set makeFullSet() {
SortedSet s = (SortedSet) TestSortedSet.this.makeFullCollection();
return getSubSet(s);
} //makeFullSet
protected void resetFull() {
TestSortedSet.this.resetFull();
TestSortedSetSubSet.this.confirmed = getSubSet((SortedSet) TestSortedSet.this.confirmed);
TestSortedSetSubSet.this.collection = getSubSet((SortedSet) TestSortedSet.this.collection);
}
protected void resetEmpty() {
TestSortedSetSubSet.this.resetFull();
TestSortedSetSubSet.this.confirmed.clear();
TestSortedSetSubSet.this.collection.clear();
}
public BulkTest bulkTestSortedSetSubSet() {
//Override returning null to prevent endless
//loop of bulk tests
return null;
} //bulkTestSortedSetSubSet
public BulkTest bulkTestSortedSetHeadSet() {
return null;
}
public BulkTest bulkTestSortedSetTailSet() {
return null;
}
static final int TYPE_SUBSET = 0;
static final int TYPE_TAILSET = 1;
static final int TYPE_HEADSET = 2;
} }
// TODO: Add the SortedSet tests!
} }

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/decorators/Attic/TestTransformedSortedSet.java,v 1.1 2003/05/11 13:18:27 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestTransformedSortedSet.java,v 1.2 2003/07/12 15:47:53 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -58,7 +58,6 @@
package org.apache.commons.collections.decorators; package org.apache.commons.collections.decorators;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
@ -73,7 +72,7 @@ import org.apache.commons.collections.TestSortedSet;
* implementation. * implementation.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/05/11 13:18:27 $ * @version $Revision: 1.2 $ $Date: 2003/07/12 15:47:53 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -92,26 +91,18 @@ public class TestTransformedSortedSet extends TestSortedSet {
junit.textui.TestRunner.main(testCaseName); junit.textui.TestRunner.main(testCaseName);
} }
public Collection makeConfirmedCollection() { //-----------------------------------------------------------------------
return new TreeSet(); protected Set makeEmptySet() {
} return TransformedSortedSet.decorate(new TreeSet(), TestTransformedCollection.NOOP_TRANSFORMER);
protected Collection makeConfirmedFullCollection() {
Set set = new TreeSet();
set.addAll(Arrays.asList(getFullElements()));
return set;
}
public Set makeEmptySet() {
return TransformedSortedSet.decorate(new HashSet(), TestTransformedCollection.NOOP_TRANSFORMER);
} }
protected Set makeFullSet() { protected Set makeFullSet() {
Set list = new TreeSet(); Set set = new TreeSet();
list.addAll(Arrays.asList(getFullElements())); set.addAll(Arrays.asList(getFullElements()));
return TransformedSortedSet.decorate(list, TestTransformedCollection.NOOP_TRANSFORMER); return TransformedSortedSet.decorate(set, TestTransformedCollection.NOOP_TRANSFORMER);
} }
//-----------------------------------------------------------------------
public void testTransformedSet() { public void testTransformedSet() {
Set set = TransformedSortedSet.decorate(new HashSet(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); Set set = TransformedSortedSet.decorate(new HashSet(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
assertEquals(0, set.size()); assertEquals(0, set.size());