1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
50
51
52
53
54
55
56
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 }