[android] #3556 - Added test to trigger NaN value on same point, updated LatLng model to handle calls for distance calls of same point

This commit is contained in:
Tobrun
2016-01-18 12:41:20 +01:00
parent 0ebf5fcaae
commit 78bedff8f9
2 changed files with 22 additions and 10 deletions

View File

@@ -129,7 +129,6 @@ public class LatLng implements ILatLng, Parcelable, Serializable {
LatLng latLng = (LatLng) o;
return Double.compare(latLng.altitude, altitude) == 0 && Double.compare(latLng.latitude, latitude) == 0 && Double.compare(latLng.longitude, longitude) == 0;
}
@Override
@@ -168,6 +167,10 @@ public class LatLng implements ILatLng, Parcelable, Serializable {
* @return distance in meters
*/
public double distanceTo(LatLng other) {
if(latitude == other.latitude && longitude == other.longitude){
// return 0.0 to avoid a NaN
return 0.0;
}
final double a1 = MathConstants.DEG2RAD * this.latitude;
final double a2 = MathConstants.DEG2RAD * this.longitude;

View File

@@ -6,6 +6,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -43,6 +44,23 @@ public class LatLngTest {
assertEquals("altitude default value", latLng2.getAltitude(), altitude, DELTA);
}
@Test
public void testDistanceTo() {
LatLng latLng1 = new LatLng(0.0, 0.0);
LatLng latLng2 = new LatLng(1.0, 1.0);
assertEquals("distances should match",
latLng1.distanceTo(latLng2),
157425.53710839353, DELTA);
}
@Test
public void testDistanceToSamePoint() {
LatLng latLng1 = new LatLng(40.71199035644531, -74.0081);
LatLng latLng2 = new LatLng(40.71199035644531, -74.0081);
double distance = latLng1.distanceTo(latLng2);
assertEquals("distance should match", 0.0, distance, DELTA);
}
/*
* A sample test where Mockito is necessary
*/
@@ -86,13 +104,4 @@ public class LatLngTest {
"LatLng [longitude=3.4, latitude=1.2, altitude=5.6]");
}
@Test
public void testDistanceTo() {
LatLng latLng1 = new LatLng(0.0, 0.0);
LatLng latLng2 = new LatLng(1.0, 1.0);
assertEquals("distances should match",
latLng1.distanceTo(latLng2),
157425.53710839353, DELTA);
}
}