001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math.distribution;
019    
020    /**
021     * Test cases for CauchyDistribution.
022     * Extends ContinuousDistributionAbstractTest.  See class javadoc for
023     * ContinuousDistributionAbstractTest for details.
024     * 
025     * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
026     */
027    public class CauchyDistributionTest extends ContinuousDistributionAbstractTest  {
028        
029        /**
030         * Constructor for CauchyDistributionTest.
031         * @param arg0
032         */
033        public CauchyDistributionTest(String arg0) {
034            super(arg0);
035        }
036        
037        //-------------- Implementations for abstract methods -----------------------
038        
039        /** Creates the default continuous distribution instance to use in tests. */
040        @Override
041        public ContinuousDistribution makeDistribution() {
042            return new CauchyDistributionImpl(1.2, 2.1);
043        }   
044        
045        /** Creates the default cumulative probability distribution test input values */
046        @Override
047        public double[] makeCumulativeTestPoints() {
048            // quantiles computed using Mathematica 
049            return new double[] {-667.2485619d, -65.6230835d, -25.48302995d,
050                    -12.05887818d, -5.263135428d, 7.663135428d, 14.45887818d,
051                    27.88302995d, 68.0230835d, 669.6485619d};
052        }
053        
054        /** Creates the default cumulative probability density test expected values */
055        @Override
056        public double[] makeCumulativeTestValues() {
057            return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.900d, 0.950d,
058                    0.975d, 0.990d, 0.999d};
059        }
060        
061        //---------------------------- Additional test cases -------------------------
062        
063        public void testInverseCumulativeProbabilityExtremes() throws Exception {
064            setInverseCumulativeTestPoints(new double[] {0.0, 1.0});
065            setInverseCumulativeTestValues(
066                    new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
067            verifyInverseCumulativeProbabilities();
068        }
069        
070        public void testMedian() {
071            CauchyDistribution distribution = (CauchyDistribution) getDistribution();
072            double expected = Math.random();
073            distribution.setMedian(expected);
074            assertEquals(expected, distribution.getMedian(), 0.0);
075        }
076        
077        public void testScale() {
078            CauchyDistribution distribution = (CauchyDistribution) getDistribution();
079            double expected = Math.random();
080            distribution.setScale(expected);
081            assertEquals(expected, distribution.getScale(), 0.0);
082        }
083        
084        public void testSetScale() {
085            CauchyDistribution distribution = (CauchyDistribution) getDistribution();
086            try {
087                distribution.setScale(0.0);
088                fail("Can not have 0.0 scale.");
089            } catch (IllegalArgumentException ex) {
090                // success
091            }
092            
093            try {
094                distribution.setScale(-1.0);
095                fail("Can not have negative scale.");
096            } catch (IllegalArgumentException ex) {
097                // success
098            }
099        }
100    }