- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathBresenhamLineTest.java
57 lines (53 loc) · 2.88 KB
/
BresenhamLineTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
packagecom.thealgorithms.geometry;
importjava.awt.Point;
importjava.util.Collection;
importjava.util.List;
importjava.util.stream.Stream;
importorg.junit.jupiter.api.Assertions;
importorg.junit.jupiter.params.ParameterizedTest;
importorg.junit.jupiter.params.provider.Arguments;
importorg.junit.jupiter.params.provider.MethodSource;
/**
* The {@code BresenhamLineTest} class contains unit tests for the
* {@code BresenhamLine} class, specifically testing the
* {@code findLine} method.
*
* <p>This class uses parameterized tests to validate the output of
* Bresenham's line algorithm for various input points.</p>
*/
classBresenhamLineTest {
/**
* Provides test cases for the parameterized test.
*
* <p>Each test case includes starting coordinates, ending coordinates,
* and the expected collection of points that should be generated by the
* {@code findLine} method.</p>
*
* @return a stream of arguments containing test cases
*/
staticStream<Arguments> linePointsProvider() {
returnStream.of(Arguments.of(0, 0, 5, 5, List.of(newPoint(0, 0), newPoint(1, 1), newPoint(2, 2), newPoint(3, 3), newPoint(4, 4), newPoint(5, 5))), Arguments.of(0, 0, 5, 0, List.of(newPoint(0, 0), newPoint(1, 0), newPoint(2, 0), newPoint(3, 0), newPoint(4, 0), newPoint(5, 0))),
Arguments.of(0, 0, 0, 5, List.of(newPoint(0, 0), newPoint(0, 1), newPoint(0, 2), newPoint(0, 3), newPoint(0, 4), newPoint(0, 5))), Arguments.of(-2, -2, -5, -5, List.of(newPoint(-2, -2), newPoint(-3, -3), newPoint(-4, -4), newPoint(-5, -5))),
Arguments.of(-1, -1, 2, 2, List.of(newPoint(-1, -1), newPoint(0, 0), newPoint(1, 1), newPoint(2, 2))), Arguments.of(2, -1, -1, -4, List.of(newPoint(2, -1), newPoint(1, -2), newPoint(0, -3), newPoint(-1, -4))));
}
/**
* Tests the {@code findLine} method of the {@code BresenhamLine} class.
*
* <p>This parameterized test runs multiple times with different sets of
* starting and ending coordinates to validate that the generated points
* match the expected output.</p>
*
* @param x0 the x-coordinate of the starting point
* @param y0 the y-coordinate of the starting point
* @param x1 the x-coordinate of the ending point
* @param y1 the y-coordinate of the ending point
* @param expected a collection of expected points that should form a line
*/
@ParameterizedTest
@MethodSource("linePointsProvider")
voidtestFindLine(intx0, inty0, intx1, inty1, Collection<Point> expected) {
List<Point> actual = BresenhamLine.findLine(x0, y0, x1, y1);
Assertions.assertEquals(expected.size(), actual.size(), "The size of the points list should match.");
Assertions.assertTrue(expected.containsAll(actual) && actual.containsAll(expected), "The points generated should match the expected points.");
}
}