@@ -124,148 +124,132 @@ private static void checkFile(@Nonnull final String baseFileName) throws Excepti
124124 final ChunkedFile chunkedFile = new ChunkedFile (baseFileName );
125125 try {
126126
127- //noinspection InfiniteLoopStatement
128- while (true ) {
129-
130- // Get next record.
131- @ Nonnull final ReferenceRec reference = getNextReferenceRecord (chunkedFile );
132-
133- while (true ) {
134- try {
135- // Add task. This may throw an exception if the queue is full. Retry in that case.
136- executor .execute (new Runnable () {
137-
138- @ Override
139- public void run () {
140- final int count = tasks .getAndIncrement ();
141- if (((count % LOG_LINE_EVERY ) == 0 )) {
142- LOG .debug ("checkFile: #{}, file={}" , count , chunkedFile .fileName );
143- LOG .debug ("checkFile: lat/lon = {}" , reference .point );
144- LOG .debug ("checkFile: expected = #{}: {}" , reference .mapcodes .size (), GSON .toJson (reference .mapcodes ));
145- }
127+ // Get next record.
128+ @ Nonnull final ReferenceRec reference = getNextReferenceRecord (chunkedFile );
129+
130+ // Add task. This may throw an exception if the queue is full. Retry in that case.
131+ executor .execute (new Runnable () {
132+
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+ }
146141
147- // Encode lat/lon to series of mapcodes and check the resulting mapcodes.
148- final List <Mapcode > results = MapcodeCodec .encode (
149- reference .point .getLatDeg (), reference .point .getLonDeg ());
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 ());
150145
151- // Check the number of mapcodes.
152- if (results .isEmpty ()) {
153- LOG .error ("checkFile: encode fails, no results found for reference={}" , reference );
154- errors .incrementAndGet ();
155- }
156-
157- // Check encodeToInternational.
158- final Mapcode resultInternational = MapcodeCodec .encodeToInternational (
159- reference .point .getLatDeg (), reference .point .getLonDeg ());
160- final Mapcode expectedInternational = results .get (results .size () - 1 );
161- if (!resultInternational .equals (expectedInternational )) {
162- LOG .error ("checkFile: encodeToInternational fails, expected={}, got={} for reference" ,
163- expectedInternational , resultInternational , reference );
164- errors .incrementAndGet ();
165- }
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+ }
166151
167- // Check the size of the results.
168- if (reference .mapcodes .size () != results .size ()) {
169- final ArrayList <MapcodeRec > resultsConverted = new ArrayList <MapcodeRec >(results .size ());
170- for (final Mapcode mapcode : results ) {
171- resultsConverted .add (new MapcodeRec (mapcode .getCode (2 ), mapcode .getTerritory ()));
172- }
173- LOG .error ("checkFile: Incorrect number of results:" +
174- "\n lat/lon = {}" +
175- "\n expected = #{}: {} results," +
176- "\n actual = #{}: {} results\n " ,
177- reference .point ,
178- reference .mapcodes .size (),
179- GSON .toJson (reference .mapcodes ),
180- results .size (),
181- GSON .toJson (resultsConverted ));
182- errors .incrementAndGet ();
183- }
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+ }
184161
185- // For every mapcode in the result set, check if it is contained in the reference set.
186- int precision = 0 ;
187- for (final Mapcode result : results ) {
188- boolean found = false ;
189- for (final MapcodeRec referenceMapcodeRec : reference .mapcodes ) {
190- precision = (referenceMapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ? 2 : 0 ;
191-
192- if (referenceMapcodeRec .territory .equals (result .getTerritory ())) {
193- if (referenceMapcodeRec .mapcode .equals (result .getCode (precision ))) {
194- found = true ;
195- break ;
196- }
197- }
198- }
199- if (!found ) {
200-
201- // This does not fail the test, but rather produces an ERROR in the log file.
202- // It indicates a discrepancy in the C and Java implementations.
203- LOG .error ("checkFile: Created '{}' at {} which is not present in the reference file!\n " +
204- "ref={}\n " + "new={}" ,
205- result .getCode (precision ), reference .point , GSON .toJson (reference ), GSON .toJson (result ));
206- errors .incrementAndGet ();
207- }
208- }
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 ()));
167+ }
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+ }
209179
210- // For every Mapcode in the reference set, check if it is contained in the result set.
211- for (final MapcodeRec referenceMapcodeRec : reference .mapcodes ) {
212- precision = (referenceMapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ? 2 : 0 ;
213- boolean found = false ;
214- for (final Mapcode result : results ) {
215- if (referenceMapcodeRec .territory .equals (result .getTerritory ())) {
216- if (referenceMapcodeRec .mapcode .equals (result .getCode (precision ))) {
217- found = true ;
218- break ;
219- }
220- }
221- }
222- if (!found ) {
223- LOG .error ("checkFile: Found '{} {}' at {} in reference file, not produced by new decoder!\n " +
224- "ref={}" ,
225- referenceMapcodeRec .territory , referenceMapcodeRec .mapcode , reference .point ,
226- GSON .toJson (reference ));
227- errors .incrementAndGet ();
228- }
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 ;
229191 }
192+ }
193+ }
194+ if (!found ) {
195+
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 ));
201+ errors .incrementAndGet ();
202+ }
203+ }
230204
231- // Check distance of decoded point to reference point.
232- for (final MapcodeRec mapcodeRec : reference .mapcodes ) {
233- //noinspection NestedTryStatement
234- try {
235- final Point result = MapcodeCodec .decode (mapcodeRec .mapcode , mapcodeRec .territory );
236- final long distanceNm = (long ) (Point .distanceInMeters (reference .point , result ) * 1000000.0 );
237- synchronized (deltaNm ) {
238- deltaNm .set (Math .max (deltaNm .get (), distanceNm ));
239- }
240-
241- final long maxDeltaNm = (long ) (((mapcodeRec .mapcode .lastIndexOf ('-' ) > 4 ) ?
242- Mapcode .getSafeMaxOffsetInMeters (2 ) : Mapcode .getSafeMaxOffsetInMeters (0 )) * 1000000.0 );
243- if (distanceNm > maxDeltaNm ) {
244- LOG .error ("Mapcode {} {} was generated for point {}, but decodes to point {} " +
245- "which is {} meters from the original point (max is {} meters)." ,
246- mapcodeRec .territory , mapcodeRec .mapcode , reference .point , result ,
247- ((double ) distanceNm ) / 1000000.0 , ((double ) maxDeltaNm ) / 1000000.0 );
248- errors .incrementAndGet ();
249- }
250- } catch (final UnknownMapcodeException unknownMapcodeException ) {
251- LOG .error ("Mapcode {} {} was generated for point {}, but cannot be decoded." ,
252- mapcodeRec .territory , mapcodeRec .mapcode , reference .point );
253- errors .incrementAndGet ();
254- }
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 ;
209+ 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 ;
255214 }
256215 }
257- });
258-
259- // Break out of loop and process next value.
260- break ;
216+ }
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+ }
261225
262- } catch (final RejectedExecutionException ignored ) {
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 ));
234+ }
263235
264- // Perfectly fine; buffer is full. Just wait a bit and re-enter while loop.
265- Thread .sleep (100 );
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 );
243+ errors .incrementAndGet ();
244+ }
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 ();
249+ }
266250 }
267251 }
268- }
252+ });
269253 } catch (final EOFException e ) {
270254 // OK.
271255 } finally {
0 commit comments