11package com .thealgorithms .randomized ;
22
3+ import static com .thealgorithms .randomized .MonteCarloIntegration .approximate ;
34import 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 ;
47
58import java .util .function .Function ;
69import org .junit .jupiter .api .Test ;
@@ -13,47 +16,76 @@ class MonteCarloIntegrationTest {
1316 void testConstantFunction () {
1417 // Integral of f(x) = 2 from 0 to 1 is 2
1518 Function <Double , Double > constant = x -> 2.0 ;
16- double result = MonteCarloIntegration . approximate (constant , 0 , 1 , 10000 );
19+ double result = approximate (constant , 0 , 1 , 10000 );
1720 assertEquals (2.0 , result , EPSILON );
1821 }
1922
2023 @ Test
2124 void testLinearFunction () {
2225 // Integral of f(x) = x from 0 to 1 is 0.5
2326 Function <Double , Double > linear = Function .identity ();
24- double result = MonteCarloIntegration . approximate (linear , 0 , 1 , 10000 );
27+ double result = approximate (linear , 0 , 1 , 10000 );
2528 assertEquals (0.5 , result , EPSILON );
2629 }
2730
2831 @ Test
2932 void testQuadraticFunction () {
3033 // Integral of f(x) = x^2 from 0 to 1 is 1/3
3134 Function <Double , Double > quadratic = x -> x * x ;
32- double result = MonteCarloIntegration . approximate (quadratic , 0 , 1 , 10000 );
35+ double result = approximate (quadratic , 0 , 1 , 10000 );
3336 assertEquals (1.0 / 3.0 , result , EPSILON );
3437 }
3538
3639 @ Test
3740 void testLargeSampleSize () {
3841 // Integral of f(x) = x^2 from 0 to 1 is 1/3
3942 Function <Double , Double > quadratic = x -> x * x ;
40- double result = MonteCarloIntegration . approximate (quadratic , 0 , 1 , 50000000 );
43+ double result = approximate (quadratic , 0 , 1 , 50000000 );
4144 assertEquals (1.0 / 3.0 , result , EPSILON / 2 ); // Larger sample size, smaller error margin
4245 }
4346
4447 @ Test
4548 void testReproducibility () {
4649 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 );
4952 assertEquals (result1 , result2 , 0.0 ); // Exactly equal
5053 }
5154
5255 @ Test
5356 void testNegativeInterval () {
5457 // Integral of f(x) = x from -1 to 1 is 0
5558 Function <Double , Double > linear = Function .identity ();
56- double result = MonteCarloIntegration . approximate (linear , -1 , 1 , 10000 );
59+ double result = approximate (linear , -1 , 1 , 10000 );
5760 assertEquals (0.0 , result , EPSILON );
5861 }
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+ }
5991}
0 commit comments