001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.distribution;
017:
018: import junit.framework.TestCase;
019:
020: /**
021: * Abstract base class for {@link IntegerDistribution} tests.
022: * <p>
023: * To create a concrete test class for an integer distribution implementation,
024: * implement makeDistribution() to return a distribution instance to use in
025: * tests and each of the test data generation methods below. In each case, the
026: * test points and test values arrays returned represent parallel arrays of
027: * inputs and expected values for the distribution returned by makeDistribution().
028: * <p>
029: * makeDensityTestPoints() -- arguments used to test probability density calculation
030: * makeDensityTestValues() -- expected probability densities
031: * makeCumulativeTestPoints() -- arguments used to test cumulative probabilities
032: * makeCumulativeTestValues() -- expected cumulative probabilites
033: * makeInverseCumulativeTestPoints() -- arguments used to test inverse cdf evaluation
034: * makeInverseCumulativeTestValues() -- expected inverse cdf values
035: * <p>
036: * To implement additional test cases with different distribution instances and test data,
037: * use the setXxx methods for the instance data in test cases and call the verifyXxx methods
038: * to verify results.
039: *
040: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
041: */
042: public abstract class IntegerDistributionAbstractTest extends TestCase {
043:
044: //-------------------- Private test instance data -------------------------
045: /** Discrete distribution instance used to perform tests */
046: private IntegerDistribution distribution;
047:
048: /** Tolerance used in comparing expected and returned values */
049: private double tolerance = 1E-4;
050:
051: /** Arguments used to test probability density calculations */
052: private int[] densityTestPoints;
053:
054: /** Values used to test probability density calculations */
055: private double[] densityTestValues;
056:
057: /** Arguments used to test cumulative probability density calculations */
058: private int[] cumulativeTestPoints;
059:
060: /** Values used to test cumulative probability density calculations */
061: private double[] cumulativeTestValues;
062:
063: /** Arguments used to test inverse cumulative probability density calculations */
064: private double[] inverseCumulativeTestPoints;
065:
066: /** Values used to test inverse cumulative probability density calculations */
067: private int[] inverseCumulativeTestValues;
068:
069: //-------------------------------------------------------------------------
070:
071: /**
072: * Constructor for IntegerDistributionAbstractTest.
073: * @param name
074: */
075: public IntegerDistributionAbstractTest(String name) {
076: super (name);
077: }
078:
079: //-------------------- Abstract methods -----------------------------------
080:
081: /** Creates the default discrete distribution instance to use in tests. */
082: public abstract IntegerDistribution makeDistribution();
083:
084: /** Creates the default probability density test input values */
085: public abstract int[] makeDensityTestPoints();
086:
087: /** Creates the default probability density test expected values */
088: public abstract double[] makeDensityTestValues();
089:
090: /** Creates the default cumulative probability density test input values */
091: public abstract int[] makeCumulativeTestPoints();
092:
093: /** Creates the default cumulative probability density test expected values */
094: public abstract double[] makeCumulativeTestValues();
095:
096: /** Creates the default inverse cumulative probability test input values */
097: public abstract double[] makeInverseCumulativeTestPoints();
098:
099: /** 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 {
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 {
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 {
139: for (int i = 0; i < densityTestPoints.length; i++) {
140: assertEquals("Incorrect density value returned for "
141: + densityTestPoints[i], densityTestValues[i],
142: distribution.probability(densityTestPoints[i]),
143: tolerance);
144: }
145: }
146:
147: /**
148: * Verifies that cumulative probability density calculations match exptected values
149: * using current test instance data
150: */
151: protected void verifyCumulativeProbabilities() throws Exception {
152: for (int i = 0; i < cumulativeTestPoints.length; i++) {
153: assertEquals(
154: "Incorrect cumulative probability value returned for "
155: + cumulativeTestPoints[i],
156: cumulativeTestValues[i],
157: distribution
158: .cumulativeProbability(cumulativeTestPoints[i]),
159: tolerance);
160: }
161: }
162:
163: /**
164: * Verifies that inverse cumulative probability density calculations match exptected values
165: * using current test instance data
166: */
167: protected void verifyInverseCumulativeProbabilities()
168: throws Exception {
169: for (int i = 0; i < inverseCumulativeTestPoints.length; i++) {
170: assertEquals(
171: "Incorrect inverse cumulative probability value returned for "
172: + inverseCumulativeTestPoints[i],
173: inverseCumulativeTestValues[i],
174: distribution
175: .inverseCumulativeProbability(inverseCumulativeTestPoints[i]));
176: }
177: }
178:
179: //------------------------ Default test cases -----------------------------
180:
181: /**
182: * Verifies that probability density calculations match exptected values
183: * using default test instance data
184: */
185: public void testDensities() throws Exception {
186: verifyDensities();
187: }
188:
189: /**
190: * Verifies that cumulative probability density calculations match exptected values
191: * using default test instance data
192: */
193: public void testCumulativeProbabilities() throws Exception {
194: verifyCumulativeProbabilities();
195: }
196:
197: /**
198: * Verifies that inverse cumulative probability density calculations match exptected values
199: * using default test instance data
200: */
201: public void testInverseCumulativeProbabilities() throws Exception {
202: verifyInverseCumulativeProbabilities();
203: }
204:
205: /**
206: * Verifies that illegal arguments are correctly handled
207: */
208: public void testIllegalArguments() throws Exception {
209: try {
210: distribution.cumulativeProbability(1, 0);
211: fail("Expecting IllegalArgumentException for bad cumulativeProbability interval");
212: } catch (IllegalArgumentException ex) {
213: // expected
214: }
215: try {
216: distribution.inverseCumulativeProbability(-1);
217: fail("Expecting IllegalArgumentException for p = -1");
218: } catch (IllegalArgumentException ex) {
219: // expected
220: }
221: try {
222: distribution.inverseCumulativeProbability(2);
223: fail("Expecting IllegalArgumentException for p = 2");
224: } catch (IllegalArgumentException ex) {
225: // expected
226: }
227: }
228:
229: //------------------ Getters / Setters for test instance data -----------
230: /**
231: * @return Returns the cumulativeTestPoints.
232: */
233: protected int[] getCumulativeTestPoints() {
234: return cumulativeTestPoints;
235: }
236:
237: /**
238: * @param cumulativeTestPoints The cumulativeTestPoints to set.
239: */
240: protected void setCumulativeTestPoints(int[] cumulativeTestPoints) {
241: this .cumulativeTestPoints = cumulativeTestPoints;
242: }
243:
244: /**
245: * @return Returns the cumulativeTestValues.
246: */
247: protected double[] getCumulativeTestValues() {
248: return cumulativeTestValues;
249: }
250:
251: /**
252: * @param cumulativeTestValues The cumulativeTestValues to set.
253: */
254: protected void setCumulativeTestValues(double[] cumulativeTestValues) {
255: this .cumulativeTestValues = cumulativeTestValues;
256: }
257:
258: /**
259: * @return Returns the densityTestPoints.
260: */
261: protected int[] getDensityTestPoints() {
262: return densityTestPoints;
263: }
264:
265: /**
266: * @param densityTestPoints The densityTestPoints to set.
267: */
268: protected void setDensityTestPoints(int[] densityTestPoints) {
269: this .densityTestPoints = densityTestPoints;
270: }
271:
272: /**
273: * @return Returns the densityTestValues.
274: */
275: protected double[] getDensityTestValues() {
276: return densityTestValues;
277: }
278:
279: /**
280: * @param densityTestValues The densityTestValues to set.
281: */
282: protected void setDensityTestValues(double[] densityTestValues) {
283: this .densityTestValues = densityTestValues;
284: }
285:
286: /**
287: * @return Returns the distribution.
288: */
289: protected IntegerDistribution getDistribution() {
290: return distribution;
291: }
292:
293: /**
294: * @param distribution The distribution to set.
295: */
296: protected void setDistribution(IntegerDistribution distribution) {
297: this .distribution = distribution;
298: }
299:
300: /**
301: * @return Returns the inverseCumulativeTestPoints.
302: */
303: protected double[] getInverseCumulativeTestPoints() {
304: return inverseCumulativeTestPoints;
305: }
306:
307: /**
308: * @param inverseCumulativeTestPoints The inverseCumulativeTestPoints to set.
309: */
310: protected void setInverseCumulativeTestPoints(
311: double[] inverseCumulativeTestPoints) {
312: this .inverseCumulativeTestPoints = inverseCumulativeTestPoints;
313: }
314:
315: /**
316: * @return Returns the inverseCumulativeTestValues.
317: */
318: protected int[] getInverseCumulativeTestValues() {
319: return inverseCumulativeTestValues;
320: }
321:
322: /**
323: * @param inverseCumulativeTestValues The inverseCumulativeTestValues to set.
324: */
325: protected void setInverseCumulativeTestValues(
326: int[] inverseCumulativeTestValues) {
327: this .inverseCumulativeTestValues = inverseCumulativeTestValues;
328: }
329:
330: /**
331: * @return Returns the tolerance.
332: */
333: protected double getTolerance() {
334: return tolerance;
335: }
336:
337: /**
338: * @param tolerance The tolerance to set.
339: */
340: protected void setTolerance(double tolerance) {
341: this.tolerance = tolerance;
342: }
343:
344: }
|