Skip to content

Commit fd0f8af

Browse files
author
Nick Rabinowitz
authored
Merge pull request #380 from nrabinowitz/edge-tests
Add exhaustive tests and benchmark for getH3UnidirectionalEdgeBoundary
2 parents 5ecee88 + 73eb456 commit fd0f8af

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ dev-docs/Doxyfile
6666
# CMake generated
6767
CMakeFiles
6868
CMakeCache.txt
69+
CPackConfig.cmake
70+
CPackSourceConfig.cmake
6971
Makefile
7072
cmake_install.cmake
7173
install_manifest.txt

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ set(OTHER_SOURCE_FILES
167167
src/apps/testapps/testVec2d.c
168168
src/apps/testapps/testVec3d.c
169169
src/apps/testapps/testH3UniEdge.c
170+
src/apps/testapps/testH3UniEdgeExhaustive.c
170171
src/apps/testapps/testLinkedGeo.c
171172
src/apps/testapps/mkRandGeo.c
172173
src/apps/testapps/testH3Api.c
@@ -191,6 +192,7 @@ set(OTHER_SOURCE_FILES
191192
src/apps/benchmarks/benchmarkH3SetToLinkedGeo.c
192193
src/apps/benchmarks/benchmarkKRing.c
193194
src/apps/benchmarks/benchmarkH3Line.c
195+
src/apps/benchmarks/benchmarkH3UniEdge.c
194196
src/apps/benchmarks/benchmarkH3Api.c)
195197

196198
set(ALL_SOURCE_FILES
@@ -580,6 +582,7 @@ if(BUILD_TESTING)
580582

581583
# The "Exhaustive" part of the test name is used by the test-fast to exclude these files.
582584
# test-fast exists so that Travis CI can run Valgrind on tests without taking a very long time.
585+
add_h3_test(testH3UniEdgeExhaustive src/apps/testapps/testH3UniEdgeExhaustive.c)
583586
add_h3_test(testH3ToLocalIjExhaustive src/apps/testapps/testH3ToLocalIjExhaustive.c)
584587
add_h3_test(testH3LineExhaustive src/apps/testapps/testH3LineExhaustive.c)
585588
add_h3_test(testH3DistanceExhaustive src/apps/testapps/testH3DistanceExhaustive.c)
@@ -602,6 +605,7 @@ if(BUILD_BENCHMARKS)
602605
add_h3_benchmark(benchmarkH3Api src/apps/benchmarks/benchmarkH3Api.c)
603606
add_h3_benchmark(benchmarkKRing src/apps/benchmarks/benchmarkKRing.c)
604607
add_h3_benchmark(benchmarkH3Line src/apps/benchmarks/benchmarkH3Line.c)
608+
add_h3_benchmark(benchmarkH3UniEdge src/apps/benchmarks/benchmarkH3UniEdge.c)
605609
add_h3_benchmark(benchmarkH3SetToLinkedGeo src/apps/benchmarks/benchmarkH3SetToLinkedGeo.c)
606610
add_h3_benchmark(benchmarkPolyfill src/apps/benchmarks/benchmarkPolyfill.c)
607611
add_h3_benchmark(benchmarkPolygon src/apps/benchmarks/benchmarkPolygon.c)

src/apps/benchmarks/benchmarkH3Api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "geoCoord.h"
1818
#include "h3api.h"
1919

20-
// Fixtures
20+
// Fixtures (arbitrary res 9 hexagon)
2121
GeoCoord coord = {0.659966917655, -2.1364398519396};
2222
H3Index hex = 0x89283080ddbffff;
2323

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2020 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+
#include "benchmark.h"
17+
#include "geoCoord.h"
18+
#include "h3api.h"
19+
20+
// Fixtures (arbitrary res 9 hexagon)
21+
H3Index edges[6] = {0};
22+
H3Index hex = 0x89283080ddbffff;
23+
24+
BEGIN_BENCHMARKS();
25+
26+
GeoBoundary outBoundary;
27+
H3_EXPORT(getH3UnidirectionalEdgesFromHexagon)(hex, edges);
28+
29+
BENCHMARK(getH3UnidirectionalEdgeBoundary, 10000, {
30+
for (int i = 0; i < 6; i++)
31+
H3_EXPORT(getH3UnidirectionalEdgeBoundary)(edges[i], &outBoundary);
32+
});
33+
34+
END_BENCHMARKS();
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2020 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+
/** @file
17+
* @brief tests H3 unidirectional edge functions using tests over a large number
18+
* of indexes.
19+
*
20+
* usage: `testH3UniEdgeExhaustive`
21+
*/
22+
23+
#include <stdio.h>
24+
#include <stdlib.h>
25+
#include <string.h>
26+
27+
#include "constants.h"
28+
#include "geoCoord.h"
29+
#include "h3Index.h"
30+
#include "test.h"
31+
#include "utility.h"
32+
33+
static void h3UniEdge_correctness_assertions(H3Index h3) {
34+
H3Index edges[6] = {0};
35+
int isPentagon = H3_EXPORT(h3IsPentagon)(h3);
36+
H3_EXPORT(getH3UnidirectionalEdgesFromHexagon)(h3, edges);
37+
H3Index destination;
38+
39+
for (int i = 0; i < 6; i++) {
40+
if (isPentagon && i == 0) {
41+
t_assert(edges[i] == H3_INVALID_INDEX,
42+
"last pentagon edge is empty");
43+
continue;
44+
}
45+
t_assert(H3_EXPORT(h3UnidirectionalEdgeIsValid)(edges[i]) == 1,
46+
"edge is an edge");
47+
t_assert(
48+
H3_EXPORT(getOriginH3IndexFromUnidirectionalEdge)(edges[i]) == h3,
49+
"origin matches input origin");
50+
51+
destination =
52+
H3_EXPORT(getDestinationH3IndexFromUnidirectionalEdge)(edges[i]);
53+
t_assert(H3_EXPORT(h3IndexesAreNeighbors)(h3, destination),
54+
"destination is a neighbor");
55+
}
56+
}
57+
58+
static void h3UniEdge_boundary_assertions(H3Index h3) {
59+
H3Index edges[6] = {0};
60+
H3_EXPORT(getH3UnidirectionalEdgesFromHexagon)(h3, edges);
61+
H3Index destination;
62+
H3Index revEdge;
63+
GeoBoundary edgeBoundary;
64+
GeoBoundary revEdgeBoundary;
65+
66+
for (int i = 0; i < 6; i++) {
67+
if (edges[i] == H3_INVALID_INDEX) continue;
68+
H3_EXPORT(getH3UnidirectionalEdgeBoundary)(edges[i], &edgeBoundary);
69+
destination =
70+
H3_EXPORT(getDestinationH3IndexFromUnidirectionalEdge)(edges[i]);
71+
revEdge = H3_EXPORT(getH3UnidirectionalEdge)(destination, h3);
72+
H3_EXPORT(getH3UnidirectionalEdgeBoundary)(revEdge, &revEdgeBoundary);
73+
74+
t_assert(edgeBoundary.numVerts == revEdgeBoundary.numVerts,
75+
"numVerts is equal for edge and reverse");
76+
77+
for (int j = 0; j < edgeBoundary.numVerts; j++) {
78+
t_assert(
79+
geoAlmostEqualThreshold(
80+
&edgeBoundary.verts[j],
81+
&revEdgeBoundary.verts[revEdgeBoundary.numVerts - 1 - j],
82+
0.000001),
83+
"Got expected vertex");
84+
}
85+
}
86+
}
87+
88+
SUITE(h3UniEdge) {
89+
TEST(h3UniEdge_correctness) {
90+
iterateAllIndexesAtRes(0, h3UniEdge_correctness_assertions);
91+
iterateAllIndexesAtRes(1, h3UniEdge_correctness_assertions);
92+
iterateAllIndexesAtRes(2, h3UniEdge_correctness_assertions);
93+
iterateAllIndexesAtRes(3, h3UniEdge_correctness_assertions);
94+
}
95+
96+
TEST(h3UniEdge_boundary) {
97+
iterateAllIndexesAtRes(0, h3UniEdge_boundary_assertions);
98+
iterateAllIndexesAtRes(1, h3UniEdge_boundary_assertions);
99+
iterateAllIndexesAtRes(2, h3UniEdge_boundary_assertions);
100+
iterateAllIndexesAtRes(3, h3UniEdge_boundary_assertions);
101+
}
102+
}

0 commit comments

Comments
 (0)