Open etorres opened 9 years ago
I'm posting a possible solution using a bi-function to compare coordinate values with a tolerance for nearly-equal values:
import java.util.function.BiFunction;
import static com.google.common.math.DoubleMath.fuzzyEquals;
public final class GeoJsons {
public static final double TOLERANCE = 0.00000001d;
public static final BiFunction<Point, Point, Boolean> POINT_FUZZY_EQUALS = (p1, p2) -> {
if (p1 == p2) return true;
else if (p1 == null || p2 == null) return false;
final LngLatAlt c1 = p1.getCoordinates();
final LngLatAlt c2 = p2.getCoordinates();
if (c1 == c2) return true;
else if (c1 == null || c2 == null) return false;
return fuzzyEquals(c1.getLongitude(), c2.getLongitude(), TOLERANCE)
&& fuzzyEquals(c1.getLatitude(), c2.getLatitude(), TOLERANCE)
&& fuzzyEquals(c1.getAltitude(), c2.getAltitude(), TOLERANCE);
};
}
Hi,
you can take a look to AssertJ (http://joel-costigliola.github.io/assertj/) who is a better remplacement of Hamcrest.
Assertions.assertThat(12D).isEqualTo(12.1, Offset.offset(0.1))
Hi, many thanks for your response and, definitely, many thanks for the tip. I didn't know about this test library. Anyway, I think that decimal places should not be trimmed during serialization, or at least a larger precision should be allowed. BTW, in my previous message I propose a replacement for the equals method that can be used to check two points that are supposed to be nearly-equal, which is a more general solution and can be used beyond basic unit testing.
Is this issue fixed?
Double to String conversion in LngLatAlt serializer breaks LngLatAlt#equals(Object) method due to rounded values. Please, take into account the following test where a new point is created from real longitude and latitude values, converted into JSON and read again into a Point object:
The error message: