1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.util;
18  
19  import org.apache.commons.math.stat.StatUtils;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * This class contains test cases for the ExpandableDoubleArray.
25   * 
26   * @version $Revision: 670469 $ $Date: 2008-06-23 04:01:38 -0400 (Mon, 23 Jun 2008) $
27   */
28  public abstract class DoubleArrayAbstractTest extends TestCase {
29  
30      protected DoubleArray da = null;
31  
32      // Array used to test rolling
33      protected DoubleArray ra = null;
34  
35      public DoubleArrayAbstractTest(String name) {
36          super(name);
37      }
38  
39      public void testAdd1000() {
40  
41          for (int i = 0; i < 1000; i++) {
42              da.addElement(i);
43          }
44  
45          assertEquals(
46              "Number of elements should be equal to 1000 after adding 1000 values",
47              1000,
48              da.getNumElements());
49  
50          assertEquals(
51              "The element at the 56th index should be 56",
52              56.0,
53              da.getElement(56),
54              Double.MIN_VALUE);
55  
56      }
57  
58      public void testGetValues() {
59          double[] controlArray = { 2.0, 4.0, 6.0 };
60  
61          da.addElement(2.0);
62          da.addElement(4.0);
63          da.addElement(6.0);
64          double[] testArray = da.getElements();
65  
66          for (int i = 0; i < da.getNumElements(); i++) {
67              assertEquals(
68                  "The testArray values should equal the controlArray values, index i: "
69                      + i
70                      + " does not match",
71                  testArray[i],
72                  controlArray[i],
73                  Double.MIN_VALUE);
74          }
75  
76      }
77  
78      public void testAddElementRolling() {
79          ra.addElement(0.5);
80          ra.addElement(1.0);
81          ra.addElement(1.0);
82          ra.addElement(1.0);
83          ra.addElement(1.0);
84          ra.addElement(1.0);
85          ra.addElementRolling(2.0);
86  
87          assertEquals(
88              "There should be 6 elements in the eda",
89              6,
90              ra.getNumElements());
91          assertEquals(
92              "The max element should be 2.0",
93              2.0,
94              StatUtils.max(ra.getElements()),
95              Double.MIN_VALUE);
96          assertEquals(
97              "The min element should be 1.0",
98              1.0,
99              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 }