View Javadoc
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.math4.legacy.stat.descriptive;
18  
19  import org.apache.commons.math4.legacy.stat.StatUtils;
20  import org.junit.Assert;
21  import org.junit.Test;
22  
23  
24  /**
25   * This class contains test cases for the ExpandableDoubleArray.
26   *
27   */
28  public abstract class DoubleArrayAbstractTest {
29  
30      protected DoubleArray da = null;
31  
32      // Array used to test rolling
33      protected DoubleArray ra = null;
34  
35      @Test
36      public void testAdd1000() {
37  
38          for (int i = 0; i < 1000; i++) {
39              da.addElement(i);
40          }
41  
42          Assert.assertEquals(
43              "Number of elements should be equal to 1000 after adding 1000 values",
44              1000,
45              da.getNumElements());
46  
47          Assert.assertEquals(
48              "The element at the 56th index should be 56",
49              56.0,
50              da.getElement(56),
51              Double.MIN_VALUE);
52      }
53  
54      @Test
55      public void testGetValues() {
56          double[] controlArray = { 2.0, 4.0, 6.0 };
57  
58          da.addElement(2.0);
59          da.addElement(4.0);
60          da.addElement(6.0);
61          double[] testArray = da.getElements();
62  
63          for (int i = 0; i < da.getNumElements(); i++) {
64              Assert.assertEquals(
65                  "The testArray values should equal the controlArray values, index i: "
66                      + i
67                      + " does not match",
68                  testArray[i],
69                  controlArray[i],
70                  Double.MIN_VALUE);
71          }
72      }
73  
74      @Test
75      public void testAddElementRolling() {
76          ra.addElement(0.5);
77          ra.addElement(1.0);
78          ra.addElement(1.0);
79          ra.addElement(1.0);
80          ra.addElement(1.0);
81          ra.addElement(1.0);
82          ra.addElementRolling(2.0);
83  
84          Assert.assertEquals(
85              "There should be 6 elements in the eda",
86              6,
87              ra.getNumElements());
88          Assert.assertEquals(
89              "The max element should be 2.0",
90              2.0,
91              StatUtils.max(ra.getElements()),
92              Double.MIN_VALUE);
93          Assert.assertEquals(
94              "The min element should be 1.0",
95              1.0,
96              StatUtils.min(ra.getElements()),
97              Double.MIN_VALUE);
98  
99          for (int i = 0; i < 1024; i++) {
100             ra.addElementRolling(i);
101         }
102 
103         Assert.assertEquals(
104             "We just inserted 1024 rolling elements, num elements should still be 6",
105             6,
106             ra.getNumElements());
107     }
108 
109     @Test
110     public void testMinMax() {
111         da.addElement(2.0);
112         da.addElement(22.0);
113         da.addElement(-2.0);
114         da.addElement(21.0);
115         da.addElement(22.0);
116         da.addElement(42.0);
117         da.addElement(62.0);
118         da.addElement(22.0);
119         da.addElement(122.0);
120         da.addElement(1212.0);
121 
122         Assert.assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE);
123         Assert.assertEquals(
124             "Max should be 1212.0",
125             1212.0,
126             StatUtils.max(da.getElements()),
127             Double.MIN_VALUE);
128     }
129 }