aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/geopoint/ViewportTest.java
blob: 11f78ad7903f7c090d6135518347e5d6a95cacb1 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package cgeo.geocaching.geopoint;

import cgeo.geocaching.ICoordinates;

import org.eclipse.jdt.annotation.NonNull;

import android.test.AndroidTestCase;

import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

public class ViewportTest extends AndroidTestCase {

    final private static @NonNull
    Viewport vpRef = new Viewport(new Geopoint(-1.0, -2.0), new Geopoint(3.0, 4.0));

    public static void assertBounds(final Viewport vp) {
        assertEquals(new Geopoint(1.0, 1.0), vp.center);
        assertEquals(new Geopoint(3.0, 4.0), vp.topRight);
        assertEquals(new Geopoint(-1.0, -2.0), vp.bottomLeft);
    }

    public static void testCreationBounds() {
        assertBounds(new Viewport(new Geopoint(-1.0, -2.0), new Geopoint(3.0, 4.0)));
        assertBounds(new Viewport(new Geopoint(3.0, 4.0), new Geopoint(-1.0, -2.0)));
        assertBounds(new Viewport(new Geopoint(-1.0, 4.0), new Geopoint(3.0, -2.0)));
        assertBounds(new Viewport(new Geopoint(3.0, -2.0), new Geopoint(-1.0, 4.0)));
    }

    public static void testCreationCenter() {
        assertBounds(new Viewport(new Geopoint(1.0, 1.0), 4.0, 6.0));
    }

    public static void testCreationSeparate() {
        assertBounds(vpRef);
    }

    public static void testMinMax() {
        assertEquals(-1.0, vpRef.getLatitudeMin());
        assertEquals(3.0, vpRef.getLatitudeMax());
        assertEquals(-2.0, vpRef.getLongitudeMin());
        assertEquals(4.0, vpRef.getLongitudeMax());
    }

    public static void testSpans() {
        assertEquals(4.0, vpRef.getLatitudeSpan());
        assertEquals(6.0, vpRef.getLongitudeSpan());
    }

    public static void testInViewport() {
        assertFalse(vpRef.contains(new Geopoint(-2.0, -2.0)));
        assertFalse(vpRef.contains(new Geopoint(4.0, 4.0)));
        assertTrue(vpRef.contains(Geopoint.ZERO));
        assertTrue(vpRef.contains(new Geopoint(-1.0, -2.0)));
        assertTrue(vpRef.contains(new Geopoint(3.0, 4.0)));
    }

    public static void testSqlWhere() {
        assertEquals("latitude >= -1.0 and latitude <= 3.0 and longitude >= -2.0 and longitude <= 4.0", vpRef.sqlWhere(null).toString());
        assertEquals("t.latitude >= -1.0 and t.latitude <= 3.0 and t.longitude >= -2.0 and t.longitude <= 4.0", vpRef.sqlWhere("t").toString());
        Locale current = null;
        try {
            current = Locale.getDefault();
            Locale.setDefault(Locale.FRENCH);
            assertEquals("1,0", String.format("%.2g", 1.0d));  // Control that we are in a locale with commma separator
            assertEquals("t.latitude >= -1.0 and t.latitude <= 3.0 and t.longitude >= -2.0 and t.longitude <= 4.0", vpRef.sqlWhere("t").toString());
        } finally {
            Locale.setDefault(current);
        }
    }

    public static void testEquals() {
        assertEquals(vpRef, vpRef);
        assertEquals(vpRef, new Viewport(vpRef.bottomLeft, vpRef.topRight));
        assertFalse(vpRef.equals(new Viewport(new Geopoint(0.0, 0.0), 1.0, 1.0)));
    }

    public static void testResize() {
        assertEquals(vpRef, vpRef.resize(1.0));
        assertEquals(new Viewport(new Geopoint(-3.0, -5.0), new Geopoint(5.0, 7.0)), vpRef.resize(2.0));
        assertEquals(new Viewport(new Geopoint(0.0, -0.5), new Geopoint(2.0, 2.5)), vpRef.resize(0.5));
    }

    public static void testIncludes() {
        assertTrue(vpRef.includes(vpRef));
        assertTrue(vpRef.includes(vpRef.resize(0.5)));
        assertFalse(vpRef.includes(vpRef.resize(2.0)));
    }

    public static void testContaining() {
        assertNull(Viewport.containing(Collections.singleton((ICoordinates) null)));
        final Set<Geopoint> points = new HashSet<Geopoint>();
        points.add(vpRef.bottomLeft);
        assertEquals(new Viewport(vpRef.bottomLeft, vpRef.bottomLeft), Viewport.containing(points));
        points.add(vpRef.topRight);
        assertEquals(vpRef, Viewport.containing(points));
        points.add(vpRef.center);
        assertEquals(vpRef, Viewport.containing(points));
    }

}