1 16 package org.apache.commons.math.distribution; 17 18 import junit.framework.TestCase; 19 20 42 public abstract class IntegerDistributionAbstractTest extends TestCase { 43 44 46 private IntegerDistribution distribution; 47 48 49 private double tolerance = 1E-4; 50 51 52 private int[] densityTestPoints; 53 54 55 private double[] densityTestValues; 56 57 58 private int[] cumulativeTestPoints; 59 60 61 private double[] cumulativeTestValues; 62 63 64 private double[] inverseCumulativeTestPoints; 65 66 67 private int[] inverseCumulativeTestValues; 68 69 71 75 public IntegerDistributionAbstractTest(String name) { 76 super(name); 77 } 78 79 81 82 public abstract IntegerDistribution makeDistribution(); 83 84 85 public abstract int[] makeDensityTestPoints(); 86 87 88 public abstract double[] makeDensityTestValues(); 89 90 91 public abstract int[] makeCumulativeTestPoints(); 92 93 94 public abstract double[] makeCumulativeTestValues(); 95 96 97 public abstract double[] makeInverseCumulativeTestPoints(); 98 99 100 public abstract int[] makeInverseCumulativeTestValues(); 101 102 104 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 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 134 138 protected void verifyDensities() throws Exception { 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 150 protected void verifyCumulativeProbabilities() throws Exception { 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 162 protected void verifyInverseCumulativeProbabilities() throws Exception { 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 172 176 public void testDensities() throws Exception { 177 verifyDensities(); 178 } 179 180 184 public void testCumulativeProbabilities() throws Exception { 185 verifyCumulativeProbabilities(); 186 } 187 188 192 public void testInverseCumulativeProbabilities() throws Exception { 193 verifyInverseCumulativeProbabilities(); 194 } 195 196 199 public void testIllegalArguments() throws Exception { 200 try { 201 distribution.cumulativeProbability(1, 0); 202 fail("Expecting IllegalArgumentException for bad cumulativeProbability interval"); 203 } catch (IllegalArgumentException ex) { 204 } 206 try { 207 distribution.inverseCumulativeProbability(-1); 208 fail("Expecting IllegalArgumentException for p = -1"); 209 } catch (IllegalArgumentException ex) { 210 } 212 try { 213 distribution.inverseCumulativeProbability(2); 214 fail("Expecting IllegalArgumentException for p = 2"); 215 } catch (IllegalArgumentException ex) { 216 } 218 } 219 220 224 protected int[] getCumulativeTestPoints() { 225 return cumulativeTestPoints; 226 } 227 228 231 protected void setCumulativeTestPoints(int[] cumulativeTestPoints) { 232 this.cumulativeTestPoints = cumulativeTestPoints; 233 } 234 235 238 protected double[] getCumulativeTestValues() { 239 return cumulativeTestValues; 240 } 241 242 245 protected void setCumulativeTestValues(double[] cumulativeTestValues) { 246 this.cumulativeTestValues = cumulativeTestValues; 247 } 248 249 252 protected int[] getDensityTestPoints() { 253 return densityTestPoints; 254 } 255 256 259 protected void setDensityTestPoints(int[] densityTestPoints) { 260 this.densityTestPoints = densityTestPoints; 261 } 262 263 266 protected double[] getDensityTestValues() { 267 return densityTestValues; 268 } 269 270 273 protected void setDensityTestValues(double[] densityTestValues) { 274 this.densityTestValues = densityTestValues; 275 } 276 277 280 protected IntegerDistribution getDistribution() { 281 return distribution; 282 } 283 284 287 protected void setDistribution(IntegerDistribution distribution) { 288 this.distribution = distribution; 289 } 290 291 294 protected double[] getInverseCumulativeTestPoints() { 295 return inverseCumulativeTestPoints; 296 } 297 298 301 protected void setInverseCumulativeTestPoints(double[] inverseCumulativeTestPoints) { 302 this.inverseCumulativeTestPoints = inverseCumulativeTestPoints; 303 } 304 305 308 protected int[] getInverseCumulativeTestValues() { 309 return inverseCumulativeTestValues; 310 } 311 312 315 protected void setInverseCumulativeTestValues(int[] inverseCumulativeTestValues) { 316 this.inverseCumulativeTestValues = inverseCumulativeTestValues; 317 } 318 319 322 protected double getTolerance() { 323 return tolerance; 324 } 325 326 329 protected void setTolerance(double tolerance) { 330 this.tolerance = tolerance; 331 } 332 333 } 334 | Popular Tags |