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

Add error return codes to polygonToCells #478

Merged
merged 6 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/apps/applib/include/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ void iterateBaseCellIndexesAtRes(int res, void (*callback)(H3Index),
int baseCell);
void iterateAllDirectedEdgesAtRes(int res, void (*callback)(H3Index));

int countNonNullIndexes(H3Index *indexes, int numCells);
int64_t countNonNullIndexes(H3Index *indexes, int64_t numCells);

#endif
4 changes: 2 additions & 2 deletions src/apps/applib/lib/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ void randomGeo(LatLng *g) {
/**
* Returns the number of non-invalid indexes in the array.
*/
int countNonNullIndexes(H3Index *indexes, int numCells) {
int nonNullIndexes = 0;
int64_t countNonNullIndexes(H3Index *indexes, int64_t numCells) {
int64_t nonNullIndexes = 0;
for (int i = 0; i < numCells; i++) {
if (indexes[i] != H3_NULL) {
nonNullIndexes++;
Expand Down
8 changes: 4 additions & 4 deletions src/apps/benchmarks/benchmarkPolygonToCells.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,25 @@ southernGeoLoop.numVerts = 23;
southernGeoLoop.verts = southernVerts;
southernGeoPolygon.geoloop = southernGeoLoop;

int numHexagons;
int64_t numHexagons;
H3Index *hexagons;

BENCHMARK(polygonToCellsSF, 500, {
numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9);
H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9, &numHexagons);
hexagons = calloc(numHexagons, sizeof(H3Index));
H3_EXPORT(polygonToCells)(&sfGeoPolygon, 9, hexagons);
free(hexagons);
});

BENCHMARK(polygonToCellsAlameda, 500, {
numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&alamedaGeoPolygon, 9);
H3_EXPORT(maxPolygonToCellsSize)(&alamedaGeoPolygon, 9, &numHexagons);
hexagons = calloc(numHexagons, sizeof(H3Index));
H3_EXPORT(polygonToCells)(&alamedaGeoPolygon, 9, hexagons);
free(hexagons);
});

BENCHMARK(polygonToCellsSouthernExpansion, 10, {
numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&southernGeoPolygon, 9);
H3_EXPORT(maxPolygonToCellsSize)(&southernGeoPolygon, 9, &numHexagons);
hexagons = calloc(numHexagons, sizeof(H3Index));
H3_EXPORT(polygonToCells)(&southernGeoPolygon, 9, hexagons);
free(hexagons);
Expand Down
46 changes: 46 additions & 0 deletions src/apps/testapps/testH3Memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ void test_prefix_free(void *ptr) {
H3Index sunnyvale = 0x89283470c27ffff;
H3Index pentagon = 0x89080000003ffff;

static LatLng sfVerts[] = {
{0.659966917655, -2.1364398519396}, {0.6595011102219, -2.1359434279405},
{0.6583348114025, -2.1354884206045}, {0.6581220034068, -2.1382437718946},
{0.6594479998527, -2.1384597563896}, {0.6599990002976, -2.1376771158464}};
static GeoLoop sfGeoLoop = {.numVerts = 6, .verts = sfVerts};
static GeoPolygon sfGeoPolygon;

SUITE(h3Memory) {
TEST(gridDisk) {
int k = 2;
Expand Down Expand Up @@ -173,4 +180,43 @@ SUITE(h3Memory) {
free(compressed);
free(sunnyvaleExpanded);
}

TEST(polygonToCells) {
sfGeoPolygon.geoloop = sfGeoLoop;
sfGeoPolygon.numHoles = 0;

int64_t numHexagons;
t_assertSuccess(
H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9, &numHexagons));
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));

resetMemoryCounters(0);
failAlloc = true;
H3Error err = H3_EXPORT(polygonToCells)(&sfGeoPolygon, 9, hexagons);
t_assert(err == E_MEMORY, "polygonToCells failed (1)");
t_assert(actualAllocCalls == 1, "alloc called once");
t_assert(actualFreeCalls == 0, "free not called");

resetMemoryCounters(1);
err = H3_EXPORT(polygonToCells)(&sfGeoPolygon, 9, hexagons);
t_assert(err == E_MEMORY, "polygonToCells failed (2)");
t_assert(actualAllocCalls == 2, "alloc called otwicence");
isaacbrodsky marked this conversation as resolved.
Show resolved Hide resolved
t_assert(actualFreeCalls == 1, "free called once");

resetMemoryCounters(2);
err = H3_EXPORT(polygonToCells)(&sfGeoPolygon, 9, hexagons);
t_assert(err == E_MEMORY, "polygonToCells failed (3)");
t_assert(actualAllocCalls == 3, "alloc called three times");
t_assert(actualFreeCalls == 2, "free called twice");

resetMemoryCounters(3);
err = H3_EXPORT(polygonToCells)(&sfGeoPolygon, 9, hexagons);
t_assert(err == E_SUCCESS, "polygonToCells succeeded (4)");
t_assert(actualAllocCalls == 3, "alloc called three times");
t_assert(actualFreeCalls == 3, "free called three times");

int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);
t_assert(actualNumIndexes == 1253, "got expected polygonToCells size");
free(hexagons);
}
}
107 changes: 66 additions & 41 deletions src/apps/testapps/testPolygonToCells.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,23 @@ static void fillIndex_assertions(H3Index h) {
.numHoles = 0,
.holes = 0};

int polygonToCellsSize =
H3_EXPORT(maxPolygonToCellsSize)(&polygon, nextRes);
int64_t polygonToCellsSize;
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(&polygon, nextRes,
&polygonToCellsSize));
H3Index *polygonToCellsOut =
calloc(polygonToCellsSize, sizeof(H3Index));
H3_EXPORT(polygonToCells)(&polygon, nextRes, polygonToCellsOut);
t_assertSuccess(
H3_EXPORT(polygonToCells)(&polygon, nextRes, polygonToCellsOut));

