    1   /*
    2    * Copyright 2005 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
    17  package org.apache.commons.math.distribution;
    18
    19  /***
    20   * Test cases for CauchyDistribution.
    21   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
    22   * ContinuousDistributionAbstractTest for details.
    23   *
    24   * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
    25   */
    26  public class CauchyDistributionTest extends ContinuousDistributionAbstractTest  {
    27
    28      /***
    29       * Constructor for CauchyDistributionTest.
    30       * @param arg0
    31       */
    32      public CauchyDistributionTest(String arg0) {
    33          super(arg0);
    34      }
    35
    36      //-------------- Implementations for abstract methods -----------------------
    37
    38      /*** Creates the default continuous distribution instance to use in tests. */
    39      public ContinuousDistribution makeDistribution() {
    40          return DistributionFactory.newInstance().createCauchyDistribution(1.2, 2.1);
    41      }
    42
    43      /*** Creates the default cumulative probability distribution test input values */
    44      public double[] makeCumulativeTestPoints() {
    45          // quantiles computed using Mathematica
    46          return new double[] {-667.2485619d, -65.6230835d, -25.48302995d,
    47                  -12.05887818d, -5.263135428d, 7.663135428d, 14.45887818d,
    48                  27.88302995d, 68.0230835d, 669.6485619d};
    49      }
    50
    51      /*** Creates the default cumulative probability density test expected values */
    52      public double[] makeCumulativeTestValues() {
    53          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.900d, 0.950d,
    54                  0.975d, 0.990d, 0.999d};
    55      }
    56
    57      //---------------------------- Additional test cases -------------------------
    58
    59      public void testInverseCumulativeProbabilityExtremes() throws Exception {
    60          setInverseCumulativeTestPoints(new double[] {0.0, 1.0});
    61          setInverseCumulativeTestValues(
    62                  new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
    63          verifyInverseCumulativeProbabilities();
    64      }
    65
    66      public void testMedian() {
    67          CauchyDistribution distribution = (CauchyDistribution) getDistribution();
    68          double expected = Math.random();
    69          distribution.setMedian(expected);
    70          assertEquals(expected, distribution.getMedian(), 0.0);
    71      }
    72
    73      public void testScale() {
    74          CauchyDistribution distribution = (CauchyDistribution) getDistribution();
    75          double expected = Math.random();
    76          distribution.setScale(expected);
    77          assertEquals(expected, distribution.getScale(), 0.0);
    78      }
    79
    80      public void testSetScale() {
    81          CauchyDistribution distribution = (CauchyDistribution) getDistribution();
    82          try {
    83              distribution.setScale(0.0);
    84              fail("Can not have 0.0 scale.");
    85          } catch (IllegalArgumentException ex) {
    86              // success
    87          }
    88
    89          try {
    90              distribution.setScale(-1.0);
    91              fail("Can not have negative scale.");
    92          } catch (IllegalArgumentException ex) {
    93              // success
    94          }
    95      }
    96  }