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.stat.descriptive;
18  
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.commons.math.TestUtils;
23  /**
24   * Test cases for the {@link SummaryStatisticsImpl} class.
25   *
26   * @version $Revision: 602305 $ $Date: 2007-12-08 03:51:23 +0100 (sam., 08 déc. 2007) $
27   * @deprecated should be moved down into SummaryStatisticsTest
28   *   when SummaryStatisticsImpl is removed in 2.0
29   */
30  
31  public abstract class SummaryStatisticsAbstractTest extends TestCase {
32      private double one = 1;
33      private float twoF = 2;
34      private long twoL = 2;
35      private int three = 3;
36      private double mean = 2;
37      private double sumSq = 18;
38      private double sum = 8;
39      private double var = 0.666666666666666666667;
40      private double std = Math.sqrt(var);
41      private double n = 4;
42      private double min = 1;
43      private double max = 3;
44      private double tolerance = 10E-15;
45  
46      public SummaryStatisticsAbstractTest(String name) {
47          super(name);
48      }
49  
50      protected abstract SummaryStatistics createSummaryStatistics();
51  
52      /** test stats */
53      public void testStats() {
54          SummaryStatistics u = createSummaryStatistics();
55          assertEquals("total count",0,u.getN(),tolerance);
56          u.addValue(one);
57          u.addValue(twoF);
58          u.addValue(twoL);
59          u.addValue(three);
60          assertEquals("N",n,u.getN(),tolerance);
61          assertEquals("sum",sum,u.getSum(),tolerance);
62          assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
63          assertEquals("var",var,u.getVariance(),tolerance);
64          assertEquals("std",std,u.getStandardDeviation(),tolerance);
65          assertEquals("mean",mean,u.getMean(),tolerance);
66          assertEquals("min",min,u.getMin(),tolerance);
67          assertEquals("max",max,u.getMax(),tolerance);
68          u.clear();
69          assertEquals("total count",0,u.getN(),tolerance);    
70      }     
71  
72      public void testN0andN1Conditions() throws Exception {
73          SummaryStatistics u = createSummaryStatistics();
74          assertTrue("Mean of n = 0 set should be NaN", 
75                  Double.isNaN( u.getMean() ) );
76          assertTrue("Standard Deviation of n = 0 set should be NaN", 
77                  Double.isNaN( u.getStandardDeviation() ) );
78          assertTrue("Variance of n = 0 set should be NaN", 
79                  Double.isNaN(u.getVariance() ) );
80  
81          /* n=1 */
82          u.addValue(one);
83          assertTrue("mean should be one (n = 1)", 
84                  u.getMean() == one);
85          assertTrue("geometric should be one (n = 1) instead it is " + u.getGeometricMean(), 
86                  u.getGeometricMean() == one);
87          assertTrue("Std should be zero (n = 1)", 
88                  u.getStandardDeviation() == 0.0);
89          assertTrue("variance should be zero (n = 1)", 
90                  u.getVariance() == 0.0);
91  
92          /* n=2 */               
93          u.addValue(twoF);
94          assertTrue("Std should not be zero (n = 2)", 
95                  u.getStandardDeviation() != 0.0);
96          assertTrue("variance should not be zero (n = 2)", 
97                  u.getVariance() != 0.0);
98  
99      }
100 
101     public void testProductAndGeometricMean() throws Exception {
102         SummaryStatistics u = createSummaryStatistics();
103         u.addValue( 1.0 );
104         u.addValue( 2.0 );
105         u.addValue( 3.0 );
106         u.addValue( 4.0 );
107 
108         assertEquals( "Geometric mean not expected", 2.213364, 
109                 u.getGeometricMean(), 0.00001 );
110     }
111 
112     public void testNaNContracts() {
113         SummaryStatistics u = createSummaryStatistics();
114         assertTrue("mean not NaN",Double.isNaN(u.getMean())); 
115         assertTrue("min not NaN",Double.isNaN(u.getMin())); 
116         assertTrue("std dev not NaN",Double.isNaN(u.getStandardDeviation())); 
117         assertTrue("var not NaN",Double.isNaN(u.getVariance())); 
118         assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
119 
120         u.addValue(1.0);
121 
122         assertEquals( "mean not expected", 1.0, 
123                 u.getMean(), Double.MIN_VALUE);
124         assertEquals( "variance not expected", 0.0, 
125                 u.getVariance(), Double.MIN_VALUE);
126         assertEquals( "geometric mean not expected", 1.0, 
127                 u.getGeometricMean(), Double.MIN_VALUE);
128 
129         u.addValue(-1.0);
130 
131         assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
132 
133         u.addValue(0.0);
134 
135         assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
136 
137         //FiXME: test all other NaN contract specs
138     }
139 
140     public void testGetSummary() {
141         SummaryStatistics u = createSummaryStatistics();
142         StatisticalSummary summary = u.getSummary();
143         verifySummary(u, summary);
144         u.addValue(1d);
145         summary = u.getSummary();
146         verifySummary(u, summary);
147         u.addValue(2d);
148         summary = u.getSummary();
149         verifySummary(u, summary);
150         u.addValue(2d);
151         summary = u.getSummary();
152         verifySummary(u, summary);     
153     }
154 
155     public void testSerialization() {
156         SummaryStatistics u = createSummaryStatistics();
157         // Empty test
158         TestUtils.checkSerializedEquality(u);
159         SummaryStatistics s = (SummaryStatistics) TestUtils.serializeAndRecover(u);
160         StatisticalSummary summary = s.getSummary();
161         verifySummary(u, summary);
162 
163         // Add some data
164         u.addValue(2d);
165         u.addValue(1d);
166         u.addValue(3d);
167         u.addValue(4d);
168         u.addValue(5d);
169 
170         // Test again
171         TestUtils.checkSerializedEquality(u);
172         s = (SummaryStatistics) TestUtils.serializeAndRecover(u);
173         summary = s.getSummary();
174         verifySummary(u, summary);
175 
176     }
177 
178     public void testEqualsAndHashCode() {
179         SummaryStatistics u = createSummaryStatistics();
180         SummaryStatistics t = null;
181         int emptyHash = u.hashCode();
182         assertTrue("reflexive", u.equals(u));
183         assertFalse("non-null compared to null", u.equals(t));
184         assertFalse("wrong type", u.equals(new Double(0)));
185         t = createSummaryStatistics();
186         assertTrue("empty instances should be equal", t.equals(u));
187         assertTrue("empty instances should be equal", u.equals(t));
188         assertEquals("empty hash code", emptyHash, t.hashCode());
189 
190         // Add some data to u
191         u.addValue(2d);
192         u.addValue(1d);
193         u.addValue(3d);
194         u.addValue(4d);
195         assertFalse("different n's should make instances not equal", t.equals(u));
196         assertFalse("different n's should make instances not equal", u.equals(t));
197         assertTrue("different n's should make hashcodes different", 
198                 u.hashCode() != t.hashCode());
199 
200         //Add data in same order to t
201         t.addValue(2d);
202         t.addValue(1d);
203         t.addValue(3d);
204         t.addValue(4d);
205         assertTrue("summaries based on same data should be equal", t.equals(u));
206         assertTrue("summaries based on same data should be equal", u.equals(t));
207         assertEquals("summaries based on same data should have same hashcodes", 
208                 u.hashCode(), t.hashCode());   
209 
210         // Clear and make sure summaries are indistinguishable from empty summary
211         u.clear();
212         t.clear();
213         assertTrue("empty instances should be equal", t.equals(u));
214         assertTrue("empty instances should be equal", u.equals(t));
215         assertEquals("empty hash code", emptyHash, t.hashCode());
216         assertEquals("empty hash code", emptyHash, u.hashCode());
217     }
218 
219     private void verifySummary(SummaryStatistics u, StatisticalSummary s) {
220         assertEquals("N",s.getN(),u.getN());
221         TestUtils.assertEquals("sum",s.getSum(),u.getSum(),tolerance);
222         TestUtils.assertEquals("var",s.getVariance(),u.getVariance(),tolerance);
223         TestUtils.assertEquals("std",s.getStandardDeviation(),u.getStandardDeviation(),tolerance);
224         TestUtils.assertEquals("mean",s.getMean(),u.getMean(),tolerance);
225         TestUtils.assertEquals("min",s.getMin(),u.getMin(),tolerance);
226         TestUtils.assertEquals("max",s.getMax(),u.getMax(),tolerance);   
227     }
228 }