forked from mapcode-foundation/mapcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferenceFileTest.java
More file actions
404 lines (353 loc) · 16.9 KB
/
Copy pathReferenceFileTest.java
File metadata and controls
404 lines (353 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Copyright (C) 2014 Stichting Mapcode Foundation (http://www.mapcode.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mapcode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@SuppressWarnings({"ProhibitedExceptionDeclared", "OverlyBroadThrowsClause"})
public class ReferenceFileTest {
private static final Logger LOG = LoggerFactory.getLogger(ReferenceFileTest.class);
public static final Gson GSON = new GsonBuilder().serializeSpecialFloatingPointValues().create();
private static final String RANDOM_REFERENCE_FILE_1 = "/random_1k.txt";
private static final String RANDOM_REFERENCE_FILE_2 = "/random_10k.txt";
private static final String RANDOM_REFERENCE_FILE_3 = "/random_100k.txt";
private static final String RANDOM_REFERENCE_FILE_1_HP = "/random_hp_1k.txt";
private static final String RANDOM_REFERENCE_FILE_2_HP = "/random_hp_10k.txt";
private static final String RANDOM_REFERENCE_FILE_3_HP = "/random_hp_100k.txt";
private static final String GRID_REFERENCE_FILE_1 = "/grid_1k.txt";
private static final String GRID_REFERENCE_FILE_2 = "/grid_10k.txt";
private static final String GRID_REFERENCE_FILE_3 = "/grid_100k.txt";
private static final String GRID_REFERENCE_FILE_1_HP = "/grid_hp_1k.txt";
private static final String GRID_REFERENCE_FILE_2_HP = "/grid_hp_10k.txt";
private static final String GRID_REFERENCE_FILE_3_HP = "/grid_hp_100k.txt";
private static final String BOUNDARIES_REFERENCE_FILE = "/boundaries.txt";
private static final String BOUNDARIES_REFERENCE_FILE_HP = "/boundaries_hp.txt";
private static final int LOG_LINE_EVERY = 25000;
private static final double METERS_DELTA = 10.0;
@SuppressWarnings("JUnitTestMethodWithNoAssertions")
@Test
public void checkRandomReferenceRecords() throws Exception {
LOG.info("checkRandomReferenceRecords");
checkFile(RANDOM_REFERENCE_FILE_1);
checkFile(RANDOM_REFERENCE_FILE_2);
checkFile(RANDOM_REFERENCE_FILE_3);
}
@SuppressWarnings("JUnitTestMethodWithNoAssertions")
@Test
public void checkGridReferenceRecords() throws Exception {
LOG.info("checkGridReferenceRecords");
checkFile(GRID_REFERENCE_FILE_1);
checkFile(GRID_REFERENCE_FILE_2);
checkFile(GRID_REFERENCE_FILE_3);
}
@SuppressWarnings("JUnitTestMethodWithNoAssertions")
@Test
public void checkBoundariesReferenceRecords() throws Exception {
LOG.info("checkBoundariesReferenceRecords");
checkFile(BOUNDARIES_REFERENCE_FILE);
}
@SuppressWarnings("JUnitTestMethodWithNoAssertions")
@Test
public void checkRandomReferenceRecordsPrecision2() throws Exception {
LOG.info("checkRandomReferenceRecordsPrecision2");
checkFile(RANDOM_REFERENCE_FILE_1_HP);
checkFile(RANDOM_REFERENCE_FILE_2_HP);
checkFile(RANDOM_REFERENCE_FILE_3_HP);
}
@SuppressWarnings("JUnitTestMethodWithNoAssertions")
@Test
public void checkGridReferenceRecordsPrecision2() throws Exception {
LOG.info("checkGridReferenceRecordsPrecision2");
checkFile(GRID_REFERENCE_FILE_1_HP);
checkFile(GRID_REFERENCE_FILE_2_HP);
checkFile(GRID_REFERENCE_FILE_3_HP);
}
@SuppressWarnings("JUnitTestMethodWithNoAssertions")
@Test
public void checkBoundariesReferenceRecordsPrecision2() throws Exception {
LOG.info("checkBoundariesReferenceRecordsPrecision2");
checkFile(BOUNDARIES_REFERENCE_FILE_HP);
}
private void checkFile(@Nonnull final String baseFileName) throws Exception {
int error = 0;
double maxdelta = 0;
// Open data file.
final ChunkedFile chunkedFile = new ChunkedFile(baseFileName);
try {
int i = 1;
//noinspection InfiniteLoopStatement
while (true) {
// Get next record.
@Nonnull final ReferenceRec reference = getNextReferenceRecord(chunkedFile);
final boolean showLogLine = ((i % LOG_LINE_EVERY) == 0);
if (showLogLine) {
LOG.info("checkFile: #{}, file={}", i, chunkedFile.fileName);
LOG.info("checkFile: lat/lon = {}", reference.point);
LOG.info("checkFile: expected = {}", GSON.toJson(reference.mapcodes));
}
/**
* Check encode.
*/
// Encode lat/lon to series of mapcodes and check the resulting mapcodes.
final List<Mapcode> results = MapcodeCodec.encode(
reference.point.getLatDeg(), reference.point.getLonDeg());
if (showLogLine) {
LOG.info("checkFile: actual = {}", GSON.toJson(results));
}
// Check the number of mapcodes.
// TODO: This check can only be enabled when the reference implementation and the
// Java version produce exactly the same number of code. For now, we will only
// log the offending mapcodes as errors in the log file, but not fail the test.
//
// Check the size and order of the results with a single assertion.
//
// assertEquals("Encode #" + i + " incorrect number of results:" +
// "\n lat/lon = " + reference.point +
// "\n expected = " + reference.mapcodes.size() + " results, " +
// GSON.toJson(reference.mapcodes) +
// "\n actual = " + results.size() + " results, " + GSON.toJson(results),
// reference.mapcodes.size(), results.size());
// For every mapcode in the result set, check if it is contained in the reference set.
for (final Mapcode result : results) {
boolean found = false;
for (final MapcodeRec referenceMapcodeRec : reference.mapcodes) {
if (referenceMapcodeRec.territory.equals(result.getTerritory())) {
if (referenceMapcodeRec.mapcode.lastIndexOf('-') > 4) {
if (referenceMapcodeRec.mapcode.equals(result.getMapcodePrecision2())) {
found = true;
break;
}
}
else {
if (referenceMapcodeRec.mapcode.equals(result.getMapcode())) {
found = true;
break;
}
}
}
}
if (!found) {
// This does not fail the test, but rather produces an ERROR in the log file.
// It indicates a discrepancy in the C and Java implementations.
LOG.error("checkFile: Mapcode '{}' at {} is not in the reference file!",
result, reference.point);
++error;
}
}
// For every Mapcode in the reference set, check if it is contained in the result set.
for (final MapcodeRec referenceMapcodeRec : reference.mapcodes) {
boolean found = false;
for (final Mapcode result : results) {
if (referenceMapcodeRec.territory.equals(result.getTerritory())) {
if (referenceMapcodeRec.mapcode.lastIndexOf('-') > 4) {
if (referenceMapcodeRec.mapcode.equals(result.getMapcodePrecision2())) {
found = true;
break;
}
}
else {
if (referenceMapcodeRec.mapcode.equals(result.getMapcode())) {
found = true;
break;
}
}
}
}
if (!found) {
LOG.error("checkFile: Mapcode '{} {}' at {} is not produced by the decoder!",
referenceMapcodeRec.territory, referenceMapcodeRec.mapcode, reference.point);
++error;
}
}
/**
* Check distance of decoded point to reference point.
*/
for (final MapcodeRec mapcodeRec : reference.mapcodes) {
//noinspection NestedTryStatement
try {
final Point result = MapcodeCodec.decode(mapcodeRec.mapcode, mapcodeRec.territory);
final double distanceMeters = Point.distanceInMeters(reference.point, result);
maxdelta = Math.max(maxdelta, distanceMeters);
if (distanceMeters > METERS_DELTA) {
LOG.error(
"Mapcode {} {} was generated for point {}, but decodes to point {} which is {} meters from the original point.",
mapcodeRec.territory, mapcodeRec.mapcode, reference.point, result, distanceMeters);
++error;
}
}
catch (final UnknownMapcodeException unknownMapcodeException) {
LOG.error("Mapcode {} {} was generated for point {}, but cannot be decoded.",
mapcodeRec.territory, mapcodeRec.mapcode, reference.point);
++error;
}
}
if (showLogLine) {
LOG.info("");
}
++i;
}
}
catch (final EOFException e) {
// OK.
}
finally {
chunkedFile.close();
}
LOG.info("checkFile: Maximum delta for this testset = {}", maxdelta);
assertEquals("Found errors", 0, error);
}
private static class MapcodeRec {
@Nonnull private final String mapcode;
@Nonnull private final Territory territory;
public MapcodeRec(@Nonnull final String mapcode, @Nonnull final Territory territory) {
this.mapcode = mapcode;
this.territory = territory;
}
}
private static class ReferenceRec {
@Nonnull private final Point point;
@Nonnull private final ArrayList<MapcodeRec> mapcodes;
public ReferenceRec(@Nonnull final Point point, @Nonnull final ArrayList<MapcodeRec> mapcodes) {
this.point = point;
this.mapcodes = mapcodes;
}
}
@Nonnull
private static ReferenceRec getNextReferenceRecord(@Nonnull final ChunkedFile chunkedFile)
throws IOException, UnknownTerritoryException {
// Read first line of data file: <nr> <lat> <lon> <x> <y> <z>
final String firstLine = chunkedFile.readNonEmptyLine();
final String[] args = firstLine.split(" ");
assertTrue("Expecting 3 or 6 elements, not " + args.length + " in line: " + firstLine,
(args.length == 3) || (args.length == 6));
final int count = Integer.parseInt(args[0]);
assertTrue("Expecting between 1 and 21 mapcodes", (1 <= count) && (count <= 21));
// Read lat/lon.
final double latDeg = Double.parseDouble(args[1]);
final double lonDeg = Double.parseDouble(args[2]);
final Point point = Point.fromMicroDeg(Point.degToMicroDeg(latDeg), Point.degToMicroDeg(lonDeg));
assertTrue("Latitude must be in [-90, 90]", (-90 <= point.getLatDeg()) && (point.getLatDeg() <= 90));
assertTrue("Longitude must be in [-180, 180]", (-180 <= point.getLonDeg()) && (point.getLonDeg() <= 180));
// Read mapcodes: <territory> <mapcode>
final ArrayList<MapcodeRec> mapcodeRecs = new ArrayList<MapcodeRec>();
for (int i = 0; i < count; ++i) {
final String line = chunkedFile.readNonEmptyLine();
assertTrue("Line should not be empty", !line.isEmpty());
final String[] mapcodeLine = line.split(" ");
assertEquals("Expecting 2 elements, territory and mapcode, got: " + mapcodeLine, 2, mapcodeLine.length);
@Nonnull final Territory territory = Territory.fromString(mapcodeLine[0]);
@Nonnull final String mapcode = mapcodeLine[1];
final MapcodeRec mapcodeRec = new MapcodeRec(mapcode, territory);
mapcodeRecs.add(mapcodeRec);
}
assertEquals("Wrong number of mapcodes found", count, mapcodeRecs.size());
final ReferenceRec referenceRec = new ReferenceRec(point, mapcodeRecs);
return referenceRec;
}
/**
* Utility class to read chunked files. Chunked files have extension appended to them
* like '.a', '.b', etc. This class provides reading lines from such files and moving
* to next chunks when needed.
*/
private class ChunkedFile {
final private String baseFileName;
private String fileName;
private char fileExt;
private InputStream inputStream;
private BufferedReader bufferedReader;
private ChunkedFile(final String baseFileName) throws IOException {
super();
this.baseFileName = baseFileName;
this.fileExt = 'a';
this.fileName = baseFileName + '.' + fileExt;
this.inputStream = getClass().getResourceAsStream(fileName);
if (inputStream != null) {
LOG.info("ChunkedFile: Reading {}...", fileName);
this.bufferedReader = new BufferedReader(new InputStreamReader(this.inputStream));
}
else {
throw new IOException();
}
}
@SuppressWarnings("OverlyBroadThrowsClause")
@Nonnull
private String readNonEmptyLine() throws IOException {
//noinspection NonConstantStringShouldBeStringBuffer
String line = null;
do {
boolean tryNextChunk;
try {
line = bufferedReader.readLine();
tryNextChunk = !bufferedReader.ready() || (line == null);
}
catch (final EOFException ignored) {
tryNextChunk = true;
}
if (line == null) {
line = "";
}
if (tryNextChunk) {
// Move to next file.
nextChunk();
}
}
while (line.isEmpty());
return line;
}
private void nextChunk() throws EOFException {
close();
++fileExt;
fileName = baseFileName + '.' + fileExt;
inputStream = getClass().getResourceAsStream(fileName);
if (inputStream != null) {
LOG.info("nextChunk: Reading {}...", fileName);
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
}
else {
LOG.debug("nextChunk: End of chunked file found (chunk {} not found)", fileName);
throw new EOFException();
}
}
private void close() {
if (inputStream != null) {
try {
bufferedReader.close();
}
catch (final IOException ignored) {
LOG.error("close: Cannot close BufferedReader: {}", fileName);
}
try {
inputStream.close();
}
catch (final IOException e) {
LOG.error("close: Cannot close InputStream: {}", fileName);
}
}
}
}
}