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

Last change on this file since 18870 was 18870, checked in by taylor.smock, 11 months ago

See #16567: Update to JUnit 5

This converts most tests to use @Annotations. There are also some performance
improvements as it relates to tests.

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