source: josm/trunk/test/unit/org/openstreetmap/josm/data/coor/LatLonTest.java@ 12746

Last change on this file since 12746 was 12746, checked in by bastiK, 7 years ago

see #15229 - fix deprecations in tests

  • Property svn:eol-style set to native
File size: 13.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.coor;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertTrue;
7
8import java.text.DecimalFormat;
9
10import org.junit.Rule;
11import org.junit.Test;
12import org.openstreetmap.josm.data.Bounds;
13import org.openstreetmap.josm.testutils.JOSMTestRules;
14
15import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16import nl.jqno.equalsverifier.EqualsVerifier;
17
18/**
19 * Unit tests for class {@link LatLon}.
20 */
21public class LatLonTest {
22
23 /**
24 * Setup test.
25 */
26 @Rule
27 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
28 public JOSMTestRules test = new JOSMTestRules().projection();
29
30 private static final double EPSILON = 1e-6;
31
32 /**
33 * Lat/Lon sample values for unit tests
34 */
35 @SuppressFBWarnings(value = "MS_PKGPROTECT")
36 public static final double[] SAMPLE_VALUES = new double[]{
37 // CHECKSTYLE.OFF: SingleSpaceSeparator
38 -180.0, -179.9, -179.6, -179.5, -179.4, -179.1, -179.0, -100.0, -99.9, -10.0, -9.9, -1.0, -0.1,
39 180.0, 179.9, 179.6, 179.5, 179.4, 179.1, 179.0, 100.0, 99.9, 10.0, 9.9, 1.0, 0.1,
40 0.12, 0.123, 0.1234, 0.12345, 0.123456, 0.1234567,
41 1.12, 1.123, 1.1234, 1.12345, 1.123456, 1.1234567,
42 10.12, 10.123, 10.1234, 10.12345, 10.123456, 10.1234567,
43 100.12, 100.123, 100.1234, 100.12345, 100.123456, 100.1234567
44 // CHECKSTYLE.ON: SingleSpaceSeparator
45 };
46
47 /**
48 * Test of {@link LatLon#roundToOsmPrecision}
49 */
50 @Test
51 public void testRoundToOsmPrecision() {
52
53 for (double value : SAMPLE_VALUES) {
54 assertEquals(LatLon.roundToOsmPrecision(value), value, 0);
55 }
56
57 assertEquals(LatLon.roundToOsmPrecision(0.0), 0.0, 0);
58 assertEquals(LatLon.roundToOsmPrecision(-0.0), 0.0, 0);
59
60 // CHECKSTYLE.OFF: SingleSpaceSeparator
61 assertEquals(LatLon.roundToOsmPrecision(0.12345678), 0.1234568, 0);
62 assertEquals(LatLon.roundToOsmPrecision(0.123456789), 0.1234568, 0);
63
64 assertEquals(LatLon.roundToOsmPrecision(1.12345678), 1.1234568, 0);
65 assertEquals(LatLon.roundToOsmPrecision(1.123456789), 1.1234568, 0);
66
67 assertEquals(LatLon.roundToOsmPrecision(10.12345678), 10.1234568, 0);
68 assertEquals(LatLon.roundToOsmPrecision(10.123456789), 10.1234568, 0);
69
70 assertEquals(LatLon.roundToOsmPrecision(100.12345678), 100.1234568, 0);
71 assertEquals(LatLon.roundToOsmPrecision(100.123456789), 100.1234568, 0);
72 // CHECKSTYLE.ON: SingleSpaceSeparator
73
74 assertEquals(LatLon.roundToOsmPrecision(100.00000001), 100.0000000, 0);
75 assertEquals(LatLon.roundToOsmPrecision(100.000000001), 100.0000000, 0);
76 assertEquals(LatLon.roundToOsmPrecision(100.0000000001), 100.0000000, 0);
77 assertEquals(LatLon.roundToOsmPrecision(100.00000000001), 100.0000000, 0);
78 assertEquals(LatLon.roundToOsmPrecision(100.000000000001), 100.0000000, 0);
79 assertEquals(LatLon.roundToOsmPrecision(100.0000000000001), 100.0000000, 0);
80 assertEquals(LatLon.roundToOsmPrecision(100.00000000000001), 100.0000000, 0);
81 assertEquals(LatLon.roundToOsmPrecision(100.000000000000001), 100.0000000, 0);
82 assertEquals(LatLon.roundToOsmPrecision(100.0000000000000001), 100.0000000, 0);
83 assertEquals(LatLon.roundToOsmPrecision(100.00000000000000001), 100.0000000, 0);
84 assertEquals(LatLon.roundToOsmPrecision(100.000000000000000001), 100.0000000, 0);
85 assertEquals(LatLon.roundToOsmPrecision(100.0000000000000000001), 100.0000000, 0);
86 assertEquals(LatLon.roundToOsmPrecision(100.00000000000000000001), 100.0000000, 0);
87
88 assertEquals(LatLon.roundToOsmPrecision(99.999999999999999999999), 100.0000000, 0);
89 assertEquals(LatLon.roundToOsmPrecision(99.99999999999999999999), 100.0000000, 0);
90 assertEquals(LatLon.roundToOsmPrecision(99.9999999999999999999), 100.0000000, 0);
91 assertEquals(LatLon.roundToOsmPrecision(99.999999999999999999), 100.0000000, 0);
92 assertEquals(LatLon.roundToOsmPrecision(99.99999999999999999), 100.0000000, 0);
93 assertEquals(LatLon.roundToOsmPrecision(99.9999999999999999), 100.0000000, 0);
94 assertEquals(LatLon.roundToOsmPrecision(99.999999999999999), 100.0000000, 0);
95 assertEquals(LatLon.roundToOsmPrecision(99.99999999999999), 100.0000000, 0);
96 assertEquals(LatLon.roundToOsmPrecision(99.9999999999999), 100.0000000, 0);
97 assertEquals(LatLon.roundToOsmPrecision(99.999999999999), 100.0000000, 0);
98 assertEquals(LatLon.roundToOsmPrecision(99.99999999999), 100.0000000, 0);
99 assertEquals(LatLon.roundToOsmPrecision(99.9999999999), 100.0000000, 0);
100 assertEquals(LatLon.roundToOsmPrecision(99.999999999), 100.0000000, 0);
101 assertEquals(LatLon.roundToOsmPrecision(99.99999999), 100.0000000, 0);
102 assertEquals(LatLon.roundToOsmPrecision(99.9999999), 99.9999999, 0);
103 }
104
105 /**
106 * Test {@link LatLon#toIntervalLat}
107 */
108 @Test
109 public void testToIntervalLat() {
110 assertEquals(-90.0, LatLon.toIntervalLat(-90.0), 0);
111 assertEquals(0.0, LatLon.toIntervalLat(0.0), 0);
112 assertEquals(90.0, LatLon.toIntervalLat(90.0), 0);
113
114 assertEquals(-90.0, LatLon.toIntervalLat(-91.0), 0);
115 assertEquals(90.0, LatLon.toIntervalLat(91.0), 0);
116 }
117
118 /**
119 * Test of {@link LatLon#toIntervalLon}
120 */
121 @Test
122 public void testToIntervalLon() {
123 assertEquals(-180.0, LatLon.toIntervalLon(-180.0), 0);
124 assertEquals(0.0, LatLon.toIntervalLon(0.0), 0);
125 assertEquals(180.0, LatLon.toIntervalLon(180.0), 0);
126
127 assertEquals(179.0, LatLon.toIntervalLon(-181.0), 0);
128 assertEquals(-179.0, LatLon.toIntervalLon(181.0), 0);
129
130 assertEquals(-1.0, LatLon.toIntervalLon(359.0), 0);
131 assertEquals(1.0, LatLon.toIntervalLon(-359.0), 0);
132
133 assertEquals(1.0, LatLon.toIntervalLon(361.0), 0);
134 assertEquals(-1.0, LatLon.toIntervalLon(-361.0), 0);
135
136 assertEquals(179.0, LatLon.toIntervalLon(539.0), 0);
137 assertEquals(-179.0, LatLon.toIntervalLon(-539.0), 0);
138
139 assertEquals(-179.0, LatLon.toIntervalLon(541.0), 0);
140 assertEquals(179.0, LatLon.toIntervalLon(-541.0), 0);
141 }
142
143 /**
144 * Unit test of methods {@link LatLon#equals} and {@link LatLon#hashCode}.
145 */
146 @Test
147 public void testEqualsContract() {
148 EqualsVerifier.forClass(LatLon.class).usingGetClass()
149 .withPrefabValues(DecimalFormat.class, new DecimalFormat("00.0"), new DecimalFormat("00.000"))
150 .verify();
151 }
152
153 /**
154 * Unit test of {@link LatLon#LatLon(LatLon)}.
155 */
156 @Test
157 public void testCopyConstructor() {
158 assertEquals(LatLon.NORTH_POLE, new LatLon(LatLon.NORTH_POLE));
159 assertEquals(LatLon.SOUTH_POLE, new LatLon(LatLon.SOUTH_POLE));
160 assertEquals(new LatLon(1, 2), new LatLon(new LatLon(1, 2)));
161 }
162
163 /**
164 * Test of {@link LatLon#bearing}
165 */
166 @Test
167 public void testBearing() {
168 LatLon c = new LatLon(47.000000, 19.000000);
169 LatLon e = new LatLon(47.000000, 19.000001);
170 LatLon n = new LatLon(47.000001, 19.000000);
171 assertEquals(0, Math.toDegrees(c.bearing(n)), EPSILON);
172 assertEquals(90, Math.toDegrees(c.bearing(e)), EPSILON);
173 assertEquals(180, Math.toDegrees(n.bearing(c)), EPSILON);
174 assertEquals(270, Math.toDegrees(e.bearing(c)), EPSILON);
175 }
176
177 /**
178 * Test of {@link LatLon#distance}
179 */
180 @Test
181 public void testDistance() {
182 assertEquals(0.0, LatLon.ZERO.distance(LatLon.ZERO), 0);
183 assertEquals(90.0, LatLon.ZERO.distance(LatLon.NORTH_POLE), 0);
184 assertEquals(180.0, LatLon.SOUTH_POLE.distance(LatLon.NORTH_POLE), 0);
185 }
186
187 /**
188 * Test of {@link LatLon#distanceSq}
189 */
190 @Test
191 public void testDistanceSq() {
192 assertEquals(0.0, LatLon.ZERO.distanceSq(LatLon.ZERO), 0);
193 assertEquals(90d*90d, LatLon.ZERO.distanceSq(LatLon.NORTH_POLE), 0);
194 assertEquals(180d*180d, LatLon.SOUTH_POLE.distanceSq(LatLon.NORTH_POLE), 0);
195 }
196
197 /**
198 * Test {@link LatLon#interpolate(LatLon, double)}
199 */
200 @Test
201 public void testInterpolate() {
202 LatLon ll1 = new LatLon(0, 0);
203 LatLon ll2 = new LatLon(30, 60);
204 LatLon ll3 = new LatLon(-70, -40);
205 // lat:
206 assertEquals(15, ll1.interpolate(ll2, 0.5).lat(), 1e-10);
207 assertEquals(0, ll1.interpolate(ll2, 0).lat(), 1e-10);
208 assertEquals(30, ll1.interpolate(ll2, 1).lat(), 1e-10);
209 assertEquals(0, ll3.interpolate(ll2, .7).lat(), 1e-10);
210 // lon
211 assertEquals(30, ll1.interpolate(ll2, 0.5).lon(), 1e-10);
212 assertEquals(0, ll1.interpolate(ll2, 0).lon(), 1e-10);
213 assertEquals(60, ll1.interpolate(ll2, 1).lon(), 1e-10);
214 assertEquals(0, ll3.interpolate(ll2, .4).lon(), 1e-10);
215 }
216
217 /**
218 * Test {@link LatLon#isOutSideWorld}
219 */
220 @Test
221 public void testIsOutSideWorld() {
222 assertFalse(LatLon.ZERO.isOutSideWorld());
223 assertTrue(LatLon.NORTH_POLE.isOutSideWorld());
224 assertTrue(LatLon.SOUTH_POLE.isOutSideWorld());
225 assertTrue(new LatLon(-181, 0).isOutSideWorld());
226 assertTrue(new LatLon(181, 0).isOutSideWorld());
227 }
228
229 /**
230 * Test {@link LatLon#isValid}
231 */
232 @Test
233 public void testIsValid() {
234 assertTrue(LatLon.ZERO.isValid());
235 assertTrue(LatLon.NORTH_POLE.isValid());
236 assertTrue(LatLon.SOUTH_POLE.isValid());
237
238 assertFalse(new LatLon(-91, 0).isValid());
239 assertFalse(new LatLon(91, 0).isValid());
240 assertFalse(new LatLon(0, -181).isValid());
241 assertFalse(new LatLon(0, 181).isValid());
242 }
243
244 /**
245 * Test {@link LatLon#isWithin}
246 */
247 @Test
248 public void testIsWithin() {
249 assertTrue(LatLon.ZERO.isWithin(new Bounds(LatLon.ZERO)));
250 assertFalse(LatLon.ZERO.isWithin(new Bounds(LatLon.NORTH_POLE)));
251 }
252
253 /**
254 * Test {@link LatLon#getCenter(LatLon)}
255 */
256 @Test
257 public void testGetCenter() {
258 LatLon ll1 = new LatLon(0, 0);
259 LatLon ll2 = new LatLon(30, 60);
260 LatLon ll3 = new LatLon(-70, -40);
261
262 assertEquals(15, ll1.getCenter(ll2).lat(), 1e-10);
263 assertEquals(15, ll2.getCenter(ll1).lat(), 1e-10);
264 assertEquals(-20, ll3.getCenter(ll2).lat(), 1e-10);
265
266 assertEquals(30, ll1.getCenter(ll2).lon(), 1e-10);
267 assertEquals(30, ll2.getCenter(ll1).lon(), 1e-10);
268 assertEquals(10, ll3.getCenter(ll2).lon(), 1e-10);
269 }
270
271 /**
272 * Unit test of {@link LatLon#parse} method.
273 */
274 @Test
275 public void testParse() {
276 assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("49.29918° 19.24788°"));
277 assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("N 49.29918 E 19.24788°"));
278 assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("49.29918° 19.24788°"));
279 assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("N 49.29918 E 19.24788"));
280 assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("n 49.29918 e 19.24788"));
281 assertEquals(new LatLon(-19 - 24.788 / 60, -49 - 29.918 / 60), LatLon.parse("W 49°29.918' S 19°24.788'"));
282 assertEquals(new LatLon(-19 - 24.788 / 60, -49 - 29.918 / 60), LatLon.parse("w 49°29.918' s 19°24.788'"));
283 assertEquals(new LatLon(49 + 29. / 60 + 04. / 3600, 19 + 24. / 60 + 43. / 3600), LatLon.parse("N 49°29'04\" E 19°24'43\""));
284 assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("49.29918 N, 19.24788 E"));
285 assertEquals(new LatLon(49.29918, 19.24788), LatLon.parse("49.29918 n, 19.24788 e"));
286 assertEquals(new LatLon(49 + 29. / 60 + 21. / 3600, 19 + 24. / 60 + 38. / 3600), LatLon.parse("49°29'21\" N 19°24'38\" E"));
287 assertEquals(new LatLon(49 + 29. / 60 + 51. / 3600, 19 + 24. / 60 + 18. / 3600), LatLon.parse("49 29 51, 19 24 18"));
288 assertEquals(new LatLon(49 + 29. / 60, 19 + 24. / 60), LatLon.parse("49 29, 19 24"));
289 assertEquals(new LatLon(19 + 24. / 60, 49 + 29. / 60), LatLon.parse("E 49 29, N 19 24"));
290 assertEquals(new LatLon(49 + 29. / 60, 19 + 24. / 60), LatLon.parse("49° 29; 19° 24"));
291 assertEquals(new LatLon(49 + 29. / 60, 19 + 24. / 60), LatLon.parse("49° 29; 19° 24"));
292 assertEquals(new LatLon(49 + 29. / 60, -19 - 24. / 60), LatLon.parse("N 49° 29, W 19° 24"));
293 assertEquals(new LatLon(-49 - 29.5 / 60, 19 + 24.6 / 60), LatLon.parse("49° 29.5 S, 19° 24.6 E"));
294 assertEquals(new LatLon(49 + 29.918 / 60, 19 + 15.88 / 60), LatLon.parse("N 49 29.918 E 19 15.88"));
295 assertEquals(new LatLon(49 + 29.4 / 60, 19 + 24.5 / 60), LatLon.parse("49 29.4 19 24.5"));
296 assertEquals(new LatLon(-49 - 29.4 / 60, 19 + 24.5 / 60), LatLon.parse("-49 29.4 N -19 24.5 W"));
297 }
298
299 /**
300 * Unit test of {@link LatLon#parse} method - invalid case 1.
301 */
302 @Test(expected = IllegalArgumentException.class)
303 public void testParseInvalid1() {
304 LatLon.parse("48°45'S 23°30'S");
305 }
306
307 /**
308 * Unit test of {@link LatLon#parse} method - invalid case 2.
309 */
310 @Test(expected = IllegalArgumentException.class)
311 public void testParseInvalid2() {
312 LatLon.parse("47°45'N 24°00'S");
313 }
314}
Note: See TracBrowser for help on using the repository browser.