001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.util;
017:
018: import org.apache.commons.math.stat.StatUtils;
019:
020: import junit.framework.TestCase;
021:
022: /**
023: * This class contains test cases for the ExpandableDoubleArray.
024: *
025: * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
026: */
027: public abstract class DoubleArrayAbstractTest extends TestCase {
028:
029: protected DoubleArray da = null;
030:
031: // Array used to test rolling
032: protected DoubleArray ra = null;
033:
034: public DoubleArrayAbstractTest(String name) {
035: super (name);
036: }
037:
038: public void testAdd1000() {
039:
040: for (int i = 0; i < 1000; i++) {
041: da.addElement(i);
042: }
043:
044: assertEquals(
045: "Number of elements should be equal to 1000 after adding 1000 values",
046: 1000, da.getNumElements());
047:
048: assertEquals("The element at the 56th index should be 56",
049: 56.0, da.getElement(56), Double.MIN_VALUE);
050:
051: }
052:
053: public void testGetValues() {
054: double[] controlArray = { 2.0, 4.0, 6.0 };
055:
056: da.addElement(2.0);
057: da.addElement(4.0);
058: da.addElement(6.0);
059: double[] testArray = da.getElements();
060:
061: for (int i = 0; i < da.getNumElements(); i++) {
062: assertEquals(
063: "The testArray values should equal the controlArray values, index i: "
064: + i + " does not match", testArray[i],
065: controlArray[i], Double.MIN_VALUE);
066: }
067:
068: }
069:
070: public void testAddElementRolling() {
071: ra.addElement(0.5);
072: ra.addElement(1.0);
073: ra.addElement(1.0);
074: ra.addElement(1.0);
075: ra.addElement(1.0);
076: ra.addElement(1.0);
077: ra.addElementRolling(2.0);
078:
079: assertEquals("There should be 6 elements in the eda", 6, ra
080: .getNumElements());
081: assertEquals("The max element should be 2.0", 2.0, StatUtils
082: .max(ra.getElements()), Double.MIN_VALUE);
083: assertEquals("The min element should be 1.0", 1.0, StatUtils
084: .min(ra.getElements()), Double.MIN_VALUE);
085:
086: for (int i = 0; i < 1024; i++) {
087: ra.addElementRolling(i);
088: }
089:
090: assertEquals(
091: "We just inserted 1024 rolling elements, num elements should still be 6",
092: 6, ra.getNumElements());
093: }
094:
095: public void testMinMax() {
096: da.addElement(2.0);
097: da.addElement(22.0);
098: da.addElement(-2.0);
099: da.addElement(21.0);
100: da.addElement(22.0);
101: da.addElement(42.0);
102: da.addElement(62.0);
103: da.addElement(22.0);
104: da.addElement(122.0);
105: da.addElement(1212.0);
106:
107: assertEquals("Min should be -2.0", -2.0, StatUtils.min(da
108: .getElements()), Double.MIN_VALUE);
109: assertEquals("Max should be 1212.0", 1212.0, StatUtils.max(da
110: .getElements()), Double.MIN_VALUE);
111: }
112:
113: }
|