LUCENE-3795 move "base" module's tests back into itself from "strategies".

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene3795_lsp_spatial_module@1292965 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David Wayne Smiley 2012-02-23 21:26:53 +00:00
parent fbc539a298
commit 56fdbaffef
10 changed files with 61 additions and 74 deletions

View File

@ -1,19 +1,4 @@
package org.apache.lucene.spatial.base.context;/* package org.apache.lucene.spatial.base.context;
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.lucene.spatial.base.context.simple.SimpleSpatialContext; import org.apache.lucene.spatial.base.context.simple.SimpleSpatialContext;
import org.apache.lucene.spatial.base.distance.DistanceUnits; import org.apache.lucene.spatial.base.distance.DistanceUnits;

View File

@ -23,6 +23,7 @@ import org.apache.lucene.spatial.base.shape.Point;
import org.apache.lucene.spatial.base.shape.Rectangle; import org.apache.lucene.spatial.base.shape.Rectangle;
import org.apache.lucene.spatial.base.shape.SpatialRelation; import org.apache.lucene.spatial.base.shape.SpatialRelation;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -47,10 +48,10 @@ public class TestDistances extends LuceneTestCase {
public void testSomeDistances() { public void testSomeDistances() {
//See to verify: from http://www.movable-type.co.uk/scripts/latlong.html //See to verify: from http://www.movable-type.co.uk/scripts/latlong.html
Point ctr = pLL(0,100); Point ctr = pLL(0,100);
assertEquals(11100, dc().distance(ctr, pLL(10, 0)),3); Assert.assertEquals(11100, dc().distance(ctr, pLL(10, 0)), 3);
assertEquals(11100, dc().distance(ctr, pLL(10, -160)),3); Assert.assertEquals(11100, dc().distance(ctr, pLL(10, -160)), 3);
assertEquals(314.40338, dc().distance(pLL(1, 2), pLL(3, 4)),EPS); Assert.assertEquals(314.40338, dc().distance(pLL(1, 2), pLL(3, 4)), EPS);
} }
@Test @Test
@ -60,15 +61,15 @@ public class TestDistances extends LuceneTestCase {
double d = 6894.1; double d = 6894.1;
Point pCtr = pLL(-20, 84); Point pCtr = pLL(-20, 84);
Point pTgt = pLL(-42, 15); Point pTgt = pLL(-42, 15);
assertTrue(dc().distance(pCtr, pTgt) < d); Assert.assertTrue(dc().distance(pCtr, pTgt) < d);
//since the pairwise distance is less than d, a bounding box from ctr with d should contain pTgt. //since the pairwise distance is less than d, a bounding box from ctr with d should contain pTgt.
Rectangle r = dc().calcBoxByDistFromPt(pCtr, d, ctx); Rectangle r = dc().calcBoxByDistFromPt(pCtr, d, ctx);
assertEquals(SpatialRelation.CONTAINS,r.relate(pTgt, ctx)); Assert.assertEquals(SpatialRelation.CONTAINS, r.relate(pTgt, ctx));
checkBBox(pCtr,d); checkBBox(pCtr,d);
} }
assertEquals("0 dist, horiz line", Assert.assertEquals("0 dist, horiz line",
-45,dc().calcBoxByDistFromPtHorizAxis(ctx.makePoint(-180,-45),0,ctx),0); -45, dc().calcBoxByDistFromPtHorizAxis(ctx.makePoint(-180, -45), 0, ctx), 0);
double MAXDIST = ctx.getUnits().earthCircumference() / 2; double MAXDIST = ctx.getUnits().earthCircumference() / 2;
checkBBox(ctx.makePoint(0,0), MAXDIST); checkBBox(ctx.makePoint(0,0), MAXDIST);
@ -84,10 +85,10 @@ public class TestDistances extends LuceneTestCase {
} }
for (int T = 0; T < 100; T++) { for (int T = 0; T < 100; T++) {
double lat = -90 + random.nextDouble()*180; double lat = -90 + LuceneTestCase.random.nextDouble()*180;
double lon = -180 + random.nextDouble()*360; double lon = -180 + LuceneTestCase.random.nextDouble()*360;
Point ctr = ctx.makePoint(lon, lat); Point ctr = ctx.makePoint(lon, lat);
double dist = MAXDIST*random.nextDouble(); double dist = MAXDIST* LuceneTestCase.random.nextDouble();
checkBBox(ctr, dist); checkBBox(ctr, dist);
} }
@ -99,31 +100,31 @@ public class TestDistances extends LuceneTestCase {
Rectangle r = dc().calcBoxByDistFromPt(ctr, dist, ctx); Rectangle r = dc().calcBoxByDistFromPt(ctr, dist, ctx);
double horizAxisLat = dc().calcBoxByDistFromPtHorizAxis(ctr,dist, ctx); double horizAxisLat = dc().calcBoxByDistFromPtHorizAxis(ctr,dist, ctx);
if (!Double.isNaN(horizAxisLat)) if (!Double.isNaN(horizAxisLat))
assertTrue(r.relate_yRange(horizAxisLat, horizAxisLat, ctx).intersects()); Assert.assertTrue(r.relate_yRange(horizAxisLat, horizAxisLat, ctx).intersects());
//horizontal //horizontal
if (r.getWidth() >= 180) { if (r.getWidth() >= 180) {
double calcDist = dc().distance(ctr,r.getMinX(), r.getMaxY() == 90 ? 90 : -90 ); double calcDist = dc().distance(ctr,r.getMinX(), r.getMaxY() == 90 ? 90 : -90 );
assertTrue(msg,calcDist <= dist+EPS); Assert.assertTrue(msg, calcDist <= dist + EPS);
//horizAxisLat is meaningless in this context //horizAxisLat is meaningless in this context
} else { } else {
Point tPt = findClosestPointOnVertToPoint(r.getMinX(), r.getMinY(), r.getMaxY(), ctr); Point tPt = findClosestPointOnVertToPoint(r.getMinX(), r.getMinY(), r.getMaxY(), ctr);
double calcDist = dc().distance(ctr,tPt); double calcDist = dc().distance(ctr,tPt);
assertEquals(msg,dist,calcDist,EPS); Assert.assertEquals(msg, dist, calcDist, EPS);
assertEquals(msg,tPt.getY(),horizAxisLat,EPS); Assert.assertEquals(msg, tPt.getY(), horizAxisLat, EPS);
} }
//vertical //vertical
double topDist = dc().distance(ctr,ctr.getX(),r.getMaxY()); double topDist = dc().distance(ctr,ctr.getX(),r.getMaxY());
if (r.getMaxY() == 90) if (r.getMaxY() == 90)
assertTrue(msg,topDist <= dist+EPS); Assert.assertTrue(msg, topDist <= dist + EPS);
else else
assertEquals(msg,dist,topDist,EPS); Assert.assertEquals(msg, dist, topDist, EPS);
double botDist = dc().distance(ctr,ctr.getX(),r.getMinY()); double botDist = dc().distance(ctr,ctr.getX(),r.getMinY());
if (r.getMinY() == -90) if (r.getMinY() == -90)
assertTrue(msg,botDist <= dist+EPS); Assert.assertTrue(msg, botDist <= dist + EPS);
else else
assertEquals(msg,dist,botDist,EPS); Assert.assertEquals(msg, dist, botDist, EPS);
} }
private Point findClosestPointOnVertToPoint(double lon, double lowLat, double highLat, Point ctr) { private Point findClosestPointOnVertToPoint(double lon, double lowLat, double highLat, Point ctr) {
@ -159,7 +160,7 @@ public class TestDistances extends LuceneTestCase {
ctx = new SimpleSpatialContext(DistanceUnits.CARTESIAN); ctx = new SimpleSpatialContext(DistanceUnits.CARTESIAN);
EPS = 10e-6;//tighter epsilon (aka delta) EPS = 10e-6;//tighter epsilon (aka delta)
for(int i = 0; i < 1000; i++) { for(int i = 0; i < 1000; i++) {
testDistCalcPointOnBearing(random.nextInt(100)); testDistCalcPointOnBearing(LuceneTestCase.random.nextInt(100));
} }
} }
@ -180,19 +181,19 @@ public class TestDistances extends LuceneTestCase {
// } // }
double maxDist = ctx.getUnits().earthCircumference() / 2; double maxDist = ctx.getUnits().earthCircumference() / 2;
for(int i = 0; i < 1000; i++) { for(int i = 0; i < 1000; i++) {
int dist = random.nextInt((int) maxDist); int dist = LuceneTestCase.random.nextInt((int) maxDist);
EPS = (dist < maxDist*0.75 ? 10e-6 : 10e-3); EPS = (dist < maxDist*0.75 ? 10e-6 : 10e-3);
testDistCalcPointOnBearing(dist); testDistCalcPointOnBearing(dist);
} }
} }
private void testDistCalcPointOnBearing(double dist) { private void testDistCalcPointOnBearing(double dist) {
for(int angDEG = 0; angDEG < 360; angDEG += random.nextInt(20)+1) { for(int angDEG = 0; angDEG < 360; angDEG += LuceneTestCase.random.nextInt(20)+1) {
Point c = ctx.makePoint(random.nextInt(360),-90+random.nextInt(181)); Point c = ctx.makePoint(LuceneTestCase.random.nextInt(360),-90+ LuceneTestCase.random.nextInt(181));
//0 distance means same point //0 distance means same point
Point p2 = dc().pointOnBearing(c, 0, angDEG, ctx); Point p2 = dc().pointOnBearing(c, 0, angDEG, ctx);
assertEquals(c,p2); Assert.assertEquals(c, p2);
p2 = dc().pointOnBearing(c, dist, angDEG, ctx); p2 = dc().pointOnBearing(c, dist, angDEG, ctx);
double calcDist = dc().distance(c, p2); double calcDist = dc().distance(c, p2);
@ -204,7 +205,7 @@ public class TestDistances extends LuceneTestCase {
double delta = Math.abs(actual - expected); double delta = Math.abs(actual - expected);
double base = Math.min(actual, expected); double base = Math.min(actual, expected);
double deltaRatio = base==0 ? delta : Math.min(delta,delta / base); double deltaRatio = base==0 ? delta : Math.min(delta,delta / base);
assertEquals(0,deltaRatio, EPS); Assert.assertEquals(0, deltaRatio, EPS);
} }
@Test @Test
@ -215,11 +216,11 @@ public class TestDistances extends LuceneTestCase {
{-90-180,90},{-90-360,-90},{90+180,-90},{90+360,90}, {-90-180,90},{-90-360,-90},{90+180,-90},{90+360,90},
{-12+180,12}}; {-12+180,12}};
for (double[] pair : lats) { for (double[] pair : lats) {
assertEquals("input "+pair[0],pair[1],ctx.normY(pair[0]),0); Assert.assertEquals("input " + pair[0], pair[1], ctx.normY(pair[0]), 0);
} }
for(int i = -1000; i < 1000; i += random.nextInt(10)*10) { for(int i = -1000; i < 1000; i += LuceneTestCase.random.nextInt(10)*10) {
double d = ctx.normY(i); double d = ctx.normY(i);
assertTrue(i + " " + d, d >= -90 && d <= 90); Assert.assertTrue(i + " " + d, d >= -90 && d <= 90);
} }
} }
@ -230,11 +231,11 @@ public class TestDistances extends LuceneTestCase {
{-180,-180},{180,-180},{0,0}, {-190,170}, {-180,-180},{180,-180},{0,0}, {-190,170},
{-180-360,-180},{-180-720,-180},{180+360,-180},{180+720,-180}}; {-180-360,-180},{-180-720,-180},{180+360,-180},{180+720,-180}};
for (double[] pair : lons) { for (double[] pair : lons) {
assertEquals("input "+pair[0],pair[1],ctx.normX(pair[0]),0); Assert.assertEquals("input " + pair[0], pair[1], ctx.normX(pair[0]), 0);
} }
for(int i = -1000; i < 1000; i += random.nextInt(10)*10) { for(int i = -1000; i < 1000; i += LuceneTestCase.random.nextInt(10)*10) {
double d = ctx.normX(i); double d = ctx.normX(i);
assertTrue(i + " " + d, d >= -180 && d < 180); Assert.assertTrue(i + " " + d, d >= -180 && d < 180);
} }
} }
@ -247,9 +248,9 @@ public class TestDistances extends LuceneTestCase {
private void assertDistToRadians(double dist) { private void assertDistToRadians(double dist) {
double radius = ctx.getUnits().earthRadius(); double radius = ctx.getUnits().earthRadius();
assertEquals( Assert.assertEquals(
DistanceUtils.pointOnBearingRAD(0, 0, DistanceUtils.dist2Radians(dist, radius), DistanceUtils.DEG_90_AS_RADS, null)[1], DistanceUtils.pointOnBearingRAD(0, 0, DistanceUtils.dist2Radians(dist, radius), DistanceUtils.DEG_90_AS_RADS, null)[1],
DistanceUtils.dist2Radians(dist, radius),10e-5); DistanceUtils.dist2Radians(dist, radius), 10e-5);
} }
private Point pLL(double lat, double lon) { private Point pLL(double lat, double lon) {

View File

@ -20,6 +20,7 @@ package org.apache.lucene.spatial.base.shape;
import org.apache.lucene.spatial.base.context.SpatialContext; import org.apache.lucene.spatial.base.context.SpatialContext;
import org.apache.lucene.spatial.base.distance.DistanceCalculator; import org.apache.lucene.spatial.base.distance.DistanceCalculator;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import java.util.Random; import java.util.Random;
@ -50,11 +51,11 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
return; return;
if (expected == WITHIN || expected == CONTAINS) { if (expected == WITHIN || expected == CONTAINS) {
if (a.getClass().equals(b.getClass())) // they are the same shape type if (a.getClass().equals(b.getClass())) // they are the same shape type
assertEquals(msg,a,b); Assert.assertEquals(msg, a, b);
else { else {
//they are effectively points or lines that are the same location //they are effectively points or lines that are the same location
assertTrue(msg,!a.hasArea()); Assert.assertTrue(msg, !a.hasArea());
assertTrue(msg,!b.hasArea()); Assert.assertTrue(msg, !b.hasArea());
Rectangle aBBox = a.getBoundingBox(); Rectangle aBBox = a.getBoundingBox();
Rectangle bBBox = b.getBoundingBox(); Rectangle bBBox = b.getBoundingBox();
@ -63,10 +64,10 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
|| aBBox.getMinY() == -90 && bBBox.getMinY() == -90)) || aBBox.getMinY() == -90 && bBBox.getMinY() == -90))
;//== a point at the pole ;//== a point at the pole
else else
assertEquals(msg, aBBox, bBBox); Assert.assertEquals(msg, aBBox, bBBox);
} }
} else { } else {
assertEquals(msg,expected,sect); Assert.assertEquals(msg, expected, sect);
} }
} }
@ -74,20 +75,20 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
double delta = Math.abs(actual - expected); double delta = Math.abs(actual - expected);
double base = Math.min(actual, expected); double base = Math.min(actual, expected);
double deltaRatio = base==0 ? delta : Math.min(delta,delta / base); double deltaRatio = base==0 ? delta : Math.min(delta,delta / base);
assertEquals(msg,0,deltaRatio, EPS); Assert.assertEquals(msg, 0, deltaRatio, EPS);
} }
protected void testRectangle(double minX, double width, double minY, double height) { protected void testRectangle(double minX, double width, double minY, double height) {
Rectangle r = ctx.makeRect(minX, minX + width, minY, minY+height); Rectangle r = ctx.makeRect(minX, minX + width, minY, minY+height);
//test equals & hashcode of duplicate //test equals & hashcode of duplicate
Rectangle r2 = ctx.makeRect(minX, minX + width, minY, minY+height); Rectangle r2 = ctx.makeRect(minX, minX + width, minY, minY+height);
assertEquals(r,r2); Assert.assertEquals(r, r2);
assertEquals(r.hashCode(),r2.hashCode()); Assert.assertEquals(r.hashCode(), r2.hashCode());
String msg = r.toString(); String msg = r.toString();
assertEquals(msg, width != 0 && height != 0, r.hasArea()); Assert.assertEquals(msg, width != 0 && height != 0, r.hasArea());
assertEquals(msg, width != 0 && height != 0, r.getArea() > 0); Assert.assertEquals(msg, width != 0 && height != 0, r.getArea() > 0);
assertEqualsRatio(msg, height, r.getHeight()); assertEqualsRatio(msg, height, r.getHeight());
assertEqualsRatio(msg, width, r.getWidth()); assertEqualsRatio(msg, width, r.getWidth());
@ -102,9 +103,9 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
double dUL = dc.distance(center, r.getMinX(), r.getMaxY()); double dUL = dc.distance(center, r.getMinX(), r.getMaxY());
double dLL = dc.distance(center, r.getMinX(), r.getMinY()); double dLL = dc.distance(center, r.getMinX(), r.getMinY());
assertEquals(msg,width != 0 || height != 0, dUR != 0); Assert.assertEquals(msg, width != 0 || height != 0, dUR != 0);
if (dUR != 0) if (dUR != 0)
assertTrue(dUR > 0 && dLL > 0); Assert.assertTrue(dUR > 0 && dLL > 0);
assertEqualsRatio(msg, dUR, dUL); assertEqualsRatio(msg, dUR, dUL);
assertEqualsRatio(msg, dLR, dLL); assertEqualsRatio(msg, dLR, dLL);
if (!ctx.isGeo() || center.getY() == 0) if (!ctx.isGeo() || center.getY() == 0)
@ -154,12 +155,12 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
Circle c = ctx.makeCircle(x, y, dist); Circle c = ctx.makeCircle(x, y, dist);
String msg = c.toString(); String msg = c.toString();
final Circle c2 = ctx.makeCircle(ctx.makePoint(x, y), dist); final Circle c2 = ctx.makeCircle(ctx.makePoint(x, y), dist);
assertEquals(c, c2); Assert.assertEquals(c, c2);
assertEquals(c.hashCode(),c2.hashCode()); Assert.assertEquals(c.hashCode(), c2.hashCode());
assertEquals(msg,dist > 0, c.hasArea()); Assert.assertEquals(msg, dist > 0, c.hasArea());
final Rectangle bbox = c.getBoundingBox(); final Rectangle bbox = c.getBoundingBox();
assertEquals(msg,dist > 0, bbox.getArea() > 0); Assert.assertEquals(msg, dist > 0, bbox.getArea() > 0);
if (!ctx.isGeo()) { if (!ctx.isGeo()) {
//if not geo then units of dist == units of x,y //if not geo then units of dist == units of x,y
assertEqualsRatio(msg, bbox.getHeight(), dist * 2); assertEqualsRatio(msg, bbox.getHeight(), dist * 2);
@ -196,8 +197,8 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
switch (ic) { switch (ic) {
case CONTAINS: case CONTAINS:
i_C++; i_C++;
p = randomPointWithin(random,r,ctx); p = randomPointWithin(LuceneTestCase.random,r,ctx);
assertEquals(CONTAINS,c.relate(p, ctx)); Assert.assertEquals(CONTAINS, c.relate(p, ctx));
break; break;
case INTERSECTS: case INTERSECTS:
i_I++; i_I++;
@ -205,15 +206,15 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
break; break;
case WITHIN: case WITHIN:
i_W++; i_W++;
p = randomPointWithin(random,c,ctx); p = randomPointWithin(LuceneTestCase.random,c,ctx);
assertEquals(CONTAINS,r.relate(p, ctx)); Assert.assertEquals(CONTAINS, r.relate(p, ctx));
break; break;
case DISJOINT: case DISJOINT:
i_O++; i_O++;
p = randomPointWithin(random,r,ctx); p = randomPointWithin(LuceneTestCase.random,r,ctx);
assertEquals(DISJOINT,c.relate(p, ctx)); Assert.assertEquals(DISJOINT, c.relate(p, ctx));
break; break;
default: fail(""+ic); default: Assert.fail("" + ic);
} }
} }
//System.out.println("Laps: "+laps); //System.out.println("Laps: "+laps);
@ -224,7 +225,7 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
/** Returns a random integer between [start, end] with a limited number of possibilities instead of end-start+1. */ /** Returns a random integer between [start, end] with a limited number of possibilities instead of end-start+1. */
private int randRange(int start, int end) { private int randRange(int start, int end) {
//I tested this. //I tested this.
double r = random.nextDouble(); double r = LuceneTestCase.random.nextDouble();
final int BUCKETS = 91; final int BUCKETS = 91;
int ir = (int) Math.round(r*(BUCKETS-1));//put into buckets int ir = (int) Math.round(r*(BUCKETS-1));//put into buckets
int result = (int)((double)((end - start) * ir) / (double)(BUCKETS-1) + (double)start); int result = (int)((double)((end - start) * ir) / (double)(BUCKETS-1) + (double)start);
@ -236,7 +237,7 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
double d = c.getDistance() * random.nextDouble(); double d = c.getDistance() * random.nextDouble();
double angleDEG = 360*random.nextDouble(); double angleDEG = 360*random.nextDouble();
Point p = ctx.getDistCalc().pointOnBearing(c.getCenter(), d, angleDEG, ctx); Point p = ctx.getDistCalc().pointOnBearing(c.getCenter(), d, angleDEG, ctx);
assertEquals(CONTAINS,c.relate(p, ctx)); Assert.assertEquals(CONTAINS, c.relate(p, ctx));
return p; return p;
} }
@ -244,7 +245,7 @@ public abstract class AbstractTestShapes extends LuceneTestCase {
double x = r.getMinX() + random.nextDouble()*r.getWidth(); double x = r.getMinX() + random.nextDouble()*r.getWidth();
double y = r.getMinY() + random.nextDouble()*r.getHeight(); double y = r.getMinY() + random.nextDouble()*r.getHeight();
Point p = ctx.makePoint(x,y); Point p = ctx.makePoint(x,y);
assertEquals(CONTAINS,r.relate(p, ctx)); Assert.assertEquals(CONTAINS, r.relate(p, ctx));
return p; return p;
} }