forked from mapcode-foundation/mapcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlphabetTest.java
More file actions
124 lines (105 loc) · 4.9 KB
/
Copy pathAlphabetTest.java
File metadata and controls
124 lines (105 loc) · 4.9 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
/*
* Copyright (C) 2014-2015 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 javax.annotation.Nonnull;
import static org.junit.Assert.assertEquals;
@SuppressWarnings({"OverlyBroadThrowsClause", "ProhibitedExceptionDeclared"})
public class AlphabetTest {
private static final Logger LOG = LoggerFactory.getLogger(AlphabetTest.class);
@Test
public void testConvertToAlphabet() throws Exception {
LOG.info("testConvertToAlphabet");
convertCodeInAlphabet("\u0397\u03a0.\u03982", Territory.GRC, Alphabet.GREEK, 0);
convertCodeInAlphabet("\u0397\u03a0.\u03982-\u03a62", Territory.GRC, Alphabet.GREEK, 2);
convertCodeInAlphabet("GRC \u0397\u03a0.\u03982-\u03a62", Territory.GRC, Alphabet.GREEK, 2);
String code = "26.53UK";
String codeGreek = Mapcode.convertStringToAlphabet(code, Alphabet.GREEK);
String codeAscii = Mapcode.convertStringToPlainAscii(codeGreek);
LOG.info("code = {}, codeGreek = {}, codeAscii = {}", code, codeGreek, codeAscii);
assertEquals(code, codeAscii);
code = "000.EU";
codeGreek = Mapcode.convertStringToAlphabet(code, Alphabet.GREEK);
codeAscii = Mapcode.convertStringToPlainAscii(codeGreek);
LOG.info("code = {}, codeGreek = {}, codeAscii = {}", code, codeGreek, codeAscii);
assertEquals(codeGreek, "\u0391\u0030\u0030.23");
assertEquals(code, codeAscii);
}
@Test
public void testFromString() throws Exception {
LOG.info("testFromString");
assertEquals(Alphabet.ROMAN, Alphabet.fromString("ROMAN"));
assertEquals(Alphabet.ROMAN, Alphabet.fromString("roman"));
for (final Alphabet alphabet : Alphabet.values()) {
assertEquals(alphabet, Alphabet.fromString(alphabet.toString()));
}
}
@Test(expected = UnknownAlphabetException.class)
public void testFromStringNumeric() throws Exception {
LOG.info("testFromStringNumeric");
assertEquals(Alphabet.ROMAN, Alphabet.fromString("0"));
}
@Test(expected = IllegalArgumentException.class)
public void testFromStringError1() {
LOG.info("testFromStringError1");
Alphabet.fromString("");
}
@Test(expected = IllegalArgumentException.class)
public void testFromStringError2() {
LOG.info("testFromStringError2");
Alphabet.fromString("1A");
}
@Test(expected = IllegalArgumentException.class)
public void testFromStringError3() {
LOG.info("testFromStringError3");
Alphabet.fromString("99");
}
@Test(expected = IllegalArgumentException.class)
public void testFromStringError4() {
LOG.info("testFromStringError4");
Alphabet.fromString("ROMAN1");
}
private static void convertCodeInAlphabet(
@Nonnull final String code,
@Nonnull final Territory territory, @Nonnull final Alphabet alphabet,
final int precision) throws Exception {
// Check type.
final int type = Mapcode.getPrecisionFormat(code);
assertEquals("code = " + code + ", type = " + type, precision, type);
// Check original code and converted to ASCII point at same location.
final String codeAscii = Mapcode.convertStringToPlainAscii(code);
final Point pointCode = MapcodeCodec.decode(code, territory);
final Point pointAscii = MapcodeCodec.decode(codeAscii, territory);
assertEquals("code = " + code + ", pointCode = " + pointCode + ", pointAscii = " + pointAscii,
pointCode, pointAscii);
// Check if it re-encodes to the same mapcode codes.
final Mapcode mapcode = MapcodeCodec.encodeToShortest(pointCode, territory);
final String codeRoman;
final String codeAlphabet;
if (Mapcode.containsTerritory(code)) {
codeRoman = mapcode.getCodeWithTerritory(precision);
codeAlphabet = mapcode.getCodeWithTerritory(precision, alphabet);
} else {
codeRoman = mapcode.getCode(precision);
codeAlphabet = mapcode.getCode(precision, alphabet);
}
LOG.info("code = {}, codeAlphabet = {}, codeAscii = {}, codeRoman = {}", code, codeAlphabet, codeAscii, codeRoman);
assertEquals(codeAscii, codeRoman);
assertEquals(code, codeAlphabet);
}
}