|
| 1 | +package com.thealgorithms.geometry; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.awt.Point; |
| 6 | +import java.util.List; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +/** |
| 10 | + * The {@code BresenhamLineTest} class contains unit tests for the {@code BresenhamLine} class. |
| 11 | + * It verifies the correctness of the findLine method, which generates a list of points |
| 12 | + * representing a straight line between two specified endpoints. |
| 13 | + * |
| 14 | + * <p>This test class uses JUnit 5 for testing and covers various scenarios including horizontal, |
| 15 | + * vertical, and diagonal lines, as well as lines with negative coordinates.</p> |
| 16 | + */ |
| 17 | +class BresenhamLineTest { |
| 18 | + |
| 19 | + // Instance of BresenhamLine to be tested |
| 20 | + private final BresenhamLine bresenhamLine = new BresenhamLine(); |
| 21 | + |
| 22 | + /** |
| 23 | + * Tests the generation of a horizontal line from (0, 0) to (5, 0). |
| 24 | + * Asserts that the generated line matches the expected list of points. |
| 25 | + */ |
| 26 | + @Test |
| 27 | + void testHorizontalLine() { |
| 28 | + List<Point> line = bresenhamLine.findLine(0, 0, 5, 0); |
| 29 | + List<Point> expected = List.of(new Point(0, 0), new Point(1, 0), new Point(2, 0), new Point(3, 0), new Point(4, 0), new Point(5, 0)); |
| 30 | + assertEquals(expected, line); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Tests the generation of a vertical line from (0, 0) to (0, 5). |
| 35 | + * Asserts that the generated line matches the expected list of points. |
| 36 | + */ |
| 37 | + @Test |
| 38 | + void testVerticalLine() { |
| 39 | + List<Point> line = bresenhamLine.findLine(0, 0, 0, 5); |
| 40 | + List<Point> expected = List.of(new Point(0, 0), new Point(0, 1), new Point(0, 2), new Point(0, 3), new Point(0, 4), new Point(0, 5)); |
| 41 | + assertEquals(expected, line); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Tests the generation of a diagonal line from (0, 0) to (5, 5). |
| 46 | + * Asserts that the generated line matches the expected list of points. |
| 47 | + */ |
| 48 | + @Test |
| 49 | + void testDiagonalLine() { |
| 50 | + List<Point> line = bresenhamLine.findLine(0, 0, 5, 5); |
| 51 | + List<Point> expected = List.of(new Point(0, 0), new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(4, 4), new Point(5, 5)); |
| 52 | + assertEquals(expected, line); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Tests the generation of a diagonal line with a negative slope from (5, 5) to (0, 0). |
| 57 | + * Asserts that the generated line matches the expected list of points. |
| 58 | + */ |
| 59 | + @Test |
| 60 | + void testNegativeSlopeDiagonal() { |
| 61 | + List<Point> line = bresenhamLine.findLine(5, 5, 0, 0); |
| 62 | + List<Point> expected = List.of(new Point(5, 5), new Point(4, 4), new Point(3, 3), new Point(2, 2), new Point(1, 1), new Point(0, 0)); |
| 63 | + assertEquals(expected, line); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tests the generation of a steep vertical line from (1, 1) to (1, 4). |
| 68 | + * Asserts that the generated line matches the expected list of points. |
| 69 | + */ |
| 70 | + @Test |
| 71 | + void testSteepDiagonal() { |
| 72 | + List<Point> line = bresenhamLine.findLine(1, 1, 1, 4); |
| 73 | + List<Point> expected = List.of(new Point(1, 1), new Point(1, 2), new Point(1, 3), new Point(1, 4)); |
| 74 | + assertEquals(expected, line); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Tests the generation of a diagonal line with negative coordinates from (-3,-3) to (-1,-1). |
| 79 | + * Asserts that the generated line matches the expected list of points. |
| 80 | + */ |
| 81 | + @Test |
| 82 | + void testMixedCoordinates() { |
| 83 | + List<Point> line = bresenhamLine.findLine(-3, -3, -1, -1); |
| 84 | + List<Point> expected = List.of(new Point(-3, -3), new Point(-2, -2), new Point(-1, -1)); |
| 85 | + assertEquals(expected, line); |
| 86 | + } |
| 87 | +} |
0 commit comments