Repository navigation
Expand file tree
/
Copy pathEncodeDecodeTest.java
More file actions
175 lines (153 loc) · 9.12 KB
/
Copy pathEncodeDecodeTest.java
File metadata and controls
175 lines (153 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (C) 2016-2021, Stichting Mapcode Foundation (http://www.mapcode.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mapcode;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
@SuppressWarnings({"OverlyBroadThrowsClause", "ProhibitedExceptionDeclared", "MagicNumber"})
public class EncodeDecodeTest {
private static final Logger LOG = LoggerFactory.getLogger(EncodeDecodeTest.class);
private static final int NUMBER_OF_POINTS = 5000;
private static final int LOG_LINE_EVERY = 500;
@Test
public void encodeDecodeTestFixedSeed() throws Exception {
final long seed = 1431977987367L;
LOG.info("encodeDecodeTestFixedSeed: seed={}", seed);
doEncodeDecode(seed);
}
@Test
public void encodeDecodeTestRandomSeed() throws Exception {
final long seed = System.currentTimeMillis();
LOG.info("encodeDecodeTestRandomSeed: seed={}", seed);
doEncodeDecode(seed);
}
private static void doEncodeDecode(final long seed) throws InterruptedException {
// Keep error count and create thread pool.
final AtomicInteger errors = new AtomicInteger(0);
final AtomicInteger tasks = new AtomicInteger(0);
final int cores = Runtime.getRuntime().availableProcessors();
final int threads = Math.min(cores * 2, 16);
LOG.info("encodeDecodeTest: Starting {} threads on {} (hyperthreaded) cores...", threads, cores);
final ExecutorService executor = Executors.newFixedThreadPool(threads);
final Random randomGenerator = new Random(seed);
for (int i = 0; i < NUMBER_OF_POINTS; i++) {
// Encode location.
final Point encode = Point.fromUniformlyDistributedRandomPoints(randomGenerator);
final double latDeg = encode.getLatDeg();
final double lonDeg = encode.getLonDeg();
// Check local and international codes.
final Mapcode mapcodeInternational = MapcodeCodec.encodeToInternational(latDeg, lonDeg);
// Check encodeToShortest and encodeToInternational.
final List<Mapcode> resultsAll = MapcodeCodec.encode(latDeg, lonDeg);
Assert.assertFalse(resultsAll.isEmpty());
assertEquals("encodeToInternational failed, result=" + resultsAll,
resultsAll.get(resultsAll.size() - 1), mapcodeInternational);
executor.execute(new Runnable() {
@Override
public void run() {
final int count = tasks.incrementAndGet();
if ((count % LOG_LINE_EVERY) == 0) {
LOG.info("encodeDecodeTest: #{}/{}", count, NUMBER_OF_POINTS);
}
// Walk through the list in reverse order to get International first.
for (final Territory territory : Territory.values()) {
final List<Mapcode> resultsLimited = MapcodeCodec.encode(latDeg, lonDeg, territory);
for (final Mapcode mapcode : resultsLimited) {
// Check if the territory matches.
assertEquals(territory, mapcode.getTerritory());
// Check max distance at every nrDigits, and verify encode(decode(m))=m
for (int nrDigits = 0; nrDigits <= 8; nrDigits++) {
final String codePrecision = mapcode.getCode(nrDigits);
final Point decodeLocation;
try {
decodeLocation = MapcodeCodec.decode(codePrecision, territory);
} catch (final UnknownMapcodeException e) {
LOG.error("FAILED {} Decode({} {}) generated from ({}, {})", count, codePrecision, territory, latDeg, lonDeg);
LOG.error("encodeDecodeTest: Unknown mapcode exception", e);
errors.getAndIncrement();
continue;
}
final double distance = Point.distanceInMeters(encode, decodeLocation);
if (distance >= Mapcode.getSafeMaxOffsetInMeters(nrDigits)) {
LOG.error("encodeDecodeTest: {} digits = {} distance = {} >= {}", mapcode, nrDigits, distance,
Mapcode.getSafeMaxOffsetInMeters(nrDigits));
errors.getAndIncrement();
} else {
// check that decode can be encoded back to original
final List<Mapcode> recodedMapcodes = MapcodeCodec.encode(decodeLocation, territory);
{
boolean found = false;
for (final Mapcode candidate : recodedMapcodes) {
if (codePrecision.equals(candidate.getCode(nrDigits))) {
found = true;
break;
}
}
if (!found) {
// perhaps it was inherited from the parent?
final Territory parentTerritory = territory.getParentTerritory();
if (parentTerritory != null) {
final List<Mapcode> recodedMapcodesFromParent = MapcodeCodec.encode(decodeLocation, parentTerritory);
for (final Mapcode candidate : recodedMapcodesFromParent) {
if (codePrecision.equals(candidate.getCode(nrDigits))) {
found = true;
break;
}
}
}
if (!found) {
if (!MapcodeCodec.isNearMultipleBorders(decodeLocation, territory)) { // but should be found!
LOG.error("Re-encode{} of {} failed for {} {} from ({},{})", nrDigits, decodeLocation, territory,
codePrecision, latDeg, lonDeg);
errors.getAndIncrement();
for (final Mapcode candidate : recodedMapcodes) {
LOG.info(" * candidate: {}", candidate.getCode(nrDigits));
}
}
}
}
}
}
}
// Check conversion from/to alphabets.
for (final Alphabet alphabet : Alphabet.values()) {
final String mapcodeAlphabet = mapcode.getCode(alphabet);
final String mapcodeAscii = Mapcode.convertStringToPlainAscii(mapcodeAlphabet);
if (!mapcode.getCode(0).equals(mapcodeAscii)) {
LOG.error("encodeDecodeTest: {} alphabet={}, original={}, mapcodeAlphabet={}, mapcodeAscii={}",
mapcode, alphabet, mapcode.getCode(0), mapcodeAlphabet, mapcodeAscii);
errors.incrementAndGet();
}
}
}
}
}
});
}
executor.shutdown();
executor.awaitTermination(60, TimeUnit.SECONDS);
assertEquals("Found errors", 0, errors.get());
LOG.info("encodeDecodeTest: Executed {} tasks", tasks);
}
}