source: josm/trunk/test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java@ 18100

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

Disable test on Github Windows runners + Java 8, minor differences appeared around 2021-07-20

  • Property svn:eol-style set to native
File size: 9.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection;
3
4import java.io.BufferedReader;
5import java.io.BufferedWriter;
6import java.io.File;
7import java.io.IOException;
8import java.io.OutputStreamWriter;
9import java.nio.charset.StandardCharsets;
10import java.nio.file.Files;
11import java.nio.file.Paths;
12import java.security.SecureRandom;
13import java.util.ArrayList;
14import java.util.List;
15import java.util.Map;
16import java.util.Random;
17import java.util.Set;
18import java.util.TreeSet;
19import java.util.stream.Collectors;
20
21import org.junit.jupiter.api.Assumptions;
22import org.junit.jupiter.api.BeforeAll;
23import org.junit.jupiter.api.Test;
24import org.openstreetmap.josm.JOSMFixture;
25import org.openstreetmap.josm.data.Bounds;
26import org.openstreetmap.josm.data.coor.EastNorth;
27import org.openstreetmap.josm.data.coor.LatLon;
28import org.openstreetmap.josm.tools.Pair;
29import org.openstreetmap.josm.tools.Platform;
30import org.openstreetmap.josm.tools.Utils;
31
32/**
33 * This test is used to monitor changes in projection code.
34 *
35 * It keeps a record of test data in the file nodist/data/projection/projection-regression-test-data.
36 * This record is generated from the current Projection classes available in JOSM. It needs to
37 * be updated, whenever a projection is added / removed or an algorithm is changed, such that
38 * the computed values are numerically different. There is no error threshold, every change is reported.
39 *
40 * So when this test fails, first check if the change is intended. Then update the regression
41 * test data, by running the main method of this class and commit the new data file.
42 */
43class ProjectionRegressionTest {
44
45 private static final String PROJECTION_DATA_FILE = "nodist/data/projection/projection-regression-test-data";
46
47 private static class TestData {
48 public String code;
49 public LatLon ll;
50 public EastNorth en;
51 public LatLon ll2;
52 }
53
54 /**
55 * Program entry point to update reference projection file.
56 * @param args not used
57 * @throws IOException if any I/O errors occurs
58 */
59 public static void main(String[] args) throws IOException {
60 setUp();
61
62 Map<String, Projection> supportedCodesMap = Projections.getAllProjectionCodes().stream()
63 .collect(Collectors.toMap(code -> code, Projections::getProjectionByCode));
64
65 List<TestData> prevData = new ArrayList<>();
66 if (new File(PROJECTION_DATA_FILE).exists()) {
67 prevData = readData();
68 }
69 Map<String, TestData> prevCodesMap = prevData.stream()
70 .collect(Collectors.toMap(data -> data.code, data -> data));
71
72 Set<String> codesToWrite = new TreeSet<>(supportedCodesMap.keySet());
73 prevData.stream()
74 .filter(data -> supportedCodesMap.containsKey(data.code)).map(data -> data.code)
75 .forEach(codesToWrite::add);
76
77 Random rand = new SecureRandom();
78 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
79 Files.newOutputStream(Paths.get(PROJECTION_DATA_FILE)), StandardCharsets.UTF_8))) {
80 out.write("# Data for test/unit/org/openstreetmap/josm/data/projection/ProjectionRegressionTest.java\n");
81 out.write("# Format: 1. Projection code; 2. lat/lon; 3. lat/lon projected -> east/north; 4. east/north (3.) inverse projected\n");
82 for (String code : codesToWrite) {
83 Projection proj = supportedCodesMap.get(code);
84 Bounds b = proj.getWorldBoundsLatLon();
85 double lat, lon;
86 TestData prev = prevCodesMap.get(proj.toCode());
87 if (prev != null) {
88 lat = prev.ll.lat();
89 lon = prev.ll.lon();
90 } else {
91 lat = b.getMin().lat() + rand.nextDouble() * (b.getMax().lat() - b.getMin().lat());
92 lon = b.getMin().lon() + rand.nextDouble() * (b.getMax().lon() - b.getMin().lon());
93 }
94 EastNorth en = proj.latlon2eastNorth(new LatLon(lat, lon));
95 LatLon ll2 = proj.eastNorth2latlon(en);
96 out.write(String.format(
97 "%s%n ll %s %s%n en %s %s%n ll2 %s %s%n", proj.toCode(), lat, lon, en.east(), en.north(), ll2.lat(), ll2.lon()));
98 }
99 }
100 System.out.println("Update successful.");
101 }
102
103 private static List<TestData> readData() throws IOException {
104 try (BufferedReader in = Files.newBufferedReader(Paths.get(PROJECTION_DATA_FILE), StandardCharsets.UTF_8)) {
105 List<TestData> result = new ArrayList<>();
106 String line;
107 while ((line = in.readLine()) != null) {
108 if (line.startsWith("#")) {
109 continue;
110 }
111 TestData next = new TestData();
112
113 Pair<Double, Double> ll = readLine("ll", in.readLine());
114 Pair<Double, Double> en = readLine("en", in.readLine());
115 Pair<Double, Double> ll2 = readLine("ll2", in.readLine());
116
117 next.code = line;
118 next.ll = new LatLon(ll.a, ll.b);
119 next.en = new EastNorth(en.a, en.b);
120 next.ll2 = new LatLon(ll2.a, ll2.b);
121
122 result.add(next);
123 }
124 return result;
125 }
126 }
127
128 private static Pair<Double, Double> readLine(String expectedName, String input) {
129 String[] fields = input.trim().split("[ ]+", -1);
130 if (fields.length != 3) throw new AssertionError();
131 if (!fields[0].equals(expectedName)) throw new AssertionError();
132 double a = Double.parseDouble(fields[1]);
133 double b = Double.parseDouble(fields[2]);
134 return Pair.create(a, b);
135 }
136
137 /**
138 * Setup test.
139 */
140 @BeforeAll
141 public static void setUp() {
142 JOSMFixture.createUnitTestFixture().init();
143 }
144
145 /**
146 * Non-regression unit test.
147 * @throws IOException if any I/O error occurs
148 */
149 @Test
150 void testNonRegression() throws IOException {
151 // Disable on Github Windows runners + Java 8, minor differences appeared around 2021-07-20
152 Assumptions.assumeFalse(
153 Utils.getJavaVersion() == 8
154 && Platform.determinePlatform() == Platform.WINDOWS
155 && System.getenv("GITHUB_WORKFLOW") != null);
156 List<TestData> allData = readData();
157 Set<String> dataCodes = allData.stream().map(data -> data.code).collect(Collectors.toSet());
158
159 StringBuilder fail = new StringBuilder();
160
161 for (String code : Projections.getAllProjectionCodes()) {
162 if (!dataCodes.contains(code)) {
163 fail.append("Did not find projection "+code+" in test data!\n");
164 }
165 }
166
167 final boolean java9 = Utils.getJavaVersion() >= 9;
168 for (TestData data : allData) {
169 Projection proj = Projections.getProjectionByCode(data.code);
170 if (proj == null) {
171 fail.append("Projection "+data.code+" from test data was not found!\n");
172 continue;
173 }
174 EastNorth en = proj.latlon2eastNorth(data.ll);
175 LatLon ll2 = proj.eastNorth2latlon(data.en);
176 if (!(java9 ? equalsJava9(en, data.en) : en.equals(data.en))) {
177 String error = String.format("%s (%s): Projecting latlon(%s,%s):%n" +
178 " expected: eastnorth(%s,%s),%n" +
179 " but got: eastnorth(%s,%s)!%n",
180 proj.toString(), data.code, data.ll.lat(), data.ll.lon(), data.en.east(), data.en.north(), en.east(), en.north());
181 fail.append(error);
182 }
183 if (!(java9 ? equalsJava9(ll2, data.ll2) : ll2.equals(data.ll2))) {
184 String error = String.format("%s (%s): Inverse projecting eastnorth(%s,%s):%n" +
185 " expected: latlon(%s,%s),%n" +
186 " but got: latlon(%s,%s)!%n",
187 proj.toString(), data.code, data.en.east(), data.en.north(), data.ll2.lat(), data.ll2.lon(), ll2.lat(), ll2.lon());
188 fail.append(error);
189 }
190 }
191
192 if (fail.length() > 0) {
193 System.err.println(fail.toString());
194 throw new AssertionError(fail.toString());
195 }
196 }
197
198 private static boolean equalsDoubleMaxUlp(double d1, double d2) {
199 // Due to error accumulation in projection computation, the difference can reach hundreds of ULPs
200 // The worst error is 1168 ULP (followed by 816 ULP then 512 ULP) with:
201 // NAD83 / Colorado South (EPSG:26955): Projecting latlon(32.24604527892822,-125.93039495227096):
202 // expected: eastnorth(-1004398.8994415681,24167.8944844745),
203 // but got: eastnorth(-1004398.8994415683,24167.894484478747)!
204 return Math.abs(d1 - d2) <= 1200 * Math.ulp(d1);
205 }
206
207 private static boolean equalsJava9(EastNorth en1, EastNorth en2) {
208 return equalsDoubleMaxUlp(en1.east(), en2.east()) &&
209 equalsDoubleMaxUlp(en1.north(), en2.north());
210 }
211
212 private static boolean equalsJava9(LatLon ll1, LatLon ll2) {
213 return equalsDoubleMaxUlp(ll1.lat(), ll2.lat()) &&
214 equalsDoubleMaxUlp(ll1.lon(), ll2.lon());
215 }
216}
Note: See TracBrowser for help on using the repository browser.