Skip to content

Commit 31c8df5

Browse files
authored
Refactor SnellLawTest for clarity and accuracy
1 parent bc2b66c commit 31c8df5

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/test/java/com/thealgorithms/physics/SnellLawTest.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,40 @@
44
import org.junit.jupiter.api.Test;
55

66
public class SnellLawTest {
7-
//tests for example environmet
7+
88
@Test
9-
public void testCalculateRefractionAngle() {
9+
public void testRefractedAngle() {
1010
double n1 = 1.0; // air
1111
double n2 = 1.5; // glass
12-
double theta1 = Math.toRadians(30); // 30 grados
13-
14-
double theta2 = SnellsLaw.calculateRefractionAngle(n1, n2, theta1);
15-
16-
// expected value using: sin(theta2) = n1/n2 * sin(theta1)
12+
double theta1 = Math.toRadians(30);
13+
14+
double theta2 = SnellLaw.refractedAngle(n1, n2, theta1);
15+
1716
double expected = Math.asin((n1 / n2) * Math.sin(theta1));
1817

19-
assertEquals(expected, theta2, 1e-9);
18+
assertEquals(expected, theta2, 1e-12);
2019
}
21-
//tests for total refraction
20+
2221
@Test
23-
public void testInvalidRefractiveIndex() {
22+
public void testTotalInternalReflection() {
23+
// total internal reflection happens when n1 > n2 AND theta1 is large
24+
double n1 = 1.5;
25+
double n2 = 1.0;
26+
double theta1 = Math.toRadians(60); // large enough angle
27+
2428
assertThrows(IllegalArgumentException.class, () -> {
25-
SnellsLaw.calculateRefractionAngle(-1, 1.5, 0.5);
29+
SnellLaw.refractedAngle(n1, n2, theta1);
2630
});
31+
}
2732

28-
assertThrows(IllegalArgumentException.class, () -> {
29-
SnellsLaw.calculateRefractionAngle(1, 0, 0.5);
33+
@Test
34+
public void testNoTotalInternalReflectionAtLowAngles() {
35+
double n1 = 1.5;
36+
double n2 = 1.0;
37+
double theta1 = Math.toRadians(10);
38+
39+
assertDoesNotThrow(() -> {
40+
SnellLaw.refractedAngle(n1, n2, theta1);
3041
});
3142
}
3243
}

0 commit comments

Comments
 (0)