KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > distribution > IntegerDistributionAbstractTest


1 /*
2  * Copyright 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.distribution;
17
18 import junit.framework.TestCase;
19
20 /**
21  * Abstract base class for {@link IntegerDistribution} tests.
22  * <p>
23  * To create a concrete test class for an integer distribution implementation,
24  * implement makeDistribution() to return a distribution instance to use in
25  * tests and each of the test data generation methods below. In each case, the
26  * test points and test values arrays returned represent parallel arrays of
27  * inputs and expected values for the distribution returned by makeDistribution().
28  * <p>
29  * makeDensityTestPoints() -- arguments used to test probability density calculation
30  * makeDensityTestValues() -- expected probability densities
31  * makeCumulativeTestPoints() -- arguments used to test cumulative probabilities
32  * makeCumulativeTestValues() -- expected cumulative probabilites
33  * makeInverseCumulativeTestPoints() -- arguments used to test inverse cdf evaluation
34  * makeInverseCumulativeTestValues() -- expected inverse cdf values
35  * <p>
36  * To implement additional test cases with different distribution instances and test data,
37  * use the setXxx methods for the instance data in test cases and call the verifyXxx methods
38  * to verify results.
39  *
40  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
41  */

42 public abstract class IntegerDistributionAbstractTest extends TestCase {
43     
44 //-------------------- Private test instance data -------------------------
45
/** Discrete distribution instance used to perform tests */
46     private IntegerDistribution distribution;
47     
48     /** Tolerance used in comparing expected and returned values */
49     private double tolerance = 1E-4;
50     
51     /** Arguments used to test probability density calculations */
52     private int[] densityTestPoints;
53     
54     /** Values used to test probability density calculations */
55     private double[] densityTestValues;
56     
57     /** Arguments used to test cumulative probability density calculations */
58     private int[] cumulativeTestPoints;
59     
60     /** Values used to test cumulative probability density calculations */
61     private double[] cumulativeTestValues;
62     
63     /** Arguments used to test inverse cumulative probability density calculations */
64     private double[] inverseCumulativeTestPoints;
65     
66     /** Values used to test inverse cumulative probability density calculations */
67     private int[] inverseCumulativeTestValues;
68     
69     //-------------------------------------------------------------------------
70

71     /**
72      * Constructor for IntegerDistributionAbstractTest.
73      * @param name
74      */

75     public IntegerDistributionAbstractTest(String JavaDoc name) {
76         super(name);
77     }
78     
79     //-------------------- Abstract methods -----------------------------------
80

81     /** Creates the default discrete distribution instance to use in tests. */
82     public abstract IntegerDistribution makeDistribution();
83     
84     /** Creates the default probability density test input values */
85     public abstract int[] makeDensityTestPoints();
86     
87     /** Creates the default probability density test expected values */
88     public abstract double[] makeDensityTestValues();
89     
90     /** Creates the default cumulative probability density test input values */
91     public abstract int[] makeCumulativeTestPoints();
92     
93     /** Creates the default cumulative probability density test expected values */
94     public abstract double[] makeCumulativeTestValues();
95     
96     /** Creates the default inverse cumulative probability test input values */
97     public abstract double[] makeInverseCumulativeTestPoints();
98     
99     /** Creates the default inverse cumulative probability density test expected values */
100     public abstract int[] makeInverseCumulativeTestValues();
101     
102     //-------------------- Setup / tear down ----------------------------------
103

104     /**
105      * Setup sets all test instance data to default values
106      */

107     protected void setUp() throws Exception JavaDoc {
108         super.setUp();
109         distribution = makeDistribution();
110         densityTestPoints = makeDensityTestPoints();
111         densityTestValues = makeDensityTestValues();
112         cumulativeTestPoints = makeCumulativeTestPoints();
113         cumulativeTestValues = makeCumulativeTestValues();
114         inverseCumulativeTestPoints = makeInverseCumulativeTestPoints();
115         inverseCumulativeTestValues = makeInverseCumulativeTestValues();
116     }
117     
118     /**
119      * Cleans up test instance data
120      */

121     protected void tearDown() throws Exception JavaDoc {
122         super.tearDown();
123         distribution = null;
124         densityTestPoints = null;
125         densityTestValues = null;
126         cumulativeTestPoints = null;
127         cumulativeTestValues = null;
128         inverseCumulativeTestPoints = null;
129         inverseCumulativeTestValues = null;
130     }
131     
132     //-------------------- Verification methods -------------------------------
133

134     /**
135      * Verifies that probability density calculations match exptected values
136      * using current test instance data
137      */

138     protected void verifyDensities() throws Exception JavaDoc {
139         for (int i = 0; i < densityTestPoints.length; i++) {
140             assertEquals("Incorrect density value returned for " + densityTestPoints[i],
141                     densityTestValues[i],
142                     distribution.probability(densityTestPoints[i]), tolerance);
143         }
144     }
145     
146     /**
147      * Verifies that cumulative probability density calculations match exptected values
148      * using current test instance data
149      */

150     protected void verifyCumulativeProbabilities() throws Exception JavaDoc {
151         for (int i = 0; i < cumulativeTestPoints.length; i++) {
152             assertEquals("Incorrect cumulative probability value returned for " + cumulativeTestPoints[i],
153                     cumulativeTestValues[i],
154                     distribution.cumulativeProbability(cumulativeTestPoints[i]), tolerance);
155         }
156     }
157     
158     /**
159      * Verifies that inverse cumulative probability density calculations match exptected values
160      * using current test instance data
161      */

162     protected void verifyInverseCumulativeProbabilities() throws Exception JavaDoc {
163         for (int i = 0; i < inverseCumulativeTestPoints.length; i++) {
164             assertEquals("Incorrect inverse cumulative probability value returned for "
165                     + inverseCumulativeTestPoints[i], inverseCumulativeTestValues[i],
166                     distribution.inverseCumulativeProbability(inverseCumulativeTestPoints[i]));
167         }
168     }
169     
170     //------------------------ Default test cases -----------------------------
171

172     /**
173      * Verifies that probability density calculations match exptected values
174      * using default test instance data
175      */

176     public void testDensities() throws Exception JavaDoc {
177         verifyDensities();
178     }
179     
180     /**
181      * Verifies that cumulative probability density calculations match exptected values
182      * using default test instance data
183      */

184     public void testCumulativeProbabilities() throws Exception JavaDoc {
185         verifyCumulativeProbabilities();
186     }
187     
188     /**
189      * Verifies that inverse cumulative probability density calculations match exptected values
190      * using default test instance data
191      */

192     public void testInverseCumulativeProbabilities() throws Exception JavaDoc {
193         verifyInverseCumulativeProbabilities();
194     }
195     
196     /**
197      * Verifies that illegal arguments are correctly handled
198      */

199     public void testIllegalArguments() throws Exception JavaDoc {
200         try {
201             distribution.cumulativeProbability(1, 0);
202             fail("Expecting IllegalArgumentException for bad cumulativeProbability interval");
203         } catch (IllegalArgumentException JavaDoc ex) {
204             // expected
205
}
206         try {
207             distribution.inverseCumulativeProbability(-1);
208             fail("Expecting IllegalArgumentException for p = -1");
209         } catch (IllegalArgumentException JavaDoc ex) {
210             // expected
211
}
212         try {
213             distribution.inverseCumulativeProbability(2);
214             fail("Expecting IllegalArgumentException for p = 2");
215         } catch (IllegalArgumentException JavaDoc ex) {
216             // expected
217
}
218     }
219     
220     //------------------ Getters / Setters for test instance data -----------
221
/**
222      * @return Returns the cumulativeTestPoints.
223      */

224     protected int[] getCumulativeTestPoints() {
225         return cumulativeTestPoints;
226     }
227
228     /**
229      * @param cumulativeTestPoints The cumulativeTestPoints to set.
230      */

231     protected void setCumulativeTestPoints(int[] cumulativeTestPoints) {
232         this.cumulativeTestPoints = cumulativeTestPoints;
233     }
234
235     /**
236      * @return Returns the cumulativeTestValues.
237      */

238     protected double[] getCumulativeTestValues() {
239         return cumulativeTestValues;
240     }
241
242     /**
243      * @param cumulativeTestValues The cumulativeTestValues to set.
244      */

245     protected void setCumulativeTestValues(double[] cumulativeTestValues) {
246         this.cumulativeTestValues = cumulativeTestValues;
247     }
248
249     /**
250      * @return Returns the densityTestPoints.
251      */

252     protected int[] getDensityTestPoints() {
253         return densityTestPoints;
254     }
255
256     /**
257      * @param densityTestPoints The densityTestPoints to set.
258      */

259     protected void setDensityTestPoints(int[] densityTestPoints) {
260         this.densityTestPoints = densityTestPoints;
261     }
262
263     /**
264      * @return Returns the densityTestValues.
265      */

266     protected double[] getDensityTestValues() {
267         return densityTestValues;
268     }
269
270     /**
271      * @param densityTestValues The densityTestValues to set.
272      */

273     protected void setDensityTestValues(double[] densityTestValues) {
274         this.densityTestValues = densityTestValues;
275     }
276
277     /**
278      * @return Returns the distribution.
279      */

280     protected IntegerDistribution getDistribution() {
281         return distribution;
282     }
283
284     /**
285      * @param distribution The distribution to set.
286      */

287     protected void setDistribution(IntegerDistribution distribution) {
288         this.distribution = distribution;
289     }
290
291     /**
292      * @return Returns the inverseCumulativeTestPoints.
293      */

294     protected double[] getInverseCumulativeTestPoints() {
295         return inverseCumulativeTestPoints;
296     }
297
298     /**
299      * @param inverseCumulativeTestPoints The inverseCumulativeTestPoints to set.
300      */

301     protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) {
302         this.inverseCumulativeTestPoints = inverseCumulativeTestPoints;
303     }
304
305     /**
306      * @return Returns the inverseCumulativeTestValues.
307      */

308     protected int[] getInverseCumulativeTestValues() {
309         return inverseCumulativeTestValues;
310     }
311
312     /**
313      * @param inverseCumulativeTestValues The inverseCumulativeTestValues to set.
314      */

315     protected void setInverseCumulativeTestValues(int[] inverseCumulativeTestValues) {
316         this.inverseCumulativeTestValues = inverseCumulativeTestValues;
317     }
318
319     /**
320      * @return Returns the tolerance.
321      */

322     protected double getTolerance() {
323         return tolerance;
324     }
325
326     /**
327      * @param tolerance The tolerance to set.
328      */

329     protected void setTolerance(double tolerance) {
330         this.tolerance = tolerance;
331     }
332
333 }
334
Popular Tags