Skip to content

Commit 1e539b2

Browse files
committed
Improved unit test, added max delta in meters to Mapcode class
1 parent 5dec9a1 commit 1e539b2

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/main/java/com/mapcode/Mapcode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public final class Mapcode {
4343
@Nonnull
4444
private final Territory territory;
4545

46+
/**
47+
* These constants define a safe maximum for the distance between a decoded mapcode and its original
48+
* location used for encoding the mapcode.
49+
*
50+
* The actual accuracy (resolution) of mapcodes are actually slightly better than this, but these are
51+
* safe values to use under normal circumstances.
52+
*/
4653
public static final double PRECISION_0_MAX_DELTA_METERS = 10.0;
4754
public static final double PRECISION_1_MAX_DELTA_METERS = 2.0;
4855
public static final double PRECISION_2_MAX_DELTA_METERS = 0.4;

src/test/java/com/mapcode/ReferenceFileTest.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ private static void checkFile(@Nonnull final String baseFileName) throws Excepti
129129
LOG.info("checkFile: expected = {}", GSON.toJson(reference.mapcodes));
130130
}
131131

132-
/**
133-
* Check encode.
134-
*/
135-
136132
// Encode lat/lon to series of mapcodes and check the resulting mapcodes.
137133
final List<Mapcode> results = MapcodeCodec.encode(
138134
reference.point.getLatDeg(), reference.point.getLonDeg());
@@ -146,33 +142,37 @@ private static void checkFile(@Nonnull final String baseFileName) throws Excepti
146142
++error;
147143
}
148144

149-
/**
150-
* Check encodeToInternational.
151-
*/
152-
final Mapcode resultInternational = MapcodeCodec.encodeToInternational(reference.point.getLatDeg(), reference.point.getLonDeg());
145+
// Check encodeToInternational.
146+
final Mapcode resultInternational = MapcodeCodec.encodeToInternational(
147+
reference.point.getLatDeg(), reference.point.getLonDeg());
153148
final Mapcode expectedInternational = results.get(results.size() - 1);
154149
if (!resultInternational.equals(expectedInternational)) {
155150
LOG.error("checkFile: encodeToInternational fails, expected={}, got={} for reference",
156151
expectedInternational, resultInternational, reference);
157152
++error;
158153
}
159154

160-
// Check the size and order of the results with a single assertion.
161-
//
162-
assertEquals("Encode #" + i + " incorrect number of results:" +
163-
"\n lat/lon = " + reference.point +
164-
"\n expected = " + reference.mapcodes.size() + " results, " +
165-
GSON.toJson(reference.mapcodes) +
166-
"\n actual = " + results.size() + " results, " + GSON.toJson(results),
167-
reference.mapcodes.size(), results.size());
155+
// Check the size of the results.
156+
if (reference.mapcodes.size() != results.size()) {
157+
LOG.error("checkFile: Encode #{} incorrect number of results:" +
158+
"\n lat/lon = {}" +
159+
"\n expected = {} results," +
160+
"\n actual = {} results",
161+
i,
162+
reference.point,
163+
reference.mapcodes.size(),
164+
GSON.toJson(reference.mapcodes),
165+
GSON.toJson(results));
166+
++error;
167+
}
168168

169169
// For every mapcode in the result set, check if it is contained in the reference set.
170170
for (final Mapcode result : results) {
171171
boolean found = false;
172172
for (final MapcodeRec referenceMapcodeRec : reference.mapcodes) {
173173
if (referenceMapcodeRec.territory.equals(result.getTerritory())) {
174174
if (referenceMapcodeRec.mapcode.lastIndexOf('-') > 4) {
175-
if (referenceMapcodeRec.mapcode.equals(result.getMapcodePrecision2())) {
175+
if (referenceMapcodeRec.mapcode.equals(result.getMapcodePrecision(2))) {
176176
found = true;
177177
break;
178178
}
@@ -200,7 +200,7 @@ private static void checkFile(@Nonnull final String baseFileName) throws Excepti
200200
for (final Mapcode result : results) {
201201
if (referenceMapcodeRec.territory.equals(result.getTerritory())) {
202202
if (referenceMapcodeRec.mapcode.lastIndexOf('-') > 4) {
203-
if (referenceMapcodeRec.mapcode.equals(result.getMapcodePrecision2())) {
203+
if (referenceMapcodeRec.mapcode.equals(result.getMapcodePrecision(2))) {
204204
found = true;
205205
break;
206206
}
@@ -219,19 +219,20 @@ private static void checkFile(@Nonnull final String baseFileName) throws Excepti
219219
}
220220
}
221221

222-
/**
223-
* Check distance of decoded point to reference point.
224-
*/
222+
// Check distance of decoded point to reference point.
225223
for (final MapcodeRec mapcodeRec : reference.mapcodes) {
226224
//noinspection NestedTryStatement
227225
try {
228226
final Point result = MapcodeCodec.decode(mapcodeRec.mapcode, mapcodeRec.territory);
229227
final double distanceMeters = Point.distanceInMeters(reference.point, result);
230228
maxdelta = Math.max(maxdelta, distanceMeters);
231-
if (distanceMeters > Mapcode.PRECISION_0_MAX_DELTA_METERS) {
232-
LOG.error(
233-
"Mapcode {} {} was generated for point {}, but decodes to point {} which is {} meters from the original point.",
234-
mapcodeRec.territory, mapcodeRec.mapcode, reference.point, result, distanceMeters);
229+
230+
final double maxDeltaMeters = (mapcodeRec.mapcode.lastIndexOf('-') > 4) ?
231+
Mapcode.PRECISION_2_MAX_DELTA_METERS : Mapcode.PRECISION_0_MAX_DELTA_METERS;
232+
if (distanceMeters > maxDeltaMeters) {
233+
LOG.error("Mapcode {} {} was generated for point {}, but decodes to point {} " +
234+
"which is {} meters from the original point (max is {} meters).",
235+
mapcodeRec.territory, mapcodeRec.mapcode, reference.point, result, distanceMeters, maxDeltaMeters);
235236
++error;
236237
}
237238
} catch (final UnknownMapcodeException unknownMapcodeException) {

0 commit comments

Comments
 (0)