Skip to content

Commit 33e3d8f

Browse files
#6219: Adding input validation
1 parent 785c8af commit 33e3d8f

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

src/main/java/com/thealgorithms/randomized/MonteCarloIntegration.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,22 @@ public static double approximate(Function<Double, Double> fx, double a, double b
6161
}
6262

6363
private static double doApproximate(Function<Double, Double> fx, double a, double b, int n, Random generator) {
64+
if (!validate(fx, a, b, n)) {
65+
throw new IllegalArgumentException("Invalid input parameters");
66+
}
6467
double totalArea = 0.0;
65-
double range = b - a;
68+
double interval = b - a;
6669
for (int i = 0; i < n; i++) {
67-
double x = a + generator.nextDouble() * range;
70+
double x = a + generator.nextDouble() * interval;
6871
totalArea += fx.apply(x);
6972
}
70-
return range * totalArea / n;
73+
return interval * totalArea / n;
74+
}
75+
76+
private static boolean validate(Function<Double, Double> fx, double a, double b, int n) {
77+
boolean isFunctionValid = fx != null;
78+
boolean isIntervalValid = a < b;
79+
boolean isSampleSizeValid = n > 0;
80+
return isFunctionValid && isIntervalValid && isSampleSizeValid;
7181
}
7282
}
Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.thealgorithms.randomized;
22

3+
import static com.thealgorithms.randomized.MonteCarloIntegration.approximate;
34
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;
47

58
import java.util.function.Function;
69
import 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

Comments
 (0)