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