Skip to content

Commit 213ec63

Browse files
committed
update java to add integer encoding tests
1 parent 7829f85 commit 213ec63

2 files changed

Lines changed: 46 additions & 17 deletions

File tree

java/src/main/java/com/google/openlocationcode/OpenLocationCode.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,6 @@ public OpenLocationCode(String code) {
188188
* @throws IllegalArgumentException if the code length is not valid.
189189
*/
190190
public OpenLocationCode(double latitude, double longitude, int codeLength) {
191-
// Limit the maximum number of digits in the code.
192-
codeLength = Math.min(codeLength, MAX_DIGIT_COUNT);
193-
// Check that the code length requested is valid.
194-
if (codeLength < PAIR_CODE_LENGTH && codeLength % 2 == 1 || codeLength < MIN_DIGIT_COUNT) {
195-
throw new IllegalArgumentException("Illegal code length " + codeLength);
196-
}
197-
198-
// Compute the code.
199191
long[] integers = degreesToIntegers(latitude, longitude);
200192
this.code = encodeIntegers(integers[0], integers[1], codeLength);
201193
}
@@ -208,7 +200,13 @@ public OpenLocationCode(double latitude, double longitude, int codeLength) {
208200
* @param codeLength The requested number of digits.
209201
* @return The OLC for the location.
210202
*/
211-
private static String encodeIntegers(long lat, long lng, int codeLength) {
203+
static String encodeIntegers(long lat, long lng, int codeLength) {
204+
// Limit the maximum number of digits in the code.
205+
codeLength = Math.min(codeLength, MAX_DIGIT_COUNT);
206+
// Check that the code length requested is valid.
207+
if (codeLength < PAIR_CODE_LENGTH && codeLength % 2 == 1 || codeLength < MIN_DIGIT_COUNT) {
208+
throw new IllegalArgumentException("Illegal code length " + codeLength);
209+
}
212210
// Store the code - we build it in reverse and reorder it afterwards.
213211
StringBuilder revCodeBuilder = new StringBuilder();
214212
// Compute the grid part of the code if necessary.

java/src/test/java/com/google/openlocationcode/EncodingTest.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ public class EncodingTest {
2323

2424
private static class TestData {
2525

26-
private final double latitude;
27-
private final double longitude;
26+
private final double latitudeDegrees;
27+
private final double longitudeDegrees;
28+
private final long latitudeInteger;
29+
private final long longitudeInteger;
2830
private final int length;
2931
private final String code;
3032

@@ -33,10 +35,12 @@ public TestData(String line) {
3335
if (parts.length != 4) {
3436
throw new IllegalArgumentException("Wrong format of testing data.");
3537
}
36-
this.latitude = Double.parseDouble(parts[0]);
37-
this.longitude = Double.parseDouble(parts[1]);
38-
this.length = Integer.parseInt(parts[2]);
39-
this.code = parts[3];
38+
this.latitudeDegrees = Double.parseDouble(parts[0]);
39+
this.longitudeDegrees = Double.parseDouble(parts[1]);
40+
this.latitudeInteger = Long.parseLong(parts[2]);
41+
this.longitudeInteger = Long.parseLong(parts[3]);
42+
this.length = Integer.parseInt(parts[4]);
43+
this.code = parts[5];
4044
}
4145
}
4246

@@ -61,9 +65,36 @@ public void testEncodeFromLatLong() {
6165
Assert.assertEquals(
6266
String.format(
6367
"Latitude %f, longitude %f and length %d were wrongly encoded.",
64-
testData.latitude, testData.longitude, testData.length),
68+
testData.latitudeDegrees, testData.longitudeDegrees, testData.length),
6569
testData.code,
66-
OpenLocationCode.encode(testData.latitude, testData.longitude, testData.length));
70+
OpenLocationCode.encode(testData.latitudeDegrees, testData.longitudeDegrees, testData.length));
71+
}
72+
}
73+
74+
@Test
75+
public void testDegreesToIntegers() {
76+
for (TestData testData : testDataList) {
77+
long[] got = OpenLocationCode.degreesToIntegers(testData.latitudeDegrees, testData.longitudeDegrees);
78+
Assert.assertEquals(
79+
String.format("Latitude %f integer conversion is incorrect", testData.latitudeDegrees),
80+
testData.latitudeInteger,
81+
got[0]);
82+
Assert.assertEquals(
83+
String.format("Longitude %f integer conversion is incorrect", testData.longitudeDegrees),
84+
testData.longitudeInteger,
85+
got[1]);
86+
}
87+
}
88+
89+
@Test
90+
public void testEncodeIntegers() {
91+
for (TestData testData : testDataList) {
92+
Assert.assertEquals(
93+
String.format(
94+
"Latitude %d, longitude %d and length %d were wrongly encoded.",
95+
testData.latitudeInteger, testData.longitudeInteger, testData.length),
96+
testData.code,
97+
OpenLocationCode.encodeIntegers(testData.latitudeInteger, testData.longitudeInteger, testData.length));
6798
}
6899
}
69100
}

0 commit comments

Comments
 (0)