Add a lot of tests

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131306 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-10-31 01:25:45 +00:00
parent 2029f95ea2
commit 27a00be12e
1 changed files with 216 additions and 4 deletions

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/AbstractTestSortedMap.java,v 1.3 2003/10/07 22:20:57 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/AbstractTestSortedMap.java,v 1.4 2003/10/31 01:25:45 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -57,13 +57,18 @@
*/
package org.apache.commons.collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* Abstract test class for {@link java.util.SortedMap} methods and contracts.
*
* @version $Revision: 1.3 $ $Date: 2003/10/07 22:20:57 $
* @version $Revision: 1.4 $ $Date: 2003/10/31 01:25:45 $
*
* @author Stephen Colebourne
*/
@ -88,7 +93,14 @@ public abstract class AbstractTestSortedMap extends AbstractTestMap {
return false;
}
// TODO: Add the SortedMap tests!
/**
* SortedMap uses TreeMap as its known comparison.
*
* @return a map that is known to be valid
*/
protected Map makeConfirmedMap() {
return new TreeMap();
}
//-----------------------------------------------------------------------
public void testComparator() {
@ -110,4 +122,204 @@ public abstract class AbstractTestSortedMap extends AbstractTestMap {
assertSame(obj, sm.lastKey());
}
//-----------------------------------------------------------------------
public BulkTest bulkTestHeadMap() {
return new TestHeadMap(this);
}
public BulkTest bulkTestTailMap() {
return new TestTailMap(this);
}
public BulkTest bulkTestSubMap() {
return new TestSubMap(this);
}
public static abstract class TestViewMap extends AbstractTestSortedMap {
protected final AbstractTestMap main;
protected final List subSortedKeys = new ArrayList();
protected final List subSortedValues = new ArrayList();
protected final List subSortedNewValues = new ArrayList();
public TestViewMap(String name, AbstractTestMap main) {
super(name);
this.main = main;
}
protected void resetEmpty() {
// needed to init verify correctly
main.resetEmpty();
super.resetEmpty();
}
protected void resetFull() {
// needed to init verify correctly
main.resetFull();
super.resetFull();
}
protected void verify() {
// cross verify changes on view with changes on main map
super.verify();
main.verify();
}
public BulkTest bulkTestHeadMap() {
return null; // block infinite recursion
}
public BulkTest bulkTestTailMap() {
return null; // block infinite recursion
}
public BulkTest bulkTestSubMap() {
return null; // block infinite recursion
}
protected Object[] getSampleKeys() {
return subSortedKeys.toArray();
}
protected Object[] getSampleValues() {
return subSortedValues.toArray();
}
protected Object[] getNewSampleValues() {
return subSortedNewValues.toArray();
}
protected String getCompatibilityVersion() {
return main.getCompatibilityVersion();
}
protected boolean isAllowNullKey() {
return main.isAllowNullKey();
}
protected boolean isAllowNullValue() {
return main.isAllowNullValue();
}
protected boolean isPutAddSupported() {
return main.isPutAddSupported();
}
protected boolean isPutChangeSupported() {
return main.isPutChangeSupported();
}
protected boolean isRemoveSupported() {
return main.isRemoveSupported();
}
protected boolean supportsEmptyCollections() {
return false;
}
protected boolean supportsFullCollections() {
return false;
}
}
public static class TestHeadMap extends TestViewMap {
static final int SUBSIZE = 6;
final Object toKey;
public TestHeadMap(AbstractTestMap main) {
super("SortedMap.HeadMap", main);
SortedMap sm = (SortedMap) main.makeFullMap();
for (Iterator it = sm.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
this.subSortedKeys.add(entry.getKey());
this.subSortedValues.add(entry.getValue());
}
this.toKey = this.subSortedKeys.get(SUBSIZE);
this.subSortedKeys.subList(SUBSIZE, this.subSortedKeys.size()).clear();
this.subSortedValues.subList(SUBSIZE, this.subSortedValues.size()).clear();
this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList(0, SUBSIZE));
}
protected Map makeEmptyMap() {
// done this way so toKey is correctly set in the returned map
return ((SortedMap) main.makeEmptyMap()).headMap(toKey);
}
protected Map makeFullMap() {
return ((SortedMap) main.makeFullMap()).headMap(toKey);
}
public void testHeadMapOutOfRange() {
if (isPutAddSupported() == false) return;
resetEmpty();
try {
((SortedMap) map).put(toKey, subSortedValues.get(0));
fail();
} catch (IllegalArgumentException ex) {}
verify();
}
}
public static class TestTailMap extends TestViewMap {
static final int SUBSIZE = 6;
final Object fromKey;
final Object invalidKey;
public TestTailMap(AbstractTestMap main) {
super("SortedMap.TailMap", main);
SortedMap sm = (SortedMap) main.makeFullMap();
for (Iterator it = sm.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
this.subSortedKeys.add(entry.getKey());
this.subSortedValues.add(entry.getValue());
}
this.fromKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE);
this.invalidKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE - 1);
this.subSortedKeys.subList(0, this.subSortedKeys.size() - SUBSIZE).clear();
this.subSortedValues.subList(0, this.subSortedValues.size() - SUBSIZE).clear();
this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList(0, SUBSIZE));
}
protected Map makeEmptyMap() {
// done this way so toKey is correctly set in the returned map
return ((SortedMap) main.makeEmptyMap()).tailMap(fromKey);
}
protected Map makeFullMap() {
return ((SortedMap) main.makeFullMap()).tailMap(fromKey);
}
public void testTailMapOutOfRange() {
if (isPutAddSupported() == false) return;
resetEmpty();
try {
((SortedMap) map).put(invalidKey, subSortedValues.get(0));
fail();
} catch (IllegalArgumentException ex) {}
verify();
}
}
public static class TestSubMap extends TestViewMap {
static final int SUBSIZE = 3;
final Object fromKey;
final Object toKey;
public TestSubMap(AbstractTestMap main) {
super("SortedMap.SubMap", main);
SortedMap sm = (SortedMap) main.makeFullMap();
for (Iterator it = sm.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
this.subSortedKeys.add(entry.getKey());
this.subSortedValues.add(entry.getValue());
}
this.fromKey = this.subSortedKeys.get(SUBSIZE);
this.toKey = this.subSortedKeys.get(this.subSortedKeys.size() - SUBSIZE);
this.subSortedKeys.subList(0, SUBSIZE).clear();
this.subSortedKeys.subList(this.subSortedKeys.size() - SUBSIZE, this.subSortedKeys.size()).clear();
this.subSortedValues.subList(0, SUBSIZE).clear();
this.subSortedValues.subList(this.subSortedValues.size() - SUBSIZE, this.subSortedValues.size()).clear();
this.subSortedNewValues.addAll(Arrays.asList(main.getNewSampleValues()).subList(
SUBSIZE, this.main.getNewSampleValues().length - SUBSIZE));
}
protected Map makeEmptyMap() {
// done this way so toKey is correctly set in the returned map
return ((SortedMap) main.makeEmptyMap()).subMap(fromKey, toKey);
}
protected Map makeFullMap() {
return ((SortedMap) main.makeFullMap()).subMap(fromKey, toKey);
}
public void testSubMapOutOfRange() {
if (isPutAddSupported() == false) return;
resetEmpty();
try {
((SortedMap) map).put(toKey, subSortedValues.get(0));
fail();
} catch (IllegalArgumentException ex) {}
verify();
}
}
}