TEST: Rewrite GeoPointParsingTests#testEqualsHashCodeContract (#27634)

The hashCode contract states that equal objects must have equal hash
codes, however the unequal objects are not required to have unequal
hashCodes.

This commit rewrites GeoPointParsingTests#testEqualsHashCodeContract
using#checkEqualsAndHashCode helper.

Closes #27633
This commit is contained in:
Nhat Nguyen 2017-12-04 16:34:34 -05:00 committed by GitHub
parent 1ff5ef9055
commit 4b558636f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,8 +29,10 @@ import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.geo.RandomGeoGenerator; import org.elasticsearch.test.geo.RandomGeoGenerator;
import java.io.IOException; import java.io.IOException;
import java.util.function.DoubleSupplier;
import static org.elasticsearch.common.geo.GeoHashUtils.stringEncode; import static org.elasticsearch.common.geo.GeoHashUtils.stringEncode;
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
public class GeoPointParsingTests extends ESTestCase { public class GeoPointParsingTests extends ESTestCase {
@ -57,32 +59,14 @@ public class GeoPointParsingTests extends ESTestCase {
} }
public void testEqualsHashCodeContract() { public void testEqualsHashCodeContract() {
// generate a random geopoint // GeoPoint doesn't care about coordinate system bounds, this simply validates equality and hashCode.
final GeoPoint x = RandomGeoGenerator.randomPoint(random()); final DoubleSupplier randomDelta = () -> randomValueOtherThan(0.0, () -> randomDoubleBetween(-1000000, 1000000, true));
final GeoPoint y = new GeoPoint(x.lat(), x.lon()); checkEqualsAndHashCode(RandomGeoGenerator.randomPoint(random()), GeoPoint::new,
final GeoPoint z = new GeoPoint(y.lat(), y.lon()); pt -> new GeoPoint(pt.lat() + randomDelta.getAsDouble(), pt.lon()));
// GeoPoint doesn't care about coordinate system bounds, this simply validates inequality checkEqualsAndHashCode(RandomGeoGenerator.randomPoint(random()), GeoPoint::new,
final GeoPoint a = new GeoPoint(x.lat() + randomIntBetween(1, 5), x.lon() + randomIntBetween(1, 5)); pt -> new GeoPoint(pt.lat(), pt.lon() + randomDelta.getAsDouble()));
checkEqualsAndHashCode(RandomGeoGenerator.randomPoint(random()), GeoPoint::new,
/** equality test */ pt -> new GeoPoint(pt.lat() + randomDelta.getAsDouble(), pt.lon() + randomDelta.getAsDouble()));
// reflexive
assertTrue(x.equals(x));
// symmetry
assertTrue(x.equals(y));
// transitivity
assertTrue(y.equals(z));
assertTrue(x.equals(z));
// inequality
assertFalse(x.equals(a));
/** hashCode test */
// symmetry
assertTrue(x.hashCode() == y.hashCode());
// transitivity
assertTrue(y.hashCode() == z.hashCode());
assertTrue(x.hashCode() == z.hashCode());
// inequality
assertFalse(x.hashCode() == a.hashCode());
} }
public void testGeoPointParsing() throws IOException { public void testGeoPointParsing() throws IOException {