Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix java encoding to accept codeLength of 2 and add test cases #362

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public OpenLocationCode(double latitude, double longitude, int codeLength) {
// Limit the maximum number of digits in the code.
codeLength = Math.min(codeLength, MAX_DIGIT_COUNT);
// Check that the code length requested is valid.
if (codeLength < PAIR_CODE_LENGTH && codeLength % 2 == 1 || codeLength < 4) {
if (codeLength < PAIR_CODE_LENGTH && codeLength % 2 == 1 || codeLength < 2) {
throw new IllegalArgumentException("Illegal code length " + codeLength);
}
// Ensure that latitude and longitude are valid.
Expand Down
14 changes: 13 additions & 1 deletion java/src/test/java/com/google/openlocationcode/EncodingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private static class TestData {
private final String code;

public TestData(String line) {
String[] parts = line.split(",");
String[] parts = line.split(",", -1);
if (parts.length != 4) {
throw new IllegalArgumentException("Wrong format of testing data.");
}
Expand Down Expand Up @@ -58,6 +58,9 @@ public void setUp() throws Exception {
@Test
public void testEncodeFromLatLong() {
for (TestData testData : testDataList) {
if (testData.code.isEmpty()) {
continue;
}
Assert.assertEquals(
String.format(
"Latitude %f, longitude %f and length %d were wrongly encoded.",
Expand All @@ -66,4 +69,13 @@ public void testEncodeFromLatLong() {
OpenLocationCode.encode(testData.latitude, testData.longitude, testData.length));
}
}

@Test(expected = IllegalArgumentException.class)
public void testEncodeThrowsIllegalArgumentException() {
for (TestData testData : testDataList) {
if (testData.code.isEmpty()) {
OpenLocationCode.encode(testData.latitude, testData.longitude, testData.length);
}
}
}
}
22 changes: 22 additions & 0 deletions test_data/encoding.csv
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@
1,1,11,6FH32222+222
################################################################################
#
# Test length 2 passes
#
################################################################################
20.5,2.5,2,7F000000+
################################################################################
#
# Test length 0 fails
#
################################################################################
20.5,2.5,0,
################################################################################
#
# Test short odd lengths fail
#
################################################################################
20.5,2.5,9,
20.5,2.5,7,
20.5,2.5,5,
20.5,2.5,3,
20.5,2.5,1,
################################################################################
#
# Special cases over 90 latitude and 180 longitude
#
################################################################################
Expand Down