From 488591c419265bc3ffa2dc6917ff23a0705ed7b6 Mon Sep 17 00:00:00 2001 From: Mike McCandless Date: Fri, 4 Mar 2016 16:53:58 -0500 Subject: [PATCH] add randomized test for GeoUtils.circleToBBox --- .../document/LatLonPointDistanceQuery.java | 2 +- .../lucene/search/TestLatLonPointQueries.java | 86 ++++++++++++++++++- .../apache/lucene/spatial/util/GeoRect.java | 5 ++ 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java index 7ac21b35c42..9374b17db4e 100644 --- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java +++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java @@ -70,7 +70,7 @@ final class LatLonPointDistanceQuery extends Query { final GeoRect box2; // crosses dateline: split - if (box.maxLon < box.minLon) { + if (box.crossesDateline()) { box1 = new GeoRect(-180.0, box.maxLon, box.minLat, box.maxLat); box2 = new GeoRect(box.minLon, 180.0, box.minLat, box.maxLat); } else { diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java index 9f9bba2c236..028d6b84476 100644 --- a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java +++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java @@ -16,11 +16,12 @@ */ package org.apache.lucene.search; -import org.apache.lucene.document.LatLonPoint; import org.apache.lucene.document.Document; +import org.apache.lucene.document.LatLonPoint; import org.apache.lucene.spatial.util.BaseGeoPointTestCase; import org.apache.lucene.spatial.util.GeoDistanceUtils; import org.apache.lucene.spatial.util.GeoRect; +import org.apache.lucene.spatial.util.GeoUtils; public class TestLatLonPointQueries extends BaseGeoPointTestCase { // TODO: remove this! @@ -146,4 +147,87 @@ public class TestLatLonPointQueries extends BaseGeoPointTestCase { final double d = GeoDistanceUtils.haversin(centerLat, centerLon, pointLat, pointLon); return d >= minRadiusMeters && d <= radiusMeters; } + + /** Returns random double min to max or up to 1% outside of that range */ + private double randomRangeMaybeSlightlyOutside(double min, double max) { + return min + (random().nextDouble() + (0.5 - random().nextDouble()) * .02) * (max - min); + } + + // We rely heavily on GeoUtils.circleToBBox so we test it here: + public void testRandomCircleToBBox() throws Exception { + int iters = atLeast(1000); + for(int iter=0;iter= bbox.minLat && lat <= bbox.maxLat) { + bboxSays = lon <= bbox.maxLon || lon >= bbox.minLon; + } else { + bboxSays = false; + } + } else { + bboxSays = lat >= bbox.minLat && lat <= bbox.maxLat && lon >= bbox.minLon && lon <= bbox.maxLon; + } + + if (haversinSays) { + if (bboxSays == false) { + System.out.println("small=" + useSmallRanges + " centerLat=" + centerLat + " cetnerLon=" + centerLon + " radiusMeters=" + radiusMeters); + System.out.println(" bbox: lat=" + bbox.minLat + " to " + bbox.maxLat + " lon=" + bbox.minLon + " to " + bbox.maxLon); + System.out.println(" point: lat=" + lat + " lon=" + lon); + System.out.println(" haversin: " + distanceMeters); + fail("point was within the distance according to haversin, but the bbox doesn't contain it"); + } + } else { + // it's fine if haversin said it was outside the radius and bbox said it was inside the box + } + } + } + } } diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java b/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java index 68a450c09fb..e147351b2ca 100644 --- a/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java +++ b/lucene/spatial/src/java/org/apache/lucene/spatial/util/GeoRect.java @@ -70,4 +70,9 @@ public class GeoRect { return b.toString(); } + + /** Returns true if this bounding box crosses the dateline */ + public boolean crossesDateline() { + return maxLon < minLon; + } }