Skip to content

Commit c23e5be

Browse files
authored
Create RelativityTest.java
Test file created for Relativity.java.
1 parent cc0a700 commit c23e5be

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.physics;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
10+
/**
11+
* Unit tests for the Relativity utility class.
12+
*/
13+
final class RelativityTest {
14+
15+
// A small tolerance (delta) for comparing floating-point numbers
16+
private static final double DELTA = 1e-9;
17+
private static final double C = Relativity.SPEED_OF_LIGHT;
18+
19+
@Test
20+
@DisplayName("Test the gamma parameter")
21+
void testGamma() {
22+
double myGamma = Relativity.gamma(0.6*C);
23+
assertEquals(1.25, myGamma, DELTA);
24+
}
25+
26+
@Test
27+
@DisplayName("Test the length contraction")
28+
void testLengthContraction() {
29+
double myLength = Relativity.lengthContraction(5.0, 0.8*C);
30+
assertEquals(3.0, myLength, DELTA);
31+
}
32+
33+
@Test
34+
@DisplayName("Test the time dilation")
35+
void testTimeDilation() {
36+
double myTime = Relativity.timeDilation(4.0, 0.6*C);
37+
assertEquals(5.0, myTime, DELTA);
38+
}
39+
40+
@Test
41+
@DisplayName("Test the velocity addition in the same direction")
42+
void testVelocityAddition() {
43+
double myVelocity = Relativity.velocityAddition(0.8*C, 0.75*C);
44+
assertEquals(0.125*C, myVelocity, DELTA);
45+
}
46+
47+
@Test
48+
@DisplayName("Test the velocity addition in different directions")
49+
void testVelocityAddition() {
50+
double myVelocity = Relativity.velocityAddition(0.8*C, -0.75*C);
51+
assertEquals(0.96875*C, myVelocity, DELTA);
52+
}
53+
54+
@Test
55+
@DisplayName("Test the velocity addition with the speed of light")
56+
void testVelocityAddition() {
57+
double myVelocity = Relativity.velocityAddition(C, 0.7*C);
58+
assertEquals(C, myVelocity, DELTA);
59+
}
60+
61+
@Test
62+
@DisplayName("Test invalid inputs throw exception")
63+
void testInvalidOrbitalVelocityInputs() {
64+
assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(1.2*C));
65+
assertThrows(IllegalArgumentException.class, () -> Relativity.gamma(-C));
66+
assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(-1.0, 0.6*C));
67+
assertThrows(IllegalArgumentException.class, () -> Relativity.lengthContraction(1.0, 1.5*C));
68+
assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(-5.0, -0.8*C));
69+
assertThrows(IllegalArgumentException.class, () -> Relativity.timeDilation(5.0, C));
70+
assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(0.3*C, -C));
71+
assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(1.4*C, 0.2*C));
72+
assertThrows(IllegalArgumentException.class, () -> Relativity.velocityAddition(-0.4*C, 1.2*C));
73+
}
74+
}

0 commit comments

Comments
 (0)