|
| 1 | +package com.thealgorithms.divideandconquer; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +public class ClosestPairTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void testBuildLocation() { |
| 12 | + ClosestPair cp = new ClosestPair(1); |
| 13 | + ClosestPair.Location point = cp.buildLocation(3.0, 4.0); |
| 14 | + assertNotNull(point); |
| 15 | + assertEquals(3.0, point.x); |
| 16 | + assertEquals(4.0, point.y); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void testCreateLocation() { |
| 21 | + ClosestPair cp = new ClosestPair(5); |
| 22 | + ClosestPair.Location[] locations = cp.createLocation(5); |
| 23 | + assertNotNull(locations); |
| 24 | + assertEquals(5, locations.length); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testXPartition() { |
| 29 | + ClosestPair cp = new ClosestPair(5); |
| 30 | + ClosestPair.Location[] points = new ClosestPair.Location[5]; |
| 31 | + points[0] = cp.buildLocation(2.0, 3.0); |
| 32 | + points[1] = cp.buildLocation(5.0, 1.0); |
| 33 | + points[2] = cp.buildLocation(1.0, 6.0); |
| 34 | + points[3] = cp.buildLocation(4.0, 7.0); |
| 35 | + points[4] = cp.buildLocation(3.0, 2.0); |
| 36 | + |
| 37 | + int pivotIndex = cp.xPartition(points, 0, 4); |
| 38 | + assertEquals(2, pivotIndex); |
| 39 | + assertEquals(2.0, points[0].x); |
| 40 | + assertEquals(1.0, points[1].x); |
| 41 | + assertEquals(3.0, points[2].x); |
| 42 | + assertEquals(4.0, points[3].x); |
| 43 | + assertEquals(5.0, points[4].x); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testYPartition() { |
| 48 | + ClosestPair cp = new ClosestPair(5); |
| 49 | + ClosestPair.Location[] points = new ClosestPair.Location[5]; |
| 50 | + points[0] = cp.buildLocation(2.0, 3.0); |
| 51 | + points[1] = cp.buildLocation(5.0, 1.0); |
| 52 | + points[2] = cp.buildLocation(1.0, 6.0); |
| 53 | + points[3] = cp.buildLocation(4.0, 7.0); |
| 54 | + points[4] = cp.buildLocation(3.0, 2.0); |
| 55 | + |
| 56 | + int pivotIndex = cp.yPartition(points, 0, 4); |
| 57 | + assertEquals(1, pivotIndex); |
| 58 | + assertEquals(2.0, points[1].y); |
| 59 | + assertEquals(3.0, points[4].y); |
| 60 | + assertEquals(1.0, points[0].y); |
| 61 | + assertEquals(6.0, points[2].y); |
| 62 | + assertEquals(7.0, points[3].y); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testBruteForce() { |
| 67 | + ClosestPair cp = new ClosestPair(2); |
| 68 | + ClosestPair.Location loc1 = cp.buildLocation(1.0, 2.0); |
| 69 | + ClosestPair.Location loc2 = cp.buildLocation(4.0, 6.0); |
| 70 | + |
| 71 | + ClosestPair.Location[] locations = new ClosestPair.Location[] {loc1, loc2}; |
| 72 | + double result = cp.bruteForce(locations); |
| 73 | + assertEquals(5.0, result, 0.01); |
| 74 | + } |
| 75 | +} |
0 commit comments