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

Last change on this file since 9862 was 9862, checked in by simon04, 8 years ago

fix #12563 - Allow to customize LatLon formatting

Use the preference keys latlon.dms.decimal-format and
latlon.dm.decimal-format.

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.coor;
3
4import static org.junit.Assert.assertEquals;
5
6import java.util.Locale;
7
8import org.junit.Before;
9import org.junit.Test;
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.projection.Projections;
12
13/**
14 * Unit tests for class {@link LatLon}.
15 */
16public class LatLonTest {
17
18 /**
19 * Setup test.
20 */
21 @Before
22 public void setUp() {
23 Locale.setDefault(Locale.GERMAN);
24 Main.initApplicationPreferences();
25 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
26 }
27
28 private static final double EPSILON = 1e-6;
29
30 /**
31 * Lat/Lon sample values for unit tests
32 */
33 public static final double[] SAMPLE_VALUES = new double[]{
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 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,
36 0.12, 0.123, 0.1234, 0.12345, 0.123456, 0.1234567,
37 1.12, 1.123, 1.1234, 1.12345, 1.123456, 1.1234567,
38 10.12, 10.123, 10.1234, 10.12345, 10.123456, 10.1234567,
39 100.12, 100.123, 100.1234, 100.12345, 100.123456, 100.1234567
40 };
41
42 /**
43 * Test of {@link LatLon#roundToOsmPrecision}
44 */
45 @Test
46 public 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 assertEquals(LatLon.roundToOsmPrecision(0.12345678), 0.1234568, 0);
56 assertEquals(LatLon.roundToOsmPrecision(0.123456789), 0.1234568, 0);
57
58 assertEquals(LatLon.roundToOsmPrecision(1.12345678), 1.1234568, 0);
59 assertEquals(LatLon.roundToOsmPrecision(1.123456789), 1.1234568, 0);
60
61 assertEquals(LatLon.roundToOsmPrecision(10.12345678), 10.1234568, 0);
62 assertEquals(LatLon.roundToOsmPrecision(10.123456789), 10.1234568, 0);
63
64 assertEquals(LatLon.roundToOsmPrecision(100.12345678), 100.1234568, 0);
65 assertEquals(LatLon.roundToOsmPrecision(100.123456789), 100.1234568, 0);
66
67 assertEquals(LatLon.roundToOsmPrecision(100.00000001), 100.0000000, 0);
68 assertEquals(LatLon.roundToOsmPrecision(100.000000001), 100.0000000, 0);
69 assertEquals(LatLon.roundToOsmPrecision(100.0000000001), 100.0000000, 0);
70 assertEquals(LatLon.roundToOsmPrecision(100.00000000001), 100.0000000, 0);
71 assertEquals(LatLon.roundToOsmPrecision(100.000000000001), 100.0000000, 0);
72 assertEquals(LatLon.roundToOsmPrecision(100.0000000000001), 100.0000000, 0);
73 assertEquals(LatLon.roundToOsmPrecision(100.00000000000001), 100.0000000, 0);
74 assertEquals(LatLon.roundToOsmPrecision(100.000000000000001), 100.0000000, 0);
75 assertEquals(LatLon.roundToOsmPrecision(100.0000000000000001), 100.0000000, 0);
76 assertEquals(LatLon.roundToOsmPrecision(100.00000000000000001), 100.0000000, 0);
77 assertEquals(LatLon.roundToOsmPrecision(100.000000000000000001), 100.0000000, 0);
78 assertEquals(LatLon.roundToOsmPrecision(100.0000000000000000001), 100.0000000, 0);
79 assertEquals(LatLon.roundToOsmPrecision(100.00000000000000000001), 100.0000000, 0);
80
81 assertEquals(LatLon.roundToOsmPrecision(99.999999999999999999999), 100.0000000, 0);
82 assertEquals(LatLon.roundToOsmPrecision(99.99999999999999999999), 100.0000000, 0);
83 assertEquals(LatLon.roundToOsmPrecision(99.9999999999999999999), 100.0000000, 0);
84 assertEquals(LatLon.roundToOsmPrecision(99.999999999999999999), 100.0000000, 0);
85 assertEquals(LatLon.roundToOsmPrecision(99.99999999999999999), 100.0000000, 0);
86 assertEquals(LatLon.roundToOsmPrecision(99.9999999999999999), 100.0000000, 0);
87 assertEquals(LatLon.roundToOsmPrecision(99.999999999999999), 100.0000000, 0);
88 assertEquals(LatLon.roundToOsmPrecision(99.99999999999999), 100.0000000, 0);
89 assertEquals(LatLon.roundToOsmPrecision(99.9999999999999), 100.0000000, 0);
90 assertEquals(LatLon.roundToOsmPrecision(99.999999999999), 100.0000000, 0);
91 assertEquals(LatLon.roundToOsmPrecision(99.99999999999), 100.0000000, 0);
92 assertEquals(LatLon.roundToOsmPrecision(99.9999999999), 100.0000000, 0);
93 assertEquals(LatLon.roundToOsmPrecision(99.999999999), 100.0000000, 0);
94 assertEquals(LatLon.roundToOsmPrecision(99.99999999), 100.0000000, 0);
95 assertEquals(LatLon.roundToOsmPrecision(99.9999999), 99.9999999, 0);
96 }
97
98 /**
99 * Test of {@link LatLon#toIntervalLon}
100 */
101 @Test
102 public void testToIntervalLon() {
103 assertEquals(-180.0, LatLon.toIntervalLon(-180.0), 0);
104 assertEquals(0.0, LatLon.toIntervalLon(0.0), 0);
105 assertEquals(180.0, LatLon.toIntervalLon(180.0), 0);
106
107 assertEquals(179.0, LatLon.toIntervalLon(-181.0), 0);
108 assertEquals(-179.0, LatLon.toIntervalLon(181.0), 0);
109
110 assertEquals(-1.0, LatLon.toIntervalLon(359.0), 0);
111 assertEquals(1.0, LatLon.toIntervalLon(-359.0), 0);
112
113 assertEquals(1.0, LatLon.toIntervalLon(361.0), 0);
114 assertEquals(-1.0, LatLon.toIntervalLon(-361.0), 0);
115
116 assertEquals(179.0, LatLon.toIntervalLon(539.0), 0);
117 assertEquals(-179.0, LatLon.toIntervalLon(-539.0), 0);
118
119 assertEquals(-179.0, LatLon.toIntervalLon(541.0), 0);
120 assertEquals(179.0, LatLon.toIntervalLon(-541.0), 0);
121 }
122
123 /**
124 * Test of {@link LatLon#equals}
125 */
126 @Test
127 public void testEquals() {
128 for (int i = 1; i < SAMPLE_VALUES.length; i++) {
129 LatLon a = new LatLon(SAMPLE_VALUES[i-1], SAMPLE_VALUES[i]);
130 LatLon b = new LatLon(SAMPLE_VALUES[i-1], SAMPLE_VALUES[i]);
131 assertEquals(a, b);
132 }
133 }
134
135 /**
136 * Test of {@link LatLon#hashCode}
137 */
138 @Test
139 public void testHashCode() {
140 for (int i = 1; i < SAMPLE_VALUES.length; i++) {
141 LatLon a = new LatLon(SAMPLE_VALUES[i-1], SAMPLE_VALUES[i]);
142 LatLon b = new LatLon(SAMPLE_VALUES[i-1], SAMPLE_VALUES[i]);
143 assertEquals(a.hashCode(), b.hashCode());
144 }
145 }
146
147 /**
148 * Test of {@link LatLon#bearing}
149 */
150 @Test
151 public void testBearing() {
152 LatLon c = new LatLon(47.000000, 19.000000);
153 LatLon e = new LatLon(47.000000, 19.000001);
154 LatLon n = new LatLon(47.000001, 19.000000);
155 assertEquals(0, Math.toDegrees(c.bearing(n)), EPSILON);
156 assertEquals(90, Math.toDegrees(c.bearing(e)), EPSILON);
157 assertEquals(180, Math.toDegrees(n.bearing(c)), EPSILON);
158 assertEquals(270, Math.toDegrees(e.bearing(c)), EPSILON);
159 }
160
161 /**
162 * Tests the methods {@link LatLon#latToString(CoordinateFormat)}, {@link LatLon#lonToString(CoordinateFormat)}.
163 */
164 @Test
165 public void testFormatting() {
166 LatLon c = new LatLon(47.000000, 19.000000);
167 assertEquals("47.0", c.latToString(CoordinateFormat.DECIMAL_DEGREES));
168 assertEquals("19.0", c.lonToString(CoordinateFormat.DECIMAL_DEGREES));
169 assertEquals("47°00'00,0\"N", c.latToString(CoordinateFormat.DEGREES_MINUTES_SECONDS));
170 assertEquals("19°00'00,0\"E", c.lonToString(CoordinateFormat.DEGREES_MINUTES_SECONDS));
171 assertEquals("47°00,000'N", c.latToString(CoordinateFormat.NAUTICAL));
172 assertEquals("19°00,000'E", c.lonToString(CoordinateFormat.NAUTICAL));
173 assertEquals("5942074.0724311", c.latToString(CoordinateFormat.EAST_NORTH));
174 assertEquals("2115070.3250722", c.lonToString(CoordinateFormat.EAST_NORTH));
175 }
176}
Note: See TracBrowser for help on using the repository browser.