Skip to content

Commit df0530c

Browse files
committed
Test against the upstream inputfiles
1 parent 4e16a51 commit df0530c

4 files changed

Lines changed: 249 additions & 0 deletions

File tree

src/test/java/com/uber/h3core/BaseTestH3Core.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.uber.h3core;
1717

18+
import com.uber.h3core.util.LatLng;
1819
import java.io.IOException;
1920
import org.junit.jupiter.api.BeforeAll;
2021

@@ -28,4 +29,20 @@ public abstract class BaseTestH3Core {
2829
public static void setup() throws IOException {
2930
h3 = H3Core.newInstance();
3031
}
32+
33+
public static boolean floatEquals(double f1, double f2) {
34+
return floatEquals(f1, f2, EPSILON);
35+
}
36+
37+
public static boolean floatEquals(double f1, double f2, double epsilon) {
38+
return (Math.abs(f1 - f2) < epsilon);
39+
}
40+
41+
public static boolean latLngEquals(LatLng l1, LatLng l2) {
42+
return latLngEquals(l1, l2, EPSILON);
43+
}
44+
45+
public static boolean latLngEquals(LatLng l1, LatLng l2, double epsilon) {
46+
return floatEquals(l1.lat, l2.lat, epsilon) && floatEquals(l1.lng, l2.lng, epsilon);
47+
}
3148
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2017-2018 Uber Technologies, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.uber.h3core.inputfiles;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
21+
import com.uber.h3core.BaseTestH3Core;
22+
import com.uber.h3core.util.LatLng;
23+
import java.io.IOException;
24+
import java.nio.file.FileSystems;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.PathMatcher;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import java.util.Scanner;
31+
import org.junit.jupiter.api.Test;
32+
33+
/** Test input files from core library. */
34+
class TestCellToBoundary extends BaseTestH3Core {
35+
private static void testFile(Path path) {
36+
try {
37+
try (Scanner in = new Scanner(path, "UTF-8")) {
38+
while (in.hasNext()) {
39+
String cell = in.next();
40+
41+
String start = in.next();
42+
assertEquals("{", start);
43+
44+
List<LatLng> verts = new ArrayList<>();
45+
while (in.hasNextDouble()) {
46+
double lat = in.nextDouble();
47+
double lng = in.nextDouble();
48+
49+
verts.add(new LatLng(lat, lng));
50+
}
51+
52+
String end = in.next();
53+
assertEquals("}", end);
54+
assertTrue(verts.size() > 0);
55+
assertTrue(verts.size() <= 10);
56+
57+
List<LatLng> actual = h3.cellToBoundary(cell);
58+
assertEquals(verts.size(), actual.size());
59+
for (int i = 0; i < verts.size(); i++) {
60+
assertTrue(latLngEquals(verts.get(i), actual.get(i)));
61+
}
62+
}
63+
}
64+
} catch (IOException ioe) {
65+
throw new RuntimeException(ioe);
66+
}
67+
}
68+
69+
@Test
70+
void test() throws IOException {
71+
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*cells.txt");
72+
assertEquals(
73+
39,
74+
Files.walk(Path.of("target/h3/tests/inputfiles"), 1)
75+
.filter(
76+
(path) -> {
77+
if (matcher.matches(path)) {
78+
testFile(path);
79+
80+
return true;
81+
} else {
82+
return false;
83+
}
84+
})
85+
.count());
86+
}
87+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2026 Uber Technologies, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.uber.h3core.inputfiles;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
21+
import com.uber.h3core.BaseTestH3Core;
22+
import com.uber.h3core.util.LatLng;
23+
import java.io.IOException;
24+
import java.nio.file.FileSystems;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.PathMatcher;
28+
import java.util.Scanner;
29+
import org.junit.jupiter.api.Test;
30+
31+
/** Test input files from core library. */
32+
class TestCellToLatLng extends BaseTestH3Core {
33+
private static void testFile(Path path) {
34+
try {
35+
try (Scanner in = new Scanner(path, "UTF-8")) {
36+
while (in.hasNext()) {
37+
String cell = in.next();
38+
double lat = in.nextDouble();
39+
double lng = in.nextDouble();
40+
41+
LatLng actual = h3.cellToLatLng(cell);
42+
43+
double epsilon = 0.000001 * Math.PI / 180.0;
44+
45+
assertTrue(latLngEquals(actual, new LatLng(lat, lng), epsilon));
46+
47+
int res = h3.getResolution(cell);
48+
String cell2 = h3.latLngToCellAddress(actual.lat, actual.lng, res);
49+
assertEquals(cell, cell2);
50+
}
51+
}
52+
} catch (IOException ioe) {
53+
throw new RuntimeException(ioe);
54+
}
55+
}
56+
57+
@Test
58+
void test() throws IOException {
59+
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/res*ic.txt");
60+
assertEquals(
61+
5,
62+
Files.walk(Path.of("target/h3/tests/inputfiles"), 1)
63+
.filter(
64+
(path) -> {
65+
if (matcher.matches(path)) {
66+
testFile(path);
67+
68+
return true;
69+
} else {
70+
return false;
71+
}
72+
})
73+
.count());
74+
}
75+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2017-2018 Uber Technologies, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.uber.h3core.inputfiles;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import com.uber.h3core.BaseTestH3Core;
21+
import java.io.IOException;
22+
import java.nio.file.FileSystems;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.PathMatcher;
26+
import java.util.Scanner;
27+
import org.junit.jupiter.api.Test;
28+
29+
/** Test input files from core library. */
30+
class TestLatLngToCell extends BaseTestH3Core {
31+
private static void testFile(Path path) {
32+
try {
33+
try (Scanner in = new Scanner(path, "UTF-8")) {
34+
while (in.hasNext()) {
35+
String cell = in.next();
36+
double lat = in.nextDouble();
37+
double lng = in.nextDouble();
38+
39+
int res = h3.getResolution(cell);
40+
41+
String cell2 = h3.latLngToCellAddress(lat, lng, res);
42+
43+
assertEquals(cell, cell2);
44+
}
45+
}
46+
} catch (IOException ioe) {
47+
throw new RuntimeException(ioe);
48+
}
49+
}
50+
51+
@Test
52+
void test() throws IOException {
53+
PathMatcher matcher =
54+
FileSystems.getDefault().getPathMatcher("glob:**/{bc*centers,rand*centers}.txt");
55+
assertEquals(
56+
35,
57+
Files.walk(Path.of("target/h3/tests/inputfiles"), 1)
58+
.filter(
59+
(path) -> {
60+
if (matcher.matches(path)) {
61+
testFile(path);
62+
63+
return true;
64+
} else {
65+
return false;
66+
}
67+
})
68+
.count());
69+
}
70+
}

0 commit comments

Comments
 (0)