|
1 | 1 | /* |
2 | | - * Copyright 2019, 2022 Uber Technologies, Inc. |
| 2 | + * Copyright 2019, 2022-2023 Uber Technologies, Inc. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 | package com.uber.h3core; |
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertNotEquals; |
19 | 20 | import static org.junit.Assert.assertTrue; |
| 21 | +import static org.junit.Assert.fail; |
20 | 22 |
|
21 | 23 | import com.google.common.collect.ImmutableList; |
22 | 24 | import com.uber.h3core.exceptions.H3Exception; |
| 25 | +import com.uber.h3core.util.LatLng; |
23 | 26 | import java.util.ArrayList; |
24 | 27 | import java.util.Collection; |
25 | 28 | import java.util.HashSet; |
@@ -74,8 +77,8 @@ public void testH3ToChildren() { |
74 | 77 |
|
75 | 78 | // res 0 pentagon has 5 hexagon children and 1 pentagon child at res 1. |
76 | 79 | // Total output will be: |
77 | | - // 5 * 7 children of res 1 hexagons |
78 | | - // 6 children of res 1 pentagon |
| 80 | + // 5 * 7 children of res 1 hexagons |
| 81 | + // 6 children of res 1 pentagon |
79 | 82 | assertEquals(5 * 7 + 6, pentagonChildren.size()); |
80 | 83 |
|
81 | 84 | // Don't crash |
@@ -188,4 +191,105 @@ public void testH3ToCenterChildNegative() { |
188 | 191 | public void testH3ToCenterChildOutOfRange() { |
189 | 192 | h3.cellToCenterChild("8928308280fffff", 16); |
190 | 193 | } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void testCellToChildPos() { |
| 197 | + assertEquals(0, h3.cellToChildPos(0x88283080ddfffffL, 8)); |
| 198 | + assertEquals(6, h3.cellToChildPos(0x88283080ddfffffL, 7)); |
| 199 | + assertEquals(41, h3.cellToChildPos("88283080ddfffff", 6)); |
| 200 | + } |
| 201 | + |
| 202 | + @Test(expected = H3Exception.class) |
| 203 | + public void testCellToChildPosError() { |
| 204 | + h3.cellToChildPos(0x88283080ddfffffL, 9); |
| 205 | + } |
| 206 | + |
| 207 | + @Test(expected = H3Exception.class) |
| 208 | + public void testCellToChildPosError2() { |
| 209 | + h3.cellToChildPos("88283080ddfffff", 9); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void childPosToCell() { |
| 214 | + assertEquals(0x88283080ddfffffL, h3.childPosToCell(0, 0x88283080ddfffffL, 8)); |
| 215 | + assertEquals( |
| 216 | + 0x88283080ddfffffL, h3.childPosToCell(6, h3.cellToParent(0x88283080ddfffffL, 7), 8)); |
| 217 | + assertEquals( |
| 218 | + "88283080ddfffff", h3.childPosToCell(41, h3.cellToParentAddress("88283080ddfffff", 6), 8)); |
| 219 | + } |
| 220 | + |
| 221 | + @Test(expected = H3Exception.class) |
| 222 | + public void testChildPosToCellError() { |
| 223 | + h3.childPosToCell(-1, 0x88283080ddfffffL, 9); |
| 224 | + } |
| 225 | + |
| 226 | + @Test(expected = H3Exception.class) |
| 227 | + public void testChildPosToCellError2() { |
| 228 | + h3.childPosToCell(10000, "88283080ddfffff", 9); |
| 229 | + } |
| 230 | + |
| 231 | + @Test |
| 232 | + public void cellToChildPosRoundTrip() { |
| 233 | + // These are somewhat arbitrary, but cover a few different parts of the globe |
| 234 | + List<LatLng> testLatLngs = |
| 235 | + ImmutableList.of( |
| 236 | + new LatLng(37.81331899988944, -122.409290778685), |
| 237 | + new LatLng(64.2868041, 8.7824902), |
| 238 | + new LatLng(5.8815246, 54.3336044), |
| 239 | + new LatLng(-41.4486737, 143.918175)); |
| 240 | + |
| 241 | + for (LatLng ll : testLatLngs) { |
| 242 | + for (int res = 0; res < 16; res++) { |
| 243 | + long child = h3.latLngToCell(ll.lat, ll.lng, res); |
| 244 | + long parent = h3.cellToParent(child, 0); |
| 245 | + long pos = h3.cellToChildPos(child, 0); |
| 246 | + long cell = h3.childPosToCell(pos, parent, res); |
| 247 | + assertNotEquals("sanity check that pos is not a reference to child", child, pos); |
| 248 | + assertEquals("round trip produced the same cell", child, cell); |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + public void childPosAndChildrenSize() { |
| 255 | + // one hexagon, one pentagon |
| 256 | + for (String index : ImmutableList.of("80bffffffffffff", "80a7fffffffffff")) { |
| 257 | + for (int res = 0; res < 16; res++) { |
| 258 | + long count = h3.cellToChildrenSize(index, res); |
| 259 | + assertTrue( |
| 260 | + "count has the right order of magnitude", |
| 261 | + Math.pow(6, res) <= count && count <= Math.pow(7, res)); |
| 262 | + |
| 263 | + String child = h3.childPosToCell(count - 1, index, res); |
| 264 | + long pos = h3.cellToChildPos(child, 0); |
| 265 | + assertEquals("got expected round trip", count - 1, pos); |
| 266 | + |
| 267 | + try { |
| 268 | + h3.childPosToCell(count, index, res); |
| 269 | + fail("Should have thrown, one more is out of range"); |
| 270 | + } catch (H3Exception e) { |
| 271 | + // expected |
| 272 | + assertEquals(2 /* E_DOMAIN */, e.getCode()); |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + @Test(expected = H3Exception.class) |
| 279 | + public void cellToChildrenSizeError() { |
| 280 | + // Invalid resolution |
| 281 | + h3.cellToChildrenSize("88283080ddfffff", 5); |
| 282 | + } |
| 283 | + |
| 284 | + @Test(expected = H3Exception.class) |
| 285 | + public void cellToChildrenSizeError2() { |
| 286 | + // Invalid resolution |
| 287 | + h3.cellToChildrenSize(0x88283080ddfffffL, -1); |
| 288 | + } |
| 289 | + |
| 290 | + @Test(expected = H3Exception.class) |
| 291 | + public void cellToChildrenSizeError3() { |
| 292 | + // Invalid index |
| 293 | + h3.cellToChildrenSize(0xffffffffffffffffL, 9); |
| 294 | + } |
191 | 295 | } |
0 commit comments