-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
MidpointCircleTest.java
55 lines (46 loc) · 1.92 KB
/
MidpointCircleTest.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
package com.thealgorithms.geometry;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
/**
* Test class for the {@code MidpointCircle} class
*/
class MidpointCircleTest {
/**
* Parameterized test to check the generated points for different circles.
* The points are checked based on the expected center and radius.
*
* @param centerX The x-coordinate of the circle's center.
* @param centerY The y-coordinate of the circle's center.
* @param radius The radius of the circle.
*/
@ParameterizedTest
@CsvSource({
"0, 0, 3", // Circle centered at (0, 0) with radius 3
"10, 10, 2" // Circle centered at (10, 10) with radius 2
})
void
testGenerateCirclePoints(int centerX, int centerY, int radius) {
List<int[]> points = MidpointCircle.generateCirclePoints(centerX, centerY, radius);
// Ensure that all points satisfy the circle equation (x - centerX)^2 + (y - centerY)^2 = radius^2
for (int[] point : points) {
int x = point[0];
int y = point[1];
int dx = x - centerX;
int dy = y - centerY;
int distanceSquared = dx * dx + dy * dy;
assertTrue(Math.abs(distanceSquared - radius * radius) <= 1, "Point (" + x + ", " + y + ") does not satisfy the circle equation.");
}
}
/**
* Test to ensure the algorithm generates points for a zero-radius circle.
*/
@Test
void testZeroRadiusCircle() {
List<int[]> points = MidpointCircle.generateCirclePoints(0, 0, 0);
// A zero-radius circle should only have one point: (0, 0)
assertTrue(points.size() == 1 && points.get(0)[0] == 0 && points.get(0)[1] == 0, "Zero-radius circle did not generate the correct point.");
}
}