|
| 1 | +package com.thealgorithms.divideandconquer; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class SkylineAlgorithmTest { |
| 10 | + |
| 11 | + private SkylineAlgorithm skylineAlgorithm; |
| 12 | + |
| 13 | + @BeforeEach |
| 14 | + public void setUp() { |
| 15 | + skylineAlgorithm = new SkylineAlgorithm(); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testProduceSubSkyLinesSinglePoint() { |
| 20 | + // Test with a single point |
| 21 | + ArrayList<SkylineAlgorithm.Point> points = new ArrayList<>(); |
| 22 | + points.add(new SkylineAlgorithm.Point(1, 10)); |
| 23 | + |
| 24 | + ArrayList<SkylineAlgorithm.Point> result = skylineAlgorithm.produceSubSkyLines(points); |
| 25 | + |
| 26 | + assertEquals(1, result.size()); |
| 27 | + assertEquals(1, result.get(0).getX()); |
| 28 | + assertEquals(10, result.get(0).getY()); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testProduceSubSkyLinesTwoPoints() { |
| 33 | + // Test with two points, one dominated by the other |
| 34 | + ArrayList<SkylineAlgorithm.Point> points = new ArrayList<>(); |
| 35 | + points.add(new SkylineAlgorithm.Point(1, 10)); |
| 36 | + points.add(new SkylineAlgorithm.Point(1, 5)); |
| 37 | + |
| 38 | + ArrayList<SkylineAlgorithm.Point> result = skylineAlgorithm.produceSubSkyLines(points); |
| 39 | + |
| 40 | + assertEquals(1, result.size()); |
| 41 | + assertEquals(1, result.get(0).getX()); |
| 42 | + assertEquals(5, result.get(0).getY()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testProduceSubSkyLinesMultiplePoints() { |
| 47 | + // Test with more than two points |
| 48 | + ArrayList<SkylineAlgorithm.Point> points = new ArrayList<>(); |
| 49 | + points.add(new SkylineAlgorithm.Point(1, 10)); |
| 50 | + points.add(new SkylineAlgorithm.Point(2, 15)); |
| 51 | + points.add(new SkylineAlgorithm.Point(3, 5)); |
| 52 | + points.add(new SkylineAlgorithm.Point(4, 20)); |
| 53 | + |
| 54 | + ArrayList<SkylineAlgorithm.Point> result = skylineAlgorithm.produceSubSkyLines(points); |
| 55 | + |
| 56 | + assertEquals(2, result.size()); |
| 57 | + |
| 58 | + // Assert the correct points in skyline |
| 59 | + assertEquals(1, result.get(0).getX()); |
| 60 | + assertEquals(10, result.get(0).getY()); |
| 61 | + assertEquals(3, result.get(1).getX()); |
| 62 | + assertEquals(5, result.get(1).getY()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testProduceFinalSkyLine() { |
| 67 | + // Test merging two skylines |
| 68 | + ArrayList<SkylineAlgorithm.Point> left = new ArrayList<>(); |
| 69 | + left.add(new SkylineAlgorithm.Point(1, 10)); |
| 70 | + left.add(new SkylineAlgorithm.Point(2, 5)); |
| 71 | + |
| 72 | + ArrayList<SkylineAlgorithm.Point> right = new ArrayList<>(); |
| 73 | + right.add(new SkylineAlgorithm.Point(3, 8)); |
| 74 | + right.add(new SkylineAlgorithm.Point(4, 3)); |
| 75 | + |
| 76 | + ArrayList<SkylineAlgorithm.Point> result = skylineAlgorithm.produceFinalSkyLine(left, right); |
| 77 | + |
| 78 | + assertEquals(3, result.size()); |
| 79 | + |
| 80 | + // Assert the correct points in the final skyline |
| 81 | + assertEquals(1, result.get(0).getX()); |
| 82 | + assertEquals(10, result.get(0).getY()); |
| 83 | + assertEquals(2, result.get(1).getX()); |
| 84 | + assertEquals(5, result.get(1).getY()); |
| 85 | + assertEquals(4, result.get(2).getX()); |
| 86 | + assertEquals(3, result.get(2).getY()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testXComparator() { |
| 91 | + // Test the XComparator used for sorting the points |
| 92 | + SkylineAlgorithm.XComparator comparator = new SkylineAlgorithm().new XComparator(); |
| 93 | + |
| 94 | + SkylineAlgorithm.Point p1 = new SkylineAlgorithm.Point(1, 10); |
| 95 | + SkylineAlgorithm.Point p2 = new SkylineAlgorithm.Point(2, 5); |
| 96 | + |
| 97 | + // Check if the XComparator sorts points by their x-value |
| 98 | + assertEquals(-1, comparator.compare(p1, p2)); // p1.x < p2.x |
| 99 | + assertEquals(1, comparator.compare(p2, p1)); // p2.x > p1.x |
| 100 | + assertEquals(0, comparator.compare(p1, new SkylineAlgorithm.Point(1, 15))); // p1.x == p2.x |
| 101 | + } |
| 102 | +} |
0 commit comments