aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/geopoint/GeopointTest.java
blob: e67bc9949294203d6015c4c110951f6c01215119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cgeo.geocaching.geopoint;

import android.test.AndroidTestCase;

import junit.framework.Assert;

public class GeopointTest extends AndroidTestCase {

    public static 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 static void testCreationAtLimit() {
        // No exception should be raised.
        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);
    }

    public static 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 static 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 static 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 static 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);
    }

}