1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to You under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
9    * or agreed to in writing, software distributed under the License is
10   * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language
12   * governing permissions and limitations under the License.
13   */
14  package org.apache.commons.math.stat.descriptive;
15  
16  import junit.framework.Test;
17  import junit.framework.TestSuite;
18  
19  import org.apache.commons.math.stat.descriptive.rank.Percentile;
20  
21  /**
22   * Test cases for the DescriptiveStatistics class.
23   * When DescriptiveStatisticsImpl is removed, this class should replace
24   * DescriptiveStatisticsAbstractTest
25   * 
26   * @version $Revision: 592121 $ $Date: 2007-08-16 15:36:33 -0500 (Thu, 16 Aug
27   *          2007) $
28   */
29  public final class DescriptiveStatisticsTest extends DescriptiveStatisticsAbstractTest {
30  
31      public DescriptiveStatisticsTest(String name) {
32          super(name);
33      }
34  
35      public static Test suite() {
36          TestSuite suite = new TestSuite(DescriptiveStatisticsTest.class);
37          suite.setName("DescriptiveStatistics Tests");
38          return suite;
39      }
40  
41      protected DescriptiveStatistics createDescriptiveStatistics() {
42          return new DescriptiveStatistics();
43      }
44      
45      public void testSetterInjection() throws Exception {
46          DescriptiveStatistics stats = new DescriptiveStatistics();
47          stats.addValue(1);
48          stats.addValue(3);
49          assertEquals(2, stats.getMean(), 1E-10);
50          // Now lets try some new math
51          stats.setMeanImpl(new deepMean());
52          assertEquals(42, stats.getMean(), 1E-10);
53      }
54      
55      public void testPercentileSetter() throws Exception {
56          DescriptiveStatistics stats = new DescriptiveStatistics();
57          stats.addValue(1);
58          stats.addValue(2);
59          stats.addValue(3);
60          assertEquals(2, stats.getPercentile(50.0), 1E-10);
61          
62          // Inject wrapped Percentile impl
63          stats.setPercentileImpl(new goodPercentile());
64          assertEquals(2, stats.getPercentile(50.0), 1E-10);
65          
66          // Try "new math" impl
67          stats.setPercentileImpl(new subPercentile());
68          assertEquals(10.0, stats.getPercentile(10.0), 1E-10);
69          
70          // Try to set bad impl
71          try {
72              stats.setPercentileImpl(new badPercentile()); 
73              fail("Expecting IllegalArgumentException");
74          } catch (IllegalArgumentException ex) {
75              // expected
76          }
77      }
78      
79      // Test UnivariateStatistics impls for setter injection tests
80      
81      /**
82       * A new way to compute the mean 
83       */
84      static class deepMean implements UnivariateStatistic {
85          private static final long serialVersionUID = 9108665370122541953L;
86  
87          public double evaluate(double[] values, int begin, int length) {
88              return 42;
89          }
90  
91          public double evaluate(double[] values) {
92              return 42;
93          }  
94      }
95      
96      /**
97       * Test percentile implementation - wraps a Percentile
98       */
99      static class goodPercentile implements UnivariateStatistic {
100         private static final long serialVersionUID = 801005145532790795L;
101         private Percentile percentile = new Percentile();
102         public void setQuantile(double quantile) {
103             percentile.setQuantile(quantile);
104         }
105         public double evaluate(double[] values, int begin, int length) {
106             return percentile.evaluate(values, begin, length);
107         }
108         public double evaluate(double[] values) {
109             return percentile.evaluate(values);
110         }  
111     }
112     
113     /**
114      * Test percentile subclass - another "new math" impl
115      * Always returns currently set quantile
116      */
117     static class subPercentile extends Percentile {
118         public double evaluate(double[] values, int begin, int length) {
119             return getQuantile();
120         }
121         public double evaluate(double[] values) {
122             return getQuantile();
123         }  
124         private static final long serialVersionUID = 8040701391045914979L;
125     }
126     
127     /**
128      * "Bad" test percentile implementation - no setQuantile
129      */
130     static class badPercentile implements UnivariateStatistic {
131         private static final long serialVersionUID = -707437653388052183L;
132         private Percentile percentile = new Percentile();
133         public double evaluate(double[] values, int begin, int length) {
134             return percentile.evaluate(values, begin, length);
135         }
136         public double evaluate(double[] values) {
137             return percentile.evaluate(values);
138         }  
139     }
140 }