Skip to content

Commit ac66782

Browse files
committed
Add tests to cover private constructor and strip loop condition
1 parent 811de62 commit ac66782

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.thealgorithms.randomized;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.lang.reflect.Constructor;
8+
import java.lang.reflect.InvocationTargetException;
49

510
import com.thealgorithms.randomized.RandomizedClosestPair.Point;
611
import org.junit.jupiter.api.Test;
@@ -9,22 +14,41 @@ public class RandomizedClosestPairTest {
914

1015
@Test
1116
public void testFindClosestPairDistance() {
12-
Point[] points = {new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(8, 8), new Point(13, 13)};
17+
Point[] points = { new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(8, 8), new Point(13, 13) };
1318
double result = RandomizedClosestPair.findClosestPairDistance(points);
1419
assertEquals(Math.sqrt(2), result, 0.00001);
1520
}
1621

1722
@Test
1823
public void testWithIdenticalPoints() {
19-
Point[] points = {new Point(0, 0), new Point(0, 0), new Point(1, 1)};
24+
Point[] points = { new Point(0, 0), new Point(0, 0), new Point(1, 1) };
2025
double result = RandomizedClosestPair.findClosestPairDistance(points);
2126
assertEquals(0.0, result, 0.00001);
2227
}
2328

2429
@Test
2530
public void testWithDistantPoints() {
26-
Point[] points = {new Point(0, 0), new Point(5, 0), new Point(10, 0)};
31+
Point[] points = { new Point(0, 0), new Point(5, 0), new Point(10, 0) };
2732
double result = RandomizedClosestPair.findClosestPairDistance(points);
2833
assertEquals(5.0, result, 0.00001);
2934
}
35+
36+
@Test
37+
public void testPrivateConstructor() throws Exception {
38+
Constructor<RandomizedClosestPair> constructor = RandomizedClosestPair.class.getDeclaredConstructor();
39+
constructor.setAccessible(true);
40+
assertThrows(InvocationTargetException.class, constructor::newInstance);
41+
}
42+
43+
@Test
44+
public void testStripConditionCoverage() {
45+
Point[] points = {
46+
new Point(0, 0),
47+
new Point(0.001, 0.001),
48+
new Point(0.002, 0.002)
49+
};
50+
double result = RandomizedClosestPair.findClosestPairDistance(points);
51+
assertTrue(result < 0.01); // distance should be covered by strip logic
52+
}
53+
3054
}

0 commit comments

Comments
 (0)