|
| 1 | +package com.thealgorithms.divideandconquer; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +public class ClosestPairTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + public void testBuildLocation() { |
| 13 | + // Test creating a point using the buildLocation method |
| 14 | + ClosestPair cp = new ClosestPair(1); |
| 15 | + ClosestPair.Location point = cp.buildLocation(3.0, 4.0); |
| 16 | + assertNotNull(point); |
| 17 | + assertEquals(3.0, point.x); |
| 18 | + assertEquals(4.0, point.y); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testCreateLocation() { |
| 23 | + // Test creating an array of locations using the createLocation method |
| 24 | + ClosestPair cp = new ClosestPair(5); |
| 25 | + ClosestPair.Location[] locations = cp.createLocation(5); |
| 26 | + assertNotNull(locations); |
| 27 | + assertEquals(5, locations.length); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testXPartition() { |
| 32 | + // Test xPartition method: Sorting points by x-coordinate |
| 33 | + ClosestPair cp = new ClosestPair(5); |
| 34 | + ClosestPair.Location[] points = new ClosestPair.Location[5]; |
| 35 | + points[0] = cp.buildLocation(2.0, 3.0); |
| 36 | + points[1] = cp.buildLocation(5.0, 1.0); |
| 37 | + points[2] = cp.buildLocation(1.0, 6.0); |
| 38 | + points[3] = cp.buildLocation(4.0, 7.0); |
| 39 | + points[4] = cp.buildLocation(3.0, 2.0); |
| 40 | + |
| 41 | + int pivotIndex = cp.xPartition(points, 0, 4); |
| 42 | + assertEquals(2, pivotIndex); // After partition, pivot should be at index 2 |
| 43 | + assertEquals(1.0, points[0].x); |
| 44 | + assertEquals(2.0, points[1].x); |
| 45 | + assertEquals(3.0, points[2].x); |
| 46 | + assertEquals(4.0, points[3].x); |
| 47 | + assertEquals(5.0, points[4].x); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testYPartition() { |
| 52 | + // Test yPartition method: Sorting points by y-coordinate |
| 53 | + ClosestPair cp = new ClosestPair(5); |
| 54 | + ClosestPair.Location[] points = new ClosestPair.Location[5]; |
| 55 | + points[0] = cp.buildLocation(2.0, 3.0); |
| 56 | + points[1] = cp.buildLocation(5.0, 1.0); |
| 57 | + points[2] = cp.buildLocation(1.0, 6.0); |
| 58 | + points[3] = cp.buildLocation(4.0, 7.0); |
| 59 | + points[4] = cp.buildLocation(3.0, 2.0); |
| 60 | + |
| 61 | + int pivotIndex = cp.yPartition(points, 0, 4); |
| 62 | + assertEquals(3, pivotIndex); // After partition, pivot should be at index 3 |
| 63 | + assertEquals(1.0, points[1].y); |
| 64 | + assertEquals(2.0, points[4].y); |
| 65 | + assertEquals(3.0, points[0].y); |
| 66 | + assertEquals(6.0, points[2].y); |
| 67 | + assertEquals(7.0, points[3].y); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testBruteForce() { |
| 72 | + // Test bruteForce method to handle 2 points |
| 73 | + ClosestPair cp = new ClosestPair(2); |
| 74 | + ClosestPair.Location loc1 = cp.buildLocation(1.0, 2.0); |
| 75 | + ClosestPair.Location loc2 = cp.buildLocation(4.0, 6.0); |
| 76 | + |
| 77 | + // Use the bruteForce method and pass the locations as an array |
| 78 | + ClosestPair.Location[] locations = new ClosestPair.Location[]{loc1, loc2}; |
| 79 | + double result = cp.bruteForce(locations); |
| 80 | + assertEquals(5.0, result, 0.01); // Distance between (1, 2) and (4, 6) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testClosestPair() { |
| 85 | + // Test closestPair method for more than 2 points |
| 86 | + ClosestPair cp = new ClosestPair(5); |
| 87 | + cp.buildLocation(2.0, 3.0); |
| 88 | + cp.buildLocation(5.0, 1.0); |
| 89 | + cp.buildLocation(1.0, 6.0); |
| 90 | + cp.buildLocation(4.0, 7.0); |
| 91 | + cp.buildLocation(3.0, 2.0); |
| 92 | + |
| 93 | + cp.xQuickSort(cp.array, 0, cp.array.length - 1); // Sorting by x-coordinate |
| 94 | + double result = cp.closestPair(cp.array, cp.array.length); |
| 95 | + assertEquals(2.8284, result, 0.0001); // Closest pair is (2, 3) and (3, 2), distance ≈ 2.8284 |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testMainFunction() { |
| 100 | + // Test the main function with the given input |
| 101 | + ClosestPair cp = new ClosestPair(12); |
| 102 | + cp.buildLocation(2, 3); |
| 103 | + cp.buildLocation(2, 16); |
| 104 | + cp.buildLocation(3, 9); |
| 105 | + cp.buildLocation(6, 3); |
| 106 | + cp.buildLocation(7, 7); |
| 107 | + cp.buildLocation(19, 4); |
| 108 | + cp.buildLocation(10, 11); |
| 109 | + cp.buildLocation(15, 2); |
| 110 | + cp.buildLocation(15, 19); |
| 111 | + cp.buildLocation(16, 11); |
| 112 | + cp.buildLocation(17, 13); |
| 113 | + cp.buildLocation(9, 12); |
| 114 | + |
| 115 | + cp.xQuickSort(cp.array, 0, cp.array.length - 1); // Sorting by x value |
| 116 | + double result = cp.closestPair(cp.array, cp.array.length); |
| 117 | + |
| 118 | + assertNotNull(cp.point1); |
| 119 | + assertNotNull(cp.point2); |
| 120 | + assertTrue(result > 0); // The minimum distance should be positive |
| 121 | + } |
| 122 | +} |
0 commit comments