|
| 1 | +package com.thealgorithms.randomized; |
| 2 | + |
| 3 | +import static com.thealgorithms.randomized.MonteCarloIntegration.approximate; |
| 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; |
| 7 | + |
| 8 | +import java.util.function.Function; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +class MonteCarloIntegrationTest { |
| 12 | + |
| 13 | + private static final double EPSILON = 0.03; // Allow 3% error margin |
| 14 | + |
| 15 | + @Test |
| 16 | + void testConstantFunction() { |
| 17 | + // Integral of f(x) = 2 from 0 to 1 is 2 |
| 18 | + Function<Double, Double> constant = x -> 2.0; |
| 19 | + double result = approximate(constant, 0, 1, 10000); |
| 20 | + assertEquals(2.0, result, EPSILON); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void testLinearFunction() { |
| 25 | + // Integral of f(x) = x from 0 to 1 is 0.5 |
| 26 | + Function<Double, Double> linear = Function.identity(); |
| 27 | + double result = approximate(linear, 0, 1, 10000); |
| 28 | + assertEquals(0.5, result, EPSILON); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void testQuadraticFunction() { |
| 33 | + // Integral of f(x) = x^2 from 0 to 1 is 1/3 |
| 34 | + Function<Double, Double> quadratic = x -> x * x; |
| 35 | + double result = approximate(quadratic, 0, 1, 10000); |
| 36 | + assertEquals(1.0 / 3.0, result, EPSILON); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testLargeSampleSize() { |
| 41 | + // Integral of f(x) = x^2 from 0 to 1 is 1/3 |
| 42 | + Function<Double, Double> quadratic = x -> x * x; |
| 43 | + double result = approximate(quadratic, 0, 1, 50000000); |
| 44 | + assertEquals(1.0 / 3.0, result, EPSILON / 2); // Larger sample size, smaller error margin |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testReproducibility() { |
| 49 | + Function<Double, Double> linear = Function.identity(); |
| 50 | + double result1 = approximate(linear, 0, 1, 10000, 42L); |
| 51 | + double result2 = approximate(linear, 0, 1, 10000, 42L); |
| 52 | + assertEquals(result1, result2, 0.0); // Exactly equal |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testNegativeInterval() { |
| 57 | + // Integral of f(x) = x from -1 to 1 is 0 |
| 58 | + Function<Double, Double> linear = Function.identity(); |
| 59 | + double result = approximate(linear, -1, 1, 10000); |
| 60 | + assertEquals(0.0, result, EPSILON); |
| 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 | + } |
| 91 | +} |
0 commit comments