|
| 1 | +package com.thealgorithms.geometry; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.stream.Stream; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.Arguments; |
| 11 | +import org.junit.jupiter.params.provider.MethodSource; |
| 12 | + |
| 13 | +/** |
| 14 | + * The {@code MidpointEllipseTest} class contains unit tests for the |
| 15 | + * {@code MidpointEllipse} class, specifically testing the |
| 16 | + * {@code drawEllipse} method. |
| 17 | + * |
| 18 | + * <p>This class uses parameterized tests to validate the output of |
| 19 | + * Midpoint Ellipse algorithm for various input points.</p> |
| 20 | + */ |
| 21 | +class MidpointEllipseTest { |
| 22 | + |
| 23 | + /** |
| 24 | + * Provides test cases for the drawEllipse method. |
| 25 | + * Each argument contains: centerX, centerY, a, b, and expected points. |
| 26 | + * |
| 27 | + * @return a stream of arguments for parameterized testing |
| 28 | + */ |
| 29 | + static Stream<Arguments> ellipseTestProvider() { |
| 30 | + return Stream.of( |
| 31 | + Arguments.of(0, 0, 5, 3, new int[][] {{0, 3}, {0, 3}, {0, -3}, {0, -3}, {1, 3}, {-1, 3}, {1, -3}, {-1, -3}, {2, 3}, {-2, 3}, {2, -3}, {-2, -3}, {3, 2}, {-3, 2}, {3, -2}, {-3, -2}, {4, 2}, {-4, 2}, {4, -2}, {-4, -2}, {5, 1}, {-5, 1}, {5, -1}, {-5, -1}, {5, 0}, {-5, 0}, {5, 0}, {-5, 0}}), |
| 32 | + Arguments.of(0, 0, 0, 5, |
| 33 | + new int[][] { |
| 34 | + {0, -5}, {0, -4}, {0, -3}, {0, -2}, {0, -1}, {0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5} // Only vertical line points and center |
| 35 | + }), |
| 36 | + Arguments.of(0, 0, 5, 0, |
| 37 | + new int[][] { |
| 38 | + {-5, 0}, {-4, 0}, {-3, 0}, {-2, 0}, {-1, 0}, {0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0} // Only horizontal line points and center |
| 39 | + }), |
| 40 | + Arguments.of(0, 0, 0, 0, |
| 41 | + new int[][] { |
| 42 | + {0, 0} // Only center point |
| 43 | + }), |
| 44 | + Arguments.of(0, 0, 4, 4, |
| 45 | + new int[][] { |
| 46 | + {0, 4}, |
| 47 | + {0, 4}, |
| 48 | + {0, -4}, |
| 49 | + {0, -4}, |
| 50 | + {1, 4}, |
| 51 | + {-1, 4}, |
| 52 | + {1, -4}, |
| 53 | + {-1, -4}, |
| 54 | + {2, 3}, |
| 55 | + {-2, 3}, |
| 56 | + {2, -3}, |
| 57 | + {-2, -3}, |
| 58 | + {3, 3}, |
| 59 | + {-3, 3}, |
| 60 | + {3, -3}, |
| 61 | + {-3, -3}, |
| 62 | + {3, 2}, |
| 63 | + {-3, 2}, |
| 64 | + {3, -2}, |
| 65 | + {-3, -2}, |
| 66 | + {4, 1}, |
| 67 | + {-4, 1}, |
| 68 | + {4, -1}, |
| 69 | + {-4, -1}, |
| 70 | + {4, 0}, |
| 71 | + {-4, 0}, |
| 72 | + {4, 0}, |
| 73 | + {-4, 0}, |
| 74 | + })); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Tests the drawEllipse method with various parameters. |
| 79 | + * |
| 80 | + * @param centerX the x-coordinate of the center of the ellipse |
| 81 | + * @param centerY the y-coordinate of the center of the ellipse |
| 82 | + * @param a the length of the semi-major axis |
| 83 | + * @param b the length of the semi-minor axis |
| 84 | + * @param expectedPoints the expected points forming the ellipse |
| 85 | + */ |
| 86 | + @ParameterizedTest |
| 87 | + @MethodSource("ellipseTestProvider") |
| 88 | + @DisplayName("Test drawing ellipses with various parameters") |
| 89 | + void testDrawEllipse(int centerX, int centerY, int a, int b, int[][] expectedPoints) { |
| 90 | + List<int[]> points = MidpointEllipse.drawEllipse(centerX, centerY, a, b); |
| 91 | + |
| 92 | + // Validate the number of points and the specific points |
| 93 | + assertEquals(expectedPoints.length, points.size(), "Number of points should match expected."); |
| 94 | + |
| 95 | + for (int i = 0; i < expectedPoints.length; i++) { |
| 96 | + assertArrayEquals(expectedPoints[i], points.get(i), "Point mismatch at index " + i); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments