|
| 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