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