Skip to content

Commit f18e922

Browse files
committed
test: test for coordinates and also relative equality implemented
1 parent cf6a097 commit f18e922

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

freepapermaps/src/main/java/io/github/mrmaxguns/freepapermaps/projections/BoundingBox.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,26 @@ public double getHeight() {
5555

5656
public double getMaxLat() { return getMaxY(); }
5757

58-
/** Returns true if the corners of both bounding boxes are equal, as defined by the Coordinate equals method. */
58+
/** Returns true if the top left and bottom right coordinates of both bounding boxes are exactly equal. */
5959
public boolean equals(BoundingBox<C> other) {
60-
return topLeftCorner.equals(other.getTopLeftCorner()) && bottomRightCorner.equals(other.getBottomRightCorner());
60+
return equals(other, 0.0);
61+
}
62+
63+
/**
64+
* Returns true if the top left and bottom right coordinates of both bounding boxes are within (inclusive) of some
65+
* <code>epsilon</code>.
66+
*/
67+
public boolean equals(BoundingBox<C> other, double epsilon) {
68+
return equals(other, epsilon, epsilon);
69+
}
70+
71+
/**
72+
* Returns true if the top left and bottom right coordinates of both bounding boxes are within (inclusive) of some
73+
* x and y epsilon.
74+
*/
75+
public boolean equals(BoundingBox<C> other, double epsilonX, double epsilonY) {
76+
return topLeftCorner.equals(other.getTopLeftCorner(), epsilonX, epsilonY) &&
77+
bottomRightCorner.equals(other.getBottomRightCorner(), epsilonX, epsilonY);
6178
}
6279

6380
public String toString() {

freepapermaps/src/main/java/io/github/mrmaxguns/freepapermaps/projections/Coordinate.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,22 @@ public T scale(double scaleFactor) {
4949
return createInstance(x * scaleFactor, y * scaleFactor);
5050
}
5151

52-
// TODO: Make this more reasonable than == with doubles
52+
/** Returns true if both coordinates are exactly equal. */
5353
public boolean equals(T other) {
54-
return x == other.getX() && y == other.getY();
54+
return equals(other, 0.0);
55+
}
56+
57+
/** Returns true if both coordinate x and y values are within (inclusive) of some <code>epsilon</code>. */
58+
public boolean equals(T other, double epsilon) {
59+
return equals(other, epsilon, epsilon);
60+
}
61+
62+
/** Returns true if both coordinate x and y values are within (inclusive) of some x and y epsilon, respectively. */
63+
public boolean equals(T other, double epsilonX, double epsilonY) {
64+
if (this == other) {
65+
return true;
66+
}
67+
return (Math.abs(this.x - other.x) <= epsilonX) && (Math.abs(this.y - other.y) <= epsilonY);
5568
}
5669

5770
/** Returns a short, human-readable identifier of the Coordinate's type (e.g. WGS84). */
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package io.github.mrmaxguns.freepapermaps.projections;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
// TODO: Add @Overrides everywhere
8+
9+
10+
public class CoordinateTest {
11+
@Test
12+
public void testConstructor() {
13+
double x = 9.1, y = 8.2;
14+
TestCoordinate coordinate = new TestCoordinate(x, y);
15+
assertAll(() -> assertEquals(x, coordinate.getX(), 0.0001, "Coordinate constructor should initialize x"),
16+
() -> assertEquals(x, coordinate.getLon(), 0.0001,
17+
"Coordinate constructor should initialize longitude"),
18+
() -> assertEquals(y, coordinate.getY(), 0.0001, "Coordinate constructor should initialize y"),
19+
() -> assertEquals(y, coordinate.getLat(), 0.0001,
20+
"Coordinate constructor should initialize latitude"));
21+
}
22+
23+
@Test
24+
void testAdd() {
25+
TestCoordinate a = new TestCoordinate(11.23, 12.42);
26+
TestCoordinate b = new TestCoordinate(22.30, -11.22);
27+
TestCoordinate result = a.add(b);
28+
assertAll(() -> assertEquals(33.53, result.getLon(), 0.0001, "add() should add componentwise"),
29+
() -> assertEquals(1.2, result.getLat(), 0.0001, "add() should add componentwise"));
30+
}
31+
32+
@Test
33+
void testSubtract() {
34+
TestCoordinate a = new TestCoordinate(-12.04, 66.99);
35+
TestCoordinate b = new TestCoordinate(-9.98, -0.01);
36+
TestCoordinate result = a.subtract(b);
37+
assertAll(() -> assertEquals(-2.06, result.getLon(), 0.0001, "subtract() should subtract componentwise"),
38+
() -> assertEquals(67, result.getLat(), 0.0001, "subtract() should subtract componentwise"));
39+
}
40+
41+
@Test
42+
void testScale() {
43+
TestCoordinate c = new TestCoordinate(-120, 15);
44+
TestCoordinate result = c.scale(0.5);
45+
assertAll(() -> assertEquals(-60, result.getX(), 0.0001, "scale() should scale each component"),
46+
() -> assertEquals(7.5, result.getY(), 0.0001, "scale() should scale each component"));
47+
}
48+
49+
@Test
50+
void testEqualsTrue() {
51+
assertTrue(new TestCoordinate(10.1, 12.4).equals(new TestCoordinate(10.1, 12.4)),
52+
"equals() should check exact equality between components");
53+
}
54+
55+
@Test
56+
void testEqualsFalse() {
57+
assertFalse(new TestCoordinate(10, 15).equals(new TestCoordinate(10, -20)),
58+
"equals() should check exact equality between components");
59+
}
60+
61+
@Test
62+
void testEqualsWithEpsilonTrue() {
63+
assertTrue(new TestCoordinate(12.573, -13.981).equals(new TestCoordinate(12.575, -13.971), 0.01),
64+
"equals(other, epsilon) should use the epsilon as a bound");
65+
}
66+
67+
@Test
68+
void testEqualsWithEpsilonFalse() {
69+
assertFalse(new TestCoordinate(8765.1, -5757.3).equals(new TestCoordinate(8765.0, -5758.4), 1),
70+
"equals(other, epsilon) should use the epsilon as a bound");
71+
}
72+
73+
@Test
74+
void testEqualsWithEpsilonXYTrue() {
75+
assertTrue(new TestCoordinate(0.5737, 14838).equals(new TestCoordinate(0.5740, 14836), 0.0005, 3),
76+
"equals(other, epsilonX, epsilonY) should use the respective epsilons as bounds");
77+
}
78+
79+
@Test
80+
void testEqualsWithEpsilonXYFalse() {
81+
assertFalse(new TestCoordinate(-9494, 11.23).equals(new TestCoordinate(-9490, 11.24), 20, 0.0001),
82+
"equals(other, epsilonX, epsilonY) should use the respective epsilons as bounds");
83+
}
84+
85+
86+
static class TestCoordinate extends Coordinate<TestCoordinate> {
87+
public TestCoordinate(double x, double y) {
88+
super(x, y);
89+
}
90+
91+
@Override
92+
protected TestCoordinate createInstance(double x, double y) {
93+
return new TestCoordinate(x, y);
94+
}
95+
96+
@Override
97+
public String getCoordinateType() {
98+
return "TestCoordinate";
99+
}
100+
101+
@Override
102+
public String getCoordinateUnit() {
103+
return "?";
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)