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  import org.apache.commons.math.TestUtils;
19  import org.apache.commons.math.stat.descriptive.moment.SecondMoment;
20  
21  /**
22   * Test cases for {@link StorelessUnivariateStatistic} classes.
23   * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
24   */
25  public abstract class StorelessUnivariateStatisticAbstractTest
26      extends UnivariateStatisticAbstractTest {
27  
28      public StorelessUnivariateStatisticAbstractTest(String name) {
29          super(name);
30      }
31      
32      /** Small sample arrays */
33      protected double[][] smallSamples = {{}, {1}, {1,2}, {1,2,3}, {1,2,3,4}};
34  
35      /** Return a new instance of the statistic */
36      public abstract UnivariateStatistic getUnivariateStatistic();
37  
38      /**Expected value for  the testArray defined in UnivariateStatisticAbstractTest */
39      public abstract double expectedValue();
40      
41      /** Verify that calling increment() in a loop over testArray results in correct state */
42      public void testIncrementation() throws Exception {
43  
44          StorelessUnivariateStatistic statistic =
45              (StorelessUnivariateStatistic) getUnivariateStatistic();
46  
47          statistic.clear();
48  
49          for (int i = 0; i < testArray.length; i++) {
50              statistic.increment(testArray[i]);
51          }
52  
53          assertEquals(expectedValue(), statistic.getResult(), getTolerance());
54          assertEquals(testArray.length, statistic.getN());
55  
56          statistic.clear();
57  
58          assertTrue(Double.isNaN(statistic.getResult()));
59          assertEquals(0, statistic.getN());
60  
61      }
62  
63      public void testSerialization() throws Exception {
64  
65          StorelessUnivariateStatistic statistic =
66              (StorelessUnivariateStatistic) getUnivariateStatistic();
67          
68          TestUtils.checkSerializedEquality(statistic);
69  
70          statistic.clear();
71  
72          for (int i = 0; i < testArray.length; i++) {
73              statistic.increment(testArray[i]);
74              if(i % 5 == 0)
75                  statistic = (StorelessUnivariateStatistic)TestUtils.serializeAndRecover(statistic); 
76          }
77          
78          TestUtils.checkSerializedEquality(statistic);
79          
80          assertEquals(expectedValue(), statistic.getResult(), getTolerance());
81  
82          statistic.clear();
83  
84          assertTrue(Double.isNaN(statistic.getResult()));
85  
86      }
87      
88      public void testEqualsAndHashCode() {
89          StorelessUnivariateStatistic statistic =
90              (StorelessUnivariateStatistic) getUnivariateStatistic();
91          StorelessUnivariateStatistic statistic2 = null;
92          
93          assertTrue("non-null, compared to null", !statistic.equals(statistic2));
94          assertTrue("reflexive, non-null", statistic.equals(statistic));
95          
96          int emptyHash = statistic.hashCode();
97          statistic2 = (StorelessUnivariateStatistic) getUnivariateStatistic();
98          assertTrue("empty stats should be equal", statistic.equals(statistic2));
99          assertEquals("empty stats should have the same hashcode", 
100                 emptyHash, statistic2.hashCode());
101         
102         statistic.increment(1d);
103         assertTrue("reflexive, non-empty", statistic.equals(statistic));
104         assertTrue("non-empty, compared to empty", !statistic.equals(statistic2));
105         assertTrue("non-empty, compared to empty", !statistic2.equals(statistic));
106         assertTrue("non-empty stat should have different hashcode from empty stat",
107                 statistic.hashCode() != emptyHash);
108         
109         statistic2.increment(1d);
110         assertTrue("stats with same data should be equal", statistic.equals(statistic2));
111         assertEquals("stats with same data should have the same hashcode", 
112                 statistic.hashCode(), statistic2.hashCode());
113         
114         statistic.increment(Double.POSITIVE_INFINITY);
115         assertTrue("stats with different n's should not be equal", !statistic2.equals(statistic));
116         assertTrue("stats with different n's should have different hashcodes",
117                 statistic.hashCode() != statistic2.hashCode());
118         
119         statistic2.increment(Double.POSITIVE_INFINITY);
120         assertTrue("stats with same data should be equal", statistic.equals(statistic2));
121         assertEquals("stats with same data should have the same hashcode", 
122                 statistic.hashCode(), statistic2.hashCode()); 
123         
124         statistic.clear();
125         statistic2.clear();
126         assertTrue("cleared stats should be equal", statistic.equals(statistic2));
127         assertEquals("cleared stats should have thashcode of empty stat", 
128                 emptyHash, statistic2.hashCode());
129         assertEquals("cleared stats should have thashcode of empty stat", 
130                 emptyHash, statistic.hashCode());
131         
132     }
133     
134     public void testMomentSmallSamples() {
135         UnivariateStatistic stat = getUnivariateStatistic();
136         if (stat instanceof SecondMoment) {
137             SecondMoment moment = (SecondMoment) getUnivariateStatistic();
138             assertTrue(Double.isNaN(moment.getResult()));
139             moment.increment(1d);
140             assertEquals(0d, moment.getResult(), 0);
141         }
142     }
143     
144     /** 
145      * Make sure that evaluate(double[]) and inrementAll(double[]), 
146      * getResult() give same results.
147      */
148     public void testConsistency() {
149         StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic();
150         stat.incrementAll(testArray);
151         assertEquals(stat.getResult(), stat.evaluate(testArray), getTolerance());
152         for (int i = 0; i < smallSamples.length; i++) {
153             stat.clear();
154             for (int j =0; j < smallSamples[i].length; j++) {
155                 stat.increment(smallSamples[i][j]);
156             }
157             TestUtils.assertEquals(stat.getResult(), stat.evaluate(smallSamples[i]), getTolerance());
158         }
159     }
160 
161 }