forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiApproximationTest.java
More file actions
169 lines (141 loc) · 5.55 KB
/
PiApproximationTest.java
File metadata and controls
169 lines (141 loc) · 5.55 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
class PiApproximationTest {
private static final double DELTA = 0.5; // Tolerance for Pi approximation
private static final double TIGHT_DELTA = 0.1; // Tighter tolerance for large samples
/**
* Test with known points that are all inside the quarter circle.
*/
@Test
public void testAllPointsInside() {
List<PiApproximation.Point> points = new ArrayList<>();
points.add(new PiApproximation.Point(0.0, 0.0)); // Origin
points.add(new PiApproximation.Point(0.5, 0.5)); // Inside
points.add(new PiApproximation.Point(0.3, 0.3)); // Inside
double result = PiApproximation.approximatePi(points);
// All points inside, so result should be 4.0
assertEquals(4.0, result, 0.001);
}
/**
* Test with known points that are all outside the quarter circle.
*/
@Test
public void testAllPointsOutside() {
List<PiApproximation.Point> points = new ArrayList<>();
points.add(new PiApproximation.Point(1.0, 1.0)); // Corner - outside
points.add(new PiApproximation.Point(0.9, 0.9)); // Outside
double result = PiApproximation.approximatePi(points);
// No points inside, so result should be 0.0
assertEquals(0.0, result, 0.001);
}
/**
* Test with mixed points (some inside, some outside).
*/
@Test
public void testMixedPoints() {
List<PiApproximation.Point> points = new ArrayList<>();
// Inside points
points.add(new PiApproximation.Point(0.0, 0.0));
points.add(new PiApproximation.Point(0.5, 0.5));
// Outside points
points.add(new PiApproximation.Point(1.0, 1.0));
points.add(new PiApproximation.Point(0.9, 0.9));
double result = PiApproximation.approximatePi(points);
// 2 out of 4 points inside: 4 * 2/4 = 2.0
assertEquals(2.0, result, 0.001);
}
/**
* Test with boundary point (on the circle).
*/
@Test
public void testBoundaryPoint() {
List<PiApproximation.Point> points = new ArrayList<>();
points.add(new PiApproximation.Point(1.0, 0.0)); // On circle: x² + y² = 1
points.add(new PiApproximation.Point(0.0, 1.0)); // On circle
double result = PiApproximation.approximatePi(points);
// Boundary points should be counted as inside (≤ 1)
assertEquals(4.0, result, 0.001);
}
/**
* Test with small random sample (moderate accuracy expected).
*/
@Test
public void testSmallRandomSample() {
List<PiApproximation.Point> points = PiApproximation.generateRandomPoints(1000);
double result = PiApproximation.approximatePi(points);
// With 1000 points, result should be reasonably close to π
assertEquals(Math.PI, result, DELTA);
}
/**
* Test with large random sample (better accuracy expected).
*/
@Test
public void testLargeRandomSample() {
List<PiApproximation.Point> points = PiApproximation.generateRandomPoints(100000);
double result = PiApproximation.approximatePi(points);
// With 100000 points, result should be very close to π
assertEquals(Math.PI, result, TIGHT_DELTA);
}
/**
* Test that result is always positive.
*/
@Test
public void testResultIsPositive() {
List<PiApproximation.Point> points = PiApproximation.generateRandomPoints(1000);
double result = PiApproximation.approximatePi(points);
assertTrue(result >= 0, "Pi approximation should be positive");
}
/**
* Test that result is bounded (0 ≤ result ≤ 4).
*/
@Test
public void testResultIsBounded() {
List<PiApproximation.Point> points = PiApproximation.generateRandomPoints(1000);
double result = PiApproximation.approximatePi(points);
assertTrue(result >= 0 && result <= 4, "Pi approximation should be between 0 and 4");
}
/**
* Test with single point inside.
*/
@Test
public void testSinglePointInside() {
List<PiApproximation.Point> points = new ArrayList<>();
points.add(new PiApproximation.Point(0.0, 0.0));
double result = PiApproximation.approximatePi(points);
assertEquals(4.0, result, 0.001);
}
/**
* Test with single point outside.
*/
@Test
public void testSinglePointOutside() {
List<PiApproximation.Point> points = new ArrayList<>();
points.add(new PiApproximation.Point(1.0, 1.0));
double result = PiApproximation.approximatePi(points);
assertEquals(0.0, result, 0.001);
}
/**
* Test that generated points are within valid range [0, 1].
*/
@Test
public void testGeneratedPointsInRange() {
List<PiApproximation.Point> points = PiApproximation.generateRandomPoints(100);
for (PiApproximation.Point p : points) {
assertTrue(p.x >= 0 && p.x <= 1, "X coordinate should be between 0 and 1");
assertTrue(p.y >= 0 && p.y <= 1, "Y coordinate should be between 0 and 1");
}
}
/**
* Test that the correct number of points are generated.
*/
@Test
public void testCorrectNumberOfPointsGenerated() {
int expectedSize = 500;
List<PiApproximation.Point> points = PiApproximation.generateRandomPoints(expectedSize);
assertEquals(expectedSize, points.size());
}
}