Skip to content

Add tests for SkylineAlgorithm.java #5556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@
* [ZigzagTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/ZigzagTraversalTest.java)
* divideandconquer
* [BinaryExponentiationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/BinaryExponentiationTest.java)
* [SkylineAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/SkylineAlgorithmTest.java)
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
* dynamicprogramming
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.thealgorithms.divideandconquer;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SkylineAlgorithmTest {

private SkylineAlgorithm skylineAlgorithm;

@BeforeEach
public void setUp() {
skylineAlgorithm = new SkylineAlgorithm();
}

@Test
public void testProduceSubSkyLinesSinglePoint() {
// Test with a single point
ArrayList<SkylineAlgorithm.Point> points = new ArrayList<>();
points.add(new SkylineAlgorithm.Point(1, 10));

ArrayList<SkylineAlgorithm.Point> result = skylineAlgorithm.produceSubSkyLines(points);

assertEquals(1, result.size());
assertEquals(1, result.get(0).getX());
assertEquals(10, result.get(0).getY());
}

@Test
public void testProduceSubSkyLinesTwoPoints() {
// Test with two points, one dominated by the other
ArrayList<SkylineAlgorithm.Point> points = new ArrayList<>();
points.add(new SkylineAlgorithm.Point(1, 10));
points.add(new SkylineAlgorithm.Point(1, 5));

ArrayList<SkylineAlgorithm.Point> result = skylineAlgorithm.produceSubSkyLines(points);

assertEquals(1, result.size());
assertEquals(1, result.get(0).getX());
assertEquals(5, result.get(0).getY());
}

@Test
public void testProduceSubSkyLinesMultiplePoints() {
// Test with more than two points
ArrayList<SkylineAlgorithm.Point> points = new ArrayList<>();
points.add(new SkylineAlgorithm.Point(1, 10));
points.add(new SkylineAlgorithm.Point(2, 15));
points.add(new SkylineAlgorithm.Point(3, 5));
points.add(new SkylineAlgorithm.Point(4, 20));

ArrayList<SkylineAlgorithm.Point> result = skylineAlgorithm.produceSubSkyLines(points);

assertEquals(2, result.size());

// Assert the correct points in skyline
assertEquals(1, result.get(0).getX());
assertEquals(10, result.get(0).getY());
assertEquals(3, result.get(1).getX());
assertEquals(5, result.get(1).getY());
}

@Test
public void testProduceFinalSkyLine() {
// Test merging two skylines
ArrayList<SkylineAlgorithm.Point> left = new ArrayList<>();
left.add(new SkylineAlgorithm.Point(1, 10));
left.add(new SkylineAlgorithm.Point(2, 5));

ArrayList<SkylineAlgorithm.Point> right = new ArrayList<>();
right.add(new SkylineAlgorithm.Point(3, 8));
right.add(new SkylineAlgorithm.Point(4, 3));

ArrayList<SkylineAlgorithm.Point> result = skylineAlgorithm.produceFinalSkyLine(left, right);

assertEquals(3, result.size());

// Assert the correct points in the final skyline
assertEquals(1, result.get(0).getX());
assertEquals(10, result.get(0).getY());
assertEquals(2, result.get(1).getX());
assertEquals(5, result.get(1).getY());
assertEquals(4, result.get(2).getX());
assertEquals(3, result.get(2).getY());
}

@Test
public void testXComparator() {
// Test the XComparator used for sorting the points
SkylineAlgorithm.XComparator comparator = new SkylineAlgorithm().new XComparator();

SkylineAlgorithm.Point p1 = new SkylineAlgorithm.Point(1, 10);
SkylineAlgorithm.Point p2 = new SkylineAlgorithm.Point(2, 5);

// Check if the XComparator sorts points by their x-value
assertEquals(-1, comparator.compare(p1, p2)); // p1.x < p2.x
assertEquals(1, comparator.compare(p2, p1)); // p2.x > p1.x
assertEquals(0, comparator.compare(p1, new SkylineAlgorithm.Point(1, 15))); // p1.x == p2.x
}
}