Repository navigation
Expand file tree
/
Copy pathEncoderTest.java
More file actions
352 lines (304 loc) · 16.7 KB
/
Copy pathEncoderTest.java
File metadata and controls
352 lines (304 loc) · 16.7 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* 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.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import static org.junit.Assert.*;
@SuppressWarnings({"OverlyBroadThrowsClause", "ProhibitedExceptionDeclared", "ValueOfIncrementOrDecrementUsed", "MagicNumber"})
public class EncoderTest {
private static final Logger LOG = LoggerFactory.getLogger(EncoderTest.class);
@Test
public void encodePoint() {
LOG.info("encodeMostResults");
assertEquals(MapcodeCodec.encode(52.5, 5.2), MapcodeCodec.encode(Point.fromDeg(52.5, 5.2)));
}
@Test
public void encodeToInterInternationalnational() {
LOG.info("encodeToInternational");
assertEquals("VHYCC.2FWB", MapcodeCodec.encodeToInternational(52.5, 5.2).getCode());
assertEquals("VHYCC.2FWB", MapcodeCodec.encodeToInternational(Point.fromDeg(52.5, 5.2)).getCode());
}
@Test
public void nearMultipleBorders() {
LOG.info("nearMultipleBorders");
assertFalse(MapcodeCodec.isNearMultipleBorders(Point.fromDeg(52.175616, 4.577179), Territory.NLD));
}
@Test
public void encodeMostResults() {
LOG.info("encodeMostResults");
final double lat = 26.904899;
final double lon = 95.138515;
final List<Mapcode> results = MapcodeCodec.encode(lat, lon);
assertEquals(21, results.size());
int i = 0;
assertEquals("MMR 9Z3R.YKP", results.get(i++).getCodeWithTerritory());
assertEquals("IN-NL WKS.H6", results.get(i++).getCodeWithTerritory());
assertEquals("IN-NL X57.Q6V", results.get(i++).getCodeWithTerritory());
assertEquals("IN-NL W7Y.9WBB", results.get(i++).getCodeWithTerritory());
assertEquals("IN-NL YT0.W584", results.get(i++).getCodeWithTerritory());
assertEquals("IN-NL XBN8.W4TD", results.get(i++).getCodeWithTerritory());
assertEquals("IN-AR 51.XYW3", results.get(i++).getCodeWithTerritory());
assertEquals("IN-AR W7Y.9WBB", results.get(i++).getCodeWithTerritory());
assertEquals("IN-AR YT0.W584", results.get(i++).getCodeWithTerritory());
assertEquals("IN-AR XBN8.W4TD", results.get(i++).getCodeWithTerritory());
assertEquals("IN-AS SC86.C8", results.get(i++).getCodeWithTerritory());
assertEquals("IN-AS W7Y.9WBB", results.get(i++).getCodeWithTerritory());
assertEquals("IN-AS YT0.W584", results.get(i++).getCodeWithTerritory());
assertEquals("IN-AS XBN8.W4TD", results.get(i++).getCodeWithTerritory());
assertEquals("IND W7Y.9WBB", results.get(i++).getCodeWithTerritory());
assertEquals("IND YT0.W584", results.get(i++).getCodeWithTerritory());
assertEquals("IND XBN8.W4TD", results.get(i++).getCodeWithTerritory());
assertEquals("CN-XZ SZW8.2TR", results.get(i++).getCodeWithTerritory());
assertEquals("CN-XZ KQLF.C2K7", results.get(i++).getCodeWithTerritory());
assertEquals("CHN KQLF.C2K7", results.get(i++).getCodeWithTerritory());
assertEquals("AAA PRP60.0RVD", results.get(i++).getCodeWithTerritory());
assertEquals(i, results.size());
// retrieve at different precisions
i = 0;
assertEquals("MMR 9Z3R.YKP-B4HPXXKP", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-NL WKS.H6-32112113", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-NL X57.Q6V-L4WW00TS", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-NL W7Y.9WBB-NQD30000", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-NL YT0.W584-FL321003", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-NL XBN8.W4TD-WL8H46QJ", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-AR 51.XYW3-6DG4", results.get(i++).getCodeWithTerritory(4));
assertEquals("IN-AR W7Y.9WBB-NQD30000", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-AR YT0.W584-FL321003", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-AR XBN8.W4TD-WL", results.get(i++).getCodeWithTerritory(2));
assertEquals("IN-AS SC86.C8-5TQ30000", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-AS W7Y.9WBB-NQD30000", results.get(i++).getCodeWithTerritory(8));
assertEquals("IN-AS YT0.W584-F", results.get(i++).getCodeWithTerritory(1));
assertEquals("IN-AS XBN8.W4TD-WL8H46QJ", results.get(i++).getCodeWithTerritory(8));
assertEquals("IND W7Y.9WBB", results.get(i++).getCodeWithTerritory(0));
assertEquals("IND YT0.W584-FL", results.get(i++).getCodeWithTerritory(2));
assertEquals("IND XBN8.W4TD-WL8", results.get(i++).getCodeWithTerritory(3));
assertEquals("CN-XZ SZW8.2TR-VJFX4DX", results.get(i++).getCodeWithTerritory(7));
assertEquals("CN-XZ KQLF.C2K7-FG2S8QMK", results.get(i++).getCodeWithTerritory(8));
assertEquals("CHN KQLF.C2K7-FG2S8", results.get(i++).getCodeWithTerritory(5));
assertEquals("AAA PRP60.0RVD-VK1X5LHD", results.get(i++).getCodeWithTerritory(8));
assertEquals(i, results.size());
}
@Test
public void encodeWithTerritory() {
LOG.info("encodeWithTerritory");
final double lat = 52.376514;
final double lon = 4.908542;
final List<Mapcode> results = MapcodeCodec.encode(lat, lon, Territory.NLD);
assertEquals(4, results.size());
assertEquals("NLD 49.4V", results.get(0).getCodeWithTerritory());
assertEquals("NLD G9.VWG", results.get(1).getCodeWithTerritory());
assertEquals("NLD DL6.H9L", results.get(2).getCodeWithTerritory());
assertEquals("NLD P25Z.N3Z", results.get(3).getCodeWithTerritory());
}
@Test
public void encodeWithoutTerritory1() {
LOG.info("encodeWithoutTerritory1");
final double lat = 52.376514;
final double lon = 4.908542;
final List<Mapcode> results = MapcodeCodec.encode(lat, lon);
assertEquals(5, results.size());
assertEquals("NLD 49.4V", results.get(0).getCodeWithTerritory());
assertEquals("NLD G9.VWG", results.get(1).getCodeWithTerritory());
assertEquals("NLD DL6.H9L", results.get(2).getCodeWithTerritory());
assertEquals("NLD P25Z.N3Z", results.get(3).getCodeWithTerritory());
assertEquals("AAA VHXGB.1J9J", results.get(4).getCodeWithTerritory());
}
@Test
public void encodeWithoutTerritory2() {
LOG.info("encodeWithoutTerritory2");
final double lat = 26.87016;
final double lon = 75.847;
final List<Mapcode> results = MapcodeCodec.encode(lat, lon);
assertEquals(10, results.size());
assertEquals("IN-RJ XX.XX", results.get(0).getCodeWithTerritory());
assertEquals("IN-RJ 6X.NHG", results.get(1).getCodeWithTerritory());
assertEquals("IN-RJ KH9.FGV", results.get(2).getCodeWithTerritory());
assertEquals("IN-RJ H8M.6FTF", results.get(3).getCodeWithTerritory());
assertEquals("IN-RJ 8BZ9.D61B", results.get(4).getCodeWithTerritory());
assertEquals("IN-MP H8M.6FTF", results.get(5).getCodeWithTerritory());
assertEquals("IN-MP 8BZ9.D61B", results.get(6).getCodeWithTerritory());
assertEquals("IND H8M.6FTF", results.get(7).getCodeWithTerritory());
assertEquals("IND 8BZ9.D61B", results.get(8).getCodeWithTerritory());
assertEquals("AAA PQ0PF.5M1H", results.get(9).getCodeWithTerritory());
}
@Test
public void encodeToInternational1() {
LOG.info("encodeToInternational1");
final double lat = 52.376514;
final double lon = 4.908542;
final Mapcode result = MapcodeCodec.encodeToInternational(lat, lon);
assertEquals("VHXGB.1J9J", result.getCode());
}
@Test
public void encodeToInternational2() {
LOG.info("encodeToInternational2");
final double lat = 26.87016;
final double lon = 75.847;
final Mapcode result = MapcodeCodec.encodeToInternational(lat, lon);
assertEquals("PQ0PF.5M1H", result.getCode());
}
@Test
public void encodeToShortest1() throws Exception {
LOG.info("encodeToShortest1");
final double lat = 52.376514;
final double lon = 4.908542;
final Mapcode result = MapcodeCodec.encodeToShortest(lat, lon, Territory.NLD);
assertEquals("NLD 49.4V", result.getCodeWithTerritory());
// test extremely precise encoding
{
double lat2 = 52.3;
double lon2 = 4.908;
Mapcode m = MapcodeCodec.encodeToShortest(lat2, lon2, Territory.fromString("NLD"));
assertEquals("NLD GG.NBC-SHR33333", m.getCodeWithTerritory(8));
lat2 = 52.3000004;
lon2 = 4.9080004;
m = MapcodeCodec.encodeToShortest(lat2, lon2, Territory.fromString("NLD"));
assertEquals("NLD GG.NBC-SHSS1010", m.getCodeWithTerritory(8));
lat2 = 52.299999999;
lon2 = 4.907999999;
m = MapcodeCodec.encodeToShortest(lat2, lon2, Territory.fromString("NLD"));
assertEquals("NLD GG.NBC-SHLWXWQB", m.getCodeWithTerritory(8));
lat2 = 52.29993200000;
lon2 = 4.90786600000;
m = MapcodeCodec.encodeToShortest(lat2, lon2, Territory.fromString("NLD"));
assertEquals("NLD GG.NBC-00000000", m.getCodeWithTerritory(8));
lat2 = 52.29993200000;
lon2 = 4.9078659999999;
m = MapcodeCodec.encodeToShortest(lat2, lon2, Territory.fromString("NLD"));
assertEquals("NLD GG.N98-45454545", m.getCodeWithTerritory(8));
}
}
@Test
public void encodeToShortest2() throws Exception {
LOG.info("encodeToShortest2");
final double lat = 26.87016;
final double lon = 75.847;
final Mapcode result = MapcodeCodec.encodeToShortest(lat, lon, Territory.IN_RJ);
assertEquals("IN-RJ XX.XX", result.getCodeWithTerritory());
}
@Test
public void encodeToShortest3() throws Exception {
LOG.info("encodeToShortest3");
final double lat = 52.376514;
final double lon = 4.908542;
final Mapcode result = MapcodeCodec.encodeToShortest(lat, lon, Territory.NLD);
assertEquals("NLD 49.4V", result.getCodeWithTerritory());
}
@Test(expected = UnknownMapcodeException.class)
public void encodeToShortest4() throws Exception {
LOG.info("encodeToShortest4");
final double lat = 26.87016;
final double lon = 75.847;
MapcodeCodec.encodeToShortest(lat, lon, Territory.NLD);
}
@SuppressWarnings("MisorderedAssertEqualsArguments")
@Test
public void legalArguments() {
LOG.info("legalArguments");
assertEquals(MapcodeCodec.encode(-90, 0).size(), 3); // 2 x ATA and AAA
assertEquals(MapcodeCodec.encode(-60, 0).size(), 2); // ATA and AAA
assertEquals(MapcodeCodec.encode(90, 0).size(), 1); // AAA only
assertEquals(MapcodeCodec.encode(0, -180), MapcodeCodec.encode(0, 180));
assertEquals(MapcodeCodec.encode(-91, 0), MapcodeCodec.encode(-90, 0));
assertEquals(MapcodeCodec.encode(91, 0), MapcodeCodec.encode(90, 0));
assertEquals(MapcodeCodec.encode(0, -181), MapcodeCodec.encode(0, 179));
assertEquals(MapcodeCodec.encode(0, 181), MapcodeCodec.encode(0, -179));
}
private static final Point CIUDAD_JUAREZ = Point.fromDeg(31.7, -106.5);
private static final Point VAALS = Point.fromDeg(50.8, 6.0);
@Test
public void testEncodeCiudadJuarez() {
LOG.info("testEncodeCiudadJuarez");
final List<Mapcode> mapcodes = MapcodeCodec.encode(CIUDAD_JUAREZ);
assertEquals(13, mapcodes.size());
assertEquals("MX-CHH 5S.0G", mapcodes.get(0).toString());
}
@Test
public void testEncodeRestrictToCountryISOCountryWithSubdivision() {
LOG.info("testEncodeRestrictToCountryISOCountryWithSubdivision");
final List<Mapcode> mapcodesMX = MapcodeCodec.encodeRestrictToCountryISO2(CIUDAD_JUAREZ, "MX");
final List<Mapcode> mapcodesMX2 = MapcodeCodec.encodeRestrictToCountryISO(CIUDAD_JUAREZ, "MX");
final List<Mapcode> mapcodesMEX = MapcodeCodec.encodeRestrictToCountryISO3(CIUDAD_JUAREZ, "MEX");
final List<Mapcode> mapcodesMEX2 = MapcodeCodec.encodeRestrictToCountryISO(CIUDAD_JUAREZ, "MEX");
assertEquals(7, mapcodesMX.size());
assertEquals(7, mapcodesMX2.size());
assertEquals(7, mapcodesMEX.size());
assertEquals(7, mapcodesMEX2.size());
assertEquals("MX-CHH 5S.0G", mapcodesMX.get(0).toString());
assertEquals("MX-CHH 5S.0G", mapcodesMX2.get(0).toString());
assertEquals("MX-CHH 5S.0G", mapcodesMEX.get(0).toString());
assertEquals("MX-CHH 5S.0G", mapcodesMEX2.get(0).toString());
final List<Mapcode> mapcodesUS = MapcodeCodec.encodeRestrictToCountryISO2(CIUDAD_JUAREZ, "us");
final List<Mapcode> mapcodesUS2 = MapcodeCodec.encodeRestrictToCountryISO(CIUDAD_JUAREZ, "us");
final List<Mapcode> mapcodesUSA = MapcodeCodec.encodeRestrictToCountryISO3(CIUDAD_JUAREZ, "usa");
final List<Mapcode> mapcodesUSA2 = MapcodeCodec.encodeRestrictToCountryISO(CIUDAD_JUAREZ, "usa");
assertEquals(5, mapcodesUS.size());
assertEquals(5, mapcodesUS2.size());
assertEquals(5, mapcodesUSA.size());
assertEquals(5, mapcodesUSA2.size());
assertEquals("US-NM T1DZ.338", mapcodesUS.get(0).toString());
assertEquals("US-NM T1DZ.338", mapcodesUS2.get(0).toString());
assertEquals("US-NM T1DZ.338", mapcodesUSA.get(0).toString());
assertEquals("US-NM T1DZ.338", mapcodesUSA2.get(0).toString());
}
@Test
public void testEncodeRestrictToCountryISOCountryWithoutSubdivision() {
LOG.info("testEncodeRestrictToCountryISO2CountryWithoutSubdivision");
final List<Mapcode> mapcodesNL = MapcodeCodec.encodeRestrictToCountryISO2(VAALS, "NL");
final List<Mapcode> mapcodesNL2 = MapcodeCodec.encodeRestrictToCountryISO(VAALS, "NL");
final List<Mapcode> mapcodesNLD = MapcodeCodec.encodeRestrictToCountryISO3(VAALS, "NLD");
final List<Mapcode> mapcodesNLD2 = MapcodeCodec.encodeRestrictToCountryISO(VAALS, "NLD");
assertEquals(2, mapcodesNL.size());
assertEquals(2, mapcodesNL2.size());
assertEquals(2, mapcodesNLD.size());
assertEquals(2, mapcodesNLD2.size());
assertEquals("NLD ZNV.W78", mapcodesNL.get(0).toString());
assertEquals("NLD ZNV.W78", mapcodesNL2.get(0).toString());
assertEquals("NLD ZNV.W78", mapcodesNLD.get(0).toString());
assertEquals("NLD ZNV.W78", mapcodesNLD2.get(0).toString());
final List<Mapcode> mapcodesBE = MapcodeCodec.encodeRestrictToCountryISO2(VAALS, "be");
final List<Mapcode> mapcodesBE2 = MapcodeCodec.encodeRestrictToCountryISO(VAALS, "be");
final List<Mapcode> mapcodesBEL = MapcodeCodec.encodeRestrictToCountryISO3(VAALS, "bel");
final List<Mapcode> mapcodesBEL2 = MapcodeCodec.encodeRestrictToCountryISO(VAALS, "bel");
assertEquals(2, mapcodesBE.size());
assertEquals(2, mapcodesBE2.size());
assertEquals(2, mapcodesBEL.size());
assertEquals(2, mapcodesBEL2.size());
assertEquals("BEL DRQ.PNK", mapcodesBE.get(0).toString());
assertEquals("BEL DRQ.PNK", mapcodesBE2.get(0).toString());
assertEquals("BEL DRQ.PNK", mapcodesBEL.get(0).toString());
assertEquals("BEL DRQ.PNK", mapcodesBEL2.get(0).toString());
}
@Test
public void testEncodeRestrictToCountryISOForAll() {
LOG.info("testEncodeRestrictToCountryISOForAll");
for (final Territory territory : Territory.values()) {
if (!"AAA".equals(territory.toString())) {
final String countryISO3;
if (territory.getParentTerritory() == null) {
countryISO3 = territory.toString();
} else {
countryISO3 = territory.getParentTerritory().toString();
}
final String countryISO2 = Territory.getCountryISO2FromISO3(countryISO3);
assertNotNull(MapcodeCodec.encodeRestrictToCountryISO2(CIUDAD_JUAREZ, countryISO2));
assertNotNull(MapcodeCodec.encodeRestrictToCountryISO3(CIUDAD_JUAREZ, countryISO3));
}
}
}
}