diff options
Diffstat (limited to 'tests/src/cgeo/geocaching/geopoint')
3 files changed, 167 insertions, 0 deletions
diff --git a/tests/src/cgeo/geocaching/geopoint/DistanceParserTest.java b/tests/src/cgeo/geocaching/geopoint/DistanceParserTest.java new file mode 100644 index 0000000..3e8336b --- /dev/null +++ b/tests/src/cgeo/geocaching/geopoint/DistanceParserTest.java @@ -0,0 +1,35 @@ +package cgeo.geocaching.geopoint; + +import cgeo.geocaching.geopoint.DistanceParser; + +import android.test.AndroidTestCase; + +import junit.framework.Assert; + +@SuppressWarnings("static-method") +public class DistanceParserTest extends AndroidTestCase { + + static private final double MM = 1e-6; // 1mm, in kilometers + + public void testFormats() { + Assert.assertEquals(1.2, DistanceParser.parseDistance("1200 m", true), MM); + Assert.assertEquals(1.2, DistanceParser.parseDistance("1.2 km", true), MM); + Assert.assertEquals(0.36576, DistanceParser.parseDistance("1200 ft", true), MM); + Assert.assertEquals(1.09728, DistanceParser.parseDistance("1200 yd", true), MM); + Assert.assertEquals(1.9312128, DistanceParser.parseDistance("1.2 mi", true), MM); + } + + public void testImplicit() { + Assert.assertEquals(1.2, DistanceParser.parseDistance("1200", true), MM); + Assert.assertEquals(0.36576, DistanceParser.parseDistance("1200", false), MM); + } + + public void testComma() { + Assert.assertEquals(1.2, DistanceParser.parseDistance("1,2km", true), MM); + } + + public void testCase() { + Assert.assertEquals(0.36576, DistanceParser.parseDistance("1200 FT", true), MM); + } + +}
\ No newline at end of file diff --git a/tests/src/cgeo/geocaching/geopoint/GeoPointParserTest.java b/tests/src/cgeo/geocaching/geopoint/GeoPointParserTest.java new file mode 100644 index 0000000..32b7562 --- /dev/null +++ b/tests/src/cgeo/geocaching/geopoint/GeoPointParserTest.java @@ -0,0 +1,48 @@ +package cgeo.geocaching.geopoint; + +import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.geopoint.GeopointParser; + +import android.test.AndroidTestCase; + +import junit.framework.Assert; + +@SuppressWarnings("static-method") +public class GeoPointParserTest extends AndroidTestCase { + + private static final double refLongitude = 8.0 + 38.564 / 60.0; + private static final double refLatitude = 49.0 + 56.031 / 60.0; + + public void testParseLatitude() { + + Assert.assertEquals(refLatitude, GeopointParser.parseLatitude("N 49° 56.031"), 1e-8); + } + + public void testParseLongitude() { + + Assert.assertEquals(refLongitude, GeopointParser.parseLongitude("E 8° 38.564"), 1e-8); + } + + public void testFullCoordinates() { + final Geopoint goal = new Geopoint(refLatitude, refLongitude); + Assert.assertTrue(goal.isEqualTo(GeopointParser.parse("N 49° 56.031 | E 8° 38.564"), 1e-6)); + } + + public void testSouth() { + Assert.assertEquals(-refLatitude, GeopointParser.parseLatitude("S 49° 56.031"), 1e-8); + } + + public void testWest() { + Assert.assertEquals(-refLongitude, GeopointParser.parseLongitude("W 8° 38.564"), 1e-8); + } + + public void testLowerCase() { + Assert.assertEquals(refLongitude, GeopointParser.parseLongitude("e 8° 38.564"), 1e-8); + } + + public void testVariousFormats() { + final Geopoint goal1 = GeopointParser.parse("N 49° 43' 57\" | E 2 12' 35"); + final Geopoint goal2 = GeopointParser.parse("N 49 43.95 E2°12.5833333333"); + Assert.assertTrue(goal1.isEqualTo(goal2, 1e-6)); + } +} diff --git a/tests/src/cgeo/geocaching/geopoint/GeopointTest.java b/tests/src/cgeo/geocaching/geopoint/GeopointTest.java new file mode 100644 index 0000000..ef1955b --- /dev/null +++ b/tests/src/cgeo/geocaching/geopoint/GeopointTest.java @@ -0,0 +1,84 @@ +package cgeo.geocaching.geopoint; + +import cgeo.geocaching.geopoint.Geopoint; +import cgeo.geocaching.geopoint.Geopoint.GeopointException; + +import android.test.AndroidTestCase; + +import junit.framework.Assert; + +@SuppressWarnings("static-method") +public class GeopointTest extends AndroidTestCase { + + public void testCreation() { + final Geopoint gp = new Geopoint(48.2, 3.5); + Assert.assertEquals(48.2, gp.getLatitude(), 1e-8); + Assert.assertEquals(3.5, gp.getLongitude(), 1e-8); + } + + public void testCreationAtLimit() { + // No exception should be raised at limits. + final Geopoint gp1 = new Geopoint(90.0, 10.0); + Assert.assertEquals(90, gp1.getLatitude(), 1e-8); + + final Geopoint gp2 = new Geopoint(-90.0, 10.0); + Assert.assertEquals(-90, gp2.getLatitude(), 1e-8); + + final Geopoint gp3 = new Geopoint(10.0, 180.0); + Assert.assertEquals(180, gp3.getLongitude(), 1e-8); + + // 180 should be preferred to -180 + final Geopoint gp4 = new Geopoint(10.0, -180.0); + Assert.assertEquals(180, gp4.getLongitude(), 1e-8); + } + + private static void createShouldFail(final double lat, final double lon) { + try { + final Geopoint gp = new Geopoint(lat, lon); + Assert.fail("creation should fail: " + gp); + } catch (GeopointException e) { + // Success + } + } + + public void testCreationFails() { + createShouldFail(90.1, 0.0); + createShouldFail(-90.1, 0.0); + createShouldFail(0.0, 180.1); + createShouldFail(0.0, -180.1); + } + + public void testEqual() { + final Geopoint gp1 = new Geopoint(48.2, 2.31); + Assert.assertTrue(gp1.equals(gp1)); + final Geopoint gp2 = new Geopoint(48.3, 2.31); + Assert.assertFalse(gp1.equals(gp2)); + } + + public void testCreateE6() { + final Geopoint gp1 = new Geopoint(48.2, 2.34); + final Geopoint gp2 = new Geopoint(48200000, 2340000); + Assert.assertTrue(gp1.isEqualTo(gp2, 1e-6)); + } + + public void testGetE6() { + final Geopoint gp = new Geopoint(41.2, -3.4); + Assert.assertEquals(41200000.0, gp.getLatitudeE6(), 1e-6); + Assert.assertEquals(-3400000.0, gp.getLongitudeE6(), 1e-6); + } + + public void testBearingDistance() { + final Geopoint gp1 = new Geopoint(-30.4, -1.2); + final Geopoint gp2 = new Geopoint(-30.1, -2.3); + + final float d12 = gp1.distanceTo(gp2); + Assert.assertEquals(110.967995, d12, 1e-6); + Assert.assertEquals(d12, gp2.distanceTo(gp1), 1e-6); + + // Bearing in both directions cannot be added, as this is + // the initial bearing of the path in both cases. + Assert.assertEquals(287.162, gp1.bearingTo(gp2), 1e-3); + Assert.assertEquals(107.715, gp2.bearingTo(gp1), 1e-3); + } + +} |
