|
| 1 | +package com.thealgorithms.geometry; |
| 2 | + |
| 3 | +import java.awt.Point; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Stream; |
| 7 | +import org.junit.jupiter.api.Assertions; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Unit tests for the LineDrawer using BresenhamLineStrategy. |
| 15 | + */ |
| 16 | +class LineDrawerTest { |
| 17 | + |
| 18 | + /** |
| 19 | + * Provides test data for drawing lines using the Bresenham algorithm. |
| 20 | + */ |
| 21 | + static Stream<Arguments> linePointsProvider() { |
| 22 | + return Stream.of(Arguments.of(0, 0, 5, 5, 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))), Arguments.of(0, 0, 5, 0, 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))), |
| 23 | + Arguments.of(0, 0, 0, 5, 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))), Arguments.of(-2, -2, -5, -5, List.of(new Point(-2, -2), new Point(-3, -3), new Point(-4, -4), new Point(-5, -5))), |
| 24 | + Arguments.of(-1, -1, 2, 2, List.of(new Point(-1, -1), new Point(0, 0), new Point(1, 1), new Point(2, 2))), Arguments.of(2, -1, -1, -4, List.of(new Point(2, -1), new Point(1, -2), new Point(0, -3), new Point(-1, -4)))); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Tests line drawing using the BresenhamLineStrategy through LineDrawer. |
| 29 | + */ |
| 30 | + @ParameterizedTest |
| 31 | + @MethodSource("linePointsProvider") |
| 32 | + void testLineDrawingWithBresenhamStrategy(int x0, int y0, int x1, int y1, Collection<Point> expected) { |
| 33 | + LineDrawingStrategy strategy = new BresenhamLineStrategy(); |
| 34 | + LineDrawer drawer = new LineDrawer(strategy); |
| 35 | + |
| 36 | + List<Point> actual = drawer.drawLine(x0, y0, x1, y1); |
| 37 | + |
| 38 | + Assertions.assertEquals(expected.size(), actual.size(), "Point count mismatch."); |
| 39 | + Assertions.assertTrue(expected.containsAll(actual) && actual.containsAll(expected), "Generated points do not match expected points."); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Demonstrates dynamic strategy switching. |
| 44 | + */ |
| 45 | + @ParameterizedTest |
| 46 | + @MethodSource("linePointsProvider") |
| 47 | + void testSetStrategyDynamicSwitch(int x0, int y0, int x1, int y1, Collection<Point> expected) { |
| 48 | + LineDrawer drawer = new LineDrawer(new BresenhamLineStrategy()); |
| 49 | + |
| 50 | + // Simulate switching strategy at runtime (e.g., in future: drawer.setStrategy(new DdaLineStrategy())) |
| 51 | + drawer.setStrategy(new BresenhamLineStrategy()); // No-op here but shows extensibility |
| 52 | + |
| 53 | + List<Point> actual = drawer.drawLine(x0, y0, x1, y1); |
| 54 | + |
| 55 | + Assertions.assertEquals(expected.size(), actual.size(), "Point count mismatch after strategy switch."); |
| 56 | + Assertions.assertTrue(expected.containsAll(actual) && actual.containsAll(expected), "Generated points do not match expected points after strategy switch."); |
| 57 | + } |
| 58 | +} |
0 commit comments