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.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertThrows;
29  
30  /**
31   * Test cases for the BinomialProportion implementations.
32   */
33  public abstract class BinomialProportionAbstractTest {
34  
35      @FunctionalInterface
36      public interface BinomialProportionMethod {
37          ConfidenceInterval calculate(int trials, double probability, double confidenceLevel);
38      }
39  
40      protected BinomialProportionMethod testMethod;
41  
42      private final int trials                  = 500;
43      private final double probabilityOfSuccess = 0.1;
44      private final double confidenceLevel      = 0.9;
45  
46      protected abstract BinomialProportionMethod getBinomialProportionMethod();
47  
48      /**
49       * Returns the confidence interval for the given statistic with the following values:
50       *
51       * <ul>
52       *  <li>trials: 500</li>
53       *  <li>probabilityOfSuccess: 0.1</li>
54       *  <li>confidenceLevel: 0.9</li>
55       * </ul>
56       * @return the Confidence Interval for the given values
57       */
58      protected ConfidenceInterval createStandardTestInterval() {
59          return testMethod.calculate(trials, probabilityOfSuccess, confidenceLevel);
60      }
61  
62      @BeforeEach
63      public void setUp() {
64          testMethod = getBinomialProportionMethod();
65      }
66  
67      @Test
68      public void testZeroConfidencelevel() {
69          assertThrows(MathIllegalArgumentException.class, () -> {
70              testMethod.calculate(trials, probabilityOfSuccess, 0d);
71          });
72      }
73  
74      @Test
75      public void testOneConfidencelevel() {
76          assertThrows(MathIllegalArgumentException.class, () -> {
77              testMethod.calculate(trials, probabilityOfSuccess, 1d);
78          });
79      }
80  
81      @Test
82      public void testZeroTrials() {
83          assertThrows(MathIllegalArgumentException.class, () -> {
84              testMethod.calculate(0, 0, confidenceLevel);
85          });
86      }
87  
88      @Test
89      public void testNegativeSuccesses() {
90          assertThrows(MathIllegalArgumentException.class, () -> {
91              testMethod.calculate(trials, -1, confidenceLevel);
92          });
93      }
94  
95      @Test
96      public void testSuccessesExceedingTrials() {
97          assertThrows(MathIllegalArgumentException.class, () -> {
98              testMethod.calculate(trials, trials + 1, confidenceLevel);
99          });
100     }
101 }