@@ -116,140 +116,145 @@ private static void checkFile(@Nonnull final String baseFileName) throws Excepti
116116 final AtomicLong deltaNm = new AtomicLong (0 );
117117 final AtomicInteger errors = new AtomicInteger (0 );
118118 final AtomicInteger tasks = new AtomicInteger (0 );
119- final int threads = Runtime .getRuntime ().availableProcessors () * 2 ;
119+
120+ final int threads = Runtime .getRuntime ().availableProcessors ();
120121 LOG .info ("checkFile: Starting {} threads..." , threads );
121122 final ExecutorService executor = Executors .newFixedThreadPool (threads );
122123
123124 // Open data file.
124125 final ChunkedFile chunkedFile = new ChunkedFile (baseFileName );
125126 try {
126127
127- // Get next record.
128- @ Nonnull final ReferenceRec reference = getNextReferenceRecord ( chunkedFile );
128+ //noinspection InfiniteLoopStatement
129+ while ( true ) {
129130
130- // Add task. This may throw an exception if the queue is full. Retry in that case .
131- executor . execute ( new Runnable () {
131+ // Get next record .
132+ @ Nonnull final ReferenceRec reference = getNextReferenceRecord ( chunkedFile );
132133
133- @ Override
134- public void run () {
135- final int count = tasks .getAndIncrement ();
136- if (((count % LOG_LINE_EVERY ) == 0 )) {
137- LOG .debug ("checkFile: #{}, file={}" , count , chunkedFile .fileName );
138- LOG .debug ("checkFile: lat/lon = {}" , reference .point );
139- LOG .debug ("checkFile: expected = #{}: {}" , reference .mapcodes .size (), GSON .toJson (reference .mapcodes ));
140- }
134+ final int count = tasks .getAndIncrement ();
135+ if (((count % LOG_LINE_EVERY ) == 0 )) {
136+ LOG .debug ("checkFile: #{}, file={}" , count , chunkedFile .fileName );
137+ LOG .debug ("checkFile: lat/lon = {}" , reference .point );
138+ LOG .debug ("checkFile: expected = #{}: {}" , reference .mapcodes .size (), GSON .toJson (reference .mapcodes ));
139+ }
141140
142- // Encode lat/lon to series of mapcodes and check the resulting mapcodes.
143- final List <Mapcode > results = MapcodeCodec .encode (
144- reference .point .getLatDeg (), reference .point .getLonDeg ());
141+ // Add task. This may throw an exception if the queue is full. Retry in that case.
142+ executor .execute (new Runnable () {
145143
146- // Check the number of mapcodes.
147- if ( results . isEmpty () ) {
148- LOG . error ( "checkFile: encode fails, no results found for reference={}" , reference );
149- errors . incrementAndGet ();
150- }
144+ @ Override
145+ public void run ( ) {
146+ // Encode lat/lon to series of mapcodes and check the resulting mapcodes.
147+ final List < Mapcode > results = MapcodeCodec . encode (
148+ reference . point . getLatDeg (), reference . point . getLonDeg ());
151149
152- // Check encodeToInternational.
153- final Mapcode resultInternational = MapcodeCodec .encodeToInternational (
154- reference .point .getLatDeg (), reference .point .getLonDeg ());
155- final Mapcode expectedInternational = results .get (results .size () - 1 );
156- if (!resultInternational .equals (expectedInternational )) {
157- LOG .error ("checkFile: encodeToInternational fails, expected={}, got={} for reference" ,
158- expectedInternational , resultInternational , reference );
159- errors .incrementAndGet ();
160- }
161-
162- // Check the size of the results.
163- if (reference .mapcodes .size () != results .size ()) {
164- final ArrayList <MapcodeRec > resultsConverted = new ArrayList <MapcodeRec >(results .size ());
165- for (final Mapcode mapcode : results ) {
166- resultsConverted .add (new MapcodeRec (mapcode .getCode (2 ), mapcode .getTerritory ()));
150+ // Check the number of mapcodes.
151+ if (results .isEmpty ()) {
152+ LOG .error ("checkFile: encode fails, no results found for reference={}" , reference );
153+ errors .incrementAndGet ();
167154 }
168- LOG .error ("checkFile: Incorrect number of results:" +
169- "\n lat/lon = {}" +
170- "\n expected = #{}: {} results," +
171- "\n actual = #{}: {} results\n " ,
172- reference .point ,
173- reference .mapcodes .size (),
174- GSON .toJson (reference .mapcodes ),
175- results .size (),
176- GSON .toJson (resultsConverted ));
177- errors .incrementAndGet ();
178- }
179155
180- // For every mapcode in the result set, check if it is contained in the reference set.
181- int precision = 0 ;
182- for (final Mapcode result : results ) {
183- boolean found = false ;
184- for (final MapcodeRec referenceMapcodeRec : reference .mapcodes ) {
185- precision = (referenceMapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ? 2 : 0 ;
186-
187- if (referenceMapcodeRec .territory .equals (result .getTerritory ())) {
188- if (referenceMapcodeRec .mapcode .equals (result .getCode (precision ))) {
189- found = true ;
190- break ;
191- }
192- }
156+ // Check encodeToInternational.
157+ final Mapcode resultInternational = MapcodeCodec .encodeToInternational (
158+ reference .point .getLatDeg (), reference .point .getLonDeg ());
159+ final Mapcode expectedInternational = results .get (results .size () - 1 );
160+ if (!resultInternational .equals (expectedInternational )) {
161+ LOG .error ("checkFile: encodeToInternational fails, expected={}, got={} for reference" ,
162+ expectedInternational , resultInternational , reference );
163+ errors .incrementAndGet ();
193164 }
194- if (!found ) {
195165
196- // This does not fail the test, but rather produces an ERROR in the log file.
197- // It indicates a discrepancy in the C and Java implementations.
198- LOG .error ("checkFile: Created '{}' at {} which is not present in the reference file!\n " +
199- "ref={}\n " + "new={}" ,
200- result .getCode (precision ), reference .point , GSON .toJson (reference ), GSON .toJson (result ));
166+ // Check the size of the results.
167+ if (reference .mapcodes .size () != results .size ()) {
168+ final ArrayList <MapcodeRec > resultsConverted = new ArrayList <MapcodeRec >(results .size ());
169+ for (final Mapcode mapcode : results ) {
170+ resultsConverted .add (new MapcodeRec (mapcode .getCode (2 ), mapcode .getTerritory ()));
171+ }
172+ LOG .error ("checkFile: Incorrect number of results:" +
173+ "\n lat/lon = {}" +
174+ "\n expected = #{}: {} results," +
175+ "\n actual = #{}: {} results\n " ,
176+ reference .point ,
177+ reference .mapcodes .size (),
178+ GSON .toJson (reference .mapcodes ),
179+ results .size (),
180+ GSON .toJson (resultsConverted ));
201181 errors .incrementAndGet ();
202182 }
203- }
204183
205- // For every Mapcode in the reference set, check if it is contained in the result set.
206- for (final MapcodeRec referenceMapcodeRec : reference .mapcodes ) {
207- precision = (referenceMapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ? 2 : 0 ;
208- boolean found = false ;
184+ // For every mapcode in the result set, check if it is contained in the reference set.
185+ int precision = 0 ;
209186 for (final Mapcode result : results ) {
210- if (referenceMapcodeRec .territory .equals (result .getTerritory ())) {
211- if (referenceMapcodeRec .mapcode .equals (result .getCode (precision ))) {
212- found = true ;
213- break ;
187+ boolean found = false ;
188+ for (final MapcodeRec referenceMapcodeRec : reference .mapcodes ) {
189+ precision = (referenceMapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ? 2 : 0 ;
190+
191+ if (referenceMapcodeRec .territory .equals (result .getTerritory ())) {
192+ if (referenceMapcodeRec .mapcode .equals (result .getCode (precision ))) {
193+ found = true ;
194+ break ;
195+ }
214196 }
215197 }
198+ if (!found ) {
199+
200+ // This does not fail the test, but rather produces an ERROR in the log file.
201+ // It indicates a discrepancy in the C and Java implementations.
202+ LOG .error ("checkFile: Created '{}' at {} which is not present in the reference file!\n " +
203+ "ref={}\n " + "new={}" ,
204+ result .getCode (precision ), reference .point , GSON .toJson (reference ), GSON .toJson (result ));
205+ errors .incrementAndGet ();
206+ }
216207 }
217- if (!found ) {
218- LOG .error ("checkFile: Found '{} {}' at {} in reference file, not produced by new decoder!\n " +
219- "ref={}" ,
220- referenceMapcodeRec .territory , referenceMapcodeRec .mapcode , reference .point ,
221- GSON .toJson (reference ));
222- errors .incrementAndGet ();
223- }
224- }
225208
226- // Check distance of decoded point to reference point.
227- for (final MapcodeRec mapcodeRec : reference .mapcodes ) {
228- //noinspection NestedTryStatement
229- try {
230- final Point result = MapcodeCodec .decode (mapcodeRec .mapcode , mapcodeRec .territory );
231- final long distanceNm = (long ) (Point .distanceInMeters (reference .point , result ) * 1000000.0 );
232- synchronized (deltaNm ) {
233- deltaNm .set (Math .max (deltaNm .get (), distanceNm ));
209+ // For every Mapcode in the reference set, check if it is contained in the result set.
210+ for (final MapcodeRec referenceMapcodeRec : reference .mapcodes ) {
211+ precision = (referenceMapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ? 2 : 0 ;
212+ boolean found = false ;
213+ for (final Mapcode result : results ) {
214+ if (referenceMapcodeRec .territory .equals (result .getTerritory ())) {
215+ if (referenceMapcodeRec .mapcode .equals (result .getCode (precision ))) {
216+ found = true ;
217+ break ;
218+ }
219+ }
234220 }
221+ if (!found ) {
222+ LOG .error ("checkFile: Found '{} {}' at {} in reference file, not produced by new decoder!\n " +
223+ "ref={}" ,
224+ referenceMapcodeRec .territory , referenceMapcodeRec .mapcode , reference .point ,
225+ GSON .toJson (reference ));
226+ errors .incrementAndGet ();
227+ }
228+ }
229+
230+ // Check distance of decoded point to reference point.
231+ for (final MapcodeRec mapcodeRec : reference .mapcodes ) {
232+ //noinspection NestedTryStatement
233+ try {
234+ final Point result = MapcodeCodec .decode (mapcodeRec .mapcode , mapcodeRec .territory );
235+ final long distanceNm = (long ) (Point .distanceInMeters (reference .point , result ) * 1000000.0 );
236+ synchronized (deltaNm ) {
237+ deltaNm .set (Math .max (deltaNm .get (), distanceNm ));
238+ }
235239
236- final long maxDeltaNm = (long ) (((mapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ?
237- Mapcode .getSafeMaxOffsetInMeters (2 ) : Mapcode .getSafeMaxOffsetInMeters (0 )) * 1000000.0 );
238- if (distanceNm > maxDeltaNm ) {
239- LOG .error ("Mapcode {} {} was generated for point {}, but decodes to point {} " +
240- "which is {} meters from the original point (max is {} meters)." ,
241- mapcodeRec .territory , mapcodeRec .mapcode , reference .point , result ,
242- ((double ) distanceNm ) / 1000000.0 , ((double ) maxDeltaNm ) / 1000000.0 );
240+ final long maxDeltaNm = (long ) (((mapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ?
241+ Mapcode .getSafeMaxOffsetInMeters (2 ) : Mapcode .getSafeMaxOffsetInMeters (0 )) * 1000000.0 );
242+ if (distanceNm > maxDeltaNm ) {
243+ LOG .error ("Mapcode {} {} was generated for point {}, but decodes to point {} " +
244+ "which is {} meters from the original point (max is {} meters)." ,
245+ mapcodeRec .territory , mapcodeRec .mapcode , reference .point , result ,
246+ ((double ) distanceNm ) / 1000000.0 , ((double ) maxDeltaNm ) / 1000000.0 );
247+ errors .incrementAndGet ();
248+ }
249+ } catch (final UnknownMapcodeException unknownMapcodeException ) {
250+ LOG .error ("Mapcode {} {} was generated for point {}, but cannot be decoded." ,
251+ mapcodeRec .territory , mapcodeRec .mapcode , reference .point );
243252 errors .incrementAndGet ();
244253 }
245- } catch (final UnknownMapcodeException unknownMapcodeException ) {
246- LOG .error ("Mapcode {} {} was generated for point {}, but cannot be decoded." ,
247- mapcodeRec .territory , mapcodeRec .mapcode , reference .point );
248- errors .incrementAndGet ();
249254 }
250255 }
251- }
252- });
256+ });
257+ }
253258 } catch (final EOFException e ) {
254259 // OK.
255260 } finally {
@@ -259,7 +264,7 @@ public void run() {
259264 executor .awaitTermination (60 , TimeUnit .SECONDS );
260265 assertEquals (0 , errors .get ());
261266 assertEquals ("Found errors" , 0 , errors .get ());
262- LOG .debug ("checkFile: Maximum delta for this testset = {}m" , ((double ) deltaNm .get ()) / 1000000.0 );
267+ LOG .debug ("checkFile: Maximum delta for this testset = {}m, executed {} tasks " , ((double ) deltaNm .get ()) / 1000000.0 , tasks );
263268 }
264269
265270 private static class MapcodeRec {
0 commit comments