* ContractableDoubleArray extends ExpandableDoubleArray - I sense the
need for a DoubleArray interface. * ExpandableDoubleArray and the extension ContractableDoubleArray should aim towards presenting a public interface that does not expose any details of the internal. To this end, one is no longer able to get the internal storage array via public double[] getValues(), and the startIndex (which was relative to the internal storage array) is no longer available. * [Expandable|Contractable]DoubleArray now allow one to discard elements from the front of the array. Before this commit, one could accomplish the same goal by changing the starting index of the element array within the internal storage array. This solution allowed one to discard elements from the front of the array (as well as) reclaiming elements by decreases the startIndex. There were two problems with this approach (especially in ContractableDoubleArray). The ContractableDoubleArray can be "compacted" at anytime thereby reseting the startIndex to zero and the size of the internal store array to number of elements plus one. Second, "reclaiming" elements from the internal storage array by finagling internal "pointers" to the start and end index seems to violate the principles of encapsulation. If you "discard" an element from the front of the array, consider it unavailable. It should be noted that calling setNumElements allows one to move the end index of the internal element array at will. Assume one has a 100 element array, and one calls setNumElements(10), thereby decreasing the ending index of the element array by 90. The 90 "dumped" elements are not currently reinitializied to the default double primitive value. This is an open question. * Tests for ExpandableDoubleArray and ContractableDoubleArray were refactored. both test classes now extend a DoubleArrayAbstractTest JUnit class which contained shared unit tests for both "implementations". An approach like this should be adopted to test the Univariate implementations. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140833 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
065a88e241
commit
7651a6b14d
|
@ -93,7 +93,7 @@ public class ContractableDoubleArray extends ExpandableDoubleArray implements Se
|
|||
*/
|
||||
public ContractableDoubleArray(int initialCapacity, float expansionFactor) {
|
||||
this.expansionFactor = expansionFactor;
|
||||
this.initialCapacity = initialCapacity;
|
||||
setInitialCapacity( initialCapacity );
|
||||
internalArray = new double[initialCapacity];
|
||||
checkContractExpand(getContractionCriteria(), expansionFactor);
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ public class ContractableDoubleArray extends ExpandableDoubleArray implements Se
|
|||
public ContractableDoubleArray(int initialCapacity, float expansionFactor, float contractionCriteria) {
|
||||
this.contractionCriteria = contractionCriteria;
|
||||
this.expansionFactor = expansionFactor;
|
||||
this.initialCapacity = initialCapacity;
|
||||
setInitialCapacity( initialCapacity );
|
||||
internalArray = new double[initialCapacity];
|
||||
checkContractExpand(contractionCriteria, expansionFactor);
|
||||
}
|
||||
|
@ -187,16 +187,6 @@ public class ContractableDoubleArray extends ExpandableDoubleArray implements Se
|
|||
super.setExpansionFactor(expansionFactor);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.ExpandableDoubleArray#setStartIndex(int)
|
||||
*/
|
||||
public synchronized void setStartIndex(int i) {
|
||||
super.setStartIndex(i);
|
||||
if( shouldContract() ) {
|
||||
contract();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The contraction criteria defines when the internal array will contract to store only the
|
||||
* number of elements in the element array. This contractionCriteria gaurantees that
|
||||
|
@ -217,9 +207,6 @@ public class ContractableDoubleArray extends ExpandableDoubleArray implements Se
|
|||
public void setContractionCriteria(float contractionCriteria) {
|
||||
checkContractExpand( contractionCriteria, getExpansionFactor() );
|
||||
|
||||
if( contractionCriteria <= 1.0 ) {
|
||||
throw new IllegalArgumentException( "The contraction criteria must be a number larger than" +
" one. If the contractionCriteria is less than or equal to one an endless loop of contraction " +
"and expansion would ensue as an internalArray.length == numElements would satisfy " +
"the contraction criteria");
|
||||
}
|
||||
this.contractionCriteria = contractionCriteria;
|
||||
}
|
||||
|
||||
|
@ -230,12 +217,36 @@ public class ContractableDoubleArray extends ExpandableDoubleArray implements Se
|
|||
* @param expansionFactor
|
||||
* @param contractionCriteria
|
||||
*/
|
||||
public void checkContractExpand( float contractionCritera, float expansionFactor ) {
|
||||
protected void checkContractExpand( float contractionCritera, float expansionFactor ) {
|
||||
|
||||
if( contractionCritera < expansionFactor ) {
|
||||
throw new IllegalArgumentException( "Contraction criteria can never be smaller than " +
"the expansion factor. This would lead to a never ending loop of expansion and " +
"contraction as a newly expanded internal storage array would immediately " +
"satisfy the criteria for contraction");
|
||||
}
|
||||
|
||||
if( contractionCriteria <= 1.0 ) {
|
||||
throw new IllegalArgumentException( "The contraction criteria must be a number larger than" +
|
||||
" one. If the contractionCriteria is less than or equal to one an endless loop of contraction " +
|
||||
"and expansion would ensue as an internalArray.length == numElements would satisfy " +
|
||||
"the contraction criteria");
|
||||
}
|
||||
|
||||
if (expansionFactor < 1.0) {
|
||||
throw new IllegalArgumentException(
|
||||
"The expansion factor must be a number greater than" + "1.0");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.ExpandableDoubleArray#discardFrontElements(int)
|
||||
*/
|
||||
public synchronized void discardFrontElements(int i) {
|
||||
super.discardFrontElements(i);
|
||||
if( shouldContract() ) {
|
||||
contract();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -164,7 +164,7 @@ public class ExpandableDoubleArray implements Serializable {
|
|||
*
|
||||
* @return the internal storage array used by this object
|
||||
*/
|
||||
public double[] getValues() {
|
||||
protected double[] getValues() {
|
||||
return (internalArray);
|
||||
}
|
||||
|
||||
|
@ -324,39 +324,21 @@ public class ExpandableDoubleArray implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the starting index from the internal array. This value should remain at
|
||||
* zero in this implementation of ExpandableDoubleArray.
|
||||
* Discards values from the front of the list. This function removes n elements from
|
||||
* the front of the array.
|
||||
*
|
||||
* @return the starting Index in the internal storage array, in this class it is always zero.
|
||||
* @param i number of elements to discard from the front of the array.
|
||||
*/
|
||||
public int getStartIndex() {
|
||||
return startIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the starting index of the element array in the internal array, and subtracts the difference
|
||||
* between the original startIndex and the new startIndex from the number of elements. This
|
||||
* method should be used with care.
|
||||
*
|
||||
* @param Index relative to the internal array from which to start the element array
|
||||
*/
|
||||
public synchronized void setStartIndex(int i) {
|
||||
public synchronized void discardFrontElements(int i) {
|
||||
|
||||
if( i > (startIndex + numElements) ) {
|
||||
throw new IllegalArgumentException( "Cannot start the element array outside of the " +
"current element array.");
|
||||
if( i > numElements ) {
|
||||
throw new IllegalArgumentException( "Cannot discard more elements than are" +
"contained in this array.");
|
||||
} else if( i < 0 ) {
|
||||
throw new IllegalArgumentException( "The starting index cannot be set to a negative index");
|
||||
throw new IllegalArgumentException( "Cannot discard a negative number" +
" of elements.");
|
||||
} else {
|
||||
|
||||
// Calculat the difference between the original start index and the current start index
|
||||
int difference = i - startIndex;
|
||||
|
||||
// "Subtract" this difference from numElements - this works both ways. If the
|
||||
// new start index is lower than the current start index then numElements is
|
||||
// incremenet by that differen
|
||||
numElements -= difference;
|
||||
|
||||
startIndex = i;
|
||||
// "Subtract" this number of discarded from numElements
|
||||
numElements -= i;
|
||||
startIndex += i;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
|
||||
/**
|
||||
* This class contains test cases for the ExpandableDoubleArray.
|
||||
*
|
||||
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
|
||||
*/
|
||||
public class ContractableDoubleArrayTest extends DoubleArrayAbstractTest {
|
||||
|
||||
public ContractableDoubleArrayTest(String name) {
|
||||
super( name );
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
eDA = new ContractableDoubleArray();
|
||||
}
|
||||
|
||||
/** Test normal operations and then test internal storage */
|
||||
public void testAdd1000() {
|
||||
super.testAdd1000();
|
||||
assertEquals("Internal Storage length should be 1024 if we started out with initial capacity of " +
|
||||
"16 and an expansion factor of 2.0",
|
||||
1024, eDA.getInternalLength());
|
||||
}
|
||||
|
||||
public void testSetElementArbitraryExpansion() {
|
||||
super.testSetElementArbitraryExpansion();
|
||||
assertEquals( "The length of the internal array should now be 1001, it isn't", eDA.getInternalLength(), 1001);
|
||||
}
|
||||
|
||||
public void testAddElementRolling() {
|
||||
super.testAddElementRolling();
|
||||
assertTrue( "Even though there are only 6 element, internal storage should be less than 2.5 times the number of elements",
|
||||
eDA.getInternalLength() < ((int) 6 * 2.5) );
|
||||
}
|
||||
|
||||
|
||||
/** Test ERROR conditions */
|
||||
/** TEST ERROR CONDITIONS **/
|
||||
|
||||
public void testIllegalInitialCapacity() {
|
||||
try {
|
||||
ContractableDoubleArray eDA = new ContractableDoubleArray(-3, 2.0f);
|
||||
fail( "That constructor should have thrown an IllegalArgumentException because " +
|
||||
"the initialCapacity was negative, if it didn't then" +
|
||||
" the range checking of initialCapacity is not working properly" );
|
||||
} catch( IllegalArgumentException iae ) {
|
||||
}
|
||||
try {
|
||||
ContractableDoubleArray eDA = new ContractableDoubleArray(0, 2.0f);
|
||||
fail( "That constructor should have thrown an IllegalArgumentException because " +
|
||||
"the initialCapacity was ZERO if it didn't then" +
|
||||
" the range checking of initialCapacity is not working properly" );
|
||||
} catch( IllegalArgumentException iae ) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testIllegalExpansionFactor() {
|
||||
try {
|
||||
ContractableDoubleArray eDA = new ContractableDoubleArray(3, 0.66f);
|
||||
fail( "That constructor should have thrown an IllegalArgumentException because " +
|
||||
"the expansionFactor for 0.66 which would shrink the array instead of expand the array");
|
||||
} catch( IllegalArgumentException iae ) {
|
||||
}
|
||||
try {
|
||||
ContractableDoubleArray eDA = new ContractableDoubleArray(3, 0.0f);
|
||||
fail( "That constructor should have thrown an IllegalArgumentException because " +
|
||||
"the expansionFactor for 0.0");
|
||||
} catch( IllegalArgumentException iae) {
|
||||
}
|
||||
|
||||
try {
|
||||
ContractableDoubleArray eDA = new ContractableDoubleArray(3, -4.35f);
|
||||
fail( "That constructor should have thrown an IllegalArgumentException because " +
|
||||
"the expansionFactor for -4.35");
|
||||
} catch( IllegalArgumentException iae) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* This class contains test cases for the ExpandableDoubleArray.
|
||||
*
|
||||
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
|
||||
*/
|
||||
public abstract class DoubleArrayAbstractTest extends TestCase {
|
||||
|
||||
protected ExpandableDoubleArray eDA = null;
|
||||
|
||||
public DoubleArrayAbstractTest(String name) {
|
||||
super( name );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** TEST NORMAL OPERATIONS **/
|
||||
|
||||
public void testAdd1000() {
|
||||
|
||||
for( int i = 0; i < 1000; i++) {
|
||||
eDA.addElement( i );
|
||||
}
|
||||
|
||||
assertEquals("Number of elements should be equal to 1000 after adding 1000 values",
|
||||
1000, eDA.getNumElements() );
|
||||
|
||||
|
||||
assertEquals("The element at the 56th index should be 56",
|
||||
56.0, eDA.getElement(56), Double.MIN_VALUE );
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void testGetValues() {
|
||||
double[] controlArray = {2.0, 4.0, 6.0};
|
||||
|
||||
eDA.addElement(2.0);
|
||||
eDA.addElement(4.0);
|
||||
eDA.addElement(6.0);
|
||||
double[] testArray = eDA.getValues();
|
||||
|
||||
for( int i = 0; i < eDA.getNumElements(); i++) {
|
||||
assertEquals( "The testArray values should equal the controlArray values, index i: " + i +
|
||||
" does not match", testArray[i], controlArray[i], Double.MIN_VALUE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testSetElementArbitraryExpansion() {
|
||||
double[] controlArray = {2.0, 4.0, 6.0};
|
||||
|
||||
eDA.addElement(2.0);
|
||||
eDA.addElement(4.0);
|
||||
eDA.addElement(6.0);
|
||||
eDA.setElement(1, 3.0);
|
||||
|
||||
// Expand the array arbitrarily to 1000 items
|
||||
eDA.setElement(1000, 3.4);
|
||||
|
||||
assertEquals( "The number of elements should now be 1001, it isn't", eDA.getNumElements(), 1001);
|
||||
|
||||
assertEquals( "Uninitialized Elements are default value of 0.0, index 766 wasn't", 0.0,
|
||||
eDA.getElement( 760 ), Double.MIN_VALUE );
|
||||
|
||||
assertEquals( "The 1000th index should be 3.4, it isn't", 3.4, eDA.getElement(1000), Double.MIN_VALUE );
|
||||
assertEquals( "The 0th index should be 2.0, it isn't", 2.0, eDA.getElement(0), Double.MIN_VALUE);
|
||||
|
||||
}
|
||||
|
||||
public void testSetNumberOfElements() {
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
assertEquals( "Number of elements should equal 6", eDA.getNumElements(), 6);
|
||||
|
||||
eDA.setNumElements( 3 );
|
||||
assertEquals( "Number of elements should equal 3", eDA.getNumElements(), 3);
|
||||
|
||||
try {
|
||||
eDA.setNumElements( -3 );
|
||||
fail( "Setting number of elements to negative should've thrown an exception");
|
||||
} catch( IllegalArgumentException iae ) {
|
||||
}
|
||||
|
||||
eDA.setNumElements(1024);
|
||||
assertEquals( "Number of elements should now be 1024", eDA.getNumElements(), 1024);
|
||||
assertEquals( "Element 453 should be a default double", eDA.getElement( 453 ), 0.0, Double.MIN_VALUE);
|
||||
|
||||
}
|
||||
|
||||
public void testAddElementRolling() {
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElementRolling( 2.0 );
|
||||
|
||||
assertEquals( "There should be 6 elements in the eda", eDA.getNumElements(), 6);
|
||||
assertEquals( "The last element should be 2.0", eDA.getElement( eDA.getNumElements() -1 ), 2.0, Double.MIN_VALUE);
|
||||
|
||||
for( int i = 0; i < 1024; i++ ) {
|
||||
eDA.addElementRolling( i );
|
||||
}
|
||||
|
||||
assertEquals( "We just inserted 1024 rolling elements, num elements should still be 6", eDA.getNumElements(), 6);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -53,200 +53,102 @@
|
|||
*/
|
||||
package org.apache.commons.math;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* This class contains test cases for the ExpandableDoubleArray.
|
||||
*
|
||||
* @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
|
||||
*/
|
||||
public class ExpandableDoubleArrayTest extends TestCase {
|
||||
public class ExpandableDoubleArrayTest extends DoubleArrayAbstractTest {
|
||||
|
||||
public ExpandableDoubleArrayTest(String name) {
|
||||
super( name );
|
||||
}
|
||||
|
||||
/** TEST NORMAL OPERATIONS **/
|
||||
|
||||
public void testAdd1000() {
|
||||
/* (non-Javadoc)
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
eDA = new ExpandableDoubleArray();
|
||||
}
|
||||
|
||||
ExpandableDoubleArray exDoubleArr = new ExpandableDoubleArray();
|
||||
|
||||
for( int i = 0; i < 1000; i++) {
|
||||
exDoubleArr.addElement( i );
|
||||
}
|
||||
|
||||
assertTrue("Number of elements should be equal to 1000 after adding 1000 values",
|
||||
exDoubleArr.getNumElements() == 1000);
|
||||
|
||||
assertTrue("Internal Storage length should be 1024 if we started out with initial capacity of " +
"16 and an expansion factor of 2.0",
|
||||
exDoubleArr.getInternalLength() == 1024);
|
||||
|
||||
assertTrue("The element at the 56th index should be 56",
|
||||
exDoubleArr.getElement(56) == 56 );
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see junit.framework.TestCase#tearDown()
|
||||
*/
|
||||
protected void tearDown() throws Exception {
|
||||
eDA = null;
|
||||
}
|
||||
|
||||
public void testWithInitialCapacity() {
|
||||
ExpandableDoubleArray exDoubleArr = new ExpandableDoubleArray(2);
|
||||
|
||||
/** TEST NORMAL OPERATIONS - calling super class test and then checking internal
|
||||
* storage **/
|
||||
|
||||
public void testAdd1000() {
|
||||
super.testAdd1000();
|
||||
assertEquals("Internal Storage length should be 1024 if we started out with initial capacity of " +
|
||||
"16 and an expansion factor of 2.0",
|
||||
1024, eDA.getInternalLength());
|
||||
}
|
||||
|
||||
public void testSetElementArbitraryExpansion() {
|
||||
super.testSetElementArbitraryExpansion();
|
||||
assertEquals( "The length of the internal array should now be 1001, it isn't", eDA.getInternalLength(), 1001);
|
||||
}
|
||||
|
||||
assertTrue("Initial internal length should be 2", exDoubleArr.getInternalLength() == 2);
|
||||
assertTrue("Initial number of elements should be 0", exDoubleArr.getNumElements() == 0);
|
||||
public void testAddElementRolling() {
|
||||
super.testAddElementRolling();
|
||||
assertEquals( "Even though there are only 6 element, internal storage should be 2048", eDA.getInternalLength(), 2048);
|
||||
}
|
||||
|
||||
/** TESTS WHICH FOCUS ON ExpandableSpecific internal storage */
|
||||
|
||||
public void testWithInitialCapacity() {
|
||||
|
||||
ExpandableDoubleArray eDA2 = new ExpandableDoubleArray(2);
|
||||
assertEquals("Initial internal length should be 2", 2, eDA2.getInternalLength());
|
||||
assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());
|
||||
|
||||
int iterations = (int) Math.pow(2.0, 15.0);
|
||||
|
||||
for( int i = 0; i < iterations; i++) {
|
||||
exDoubleArr.addElement( i );
|
||||
eDA2.addElement( i );
|
||||
}
|
||||
|
||||
assertTrue("Number of elements should be equal to 2^15", exDoubleArr.getNumElements() == (int) Math.pow(2.0, 15.0));
|
||||
assertTrue("Internal length should be 2^15", exDoubleArr.getInternalLength() == (int) Math.pow(2.0, 15.0));
|
||||
assertEquals("Number of elements should be equal to 2^15", (int) Math.pow(2.0, 15.0), eDA2.getNumElements());
|
||||
assertEquals("Internal length should be 2^15", (int) Math.pow(2.0, 15.0), eDA2.getInternalLength());
|
||||
|
||||
exDoubleArr.addElement( 2.0 );
|
||||
eDA2.addElement( 2.0 );
|
||||
|
||||
assertTrue("Number of elements should be equals to 2^15 + 1",
|
||||
exDoubleArr.getNumElements() == ( (int) Math.pow(2.0, 15.0) + 1 ) );
|
||||
assertTrue("Internal length should be 2^16", exDoubleArr.getInternalLength() == (int) Math.pow(2.0, 16.0));
|
||||
|
||||
|
||||
assertEquals("Number of elements should be equals to 2^15 + 1",
|
||||
( (int) Math.pow(2.0, 15.0) + 1 ), eDA2.getNumElements() );
|
||||
assertEquals("Internal length should be 2^16", (int) Math.pow(2.0, 16.0), eDA2.getInternalLength());
|
||||
}
|
||||
|
||||
public void testWithInitialCapacitAndExpansionFactor() {
|
||||
ExpandableDoubleArray exDoubleArr = new ExpandableDoubleArray(3, 3.0f);
|
||||
public void testWithInitialCapacityAndExpansionFactor() {
|
||||
|
||||
assertTrue("Initial internal length should be 3", exDoubleArr.getInternalLength() == 3);
|
||||
assertTrue("Initial number of elements should be 0", exDoubleArr.getNumElements() == 0);
|
||||
ExpandableDoubleArray eDA3 = new ExpandableDoubleArray(3, 3.0f);
|
||||
assertEquals("Initial internal length should be 3", 3, eDA3.getInternalLength() );
|
||||
assertEquals("Initial number of elements should be 0", 0, eDA3.getNumElements() );
|
||||
|
||||
int iterations = (int) Math.pow(3.0, 7.0);
|
||||
|
||||
for( int i = 0; i < iterations; i++) {
|
||||
exDoubleArr.addElement( i );
|
||||
eDA3.addElement( i );
|
||||
}
|
||||
|
||||
assertTrue("Number of elements should be equal to 3^7", exDoubleArr.getNumElements() == (int) Math.pow(3.0, 7.0));
|
||||
assertTrue("Internal length should be 3^7", exDoubleArr.getInternalLength() == (int) Math.pow(3.0, 7.0));
|
||||
assertEquals("Number of elements should be equal to 3^7", (int) Math.pow(3.0, 7.0), eDA3.getNumElements());
|
||||
assertEquals("Internal length should be 3^7", (int) Math.pow(3.0, 7.0), eDA3.getInternalLength());
|
||||
|
||||
exDoubleArr.addElement( 2.0 );
|
||||
eDA3.addElement( 2.0 );
|
||||
|
||||
assertTrue("Number of elements should be equals to 3^7 + 1",
|
||||
exDoubleArr.getNumElements() == ( (int) Math.pow(3.0, 7.0) + 1 ) );
|
||||
assertTrue("Internal length should be 3^8", exDoubleArr.getInternalLength() == (int) Math.pow(3.0, 8.0));
|
||||
assertEquals("Number of elements should be equals to 3^7 + 1",
|
||||
( (int) Math.pow(3.0, 7.0) + 1 ), eDA3.getNumElements() );
|
||||
assertEquals("Internal length should be 3^8", (int) Math.pow(3.0, 8.0), eDA3.getInternalLength());
|
||||
|
||||
assertTrue("Expansion factor should equal 3.0", exDoubleArr.getExpansionFactor() == 3.0f);
|
||||
assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
|
||||
}
|
||||
|
||||
public void testGetValues() {
|
||||
|
||||
ExpandableDoubleArray eDA = new ExpandableDoubleArray();
|
||||
|
||||
double[] controlArray = {2.0, 4.0, 6.0};
|
||||
|
||||
eDA.addElement(2.0);
|
||||
eDA.addElement(4.0);
|
||||
eDA.addElement(6.0);
|
||||
double[] testArray = eDA.getValues();
|
||||
|
||||
for( int i = 0; i < eDA.getNumElements(); i++) {
|
||||
assertTrue( "The testArray values should equal the controlArray values, index i: " + i +
|
||||
" does not match", testArray[i] == controlArray[i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void testSetElementArbitraryExpansion() {
|
||||
|
||||
ExpandableDoubleArray eDA = new ExpandableDoubleArray();
|
||||
|
||||
double[] controlArray = {2.0, 4.0, 6.0};
|
||||
|
||||
eDA.addElement(2.0);
|
||||
eDA.addElement(4.0);
|
||||
eDA.addElement(6.0);
|
||||
eDA.setElement(1, 3.0);
|
||||
|
||||
// Expand the array arbitrarily to 1000 items
|
||||
eDA.setElement(1000, 3.4);
|
||||
|
||||
assertTrue( "The length of the internal array should now be 1001, it isn't", eDA.getInternalLength() == 1001);
|
||||
assertTrue( "The number of elements should now be 1001, it isn't", eDA.getNumElements() == 1001);
|
||||
|
||||
assertTrue( "Uninitialized Elements are default value of 0.0, index 766 wasn't",
|
||||
eDA.getElement( 760 ) == 0.0);
|
||||
|
||||
assertTrue( "The 1000th index should be 3.4, it isn't", eDA.getElement(1000) == 3.4);
|
||||
assertTrue( "The 0th index should be 2.0, it isn't", eDA.getElement(0) == 2.0);
|
||||
|
||||
}
|
||||
|
||||
public void testSetNumberOfElements() {
|
||||
|
||||
ExpandableDoubleArray eDA = new ExpandableDoubleArray();
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
assertTrue( "Number of elements should equal 6", eDA.getNumElements() == 6);
|
||||
|
||||
eDA.setNumElements( 3 );
|
||||
assertTrue( "Number of elements should equal 3", eDA.getNumElements() == 3);
|
||||
|
||||
try {
|
||||
eDA.setNumElements( -3 );
|
||||
fail( "Setting number of elements to negative should've thrown an exception");
|
||||
} catch( IllegalArgumentException iae ) {
|
||||
}
|
||||
|
||||
eDA.setNumElements(1024);
|
||||
assertTrue( "Number of elements should now be 1024", eDA.getNumElements() == 1024);
|
||||
assertTrue( "Element 453 should be a default double", eDA.getElement( 453 ) == 0.0);
|
||||
|
||||
}
|
||||
|
||||
public void testAddElementRolling() {
|
||||
|
||||
ExpandableDoubleArray eDA = new ExpandableDoubleArray();
|
||||
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElement( 1.0 );
|
||||
eDA.addElementRolling( 2.0 );
|
||||
|
||||
assertTrue( "There should be 6 elements in the eda", eDA.getNumElements() == 6);
|
||||
assertTrue( "The last element should be 2.0", eDA.getElement( eDA.getNumElements() -1 ) == 2.0);
|
||||
|
||||
for( int i = 0; i < 1024; i++ ) {
|
||||
eDA.addElementRolling( i );
|
||||
}
|
||||
|
||||
assertTrue( "We just inserted 1024 rolling elements, num elements should still be 6", eDA.getNumElements() == 6);
|
||||
assertTrue( "Even though there are only 6 element, internal storage should be 2048", eDA.getInternalLength() == 2048);
|
||||
assertEquals( "The start index should be 1025", 1025, eDA.getStartIndex());
|
||||
|
||||
eDA.setStartIndex( 0 );
|
||||
|
||||
assertEquals( "There shoud now be 1031 elements in this array", 1031, eDA.getNumElements(), 0.001);
|
||||
assertEquals( "The first element should be 1.0",1.0, eDA.getElement(0), 0.001);
|
||||
|
||||
try {
|
||||
eDA.setStartIndex( 100000 );
|
||||
fail( "TRying to set the start index outside of the current array should have caused an error");
|
||||
} catch( IllegalArgumentException iae ) {
|
||||
}
|
||||
|
||||
try {
|
||||
eDA.setStartIndex( -1 );
|
||||
fail( "TRying to set the start index to a negative number should have caused an error");
|
||||
} catch( IllegalArgumentException iae ) {
|
||||
}
|
||||
}
|
||||
|
||||
/** TEST ERROR CONDITIONS **/
|
||||
|
||||
public void testIllegalInitialCapacity() {
|
||||
|
@ -285,41 +187,4 @@ public class ExpandableDoubleArrayTest extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testGetOutOfBounds() {
|
||||
|
||||
ExpandableDoubleArray eDA = new ExpandableDoubleArray();
|
||||
eDA.addElement(2.0);
|
||||
eDA.addElement(3.0);
|
||||
|
||||
try {
|
||||
eDA.getElement(0);
|
||||
eDA.getElement(1);
|
||||
} catch( NoSuchElementException nsee ) {
|
||||
fail( "There are values for index 0 and 1, this should not have thrown an exception");
|
||||
}
|
||||
|
||||
try {
|
||||
eDA.getElement(2);
|
||||
fail( "There are 2 elements in the array, you asked for index 2 implying that there are 3. " +
"exception should have been thrown");
|
||||
} catch( NoSuchElementException nsee ) {
|
||||
}
|
||||
|
||||
try {
|
||||
eDA.getElement(-234);
|
||||
fail( "You tried to retrieve a negative index, this should have thrown an exception. " );
|
||||
} catch( IllegalArgumentException iae) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testSetOutOfBounds() {
|
||||
|
||||
ExpandableDoubleArray eDA = new ExpandableDoubleArray();
|
||||
|
||||
try {
|
||||
eDA.setElement( -3, 3.4 );
|
||||
fail( "You tried to set an element with a negative index, thisshould have thrown an error");
|
||||
} catch( IllegalArgumentException iae ) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -56,12 +56,11 @@ package org.apache.commons.math;
|
|||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import junit.framework.AssertionFailedError;
|
||||
/**
|
||||
* Test cases for the TestStatistic class.
|
||||
*
|
||||
* @author Phil Steitz
|
||||
* @version $Revision: 1.1 $ $Date: 2003/05/15 21:58:23 $
|
||||
* @version $Revision: 1.2 $ $Date: 2003/05/16 03:55:34 $
|
||||
*/
|
||||
|
||||
public final class TestStatisticTest extends TestCase {
|
||||
|
|
Loading…
Reference in New Issue