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