    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.util;
    17
    18  import org.apache.commons.math.stat.StatUtils;
    19
    20  import junit.framework.TestCase;
    21
    22  /***
    23   * This class contains test cases for the ExpandableDoubleArray.
    24   *
    25   * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
    26   */
    27  public abstract class DoubleArrayAbstractTest extends TestCase {
    28
    29      protected DoubleArray da = null;
    30
    31      // Array used to test rolling
    32      protected DoubleArray ra = null;
    33
    34      public DoubleArrayAbstractTest(String name) {
    35          super(name);
    36      }
    37
    38      public void testAdd1000() {
    39
    40          for (int i = 0; i < 1000; i++) {
    41              da.addElement(i);
    42          }
    43
    44          assertEquals(
    45              "Number of elements should be equal to 1000 after adding 1000 values",
    46              1000,
    47              da.getNumElements());
    48
    49          assertEquals(
    50              "The element at the 56th index should be 56",
    51              56.0,
    52              da.getElement(56),
    53              Double.MIN_VALUE);
    54
    55      }
    56
    57      public void testGetValues() {
    58          double[] controlArray = { 2.0, 4.0, 6.0 };
    59
    60          da.addElement(2.0);
    61          da.addElement(4.0);
    62          da.addElement(6.0);
    63          double[] testArray = da.getElements();
    64
    65          for (int i = 0; i < da.getNumElements(); i++) {
    66              assertEquals(
    67                  "The testArray values should equal the controlArray values, index i: "
    68                      + i
    69                      + " does not match",
    70                  testArray[i],
    71                  controlArray[i],
    72                  Double.MIN_VALUE);
    73          }
    74
    75      }
    76
    77      public void testAddElementRolling() {
    78          ra.addElement(0.5);
    79          ra.addElement(1.0);
    80          ra.addElement(1.0);
    81          ra.addElement(1.0);
    82          ra.addElement(1.0);
    83          ra.addElement(1.0);
    84          ra.addElementRolling(2.0);
    85
    86          assertEquals(
    87              "There should be 6 elements in the eda",
    88              6,
    89              ra.getNumElements());
    90          assertEquals(
    91              "The max element should be 2.0",
    92              2.0,
    93              StatUtils.max(ra.getElements()),
    94              Double.MIN_VALUE);
    95          assertEquals(
    96              "The min element should be 1.0",
    97              1.0,
    98              StatUtils.min(ra.getElements()),
    99              Double.MIN_VALUE);
    100
    101         for (int i = 0; i < 1024; i++) {
    102             ra.addElementRolling(i);
    103         }
    104
    105         assertEquals(
    106             "We just inserted 1024 rolling elements, num elements should still be 6",
    107             6,
    108             ra.getNumElements());
    109     }
    110
    111     public void testMinMax() {
    112         da.addElement(2.0);
    113         da.addElement(22.0);
    114         da.addElement(-2.0);
    115         da.addElement(21.0);
    116         da.addElement(22.0);
    117         da.addElement(42.0);
    118         da.addElement(62.0);
    119         da.addElement(22.0);
    120         da.addElement(122.0);
    121         da.addElement(1212.0);
    122
    123         assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE);
    124         assertEquals(
    125             "Max should be 1212.0",
    126             1212.0,
    127             StatUtils.max(da.getElements()),
    128             Double.MIN_VALUE);
    129     }
    130
    131 }