int polygonToCellsCount =
int64_t polygonToCellsCount =
countNonNullIndexes(polygonToCellsOut, polygonToCellsSize);

int64_t childrenSize = H3_EXPORT(cellToChildrenSize)(h, nextRes);
H3Index *children = calloc(childrenSize, sizeof(H3Index));
H3_EXPORT(cellToChildren)(h, nextRes, children);

int cellToChildrenCount = countNonNullIndexes(children, childrenSize);
int64_t cellToChildrenCount =
countNonNullIndexes(children, childrenSize);

t_assert(polygonToCellsCount == cellToChildrenCount,
"PolygonToCells count matches cellToChildren count");
Expand Down Expand Up @@ -124,47 +127,59 @@ SUITE(polygonToCells) {
emptyGeoPolygon.numHoles = 0;

TEST(maxPolygonToCellsSize) {
int numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9);
int64_t numHexagons;
t_assertSuccess(
H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9, &numHexagons));
t_assert(numHexagons == 5613, "got expected max polygonToCells size");

numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&holeGeoPolygon, 9);
t_assertSuccess(
H3_EXPORT(maxPolygonToCellsSize)(&holeGeoPolygon, 9, &numHexagons));
t_assert(numHexagons == 5613,
"got expected max polygonToCells size (hole)");

numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&emptyGeoPolygon, 9);
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(&emptyGeoPolygon, 9,
&numHexagons));
t_assert(numHexagons == 15,
"got expected max polygonToCells size (empty)");
}

TEST(polygonToCells) {
int numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9);
int64_t numHexagons;
t_assertSuccess(
H3_EXPORT(maxPolygonToCellsSize)(&sfGeoPolygon, 9, &numHexagons));
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)(&sfGeoPolygon, 9, hexagons);
int actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);
t_assertSuccess(H3_EXPORT(polygonToCells)(&sfGeoPolygon, 9, hexagons));
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);

t_assert(actualNumIndexes == 1253, "got expected polygonToCells size");
free(hexagons);
}

TEST(polygonToCellsHole) {
int numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&holeGeoPolygon, 9);
int64_t numHexagons;
t_assertSuccess(
H3_EXPORT(maxPolygonToCellsSize)(&holeGeoPolygon, 9, &numHexagons));
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)(&holeGeoPolygon, 9, hexagons);
int actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);
t_assertSuccess(
H3_EXPORT(polygonToCells)(&holeGeoPolygon, 9, hexagons));
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);

t_assert(actualNumIndexes == 1214,
"got expected polygonToCells size (hole)");
free(hexagons);
}

TEST(polygonToCellsEmpty) {
int numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&emptyGeoPolygon, 9);
int64_t numHexagons;
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(&emptyGeoPolygon, 9,
&numHexagons));
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)(&emptyGeoPolygon, 9, hexagons);
int actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);
t_assertSuccess(
H3_EXPORT(polygonToCells)(&emptyGeoPolygon, 9, hexagons));
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);

t_assert(actualNumIndexes == 0,
"got expected polygonToCells size (empty)");
Expand All @@ -191,11 +206,13 @@ SUITE(polygonToCells) {
someHexagon.geoloop = someGeoLoop;
someHexagon.numHoles = 0;

int numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&someHexagon, 9);
int64_t numHexagons;
t_assertSuccess(
H3_EXPORT(maxPolygonToCellsSize)(&someHexagon, 9, &numHexagons));
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)(&someHexagon, 9, hexagons);
int actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);
t_assertSuccess(H3_EXPORT(polygonToCells)(&someHexagon, 9, hexagons));
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);

