KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > stat > descriptive > SummaryStatisticsImplTest


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.stat.descriptive;
17
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22
23 import org.apache.commons.math.TestUtils;
24 /**
25  * Test cases for the {@link DescriptiveStatistics} class.
26  *
27  * @version $Revision$ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
28  */

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