|
31 | 31 | @SuppressWarnings({"JUnitTestMethodWithNoAssertions", "OverlyBroadThrowsClause", "ProhibitedExceptionDeclared"}) |
32 | 32 | public class EncodeDecodeTest { |
33 | 33 | private static final Logger LOG = LoggerFactory.getLogger(EncodeDecodeTest.class); |
34 | | - |
35 | | - private static final int NUMBER_OF_POINTS = 1000; |
36 | | - private static final int LOG_LINE_EVERY = 500; |
37 | | - private static final double ALLOWED_DISTANCE_DELTA_METERS = 10.0; |
38 | | - public static final Gson GSON = |
| 34 | + private static final Gson GSON = |
39 | 35 | new GsonBuilder().serializeSpecialFloatingPointValues().create(); |
40 | 36 |
|
| 37 | + private static final int NUMBER_OF_POINTS = 5000; |
| 38 | + private static final int LOG_LINE_EVERY = 100; |
| 39 | + |
| 40 | + private static final double PRECISION_0_METERS = 10.0; |
| 41 | + private static final double PRECISION_1_METERS = 2.0; |
| 42 | + private static final double PRECISION_2_METERS = 0.4; |
| 43 | + |
41 | 44 | @Test |
42 | 45 | public void encodeDecodeTestFixedSeed() throws Exception { |
43 | 46 | LOG.info("encodeDecodeTestFixedSeed"); |
44 | | - doEncodeDecode(12345678); |
| 47 | + doEncodeDecode(123212321); |
45 | 48 | } |
46 | 49 |
|
47 | 50 | @Test |
48 | 51 | public void encodeDecodeTestRandomSeed() throws Exception { |
49 | 52 | LOG.info("encodeDecodeTestRandomSeed"); |
50 | | - doEncodeDecode(System.currentTimeMillis()); |
| 53 | + final long seed = System.currentTimeMillis(); |
| 54 | + LOG.info("encodeDecodeTestRandomSeed: seed={}", seed); |
| 55 | + doEncodeDecode(seed); |
51 | 56 | } |
52 | 57 |
|
53 | 58 | private static void doEncodeDecode(final long seed) throws UnknownMapcodeException { |
54 | 59 | final Random randomGenerator = new Random(seed); |
| 60 | + double maxDistancePrecision0Meters = 0.0; |
| 61 | + double maxDistancePrecision1Meters = 0.0; |
| 62 | + double maxDistancePrecision2Meters = 0.0; |
55 | 63 | for (int i = 0; i < NUMBER_OF_POINTS; i++) { |
56 | 64 | boolean showLogLine = ((i % LOG_LINE_EVERY) == 0); |
57 | 65 |
|
| 66 | + // Encode location. |
| 67 | + final Point encode = Point.fromUniformlyDistributedRandomPoints(randomGenerator); |
| 68 | + final double latDeg = encode.getLatDeg(); |
| 69 | + final double lonDeg = encode.getLonDeg(); |
| 70 | + |
| 71 | + // Check local and international codes. |
| 72 | + final Mapcode resultInternational = MapcodeCodec.encodeToInternational(latDeg, lonDeg); |
| 73 | + |
| 74 | + // Check encodeToShortest and encodeToInternational. |
| 75 | + final List<Mapcode> resultsAll = MapcodeCodec.encode(latDeg, lonDeg); |
| 76 | + assertTrue(!resultsAll.isEmpty()); |
| 77 | + assertEquals("encodeToInternational failed, result=" + resultsAll, |
| 78 | + resultsAll.get(resultsAll.size() - 1), resultInternational); |
| 79 | + |
58 | 80 | // Every point must have a Mapcode. |
59 | 81 | boolean found = false; |
60 | | - final Point encode = Point.fromUniformlyDistributedRandomPoints(randomGenerator); |
61 | 82 |
|
62 | 83 | // Walk through the list in reverse order to get International first. |
63 | 84 | for (final Territory territory : Territory.values()) { |
64 | | - |
65 | | - // Encode location. |
66 | | - final double latDeg = encode.getLatDeg(); |
67 | | - final double lonDeg = encode.getLonDeg(); |
68 | | - |
69 | | - final List<Mapcode> results = MapcodeCodec.encode(latDeg, lonDeg, territory); |
70 | | - for (final Mapcode result : results) { |
| 85 | + final List<Mapcode> resultsLimited = MapcodeCodec.encode(latDeg, lonDeg, territory); |
| 86 | + for (final Mapcode result : resultsLimited) { |
71 | 87 | found = true; |
72 | 88 | if (showLogLine) { |
73 | 89 | LOG.info("encodeDecodeTest: #{}/{}, encode={}, {} {} --> results={}", |
74 | | - i, NUMBER_OF_POINTS, latDeg, lonDeg, territory, GSON.toJson(results)); |
| 90 | + i, NUMBER_OF_POINTS, latDeg, lonDeg, territory, GSON.toJson(resultsLimited)); |
75 | 91 | } |
76 | 92 |
|
77 | | - // Decode location, up to '/'. |
78 | | - final String mapcode = result.getMapcode(); |
79 | | - |
80 | 93 | // Check if the territory matches. |
| 94 | + final String mapcodePrecision0 = result.getMapcodePrecision(0); |
| 95 | + final String mapcodePrecision1 = result.getMapcodePrecision(1); |
| 96 | + final String mapcodePrecision2 = result.getMapcodePrecision(2); |
81 | 97 | assertEquals(territory, result.getTerritory()); |
82 | 98 |
|
83 | | - final Point decodeLocation = MapcodeCodec.decode(mapcode, territory); |
84 | | - final double distanceMeters = Point.distanceInMeters(encode, decodeLocation); |
| 99 | + // Check max distance. |
| 100 | + final Point decodeLocationPrecision0 = MapcodeCodec.decode(mapcodePrecision0, territory); |
| 101 | + final Point decodeLocationPrecision1 = MapcodeCodec.decode(mapcodePrecision1, territory); |
| 102 | + final Point decodeLocationPrecision2 = MapcodeCodec.decode(mapcodePrecision2, territory); |
| 103 | + final double distancePrecision0Meters = Point.distanceInMeters(encode, decodeLocationPrecision0); |
| 104 | + final double distancePrecision1Meters = Point.distanceInMeters(encode, decodeLocationPrecision1); |
| 105 | + final double distancePrecision2Meters = Point.distanceInMeters(encode, decodeLocationPrecision2); |
| 106 | + |
| 107 | + maxDistancePrecision0Meters = Math.max(maxDistancePrecision0Meters, distancePrecision0Meters); |
| 108 | + maxDistancePrecision1Meters = Math.max(maxDistancePrecision1Meters, distancePrecision1Meters); |
| 109 | + maxDistancePrecision2Meters = Math.max(maxDistancePrecision2Meters, distancePrecision2Meters); |
| 110 | + |
| 111 | + assertTrue("distancePrecision0Meters=" + distancePrecision0Meters + " >= " + PRECISION_0_METERS, |
| 112 | + distancePrecision0Meters < PRECISION_0_METERS); |
| 113 | + assertTrue("distancePrecision1Meters=" + distancePrecision1Meters + " >= " + PRECISION_1_METERS, |
| 114 | + distancePrecision1Meters < PRECISION_1_METERS); |
| 115 | + assertTrue("distancePrecision2Meters=" + distancePrecision2Meters + " >= " + PRECISION_2_METERS, |
| 116 | + distancePrecision2Meters < PRECISION_2_METERS); |
85 | 117 |
|
86 | 118 | if (showLogLine) { |
87 | 119 | LOG.info("encodeDecodeTest: #{}/{}, result={}, mapcode={}, territory={} --> " + |
88 | 120 | "lat={}, lon={}; delta={}", i, NUMBER_OF_POINTS, |
89 | | - result, mapcode, territory.getFullName(), decodeLocation.getLatDeg(), |
90 | | - decodeLocation.getLonDeg(), distanceMeters); |
| 121 | + result, mapcodePrecision0, territory.getFullName(), decodeLocationPrecision0.getLatDeg(), |
| 122 | + decodeLocationPrecision0.getLonDeg(), distancePrecision0Meters); |
91 | 123 | LOG.info(""); |
92 | 124 | } |
93 | | - |
94 | | - // Check if the distance is not too great. |
95 | | - assertTrue("distanceMeters=" + distanceMeters + " >= " + ALLOWED_DISTANCE_DELTA_METERS, |
96 | | - distanceMeters < ALLOWED_DISTANCE_DELTA_METERS); |
97 | 125 | showLogLine = false; |
98 | 126 | } |
99 | 127 | } |
100 | 128 | assertTrue(found); |
101 | 129 | } |
| 130 | + LOG.info("encodeDecodeTest: maximum distances, precision 0, 1, 2: {}, {}, {} meters, ", |
| 131 | + maxDistancePrecision0Meters, maxDistancePrecision1Meters, maxDistancePrecision2Meters); |
102 | 132 | } |
103 | 133 | } |
0 commit comments