Skip to content

Commit e5a4db7

Browse files
committed
Add tests for SkylineAlgorithm.java
1 parent 842ff52 commit e5a4db7

File tree

1 file changed

+115
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)