2323import org .slf4j .LoggerFactory ;
2424
2525import javax .annotation .Nonnull ;
26- import java .io .BufferedReader ;
27- import java .io .EOFException ;
28- import java .io .IOException ;
29- import java .io .InputStream ;
30- import java .io .InputStreamReader ;
26+ import java .io .*;
3127import java .util .ArrayList ;
3228import java .util .List ;
3329import java .util .concurrent .ExecutorService ;
4137
4238@ SuppressWarnings ({"ProhibitedExceptionDeclared" , "OverlyBroadThrowsClause" })
4339public class ReferenceFileTest {
44- private static final Logger LOG = LoggerFactory .getLogger (ReferenceFileTest .class );
45- private static final Gson GSON = new GsonBuilder ().serializeSpecialFloatingPointValues ().create ();
46-
47- private static final String RANDOM_REFERENCE_FILE_1 = "/random_e8_1k.txt" ;
48- private static final String RANDOM_REFERENCE_FILE_2 = "/random_e8_10k.txt" ;
49- private static final String RANDOM_REFERENCE_FILE_3 = "/random_e8_100k.txt" ;
50- private static final String GRID_REFERENCE_FILE_1 = "/grid_e8_1k.txt" ;
51- private static final String GRID_REFERENCE_FILE_2 = "/grid_e8_10k.txt" ;
52- private static final String GRID_REFERENCE_FILE_3 = "/grid_e8_100k.txt" ;
40+ private static final Logger LOG = LoggerFactory .getLogger (ReferenceFileTest .class );
41+ private static final Gson GSON = new GsonBuilder ().serializeSpecialFloatingPointValues ().create ();
42+
43+ private static final String RANDOM_REFERENCE_FILE_1 = "/random_e8_1k.txt" ;
44+ private static final String RANDOM_REFERENCE_FILE_2 = "/random_e8_10k.txt" ;
45+ private static final String RANDOM_REFERENCE_FILE_3 = "/random_e8_100k.txt" ;
46+ private static final String GRID_REFERENCE_FILE_1 = "/grid_e8_1k.txt" ;
47+ private static final String GRID_REFERENCE_FILE_2 = "/grid_e8_10k.txt" ;
48+ private static final String GRID_REFERENCE_FILE_3 = "/grid_e8_100k.txt" ;
5349 private static final String BOUNDARIES_REFERENCE_FILE = "/boundaries_e8.txt" ;
5450
5551 private static final int PRECISION_MAX = 8 ;
@@ -60,49 +56,33 @@ public class ReferenceFileTest {
6056 @ Test
6157 public void checkRandomReferenceRecords () throws Exception {
6258 LOG .info ("checkRandomReferenceRecords" );
63- checkFile (0 , RANDOM_REFERENCE_FILE_1 );
59+ for (int i = 0 ; i < 8 ; ++i ) {
60+ checkFile (i , RANDOM_REFERENCE_FILE_1 );
61+ }
6462 checkFile (0 , RANDOM_REFERENCE_FILE_2 );
6563 checkFile (0 , RANDOM_REFERENCE_FILE_3 );
64+ checkFile (8 , RANDOM_REFERENCE_FILE_2 );
65+ checkFile (8 , RANDOM_REFERENCE_FILE_3 );
6666 }
6767
6868 @ SuppressWarnings ("JUnitTestMethodWithNoAssertions" )
6969 @ Test
7070 public void checkGridReferenceRecords () throws Exception {
7171 LOG .info ("checkGridReferenceRecords" );
72- checkFile (0 , GRID_REFERENCE_FILE_1 );
72+ for (int i = 0 ; i < 8 ; ++i ) {
73+ checkFile (i , GRID_REFERENCE_FILE_1 );
74+ }
7375 checkFile (0 , GRID_REFERENCE_FILE_2 );
7476 checkFile (0 , GRID_REFERENCE_FILE_3 );
77+ checkFile (8 , GRID_REFERENCE_FILE_2 );
78+ checkFile (8 , GRID_REFERENCE_FILE_3 );
7579 }
7680
7781 @ SuppressWarnings ("JUnitTestMethodWithNoAssertions" )
7882 @ Test
7983 public void checkBoundariesReferenceRecords () throws Exception {
8084 LOG .info ("checkBoundariesReferenceRecords" );
8185 checkFile (0 , BOUNDARIES_REFERENCE_FILE );
82- }
83-
84- @ SuppressWarnings ("JUnitTestMethodWithNoAssertions" )
85- @ Test
86- public void checkRandomReferenceRecordsPrecision8 () throws Exception {
87- LOG .info ("checkRandomReferenceRecordsPrecision8" );
88- checkFile (8 , RANDOM_REFERENCE_FILE_1 );
89- checkFile (8 , RANDOM_REFERENCE_FILE_2 );
90- checkFile (8 , RANDOM_REFERENCE_FILE_3 );
91- }
92-
93- @ SuppressWarnings ("JUnitTestMethodWithNoAssertions" )
94- @ Test
95- public void checkGridReferenceRecordsPrecision8 () throws Exception {
96- LOG .info ("checkGridReferenceRecordsPrecision8" );
97- checkFile (8 , GRID_REFERENCE_FILE_1 );
98- checkFile (8 , GRID_REFERENCE_FILE_2 );
99- checkFile (8 , GRID_REFERENCE_FILE_3 );
100- }
101-
102- @ SuppressWarnings ("JUnitTestMethodWithNoAssertions" )
103- @ Test
104- public void checkBoundariesReferenceRecordsPrecision8 () throws Exception {
105- LOG .info ("checkBoundariesReferenceRecordsPrecision8" );
10686 checkFile (8 , BOUNDARIES_REFERENCE_FILE );
10787 }
10888
@@ -144,7 +124,7 @@ public void run() {
144124
145125 // Encode lat/lon to series of mapcodes and check the resulting mapcodes.
146126 final List <Mapcode > results = MapcodeCodec .encode (
147- reference .point .getLatDeg (), reference .point .getLonDeg ());
127+ reference .point .getLatDeg (), reference .point .getLonDeg ());
148128
149129 // Check the number of mapcodes.
150130 if (results .isEmpty ()) {
@@ -154,11 +134,11 @@ public void run() {
154134
155135 // Check if last mapcode is the international code.
156136 final Mapcode resultInternational = MapcodeCodec .encodeToInternational (
157- reference .point .getLatDeg (), reference .point .getLonDeg ());
137+ reference .point .getLatDeg (), reference .point .getLonDeg ());
158138 final Mapcode expectedInternational = results .get (results .size () - 1 );
159139 if (!resultInternational .equals (expectedInternational )) {
160140 LOG .error ("checkFile: encodeToInternational fails, expected={}, got={} for reference" ,
161- expectedInternational , resultInternational , reference );
141+ expectedInternational , resultInternational , reference );
162142 errors .incrementAndGet ();
163143 }
164144
@@ -169,17 +149,17 @@ public void run() {
169149 final ArrayList <MapcodeRec > resultsConverted = new ArrayList <MapcodeRec >(results .size ());
170150 for (final Mapcode mapcode : results ) {
171151 resultsConverted .add (
172- new MapcodeRec (mapcode .getCode (precision ), mapcode .getTerritory ()));
152+ new MapcodeRec (mapcode .getCode (precision ), mapcode .getTerritory ()));
173153 }
174154 LOG .error ("checkFile: Incorrect number of results:" +
175- "\n lat/lon = {}" +
176- "\n expected = #{}: {} results," +
177- "\n actual = #{}: {} results\n " ,
178- reference .point ,
179- reference .mapcodes .size (),
180- GSON .toJson (reference .mapcodes ),
181- results .size (),
182- GSON .toJson (resultsConverted ));
155+ "\n lat/lon = {}" +
156+ "\n expected = #{}: {} results," +
157+ "\n actual = #{}: {} results\n " ,
158+ reference .point ,
159+ reference .mapcodes .size (),
160+ GSON .toJson (reference .mapcodes ),
161+ results .size (),
162+ GSON .toJson (resultsConverted ));
183163 errors .incrementAndGet ();
184164 }
185165
@@ -196,11 +176,11 @@ public void run() {
196176 final int endOfMapcode = indexOfDash + ((precision > 0 ) ? (precision + 1 ) : 0 );
197177 if (referenceMapcodeRec .mapcode .length () < endOfMapcode ) {
198178 LOG .error ("checkFile: Reference mapcode not generated at highest precision: {}" ,
199- referenceMapcodeRec .mapcode );
179+ referenceMapcodeRec .mapcode );
200180 errors .incrementAndGet ();
201181 }
202182 final String referenceMapcode =
203- referenceMapcodeRec .mapcode .substring (0 , endOfMapcode );
183+ referenceMapcodeRec .mapcode .substring (0 , endOfMapcode );
204184 final String generatedMapcode = result .getCode (precision );
205185 if (referenceMapcode .equals (generatedMapcode )) {
206186 found = true ;
@@ -213,10 +193,10 @@ public void run() {
213193 // This does not fail the test, but rather produces an ERROR in the log file.
214194 // It indicates a discrepancy in the C and Java implementations.
215195 LOG .error (
216- "checkFile: Created '{}' at {} which is not present in the reference file!\n " +
217- "reference={}\n " + "created={}" ,
218- result .getCode (precision ), reference .point , GSON .toJson (reference ),
219- GSON .toJson (result ));
196+ "checkFile: Created '{}' at {} which is not present in the reference file!\n " +
197+ "reference={}\n " + "created={}" ,
198+ result .getCode (precision ), reference .point , GSON .toJson (reference ),
199+ GSON .toJson (result ));
220200 errors .incrementAndGet ();
221201 }
222202 }
@@ -243,10 +223,10 @@ public void run() {
243223 }
244224 if (!found ) {
245225 LOG .error (
246- "checkFile: Found '{} {}' at {} in reference file, not produced by new decoder!\n " +
247- "reference={}" ,
248- referenceMapcodeRec .territory , referenceMapcodeRec .mapcode , reference .point ,
249- GSON .toJson (reference ));
226+ "checkFile: Found '{} {}' at {} in reference file, not produced by new decoder!\n " +
227+ "reference={}" ,
228+ referenceMapcodeRec .territory , referenceMapcodeRec .mapcode , reference .point ,
229+ GSON .toJson (reference ));
250230 errors .incrementAndGet ();
251231 }
252232 }
@@ -260,48 +240,45 @@ public void run() {
260240 final int endOfMapcode = indexOfDash + ((precision > 0 ) ? (precision + 1 ) : 0 );
261241 assert referenceMapcodeRec .mapcode .length () >= endOfMapcode ;
262242 final String cutOffReferenceMapcode =
263- referenceMapcodeRec .mapcode .substring (0 , endOfMapcode );
243+ referenceMapcodeRec .mapcode .substring (0 , endOfMapcode );
264244
265245 // Decode cut-off mapcode to lat/lon.
266246 final Point result =
267- MapcodeCodec .decode (cutOffReferenceMapcode , referenceMapcodeRec .territory );
247+ MapcodeCodec .decode (cutOffReferenceMapcode , referenceMapcodeRec .territory );
268248
269249 // Get distance to reference point.
270250 final double distanceM = Point .distanceInMeters (reference .point , result );
271251
272252 // Keep distance. This is a multi-threaded get/set; requires synchronized.
273253 synchronized (deltaNm ) {
274254 deltaNm [precision ].set (
275- Math .max (deltaNm [precision ].get (), (long ) (distanceM * 1000000.0 )));
255+ Math .max (deltaNm [precision ].get (), (long ) (distanceM * 1000000.0 )));
276256 }
277257
278258 // Check if the distance is no greater than the safe maximum specified.
279259 final double maxDeltaM = Mapcode .getSafeMaxOffsetInMeters (precision );
280260 if (distanceM > maxDeltaM ) {
281261 LOG .error (
282- "checkFile: Precision {}: mapcode {} {} was generated for point {}, but decodes to point {} " +
283- "which is {} meters from the original point (max is {} meters)." ,
284- precision ,
285- referenceMapcodeRec .territory , referenceMapcodeRec .mapcode , reference .point ,
286- result , distanceM , maxDeltaM );
262+ "checkFile: Precision {}: mapcode {} {} was generated for point {}, but decodes to point {} " +
263+ "which is {} meters from the original point (max is {} meters)." ,
264+ precision ,
265+ referenceMapcodeRec .territory , referenceMapcodeRec .mapcode , reference .point ,
266+ result , distanceM , maxDeltaM );
287267 errors .incrementAndGet ();
288268 }
289269 }
290270 }
291- }
292- catch (final UnknownMapcodeException e ) {
271+ } catch (final UnknownMapcodeException e ) {
293272 LOG .error ("Mapcode was generated for point {}, but cannot be decoded, msg={}" ,
294- reference .point , e .getMessage ());
273+ reference .point , e .getMessage ());
295274 errors .incrementAndGet ();
296275 }
297276 }
298277 });
299278 }
300- }
301- catch (final EOFException e ) {
279+ } catch (final EOFException e ) {
302280 // OK.
303- }
304- finally {
281+ } finally {
305282 chunkedFile .close ();
306283 }
307284 executor .shutdown ();
@@ -314,13 +291,13 @@ public void run() {
314291 final double m = ((double ) deltaNm [p ].get ()) / 1000000.0 ;
315292 final double safeMaxOffsetInMeters = Mapcode .getSafeMaxOffsetInMeters (p );
316293 LOG .info ("checkFile: Precision {}: max found is {} meters (absolute max is {} meters, delta is {} meters}" ,
317- p , m , safeMaxOffsetInMeters , safeMaxOffsetInMeters - m );
294+ p , m , safeMaxOffsetInMeters , safeMaxOffsetInMeters - m );
318295 }
319296 }
320297
321298 private static class MapcodeRec {
322299 @ Nonnull
323- private final String mapcode ;
300+ private final String mapcode ;
324301 @ Nonnull
325302 private final Territory territory ;
326303
@@ -332,7 +309,7 @@ public MapcodeRec(@Nonnull final String mapcode, @Nonnull final Territory territ
332309
333310 private static class ReferenceRec {
334311 @ Nonnull
335- private final Point point ;
312+ private final Point point ;
336313 @ Nonnull
337314 private final ArrayList <MapcodeRec > mapcodes ;
338315
@@ -344,13 +321,13 @@ public ReferenceRec(@Nonnull final Point point, @Nonnull final ArrayList<Mapcode
344321
345322 @ Nonnull
346323 private static ReferenceRec getNextReferenceRecord (@ Nonnull final ChunkedFile chunkedFile )
347- throws IOException , UnknownTerritoryException {
324+ throws IOException , UnknownTerritoryException {
348325
349326 // Read first line of data file: <nr> <lat> <lon> <x> <y> <z>
350327 final String firstLine = chunkedFile .readNonEmptyLine ();
351328 final String [] args = firstLine .split (" " );
352329 assertTrue ("Expecting 3 or 6 elements, not " + args .length + " in line: " + firstLine ,
353- (args .length == 3 ) || (args .length == 6 ));
330+ (args .length == 3 ) || (args .length == 6 ));
354331
355332 final int count = Integer .parseInt (args [0 ]);
356333 assertTrue ("Expecting between 1 and 22 mapcodes" , (1 <= count ) && (count <= 22 ));
@@ -370,15 +347,14 @@ private static ReferenceRec getNextReferenceRecord(@Nonnull final ChunkedFile ch
370347
371348 final String [] mapcodeLine = line .split (" " );
372349 assertTrue ("Expecting 1 or 2 elements, territory and mapcode, got: " + mapcodeLine .length + ", " + line ,
373- mapcodeLine .length <= 2 );
350+ mapcodeLine .length <= 2 );
374351
375352 @ Nonnull final Territory territory ;
376353 @ Nonnull final String mapcode ;
377354 if (mapcodeLine .length == 1 ) {
378355 territory = Territory .AAA ;
379356 mapcode = mapcodeLine [0 ];
380- }
381- else {
357+ } else {
382358 territory = Territory .fromString (mapcodeLine [0 ]);
383359 mapcode = mapcodeLine [1 ];
384360 }
@@ -396,11 +372,11 @@ private static ReferenceRec getNextReferenceRecord(@Nonnull final ChunkedFile ch
396372 * to next chunks when needed.
397373 */
398374 private static class ChunkedFile {
399- final private String baseFileName ;
400- private String fileName ;
401- private char fileExt ;
402- private InputStream inputStream ;
403- private BufferedReader bufferedReader ;
375+ final private String baseFileName ;
376+ private String fileName ;
377+ private char fileExt ;
378+ private InputStream inputStream ;
379+ private BufferedReader bufferedReader ;
404380
405381 private ChunkedFile (final String baseFileName ) throws IOException {
406382 super ();
@@ -411,8 +387,7 @@ private ChunkedFile(final String baseFileName) throws IOException {
411387 if (inputStream != null ) {
412388 LOG .info ("ChunkedFile: Reading {}..." , fileName );
413389 this .bufferedReader = new BufferedReader (new InputStreamReader (this .inputStream ));
414- }
415- else {
390+ } else {
416391 throw new IOException ();
417392 }
418393 }
@@ -427,8 +402,7 @@ private String readNonEmptyLine() throws IOException {
427402 try {
428403 line = bufferedReader .readLine ();
429404 tryNextChunk = !bufferedReader .ready () || (line == null );
430- }
431- catch (final EOFException ignored ) {
405+ } catch (final EOFException ignored ) {
432406 tryNextChunk = true ;
433407 }
434408 if (line == null ) {
@@ -453,8 +427,7 @@ private void nextChunk() throws EOFException {
453427 if (inputStream != null ) {
454428 LOG .info ("nextChunk: Reading {}..." , fileName );
455429 bufferedReader = new BufferedReader (new InputStreamReader (inputStream ));
456- }
457- else {
430+ } else {
458431 LOG .debug ("nextChunk: End of chunked file found (chunk {} not found)" , fileName );
459432 throw new EOFException ();
460433 }
@@ -464,14 +437,12 @@ private void close() {
464437 if (inputStream != null ) {
465438 try {
466439 bufferedReader .close ();
467- }
468- catch (final IOException ignored ) {
440+ } catch (final IOException ignored ) {
469441 LOG .error ("close: Cannot close BufferedReader: {}" , fileName );
470442 }
471443 try {
472444 inputStream .close ();
473- }
474- catch (final IOException e ) {
445+ } catch (final IOException e ) {
475446 LOG .error ("close: Cannot close InputStream: {}" , fileName );
476447 }
477448 }
0 commit comments