1
1
package com .thealgorithms .randomized ;
2
2
3
+ import static com .thealgorithms .randomized .MonteCarloIntegration .approximate ;
3
4
import static org .junit .jupiter .api .Assertions .assertEquals ;
5
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
6
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
4
7
5
8
import java .util .function .Function ;
6
9
import org .junit .jupiter .api .Test ;
@@ -13,47 +16,76 @@ class MonteCarloIntegrationTest {
13
16
void testConstantFunction () {
14
17
// Integral of f(x) = 2 from 0 to 1 is 2
15
18
Function <Double , Double > constant = x -> 2.0 ;
16
- double result = MonteCarloIntegration . approximate (constant , 0 , 1 , 10000 );
19
+ double result = approximate (constant , 0 , 1 , 10000 );
17
20
assertEquals (2.0 , result , EPSILON );
18
21
}
19
22
20
23
@ Test
21
24
void testLinearFunction () {
22
25
// Integral of f(x) = x from 0 to 1 is 0.5
23
26
Function <Double , Double > linear = Function .identity ();
24
- double result = MonteCarloIntegration . approximate (linear , 0 , 1 , 10000 );
27
+ double result = approximate (linear , 0 , 1 , 10000 );
25
28
assertEquals (0.5 , result , EPSILON );
26
29
}
27
30
28
31
@ Test
29
32
void testQuadraticFunction () {
30
33
// Integral of f(x) = x^2 from 0 to 1 is 1/3
31
34
Function <Double , Double > quadratic = x -> x * x ;
32
- double result = MonteCarloIntegration . approximate (quadratic , 0 , 1 , 10000 );
35
+ double result = approximate (quadratic , 0 , 1 , 10000 );
33
36
assertEquals (1.0 / 3.0 , result , EPSILON );
34
37
}
35
38
36
39
@ Test
37
40
void testLargeSampleSize () {
38
41
// Integral of f(x) = x^2 from 0 to 1 is 1/3
39
42
Function <Double , Double > quadratic = x -> x * x ;
40
- double result = MonteCarloIntegration . approximate (quadratic , 0 , 1 , 50000000 );
43
+ double result = approximate (quadratic , 0 , 1 , 50000000 );
41
44
assertEquals (1.0 / 3.0 , result , EPSILON / 2 ); // Larger sample size, smaller error margin
42
45
}
43
46
44
47
@ Test
45
48
void testReproducibility () {
46
49
Function <Double , Double > linear = Function .identity ();
47
- double result1 = MonteCarloIntegration . approximate (linear , 0 , 1 , 10000 , 42L );
48
- double result2 = MonteCarloIntegration . approximate (linear , 0 , 1 , 10000 , 42L );
50
+ double result1 = approximate (linear , 0 , 1 , 10000 , 42L );
51
+ double result2 = approximate (linear , 0 , 1 , 10000 , 42L );
49
52
assertEquals (result1 , result2 , 0.0 ); // Exactly equal
50
53
}
51
54
52
55
@ Test
53
56
void testNegativeInterval () {
54
57
// Integral of f(x) = x from -1 to 1 is 0
55
58
Function <Double , Double > linear = Function .identity ();
56
- double result = MonteCarloIntegration . approximate (linear , -1 , 1 , 10000 );
59
+ double result = approximate (linear , -1 , 1 , 10000 );
57
60
assertEquals (0.0 , result , EPSILON );
58
61
}
62
+
63
+ @ Test
64
+ void testNullFunction () {
65
+ Exception exception = assertThrows (IllegalArgumentException .class , () -> approximate (null , 0 , 1 , 1000 ));
66
+ assertNotNull (exception );
67
+ }
68
+
69
+ @ Test
70
+ void testInvalidInterval () {
71
+ Function <Double , Double > linear = Function .identity ();
72
+ Exception exception = assertThrows (IllegalArgumentException .class , () -> {
73
+ approximate (linear , 2 , 1 , 1000 ); // b <= a
74
+ });
75
+ assertNotNull (exception );
76
+ }
77
+
78
+ @ Test
79
+ void testZeroSampleSize () {
80
+ Function <Double , Double > linear = Function .identity ();
81
+ Exception exception = assertThrows (IllegalArgumentException .class , () -> approximate (linear , 0 , 1 , 0 ));
82
+ assertNotNull (exception );
83
+ }
84
+
85
+ @ Test
86
+ void testNegativeSampleSize () {
87
+ Function <Double , Double > linear = Function .identity ();
88
+ Exception exception = assertThrows (IllegalArgumentException .class , () -> approximate (linear , 0 , 1 , -100 ));
89
+ assertNotNull (exception );
90
+ }
59
91
}
0 commit comments