You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/07/12 17:47:54 UTC

cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/decorators TestTransformedSortedSet.java

scolebourne    2003/07/12 08:47:54

  Modified:    collections/src/test/org/apache/commons/collections
                        TestMap.java TestSortedSet.java TestCollection.java
                        TestSet.java
               collections/src/test/org/apache/commons/collections/decorators
                        TestTransformedSortedSet.java
  Log:
  Improve testing for SortedSet
  includes some code from Dieter Wimberger
  
  Revision  Changes    Path
  1.24      +19 -6     jakarta-commons/collections/src/test/org/apache/commons/collections/TestMap.java
  
  Index: TestMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMap.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- TestMap.java	26 Feb 2003 01:33:22 -0000	1.23
  +++ TestMap.java	12 Jul 2003 15:47:53 -0000	1.24
  @@ -246,11 +246,24 @@
   
   
       protected Object[] getOtherKeys() {
  -        return TestCollection.getOtherNonNullStringElements();
  +        return getOtherNonNullStringElements();
       }
   
       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"
  +        };
       }
   
       /**
  
  
  
  1.2       +266 -10   jakarta-commons/collections/src/test/org/apache/commons/collections/TestSortedSet.java
  
  Index: TestSortedSet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestSortedSet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestSortedSet.java	11 May 2003 13:11:37 -0000	1.1
  +++ TestSortedSet.java	12 Jul 2003 15:47:53 -0000	1.2
  @@ -57,27 +57,283 @@
    */
   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
    * @version $Revision$ $Date$
    * 
    * @author Stephen Colebourne
  + * @author Dieter Wimberger
    */
   public abstract class TestSortedSet extends TestSet {
   
  -    public TestSortedSet(String testName) {
  -        super(testName);
  +    /**
  +     * Constructor.
  +     *
  +     * @param name  name for test
  +     */
  +    public TestSortedSet(String name) {
  +        super(name);
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * 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;
       }
  -    
  -    protected Object[] getFullElements() {
  -        return new Object[] {"1", "3", "5", "7", "2", "4", "6"};
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     *  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);
  +
       }
   
  -    protected Object[] getOtherElements() {
  -        return new Object[] {"9", "88", "678", "87", "98", "78", "99"};
  +    /**
  +     *  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!
   }
  
  
  
  1.13      +82 -84    jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollection.java
  
  Index: TestCollection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollection.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- TestCollection.java	12 Jul 2003 15:11:25 -0000	1.12
  +++ TestCollection.java	12 Jul 2003 15:47:53 -0000	1.13
  @@ -444,6 +444,84 @@
       
       //-----------------------------------------------------------------------
       /**
  +     *  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)}.
        */
       public void testCollectionAdd() {
  @@ -1230,84 +1308,4 @@
           }
       }
   
  -
  -    /**
  -     *  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"
  -        };
  -    }
   }
  
  
  
  1.4       +9 -9      jakarta-commons/collections/src/test/org/apache/commons/collections/TestSet.java
  
  Index: TestSet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestSet.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestSet.java	12 Jul 2003 15:11:25 -0000	1.3
  +++ TestSet.java	12 Jul 2003 15:47:53 -0000	1.4
  @@ -103,7 +103,7 @@
           assertEquals("Sets should be equal", confirmed, collection);
           assertEquals("Sets should have equal hashCodes", 
                        confirmed.hashCode(), collection.hashCode());
  -        HashSet set = new HashSet();
  +        Collection set = makeConfirmedCollection();
           Iterator iterator = collection.iterator();
           while (iterator.hasNext()) {
               assertTrue("Set.iterator should only return unique elements", 
  @@ -113,7 +113,7 @@
   
       //-----------------------------------------------------------------------
       /**
  -     *  Returns an empty {@link HashSet} for use in modification testing.
  +     *  Returns an empty Set for use in modification testing.
        *
        *  @return a confirmed empty collection
        */
  @@ -122,12 +122,12 @@
       }
   
       /**
  -     *  Returns a full {@link HashSet} for use in modification testing.
  +     *  Returns a full Set for use in modification testing.
        *
        *  @return a confirmed full collection
        */
       protected Collection makeConfirmedFullCollection() {
  -        HashSet set = new HashSet();
  +        Collection set = makeConfirmedCollection();
           set.addAll(Arrays.asList(getFullElements()));
           return set;
       }
  @@ -198,7 +198,7 @@
                        getSet(), getConfirmedSet());
           verify();
   
  -        HashSet set2 = new HashSet();
  +        Collection set2 = makeConfirmedCollection();
           set2.add("foo");
           assertTrue("Empty set shouldn't equal nonempty set", 
                      !getSet().equals(set2));
  
  
  
  1.2       +8 -17     jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestTransformedSortedSet.java
  
  Index: TestTransformedSortedSet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestTransformedSortedSet.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestTransformedSortedSet.java	11 May 2003 13:18:27 -0000	1.1
  +++ TestTransformedSortedSet.java	12 Jul 2003 15:47:53 -0000	1.2
  @@ -58,7 +58,6 @@
   package org.apache.commons.collections.decorators;
   
   import java.util.Arrays;
  -import java.util.Collection;
   import java.util.HashSet;
   import java.util.Set;
   import java.util.TreeSet;
  @@ -92,26 +91,18 @@
           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() {
  +    protected Set makeFullSet() {
           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() {
  -        Set list = new TreeSet();
  -        list.addAll(Arrays.asList(getFullElements()));
  -        return TransformedSortedSet.decorate(list, TestTransformedCollection.NOOP_TRANSFORMER);
  +        return TransformedSortedSet.decorate(set, TestTransformedCollection.NOOP_TRANSFORMER);
       }
       
  +    //-----------------------------------------------------------------------
       public void testTransformedSet() {
           Set set = TransformedSortedSet.decorate(new HashSet(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
           assertEquals(0, set.size());
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org