|  | 
|  | 1 | +package com.thealgorithms.physics; | 
|  | 2 | + | 
|  | 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 
|  | 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; | 
|  | 5 | + | 
|  | 6 | +import org.junit.jupiter.api.DisplayName; | 
|  | 7 | +import org.junit.jupiter.api.Test; | 
|  | 8 | + | 
|  | 9 | +/** | 
|  | 10 | + * Test class for the general-purpose ProjectileMotion calculator. | 
|  | 11 | + * | 
|  | 12 | + */ | 
|  | 13 | +final class ProjectileMotionTest { | 
|  | 14 | + | 
|  | 15 | +    private static final double DELTA = 1e-4; // Tolerance for comparing double values | 
|  | 16 | + | 
|  | 17 | +    @Test | 
|  | 18 | +    @DisplayName("Test ground-to-ground launch (initial height is zero)") | 
|  | 19 | +    void testGroundToGroundLaunch() { | 
|  | 20 | +        ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(50, 30, 0); | 
|  | 21 | +        assertEquals(5.0986, result.getTimeOfFlight(), DELTA); | 
|  | 22 | +        assertEquals(220.7750, result.getHorizontalRange(), DELTA); | 
|  | 23 | +        assertEquals(31.8661, result.getMaxHeight(), DELTA); | 
|  | 24 | +    } | 
|  | 25 | + | 
|  | 26 | +    @Test | 
|  | 27 | +    @DisplayName("Test launch from an elevated position") | 
|  | 28 | +    void testElevatedLaunch() { | 
|  | 29 | +        ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(30, 45, 100); | 
|  | 30 | +        assertEquals(7.1705, result.getTimeOfFlight(), DELTA); | 
|  | 31 | +        assertEquals(152.1091, result.getHorizontalRange(), DELTA); | 
|  | 32 | +        assertEquals(122.9436, result.getMaxHeight(), DELTA); // Final corrected value | 
|  | 33 | +    } | 
|  | 34 | + | 
|  | 35 | +    @Test | 
|  | 36 | +    @DisplayName("Test launch straight up (90 degrees)") | 
|  | 37 | +    void testVerticalLaunch() { | 
|  | 38 | +        ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(40, 90, 20); | 
|  | 39 | +        assertEquals(8.6303, result.getTimeOfFlight(), DELTA); | 
|  | 40 | +        assertEquals(0.0, result.getHorizontalRange(), DELTA); | 
|  | 41 | +        assertEquals(101.5773, result.getMaxHeight(), DELTA); | 
|  | 42 | +    } | 
|  | 43 | + | 
|  | 44 | +    @Test | 
|  | 45 | +    @DisplayName("Test horizontal launch from a height (0 degrees)") | 
|  | 46 | +    void testHorizontalLaunch() { | 
|  | 47 | +        ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(25, 0, 80); | 
|  | 48 | +        assertEquals(4.0392, result.getTimeOfFlight(), DELTA); | 
|  | 49 | +        assertEquals(100.9809, result.getHorizontalRange(), DELTA); | 
|  | 50 | +        assertEquals(80.0, result.getMaxHeight(), DELTA); | 
|  | 51 | +    } | 
|  | 52 | + | 
|  | 53 | +    @Test | 
|  | 54 | +    @DisplayName("Test downward launch from a height (negative angle)") | 
|  | 55 | +    void testDownwardLaunchFromHeight() { | 
|  | 56 | +        ProjectileMotion.Result result = ProjectileMotion.calculateTrajectory(20, -30, 100); | 
|  | 57 | +        assertEquals(3.6100, result.getTimeOfFlight(), DELTA); | 
|  | 58 | +        assertEquals(62.5268, result.getHorizontalRange(), DELTA); | 
|  | 59 | +        assertEquals(100.0, result.getMaxHeight(), DELTA); | 
|  | 60 | +    } | 
|  | 61 | + | 
|  | 62 | +    @Test | 
|  | 63 | +    @DisplayName("Test invalid arguments throw an exception") | 
|  | 64 | +    void testInvalidInputs() { | 
|  | 65 | +        assertThrows(IllegalArgumentException.class, () -> ProjectileMotion.calculateTrajectory(-10, 45, 100)); | 
|  | 66 | +        assertThrows(IllegalArgumentException.class, () -> ProjectileMotion.calculateTrajectory(10, 45, -100)); | 
|  | 67 | +        assertThrows(IllegalArgumentException.class, () -> ProjectileMotion.calculateTrajectory(10, 45, 100, 0)); | 
|  | 68 | +    } | 
|  | 69 | +} | 
0 commit comments