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

Last change on this file since 17442 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

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