mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Add RandomizedClosestPair Algorithm and Unit Tests (#6339)
This commit is contained in:
@ -0,0 +1,30 @@
|
||||
package com.thealgorithms.randomized;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.thealgorithms.randomized.RandomizedClosestPair.Point;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class RandomizedClosestPairTest {
|
||||
|
||||
@Test
|
||||
public void testFindClosestPairDistance() {
|
||||
Point[] points = {new Point(1, 1), new Point(2, 2), new Point(3, 3), new Point(8, 8), new Point(13, 13)};
|
||||
double result = RandomizedClosestPair.findClosestPairDistance(points);
|
||||
assertEquals(Math.sqrt(2), result, 0.00001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithIdenticalPoints() {
|
||||
Point[] points = {new Point(0, 0), new Point(0, 0), new Point(1, 1)};
|
||||
double result = RandomizedClosestPair.findClosestPairDistance(points);
|
||||
assertEquals(0.0, result, 0.00001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithDistantPoints() {
|
||||
Point[] points = {new Point(0, 0), new Point(5, 0), new Point(10, 0)};
|
||||
double result = RandomizedClosestPair.findClosestPairDistance(points);
|
||||
assertEquals(5.0, result, 0.00001);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user