aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/geo/OneDegreeParserTestCase.java
blob: 4f3bc471e8dee800ed0e6a27a083b892fe9d295d (plain) (blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.geo;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * Tests for the OneDegreeParser class.
 *
 * @author arnej27959
 */
public class OneDegreeParserTestCase {

    private static final double delta = 0.000000000001;

    private OneDegreeParser parser;

    private void checkLat(boolean assumeLatitude, String toParse, double expected) {
        parser = new OneDegreeParser(assumeLatitude, toParse);
        assertEquals(expected, parser.latitude, delta);
        assertTrue(parser.foundLatitude);
        assertFalse(parser.foundLongitude);
    }
    private void checkLon(boolean assumeLatitude, String toParse, double expected) {
        parser = new OneDegreeParser(assumeLatitude, toParse);
        assertEquals(expected, parser.longitude, delta);
        assertFalse(parser.foundLatitude);
        assertTrue(parser.foundLongitude);
    }
    private void checkLat(String toParse, double expected) {
        checkLat(true, toParse, expected);
        checkLat(false, toParse, expected);
    }
    private void checkLon(String toParse, double expected) {
        checkLon(true, toParse, expected);
        checkLon(false, toParse, expected);
    }

    private void checkZeroLat(boolean assumeLatitude, String toParse) {
        checkLat(assumeLatitude, toParse, 0d);
    }

    private void checkZeroLon(boolean assumeLatitude, String toParse) {
        checkLon(assumeLatitude, toParse, 0d);
    }

    /**
     * Tests different inputs that should all produce 0 or -0.
     */
    @Test
    public void testZero() {
        checkZeroLat(true, "0");
        checkZeroLat(true, "0.0");
        checkZeroLat(true, "0o0.0");
        checkZeroLat(true, "0o0'0");
        checkZeroLat(true, "0\u00B00'0");

        checkZeroLon(false, "0");
        checkZeroLon(false, "0.0");
        checkZeroLon(false, "0o0.0");
        checkZeroLon(false, "0o0'0");
        checkZeroLon(false, "0\u00B00'0");

        checkZeroLat(false, "N0");
        checkZeroLat(false, "N0.0");
        checkZeroLat(false, "N0\u00B00'0");
        checkZeroLat(false, "S0");
        checkZeroLat(false, "S0.0");
        checkZeroLat(false, "S0o0'0");
        checkZeroLat(false, "S0\u00B00'0");

        checkZeroLon(true, "E0");
        checkZeroLon(true, "E0.0");
        checkZeroLon(true, "E0\u00B00'0");
        checkZeroLon(true, "W0");
        checkZeroLon(true, "W0.0");
        checkZeroLon(true, "W0o0'0");
        checkZeroLon(true, "W0\u00B00'0");
    }

    /**
     * Tests inputs that are close to 0.
     */
    @Test
    public void testNearZero() {
        checkLat("N0.0001", 0.0001);
        checkLat("S0.0001", -0.0001);
        checkLon("E0.0001", 0.0001);
        checkLon("W0.0001", -0.0001);

        checkLat("N0.000001", 0.000001);
        checkLat("S0.000001", -0.000001);
        checkLon("E0.000001", 0.000001);
        checkLon("W0.000001", -0.000001);

        checkLat("N0\u00B00'1", 1/3600d);
        checkLat("S0\u00B00'1", -1/3600d);
        checkLon("E0\u00B00'1", 1/3600d);
        checkLon("W0\u00B00'1", -1/3600d);
    }

    /**
     * Tests inputs that are close to latitude 90/-90 degrees and longitude 180/-180 degrees.
     */
    @Test
    public void testNearBoundary() {
        checkLat("N89.9999", 89.9999);
        checkLat("S89.9999", -89.9999);
        checkLon("E179.9999", 179.9999);
        checkLon("W179.9999", -179.9999);

        checkLat("N89.999999", 89.999999);
        checkLat("S89.999999", -89.999999);
        checkLon("E179.999999", 179.999999);
        checkLon("W179.999999", -179.999999);

        checkLat("N89\u00B059'59", 89+59/60d+59/3600d);
        checkLat("S89\u00B059'59", -(89+59/60d+59/3600d));
        checkLon("E179\u00B059'59", 179+59/60d+59/3600d);
        checkLon("W179\u00B059'59", -(179+59/60d+59/3600d));
    }

    /**
     * Tests inputs that are on latitude 90/-90 degrees and longitude 180/-180 degrees.
     */
    @Test
    public void testOnBoundary() {
        checkLat("N90", 90d);
        checkLat("N90\u00B00'0", 90d);
        checkLat("S90", -90d);
        checkLat("S90\u00B00'0", -90d);

        checkLon("E180", 180d);
        checkLon("E180\u00B00'0", 180d);
        checkLon("W180", -180d);
        checkLon("W180\u00B00'0", -180d);
    }

    private String parseException(boolean assumeLatitude, String toParse) {
        String message = "";
        try {
            parser = new OneDegreeParser(assumeLatitude, toParse);
            assertTrue(false);
        } catch (IllegalArgumentException e) {
            message = e.getMessage();
        }
        return message;
    }

    /**
     * Tests inputs that are above latitude 90/-90 degrees and longitude 180/-180 degrees.
     */
    @Test
    public void testAboveBoundary() {
        String message = parseException(false, "N90.0001");
        assertEquals("out of range [-90,+90]: 90.0001 when parsing <N90.0001>", message);
        message = parseException(false, "S90.0001");
        assertEquals("out of range [-90,+90]: -90.0001 when parsing <S90.0001>", message);
        message = parseException(true, "E180.0001");
        assertEquals("out of range [-180,+180]: 180.0001 when parsing <E180.0001>", message);
        message = parseException(true, "W180.0001");
        assertEquals("out of range [-180,+180]: -180.0001 when parsing <W180.0001>", message);
        message = parseException(false, "N90.000001");
        assertEquals("out of range [-90,+90]: 90.000001 when parsing <N90.000001>", message);
        message = parseException(false, "S90.000001");
        assertEquals("out of range [-90,+90]: -90.000001 when parsing <S90.000001>", message);
        message = parseException(true, "E180.000001");
        assertEquals("out of range [-180,+180]: 180.000001 when parsing <E180.000001>", message);
        message = parseException(true, "W180.000001");
        assertEquals("out of range [-180,+180]: -180.000001 when parsing <W180.000001>", message);
    }

    /**
     * Tests various inputs that contain syntax errors.
     */
    @Test
    public void testInputErrors() {
        String message = parseException(false, "N90S90");
        assertEquals("already set direction once, cannot add direction: S when parsing <N90S90>", message);
        message = parseException(false, "E120W120");
        assertEquals("already set direction once, cannot add direction: W when parsing <E120W120>", message);
        message = parseException(false, "E");
        assertEquals("end of field without any number seen when parsing <E>", message);
        message = parseException(false, "");
        assertEquals("end of field without any number seen when parsing <>", message);
        message = parseException(false, "NW25");
        assertEquals("already set direction once, cannot add direction: W when parsing <NW25>", message);
        message = parseException(false, "N16.25\u00B0");
        assertEquals("cannot have fractional degrees before degrees sign when parsing <N16.25\u00B0>", message);
        message = parseException(false, "N16\u00B022.40'");
        assertEquals("cannot have fractional minutes before minutes sign when parsing <N16\u00B022.40'>", message);
        message = parseException(false, "");
        assertEquals("end of field without any number seen when parsing <>", message);
        message = parseException(false, "Yahoo!");
        assertEquals("invalid character: Y when parsing <Yahoo!>", message);
        message = parseException(false, "N63O025.105");
        assertEquals("invalid character: O when parsing <N63O025.105>", message);
    }

}