|
4 | 4 | import org.junit.jupiter.api.Test; |
5 | 5 |
|
6 | 6 | public class SnellLawTest { |
7 | | -//tests for example environmet |
| 7 | + |
8 | 8 | @Test |
9 | | - public void testCalculateRefractionAngle() { |
| 9 | + public void testRefractedAngle() { |
10 | 10 | double n1 = 1.0; // air |
11 | 11 | 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 | + |
17 | 16 | double expected = Math.asin((n1 / n2) * Math.sin(theta1)); |
18 | 17 |
|
19 | | - assertEquals(expected, theta2, 1e-9); |
| 18 | + assertEquals(expected, theta2, 1e-12); |
20 | 19 | } |
21 | | -//tests for total refraction |
| 20 | + |
22 | 21 | @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 | + |
24 | 28 | assertThrows(IllegalArgumentException.class, () -> { |
25 | | - SnellsLaw.calculateRefractionAngle(-1, 1.5, 0.5); |
| 29 | + SnellLaw.refractedAngle(n1, n2, theta1); |
26 | 30 | }); |
| 31 | + } |
27 | 32 |
|
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); |
30 | 41 | }); |
31 | 42 | } |
32 | 43 | } |
0 commit comments