t_assert(actualNumIndexes == 1, "got expected polygonToCells size (1)");
free(hexagons);
Expand Down Expand Up @@ -232,16 +249,18 @@ SUITE(polygonToCells) {
GeoPolygon transMeridianFilledHoleGeoPolygon = {
.geoloop = transMeridianHoleGeoLoop, .numHoles = 0};

int expectedSize;
int64_t expectedSize;

// Prime meridian case
expectedSize = 4228;
int numHexagons =
H3_EXPORT(maxPolygonToCellsSize)(&primeMeridianGeoPolygon, 7);
int64_t numHexagons;
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(
&primeMeridianGeoPolygon, 7, &numHexagons));
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)(&primeMeridianGeoPolygon, 7, hexagons);
int actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);
t_assertSuccess(
H3_EXPORT(polygonToCells)(&primeMeridianGeoPolygon, 7, hexagons));
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);

t_assert(actualNumIndexes == expectedSize,
"got expected polygonToCells size (prime meridian)");
Expand All @@ -250,33 +269,35 @@ SUITE(polygonToCells) {
// This doesn't exactly match the prime meridian count because of slight
// differences in hex size and grid offset between the two cases
expectedSize = 4238;
numHexagons =
H3_EXPORT(maxPolygonToCellsSize)(&transMeridianGeoPolygon, 7);
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(
&transMeridianGeoPolygon, 7, &numHexagons));
H3Index *hexagonsTM = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)(&transMeridianGeoPolygon, 7, hexagonsTM);
t_assertSuccess(
H3_EXPORT(polygonToCells)(&transMeridianGeoPolygon, 7, hexagonsTM));
actualNumIndexes = countNonNullIndexes(hexagonsTM, numHexagons);

t_assert(actualNumIndexes == expectedSize,
"got expected polygonToCells size (transmeridian)");

// Transmeridian filled hole case -- only needed for calculating hole
// size
numHexagons = H3_EXPORT(maxPolygonToCellsSize)(
&transMeridianFilledHoleGeoPolygon, 7);
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(
&transMeridianFilledHoleGeoPolygon, 7, &numHexagons));
H3Index *hexagonsTMFH = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)
(&transMeridianFilledHoleGeoPolygon, 7, hexagonsTMFH);
int actualNumHoleIndexes =
t_assertSuccess(H3_EXPORT(polygonToCells)(
&transMeridianFilledHoleGeoPolygon, 7, hexagonsTMFH));
int64_t actualNumHoleIndexes =
countNonNullIndexes(hexagonsTMFH, numHexagons);

// Transmeridian hole case
numHexagons =
H3_EXPORT(maxPolygonToCellsSize)(&transMeridianHoleGeoPolygon, 7);
t_assertSuccess(H3_EXPORT(maxPolygonToCellsSize)(
&transMeridianHoleGeoPolygon, 7, &numHexagons));
H3Index *hexagonsTMH = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)(&transMeridianHoleGeoPolygon, 7, hexagonsTMH);
t_assertSuccess(H3_EXPORT(polygonToCells)(&transMeridianHoleGeoPolygon,
7, hexagonsTMH));
actualNumIndexes = countNonNullIndexes(hexagonsTMH, numHexagons);

t_assert(actualNumIndexes == expectedSize - actualNumHoleIndexes,
Expand All @@ -298,12 +319,14 @@ SUITE(polygonToCells) {
GeoLoop geoloop = {.numVerts = 6, .verts = verts};
GeoPolygon polygon = {.geoloop = geoloop, .numHoles = 0};

int numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&polygon, 4);
int64_t numHexagons;
t_assertSuccess(
H3_EXPORT(maxPolygonToCellsSize)(&polygon, 4, &numHexagons));

H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));
H3_EXPORT(polygonToCells)(&polygon, 4, hexagons);
t_assertSuccess(H3_EXPORT(polygonToCells)(&polygon, 4, hexagons));

int actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);
int64_t actualNumIndexes = countNonNullIndexes(hexagons, numHexagons);

t_assert(actualNumIndexes == 1204,
"got expected polygonToCells size (complex transmeridian)");
Expand Down Expand Up @@ -347,10 +370,12 @@ SUITE(polygonToCells) {
polygon.geoloop = geoloop;
polygon.numHoles = 0;

int numHexagons = H3_EXPORT(maxPolygonToCellsSize)(&polygon, 9);
int64_t numHexagons;
t_assertSuccess(
H3_EXPORT(maxPolygonToCellsSize)(&polygon, 9, &numHexagons));
H3Index *hexagons = calloc(numHexagons, sizeof(H3Index));

H3_EXPORT(polygonToCells)(&polygon, 9, hexagons);
t_assertSuccess(H3_EXPORT(polygonToCells)(&polygon, 9, hexagons));

int found = 0;
int numPentagons = 0;
Expand Down
Loading