|
| 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 CoulombsLaw utility class. |
| 12 | + */ |
| 13 | +final class CoulombsLawTest { |
| 14 | + |
| 15 | + // A small tolerance (delta) for comparing floating-point numbers |
| 16 | + private static final double DELTA = 1e-9; |
| 17 | + private static final double K = CoulombsLaw.COULOMBS_CONSTANT; |
| 18 | + |
| 19 | + @Test |
| 20 | + @DisplayName("Test repulsive force between two charges on the x-axis") |
| 21 | + void testSimpleRepulsiveForce() { |
| 22 | + // Two positive 1C charges, 1 meter apart. |
| 23 | + // Force on q2 should be F = K*1*1 / 1^2 = K, directed away from q1 (positive x) |
| 24 | + double[] forceOnB = CoulombsLaw.calculateForceVector(1.0, 0, 0, 1.0, 1, 0); |
| 25 | + assertArrayEquals(new double[] {K, 0.0}, forceOnB, DELTA); |
| 26 | + |
| 27 | + // Force on q1 should be equal and opposite (negative x) |
| 28 | + double[] forceOnA = CoulombsLaw.calculateForceVector(1.0, 1, 0, 1.0, 0, 0); |
| 29 | + assertArrayEquals(new double[] {-K, 0.0}, forceOnA, DELTA); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @DisplayName("Test attractive force between two charges on the x-axis") |
| 34 | + void testSimpleAttractiveForce() { |
| 35 | + // One positive 1C, one negative -1C, 1 meter apart. |
| 36 | + // Force on q2 should be F = K*1*(-1) / 1^2 = -K, directed toward q1 (negative x) |
| 37 | + double[] forceOnB = CoulombsLaw.calculateForceVector(1.0, 0, 0, -1.0, 1, 0); |
| 38 | + assertArrayEquals(new double[] {-K, 0.0}, forceOnB, DELTA); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + @DisplayName("Test electrostatic force in a 2D plane (repulsive)") |
| 43 | + void test2DRepulsiveForce() { |
| 44 | + // q1 at (0,0) with charge +2C |
| 45 | + // q2 at (3,4) with charge +1C |
| 46 | + // Distance is 5 meters. |
| 47 | + double magnitude = K * 2.0 * 1.0 / 25.0; // 2K/25 |
| 48 | + // Unit vector from 1 to 2 is (3/5, 4/5) |
| 49 | + double expectedFx = magnitude * (3.0 / 5.0); // 6K / 125 |
| 50 | + double expectedFy = magnitude * (4.0 / 5.0); // 8K / 125 |
| 51 | + |
| 52 | + double[] forceOnB = CoulombsLaw.calculateForceVector(2.0, 0, 0, 1.0, 3, 4); |
| 53 | + assertArrayEquals(new double[] {expectedFx, expectedFy}, forceOnB, DELTA); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @DisplayName("Test overlapping charges should result in zero force") |
| 58 | + void testOverlappingCharges() { |
| 59 | + double[] force = CoulombsLaw.calculateForceVector(1.0, 1.5, -2.5, -1.0, 1.5, -2.5); |
| 60 | + assertArrayEquals(new double[] {0.0, 0.0}, force, DELTA); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("Test circular orbit velocity with simple values") |
| 65 | + void testCircularOrbitVelocity() { |
| 66 | + // v = sqrt( (K*1*1 / 1^2) * 1 / 1 ) = sqrt(K) |
| 67 | + double velocity = CoulombsLaw.calculateCircularOrbitVelocity(1.0, 1.0, 1.0, 1.0); |
| 68 | + assertEquals(Math.sqrt(K), velocity, DELTA); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @DisplayName("Test orbital velocity for a Hydrogen atom (Bohr model)") |
| 73 | + void testHydrogenAtomVelocity() { |
| 74 | + // Charge of a proton |
| 75 | + double protonCharge = 1.602176634e-19; |
| 76 | + // Charge of an electron |
| 77 | + double electronCharge = -1.602176634e-19; |
| 78 | + // Mass of an electron |
| 79 | + double electronMass = 9.1093837e-31; |
| 80 | + // Bohr radius (avg distance) |
| 81 | + double bohrRadius = 5.29177e-11; |
| 82 | + |
| 83 | + double expectedVelocity = 2.1876917e6; |
| 84 | + |
| 85 | + double velocity = CoulombsLaw.calculateCircularOrbitVelocity(protonCharge, electronCharge, electronMass, bohrRadius); |
| 86 | + // Use a wider delta for this real-world calculation |
| 87 | + assertEquals(expectedVelocity, velocity, 1.0); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + @DisplayName("Test invalid inputs for orbital velocity throw exception") |
| 92 | + void testInvalidOrbitalVelocityInputs() { |
| 93 | + // Non-positive mass |
| 94 | + assertThrows(IllegalArgumentException.class, () -> CoulombsLaw.calculateCircularOrbitVelocity(1, 1, 0, 100)); |
| 95 | + assertThrows(IllegalArgumentException.class, () -> CoulombsLaw.calculateCircularOrbitVelocity(1, 1, -1, 100)); |
| 96 | + // Non-positive radius |
| 97 | + assertThrows(IllegalArgumentException.class, () -> CoulombsLaw.calculateCircularOrbitVelocity(1, 1, 1, 0)); |
| 98 | + assertThrows(IllegalArgumentException.class, () -> CoulombsLaw.calculateCircularOrbitVelocity(1, 1, 1, -100)); |
| 99 | + } |
| 100 | +} |
0 commit comments