1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math.distribution;
19  
20  /**
21   * Test cases for WeibullDistribution.
22   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
23   * ContinuousDistributionAbstractTest for details.
24   * 
25   * @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
26   */
27  public class WeibullDistributionTest extends ContinuousDistributionAbstractTest  {
28      
29      /**
30       * Constructor for CauchyDistributionTest.
31       * @param arg0
32       */
33      public WeibullDistributionTest(String arg0) {
34          super(arg0);
35      }
36      
37      //-------------- Implementations for abstract methods -----------------------
38      
39      /** Creates the default continuous distribution instance to use in tests. */
40      @Override
41      public ContinuousDistribution makeDistribution() {
42          return new WeibullDistributionImpl(1.2, 2.1);
43      }   
44      
45      /** Creates the default cumulative probability distribution test input values */
46      @Override
47      public double[] makeCumulativeTestPoints() {
48          // quantiles computed using Mathematica 
49          return new double[] {0.00664355181d, 0.04543282833d, 0.09811627374d,
50                  0.1767135246d, 0.3219468654d, 4.207902826d, 5.23968437d,
51                  6.232056007d, 7.497630467d, 10.51154969d};
52      }
53      
54      /** Creates the default cumulative probability density test expected values */
55      @Override
56      public double[] makeCumulativeTestValues() {
57          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.900d, 0.950d,
58                  0.975d, 0.990d, 0.999d};
59      }
60      
61      //---------------------------- Additional test cases -------------------------
62      
63      public void testInverseCumulativeProbabilityExtremes() throws Exception {
64          setInverseCumulativeTestPoints(new double[] {0.0, 1.0});
65          setInverseCumulativeTestValues(
66                  new double[] {0.0, Double.POSITIVE_INFINITY});
67          verifyInverseCumulativeProbabilities();
68      }
69      
70      public void testAlpha() {
71          WeibullDistribution distribution = (WeibullDistribution) getDistribution();
72          double expected = Math.random();
73          distribution.setShape(expected);
74          assertEquals(expected, distribution.getShape(), 0.0);
75      }
76      
77      public void testBeta() {
78          WeibullDistribution distribution = (WeibullDistribution) getDistribution();
79          double expected = Math.random();
80          distribution.setScale(expected);
81          assertEquals(expected, distribution.getScale(), 0.0);
82      }
83      
84      public void testSetAlpha() {
85          WeibullDistribution distribution = (WeibullDistribution) getDistribution();
86          try {
87              distribution.setShape(0.0);
88              fail("Can not have 0.0 alpha.");
89          } catch (IllegalArgumentException ex) {
90              // success
91          }
92          
93          try {
94              distribution.setShape(-1.0);
95              fail("Can not have negative alpha.");
96          } catch (IllegalArgumentException ex) {
97              // success
98          }
99      }
100     
101     public void testSetBeta() {
102         WeibullDistribution distribution = (WeibullDistribution) getDistribution();
103         try {
104             distribution.setScale(0.0);
105             fail("Can not have 0.0 beta.");
106         } catch (IllegalArgumentException ex) {
107             // success
108         }
109         
110         try {
111             distribution.setScale(-1.0);
112             fail("Can not have negative beta.");
113         } catch (IllegalArgumentException ex) {
114             // success
115         }
116     }
117 }