1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SolutionTest {
@Test
public void test01_Example() {
List<Point> points = Arrays.asList(
new Point(2, 2), //A
new Point(2, 8), //B
new Point(5, 5), //C
new Point(6, 3), //D
new Point(6, 7), //E
new Point(7, 4), //F
new Point(7, 9) //G
);
List<Point> result = Solution.closestPair(points);
List<Point> expected = Arrays.asList(new Point(6, 3), new Point(7, 4));
verify(expected, result);
}
@Test
public void test02_TwoPoints() {
List<Point> points = Arrays.asList(
new Point(2, 2),
new Point(6, 3)
);
List<Point> result = Solution.closestPair(points);
List<Point> expected = Arrays.asList(new Point(6, 3), new Point(2, 2));
verify(expected, result);
}
@Test
public void test03_DuplicatedPoint() {
List<Point> points = Arrays.asList(
new Point(2, 2), //A
new Point(2, 8), //B
new Point(5, 5), //C
new Point(5, 5), //C
new Point(6, 3), //D
new Point(6, 7), //E
new Point(7, 4), //F
new Point(7, 9) //G
);
List<Point> result = Solution.closestPair(points);
List<Point> expected = Arrays.asList(new Point(5, 5), new Point(5,5));
verify(expected, result);
}
private void verify(List<Point> expected, List<Point> actual) {
Comparator<Point> comparer = Comparator.<Point>comparingDouble(p -> p.x);
Assert.assertNotNull("Returned array cannot be null.", actual);
Assert.assertEquals("Expected exactly two points.", 2, actual.size());
Assert.assertFalse("Returned points must not be null.", actual.get(0) == null || actual.get(1) == null);
expected.sort(comparer);
actual.sort(comparer);
boolean eq = expected.get(0).x == actual.get(0).x && expected.get(0).y == actual.get(0).y
&& expected.get(1).x == actual.get(1).x && expected.get(1).y == actual.get(1).y;
Assert.assertTrue(String.format("Expected: %s, Actual: %s", expected.toString(), actual.toString()), eq);
}
}
|