|
| 1 | +package com.thealgorithms.conversions; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.CsvSource; |
| 8 | + |
| 9 | +public class CoordinateConverterTest { |
| 10 | + |
| 11 | + @ParameterizedTest |
| 12 | + @CsvSource({"0, 0, 0, 0", "1, 0, 1, 0", "0, 1, 1, 90", "-1, 0, 1, 180", "0, -1, 1, -90", "3, 4, 5, 53.13010235415599"}) |
| 13 | + void testCartesianToPolar(double x, double y, double expectedR, double expectedTheta) { |
| 14 | + assertArrayEquals(new double[] {expectedR, expectedTheta}, CoordinateConverter.cartesianToPolar(x, y), 1e-9); |
| 15 | + } |
| 16 | + |
| 17 | + @ParameterizedTest |
| 18 | + @CsvSource({"1, 0, 1, 0", "1, 90, 0, 1", "1, 180, -1, 0", "1, -90, 0, -1", "5, 53.13010235415599, 3, 4"}) |
| 19 | + void testPolarToCartesian(double r, double theta, double expectedX, double expectedY) { |
| 20 | + assertArrayEquals(new double[] {expectedX, expectedY}, CoordinateConverter.polarToCartesian(r, theta), 1e-9); |
| 21 | + } |
| 22 | + |
| 23 | + @ParameterizedTest |
| 24 | + @CsvSource({"NaN, 1", "1, NaN", "Infinity, 1", "1, Infinity", "-Infinity, 1", "1, -Infinity"}) |
| 25 | + void testCartesianToPolarInvalidInputs(double x, double y) { |
| 26 | + assertThrows(IllegalArgumentException.class, () -> CoordinateConverter.cartesianToPolar(x, y)); |
| 27 | + } |
| 28 | + |
| 29 | + @ParameterizedTest |
| 30 | + @CsvSource({"-1, 0", "1, NaN", "1, Infinity", "1, -Infinity"}) |
| 31 | + void testPolarToCartesianInvalidInputs(double r, double theta) { |
| 32 | + assertThrows(IllegalArgumentException.class, () -> CoordinateConverter.polarToCartesian(r, theta)); |
| 33 | + } |
| 34 | +} |
0 commit comments