View Javadoc
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    *      https://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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat.interval;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  /**
29   * Test cases for the BinomialProportion implementations.
30   */
31  public abstract class BinomialProportionAbstractTest {
32  
33      @FunctionalInterface
34      public interface BinomialProportionMethod {
35          ConfidenceInterval calculate(int trials, double probability, double confidenceLevel);
36      }
37  
38      protected BinomialProportionMethod testMethod;
39  
40      private final int trials                  = 500;
41      private final double probabilityOfSuccess = 0.1;
42      private final double confidenceLevel      = 0.9;
43  
44      protected abstract BinomialProportionMethod getBinomialProportionMethod();
45  
46      /**
47       * Returns the confidence interval for the given statistic with the following values:
48       *
49       * <ul>
50       *  <li>trials: 500</li>
51       *  <li>probabilityOfSuccess: 0.1</li>
52       *  <li>confidenceLevel: 0.9</li>
53       * </ul>
54       * @return the Confidence Interval for the given values
55       */
56      protected ConfidenceInterval createStandardTestInterval() {
57          return testMethod.calculate(trials, probabilityOfSuccess, confidenceLevel);
58      }
59  
60      @Before
61      public void setUp() {
62          testMethod = getBinomialProportionMethod();
63      }
64  
65      @Test(expected = MathIllegalArgumentException.class)
66      public void testZeroConfidencelevel() {
67          testMethod.calculate(trials, probabilityOfSuccess, 0d);
68      }
69  
70      @Test(expected = MathIllegalArgumentException.class)
71      public void testOneConfidencelevel() {
72          testMethod.calculate(trials, probabilityOfSuccess, 1d);
73      }
74  
75      @Test(expected = MathIllegalArgumentException.class)
76      public void testZeroTrials() {
77          testMethod.calculate(0, 0, confidenceLevel);
78      }
79  
80      @Test(expected = MathIllegalArgumentException.class)
81      public void testNegativeSuccesses() {
82          testMethod.calculate(trials, -1, confidenceLevel);
83      }
84  
85      @Test(expected = MathIllegalArgumentException.class)
86      public void testSuccessesExceedingTrials() {
87          testMethod.calculate(trials, trials + 1, confidenceLevel);
88      }
89  }