Skip to content

Commit d587789

Browse files
author
Isaac Brodsky
committed
Add additional tests
1 parent 14016f8 commit d587789

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

src/test/java/com/uber/h3core/TestH3Core.java

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ public void testCellArea() {
148148
}
149149
}
150150

151+
@Test
152+
public void testCellAreaInvalid() {
153+
// Passing in a zero should not cause a crash
154+
h3.cellArea(0, AreaUnit.rads2);
155+
}
156+
157+
@Test(expected = IllegalArgumentException.class)
158+
public void testCellAreaInvalidUnit() {
159+
long cell = h3.geoToH3(0, 0, 0);
160+
h3.cellArea(cell, null);
161+
}
162+
151163
@Test
152164
public void testExactEdgeLength() {
153165
for (int res = 0; res <= 15; res++) {
@@ -176,19 +188,79 @@ public void testExactEdgeLength() {
176188
}
177189
}
178190

191+
@Test
192+
public void testExactEdgeLengthInvalid() {
193+
// Passing in a zero should not cause a crash
194+
h3.exactEdgeLength(0, LengthUnit.rads);
195+
}
196+
197+
@Test(expected = IllegalArgumentException.class)
198+
public void testExactEdgeLengthInvalidUnit() {
199+
long cell = h3.geoToH3(0, 0, 0);
200+
long edge = h3.getH3UnidirectionalEdgesFromHexagon(cell).get(0);
201+
h3.exactEdgeLength(edge, null);
202+
}
203+
179204
@Test
180205
public void testPointDist() {
181-
GeoCoord a = new GeoCoord(10, 10);
182-
GeoCoord b = new GeoCoord(10, -10);
206+
GeoCoord[] testA = { new GeoCoord(10, 10), new GeoCoord(0, 0), new GeoCoord(23, 23) };
207+
GeoCoord[] testB = { new GeoCoord(10, -10), new GeoCoord(-10, 0),new GeoCoord(23, 23) };
208+
double[] testDistanceDegrees = { 20, 10, 0 };
209+
210+
for (int i = 0; i < testA.length; i++) {
211+
GeoCoord a = testA[i];
212+
GeoCoord b = testB[i];
213+
double expectedRads = Math.toRadians(testDistanceDegrees[i]);
214+
215+
double distRads = h3.pointDist(a, b, LengthUnit.rads);
216+
double distKm = h3.pointDist(a, b, LengthUnit.km);
217+
double distM = h3.pointDist(a, b, LengthUnit.m);
218+
219+
// TODO: Epsilon is unusually large in the core H3 tests
220+
assertEquals("radians distance is as expected", expectedRads, distRads, EPSILON * 10000);
221+
if (expectedRads == 0) {
222+
assertEquals("m distance is zero", 0, distM, EPSILON);
223+
assertEquals("km distance is zero", 0, distKm, EPSILON);
224+
} else {
225+
assertTrue("m distance greater than km distance", distM > distKm);
226+
assertTrue("km distance greater than rads distance", distKm > distRads);
227+
}
228+
}
229+
}
230+
231+
@Test
232+
public void testPointDistNaN() {
233+
GeoCoord zero = new GeoCoord(0, 0);
234+
GeoCoord nan = new GeoCoord(Double.NaN, Double.NaN);
235+
double dist1 = h3.pointDist(nan, zero, LengthUnit.rads);
236+
double dist2 = h3.pointDist(zero, nan, LengthUnit.km);
237+
double dist3 = h3.pointDist(nan, nan, LengthUnit.m);
238+
assertTrue("nan distance results in nan", Double.isNaN(dist1));
239+
assertTrue("nan distance results in nan", Double.isNaN(dist2));
240+
assertTrue("nan distance results in nan", Double.isNaN(dist3));
241+
}
183242

184-
double distRads = h3.pointDist(a, b, LengthUnit.rads);
185-
double distKm = h3.pointDist(a, b, LengthUnit.km);
186-
double distM = h3.pointDist(a, b, LengthUnit.m);
243+
@Test
244+
public void testPointDistPositiveInfinity() {
245+
GeoCoord posInf = new GeoCoord(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
246+
GeoCoord zero = new GeoCoord(0, 0);
247+
double dist = h3.pointDist(posInf, zero, LengthUnit.m);
248+
assertTrue("+Infinity distance results in NaN", Double.isNaN(dist));
249+
}
250+
251+
@Test
252+
public void testPointDistNegativeInfinity() {
253+
GeoCoord negInf = new GeoCoord(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY);
254+
GeoCoord zero = new GeoCoord(0, 0);
255+
double dist = h3.pointDist(negInf, zero, LengthUnit.m);
256+
assertTrue("-Infinity distance results in NaN", Double.isNaN(dist));
257+
}
187258

188-
// TODO: Epsilon is unusually large in the core H3 tests
189-
assertEquals("radians distance is as expected", Math.toRadians(20), distRads, EPSILON * 10000);
190-
assertTrue("m distance greater than km distance", distM > distKm);
191-
assertTrue("km distance greater than rads distance", distKm > distRads);
259+
@Test(expected = IllegalArgumentException.class)
260+
public void testPointDistInvalid() {
261+
GeoCoord a = new GeoCoord(0, 0);
262+
GeoCoord b = new GeoCoord(0, 0);
263+
h3.pointDist(a, b, null);
192264
}
193265

194266
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)