Skip to content

Commit c2fd9c2

Browse files
authored
Add tests for Snell's Law calculations
1 parent 19aa44a commit c2fd9c2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.thealgorithms.physics;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class SnellsLawTest {
7+
//tests for example environmet
8+
@Test
9+
public void testCalculateRefractionAngle() {
10+
double n1 = 1.0; // air
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)
17+
double expected = Math.asin((n1 / n2) * Math.sin(theta1));
18+
19+
assertEquals(expected, theta2, 1e-9);
20+
}
21+
//tests for total refraction
22+
@Test
23+
public void testInvalidRefractiveIndex() {
24+
assertThrows(IllegalArgumentException.class, () -> {
25+
SnellsLaw.calculateRefractionAngle(-1, 1.5, 0.5);
26+
});
27+
28+
assertThrows(IllegalArgumentException.class, () -> {
29+
SnellsLaw.calculateRefractionAngle(1, 0, 0.5);
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)