/
shvidkuli
/
kkrjava
Обзор
Документация
Войти
/
shvidkuli
/
kkrjava
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/com/montecarlo/tests/MonteCarloCalculatorTest.java
148 строк
5 KB
kroc400
first_commit
30 апр 2026, 22:44
30 апр 2026, 22:44
2347fdb
Код
Авторство
О чём код?
package com.montecarlo.tests; import com.montecarlo.geometry.Arc; import com.montecarlo.geometry.FigureDebf; import com.montecarlo.geometry.Point; import com.montecarlo.geometry.Triangle; import com.montecarlo.montecarlo.MonteCarloCalculator; import com.montecarlo.montecarlo.MonteCarloCalculator.ExperimentResult; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; /** * Unit tests for {@link MonteCarloCalculator}. * * <p>Because Monte Carlo is stochastic, deterministic tests use a fixed RNG seed * to guarantee reproducibility. Convergence tests verify that the relative error * decreases as N increases.</p> */ public class MonteCarloCalculatorTest { /** Fixed RNG seed used in all tests for reproducibility. */ private static final long SEED = 42L; private FigureDebf figure; private double analyticalArea; @Before public void setUp() { Triangle triangle = new Triangle( new Point(4, 0), new Point(4, 2), new Point(2, 0) ); Arc arc = new Arc( new Point(3, 1), Math.sqrt(2), new Point(0, 4) ); figure = new FigureDebf(triangle, arc); analyticalArea = figure.analyticalArea(); // 2 + π ≈ 5.14159 } // ----------------------------------------------------------------------- // Basic estimate // ----------------------------------------------------------------------- @Test public void testEstimatePositive() { MonteCarloCalculator calc = new MonteCarloCalculator(figure, SEED); double est = calc.estimate(1_000L); assertTrue("Estimated area must be positive", est > 0); } @Test public void testEstimateWithinBoundingBox() { MonteCarloCalculator calc = new MonteCarloCalculator(figure, SEED); double est = calc.estimate(10_000L); double boxArea = figure.getBoundingBox().area(); assertTrue("Estimate must not exceed bounding box area", est <= boxArea + 1e-9); } @Test(expected = IllegalArgumentException.class) public void testEstimateZeroSamplesThrows() { MonteCarloCalculator calc = new MonteCarloCalculator(figure, SEED); calc.estimate(0L); } @Test(expected = IllegalArgumentException.class) public void testEstimateNegativeSamplesThrows() { MonteCarloCalculator calc = new MonteCarloCalculator(figure, SEED); calc.estimate(-1L); } // ----------------------------------------------------------------------- // Convergence — error decreases with growing N // ----------------------------------------------------------------------- @Test public void testConvergenceErrorBelow5PctAt100k() { MonteCarloCalculator calc = new MonteCarloCalculator(figure, SEED); double est = calc.estimate(100_000L); double relErr = Math.abs(est - analyticalArea) / analyticalArea * 100; assertTrue("Relative error at N=100 000 should be < 5%, was: " + relErr + "%", relErr < 5.0); } @Test public void testConvergenceErrorBelow1PctAt1M() { MonteCarloCalculator calc = new MonteCarloCalculator(figure, SEED); double est = calc.estimate(1_000_000L); double relErr = Math.abs(est - analyticalArea) / analyticalArea * 100; assertTrue("Relative error at N=1 000 000 should be < 1%, was: " + relErr + "%", relErr < 1.0); } // ----------------------------------------------------------------------- // ExperimentResult // ----------------------------------------------------------------------- @Test public void testRunExperimentFieldsPopulated() { MonteCarloCalculator calc = new MonteCarloCalculator(figure, SEED); ExperimentResult r = calc.runExperiment(10_000L, analyticalArea); assertEquals(10_000L, r.n); assertTrue(r.estimatedArea > 0); assertEquals(analyticalArea, r.analyticalArea, 1e-12); assertTrue(r.relativeErrorPct >= 0); assertTrue(r.elapsedMillis >= 0); } @Test public void testRunAllExperimentsReturnsCorrectCount() { MonteCarloCalculator calc = new MonteCarloCalculator(figure, SEED); long[] nValues = {1_000L, 10_000L, 100_000L}; ExperimentResult[] results = calc.runAllExperiments(nValues, analyticalArea); assertEquals(3, results.length); assertEquals(1_000L, results[0].n); assertEquals(10_000L, results[1].n); assertEquals(100_000L, results[2].n); } // ----------------------------------------------------------------------- // Determinism with seed // ----------------------------------------------------------------------- @Test public void testDeterministicWithSameSeed() { MonteCarloCalculator c1 = new MonteCarloCalculator(figure, SEED); MonteCarloCalculator c2 = new MonteCarloCalculator(figure, SEED); double e1 = c1.estimate(50_000L); double e2 = c2.estimate(50_000L); assertEquals("Same seed must produce identical estimates", e1, e2, 0.0); } @Test public void testDifferentSeedsGiveDifferentResults() { MonteCarloCalculator c1 = new MonteCarloCalculator(figure, 1L); MonteCarloCalculator c2 = new MonteCarloCalculator(figure, 2L); double e1 = c1.estimate(1_000L); double e2 = c2.estimate(1_000L); // Extremely unlikely to be equal for different seeds assertNotEquals("Different seeds should produce different estimates", e1, e2, 0.0); } }