Skip to content

Commit 9ab1053

Browse files
authored
Merge pull request #468 from isaacbrodsky/error-returns-compact
Add error return codes to compactCells
2 parents de84326 + a23cea4 commit 9ab1053

File tree

12 files changed

+160
-151
lines changed

12 files changed

+160
-151
lines changed

dev-docs/RFCs/v4.0.0/error-handling-rfc.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ SQLite's approach to this is to define broad error categories (in 8 bits), and t
254254
| 11 | E_NOT_NEIGHBORS | `H3Index` cell arguments were not neighbors
255255
| 12 | E_RES_MISMATCH | `H3Index` cell arguments had incompatible resolutions
256256
| 13 | E_MEMORY | Necessary memory allocation failed
257+
| 14 | E_MEMORY_BOUNDS | Bounds of provided memory were not large enough
257258

258259
The H3 library may always add additional error messages. Error messages not recognized by the application should be treated as `E_FAILED`.
259260

examples/compactCells.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ int main(int argc, char* argv[]) {
3535
printf("Starting with %d indexes.\n", inputSize);
3636

3737
H3Index* compacted = calloc(inputSize, sizeof(H3Index));
38-
int err = compactCells(input, compacted, inputSize);
38+
H3Error err = compactCells(input, compacted, inputSize);
3939
// An error case can occur on e.g. duplicate input.
40-
assert(err == 0);
40+
assert(err == E_SUCCESS);
4141

4242
int compactedCount = 0;
4343
printf("Compacted:\n");
@@ -50,14 +50,16 @@ int main(int argc, char* argv[]) {
5050
printf("Compacted to %d indexes.\n", compactedCount);
5151

5252
int uncompactRes = 10;
53-
int64_t uncompactedSize =
54-
uncompactCellsSize(compacted, inputSize, uncompactRes);
53+
int64_t uncompactedSize;
54+
H3Error err2 = uncompactCellsSize(compacted, inputSize, uncompactRes,
55+
&uncompactedSize);
56+
assert(err2 == E_SUCCESS);
5557
H3Index* uncompacted = calloc(uncompactedSize, sizeof(H3Index));
56-
int err2 = uncompactCells(compacted, compactedCount, uncompacted,
58+
int err3 = uncompactCells(compacted, compactedCount, uncompacted,
5759
uncompactedSize, uncompactRes);
5860
// An error case could happen if the output array is too small, or indexes
5961
// have a higher resolution than uncompactRes.
60-
assert(err2 == 0);
62+
assert(err3 == E_SUCCESS);
6163

6264
int uncompactedCount = 0;
6365
printf("Uncompacted:\n");

src/apps/testapps/testCompactCells.c

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ SUITE(compactCells) {
3838
H3_EXPORT(gridDisk)(sunnyvale, k, sunnyvaleExpanded);
3939

4040
H3Index* compressed = calloc(hexCount, sizeof(H3Index));
41-
int err =
42-
H3_EXPORT(compactCells)(sunnyvaleExpanded, compressed, hexCount);
43-
t_assert(err == 0, "no error on compactCells");
41+
t_assertSuccess(
42+
H3_EXPORT(compactCells)(sunnyvaleExpanded, compressed, hexCount));
4443

4544
int count = 0;
4645
for (int i = 0; i < hexCount; i++) {
@@ -50,12 +49,12 @@ SUITE(compactCells) {
5049
}
5150
t_assert(count == expectedCompactCount, "got expected compacted count");
5251

53-
H3Index* decompressed =
54-
calloc(H3_EXPORT(uncompactCellsSize)(compressed, count, 9),
55-
sizeof(H3Index));
56-
int err2 = H3_EXPORT(uncompactCells)(compressed, count, decompressed,
57-
hexCount, 9);
58-
t_assert(err2 == 0, "no error on uncompactCells");
52+
int64_t countUncompact;
53+
t_assertSuccess(H3_EXPORT(uncompactCellsSize)(compressed, count, 9,
54+
&countUncompact));
55+
H3Index* decompressed = calloc(countUncompact, sizeof(H3Index));
56+
t_assertSuccess(H3_EXPORT(uncompactCells)(compressed, count,
57+
decompressed, hexCount, 9));
5958

6059
int count2 = 0;
6160
for (int i = 0; i < hexCount; i++) {
@@ -78,8 +77,8 @@ SUITE(compactCells) {
7877
setH3Index(&res0Hexes[i], 0, i, 0);
7978
}
8079
H3Index* compressed = calloc(hexCount, sizeof(H3Index));
81-
int err = H3_EXPORT(compactCells)(res0Hexes, compressed, hexCount);
82-
t_assert(err == 0, "no error on compact");
80+
t_assertSuccess(
81+
H3_EXPORT(compactCells)(res0Hexes, compressed, hexCount));
8382

8483
for (int i = 0; i < hexCount; i++) {
8584
// At resolution 0, it will be an exact copy.
@@ -89,12 +88,12 @@ SUITE(compactCells) {
8988
"got expected compressed result");
9089
}
9190

92-
H3Index* decompressed =
93-
calloc(H3_EXPORT(uncompactCellsSize)(compressed, hexCount, 0),
94-
sizeof(H3Index));
95-
int err2 = H3_EXPORT(uncompactCells)(compressed, hexCount, decompressed,
96-
hexCount, 0);
97-
t_assert(err2 == 0, "no error on uncompactCells");
91+
int64_t countUncompact;
92+
t_assertSuccess(H3_EXPORT(uncompactCellsSize)(compressed, hexCount, 0,
93+
&countUncompact));
94+
H3Index* decompressed = calloc(countUncompact, sizeof(H3Index));
95+
t_assertSuccess(H3_EXPORT(uncompactCells)(compressed, hexCount,
96+
decompressed, hexCount, 0));
9897

9998
int count2 = 0;
10099
for (int i = 0; i < hexCount; i++) {
@@ -114,8 +113,8 @@ SUITE(compactCells) {
114113
int expectedCompactCount = 3;
115114

116115
H3Index* compressed = calloc(hexCount, sizeof(H3Index));
117-
int err = H3_EXPORT(compactCells)(uncompactable, compressed, hexCount);
118-
t_assert(err == 0, "no error on compact");
116+
t_assertSuccess(
117+
H3_EXPORT(compactCells)(uncompactable, compressed, hexCount));
119118

120119
int count = 0;
121120
for (int i = 0; i < hexCount; i++) {
@@ -125,12 +124,12 @@ SUITE(compactCells) {
125124
}
126125
t_assert(count == expectedCompactCount, "got expected compacted count");
127126

128-
H3Index* decompressed =
129-
calloc(H3_EXPORT(uncompactCellsSize)(compressed, count, 9),
130-
sizeof(H3Index));
131-
int err2 = H3_EXPORT(uncompactCells)(compressed, count, decompressed,
132-
hexCount, 9);
133-
t_assert(err2 == 0, "no error on uncompactCells");
127+
int64_t countUncompact;
128+
t_assertSuccess(H3_EXPORT(uncompactCellsSize)(compressed, count, 9,
129+
&countUncompact));
130+
H3Index* decompressed = calloc(countUncompact, sizeof(H3Index));
131+
t_assertSuccess(H3_EXPORT(uncompactCells)(compressed, count,
132+
decompressed, hexCount, 9));
134133

135134
int count2 = 0;
136135
for (int i = 0; i < hexCount; i++) {
@@ -152,7 +151,8 @@ SUITE(compactCells) {
152151
}
153152
H3Index compressed[10];
154153

155-
t_assert(H3_EXPORT(compactCells)(someHexagons, compressed, numHex) != 0,
154+
t_assert(H3_EXPORT(compactCells)(someHexagons, compressed, numHex) ==
155+
E_DUPLICATE_INPUT,
156156
"compactCells fails on duplicate input");
157157
}
158158

@@ -172,9 +172,9 @@ SUITE(compactCells) {
172172

173173
H3Index* output = calloc(arrSize, sizeof(H3Index));
174174

175-
int compactCellsResult =
175+
H3Error compactCellsResult =
176176
H3_EXPORT(compactCells)(children, output, arrSize);
177-
t_assert(compactCellsResult == COMPACT_DUPLICATE,
177+
t_assert(compactCellsResult == E_DUPLICATE_INPUT,
178178
"compactCells fails on duplicate input (single duplicate)");
179179

180180
free(output);
@@ -197,9 +197,9 @@ SUITE(compactCells) {
197197

198198
H3Index* output = calloc(arrSize, sizeof(H3Index));
199199

200-
int compactCellsResult =
200+
H3Error compactCellsResult =
201201
H3_EXPORT(compactCells)(children, output, arrSize);
202-
t_assert(compactCellsResult == COMPACT_DUPLICATE,
202+
t_assert(compactCellsResult == E_DUPLICATE_INPUT,
203203
"compactCells fails on duplicate input (pentagon parent)");
204204

205205
free(output);
@@ -224,17 +224,14 @@ SUITE(compactCells) {
224224

225225
H3Index* output = calloc(arrSize, sizeof(H3Index));
226226

227-
int compactCellsResult =
228-
H3_EXPORT(compactCells)(children, output, arrSize);
229-
t_assert(compactCellsResult == COMPACT_SUCCESS,
230-
"compactCells succeeds on duplicate input (correct count)");
227+
t_assertSuccess(H3_EXPORT(compactCells)(children, output, arrSize));
231228

232229
free(output);
233230
free(children);
234231
}
235232

236233
TEST(compactCells_empty) {
237-
t_assert(H3_EXPORT(compactCells)(NULL, NULL, 0) == 0,
234+
t_assert(H3_EXPORT(compactCells)(NULL, NULL, 0) == E_SUCCESS,
238235
"compactCells succeeds on empty input");
239236
}
240237

@@ -248,8 +245,9 @@ SUITE(compactCells) {
248245
}
249246
H3Index output[] = {0, 0, 0, 0, 0, 0, 0};
250247

251-
t_assert(H3_EXPORT(compactCells)(disparate, output, numHex) == 0,
252-
"compactCells succeeds on disparate input");
248+
t_assert(
249+
H3_EXPORT(compactCells)(disparate, output, numHex) == E_SUCCESS,
250+
"compactCells succeeds on disparate input");
253251

254252
// Assumes that `output` is an exact copy of `disparate`, including
255253
// the ordering (which may not necessarily be the case)
@@ -265,30 +263,30 @@ SUITE(compactCells) {
265263
setH3Index(&someHexagons[i], 5, i, 0);
266264
}
267265

268-
int64_t sizeResult =
269-
H3_EXPORT(uncompactCellsSize)(someHexagons, numHex, 4);
270-
t_assert(sizeResult < 0,
266+
int64_t sizeResult;
267+
t_assert(H3_EXPORT(uncompactCellsSize)(someHexagons, numHex, 4,
268+
&sizeResult) == E_RES_MISMATCH,
271269
"uncompactCellsSize fails when given illogical resolutions");
272-
sizeResult = H3_EXPORT(uncompactCellsSize)(someHexagons, numHex, -1);
273-
t_assert(sizeResult < 0,
270+
t_assert(H3_EXPORT(uncompactCellsSize)(someHexagons, numHex, -1,
271+
&sizeResult) == E_RES_MISMATCH,
274272
"uncompactCellsSize fails when given illegal resolutions");
275-
sizeResult =
276-
H3_EXPORT(uncompactCellsSize)(someHexagons, numHex, MAX_H3_RES + 1);
277-
t_assert(sizeResult < 0,
278-
"uncompactCellsSize fails when given resolutions beyond max");
273+
t_assert(
274+
H3_EXPORT(uncompactCellsSize)(someHexagons, numHex, MAX_H3_RES + 1,
275+
&sizeResult) == E_RES_MISMATCH,
276+
"uncompactCellsSize fails when given resolutions beyond max");
279277

280278
H3Index uncompressed[] = {0, 0, 0};
281-
int uncompactCellsResult = H3_EXPORT(uncompactCells)(
279+
H3Error uncompactCellsResult = H3_EXPORT(uncompactCells)(
282280
someHexagons, numHex, uncompressed, numHex, 0);
283-
t_assert(uncompactCellsResult != 0,
281+
t_assert(uncompactCellsResult == E_RES_MISMATCH,
284282
"uncompactCells fails when given illogical resolutions");
285283
uncompactCellsResult = H3_EXPORT(uncompactCells)(
286284
someHexagons, numHex, uncompressed, numHex, 6);
287-
t_assert(uncompactCellsResult != 0,
285+
t_assert(uncompactCellsResult == E_MEMORY_BOUNDS,
288286
"uncompactCells fails when given too little buffer");
289287
uncompactCellsResult = H3_EXPORT(uncompactCells)(
290288
someHexagons, numHex, uncompressed, numHex - 1, 5);
291-
t_assert(uncompactCellsResult != 0,
289+
t_assert(uncompactCellsResult == E_MEMORY_BOUNDS,
292290
"uncompactCells fails when given too little buffer (same "
293291
"resolution)");
294292

@@ -297,24 +295,23 @@ SUITE(compactCells) {
297295
}
298296
uncompactCellsResult = H3_EXPORT(uncompactCells)(
299297
someHexagons, numHex, uncompressed, numHex * 7, MAX_H3_RES + 1);
300-
t_assert(uncompactCellsResult != 0,
298+
t_assert(uncompactCellsResult == E_RES_MISMATCH,
301299
"uncompactCells fails when given resolutions beyond max");
302300
}
303301

304302
TEST(someHexagon) {
305303
H3Index origin;
306304
setH3Index(&origin, 1, 5, 0);
307305

308-
int64_t childrenSz = H3_EXPORT(uncompactCellsSize)(&origin, 1, 2);
306+
int64_t childrenSz;
307+
t_assertSuccess(
308+
H3_EXPORT(uncompactCellsSize)(&origin, 1, 2, &childrenSz));
309309
H3Index* children = calloc(childrenSz, sizeof(H3Index));
310-
int uncompactCellsResult =
311-
H3_EXPORT(uncompactCells)(&origin, 1, children, childrenSz, 2);
312-
t_assert(uncompactCellsResult == 0, "uncompactCells origin succeeds");
310+
t_assertSuccess(
311+
H3_EXPORT(uncompactCells)(&origin, 1, children, childrenSz, 2));
313312

314313
H3Index* result = calloc(childrenSz, sizeof(H3Index));
315-
int compactResult =
316-
H3_EXPORT(compactCells)(children, result, childrenSz);
317-
t_assert(compactResult == 0, "compact origin succeeds");
314+
t_assertSuccess(H3_EXPORT(compactCells)(children, result, childrenSz));
318315

319316
int found = 0;
320317
for (int i = 0; i < childrenSz; i++) {
@@ -330,9 +327,11 @@ SUITE(compactCells) {
330327
}
331328

332329
TEST(uncompactCells_empty) {
333-
int64_t uncompactSz = H3_EXPORT(uncompactCellsSize)(NULL, 0, 0);
330+
int64_t uncompactSz;
331+
t_assertSuccess(
332+
H3_EXPORT(uncompactCellsSize)(NULL, 0, 0, &uncompactSz));
334333
t_assert(uncompactSz == 0, "uncompactCellsSize accepts empty input");
335-
t_assert(H3_EXPORT(uncompactCells)(NULL, 0, NULL, 0, 0) == 0,
334+
t_assert(H3_EXPORT(uncompactCells)(NULL, 0, NULL, 0, 0) == E_SUCCESS,
336335
"uncompactCells accepts empty input");
337336
}
338337

@@ -343,11 +342,12 @@ SUITE(compactCells) {
343342

344343
H3Index origin = 0;
345344

346-
int64_t childrenSz = H3_EXPORT(uncompactCellsSize)(&origin, 1, 2);
345+
int64_t childrenSz;
346+
t_assertSuccess(
347+
H3_EXPORT(uncompactCellsSize)(&origin, 1, 2, &childrenSz));
347348
H3Index* children = calloc(childrenSz, sizeof(H3Index));
348-
int uncompactCellsResult =
349-
H3_EXPORT(uncompactCells)(&origin, 1, children, childrenSz, 2);
350-
t_assert(uncompactCellsResult == 0, "uncompactCells only zero success");
349+
t_assertSuccess(
350+
H3_EXPORT(uncompactCells)(&origin, 1, children, childrenSz, 2));
351351

352352
free(children);
353353
}
@@ -356,13 +356,12 @@ SUITE(compactCells) {
356356
// uncompactCellsSize and uncompactSize both permit 0 indexes
357357
// in the input array, and skip them.
358358

359-
int64_t childrenSz =
360-
H3_EXPORT(uncompactCellsSize)(uncompactableWithZero, 4, 10);
359+
int64_t childrenSz;
360+
t_assertSuccess(H3_EXPORT(uncompactCellsSize)(uncompactableWithZero, 4,
361+
10, &childrenSz));
361362
H3Index* children = calloc(childrenSz, sizeof(H3Index));
362-
int uncompactCellsResult = H3_EXPORT(uncompactCells)(
363-
uncompactableWithZero, 4, children, childrenSz, 10);
364-
t_assert(uncompactCellsResult == 0,
365-
"uncompactCells with zero succeeds");
363+
t_assertSuccess(H3_EXPORT(uncompactCells)(uncompactableWithZero, 4,
364+
children, childrenSz, 10));
366365

367366
int found = 0;
368367
for (int i = 0; i < childrenSz; i++) {
@@ -380,16 +379,15 @@ SUITE(compactCells) {
380379
H3Index pentagon;
381380
setH3Index(&pentagon, 1, 4, 0);
382381

383-
int64_t childrenSz = H3_EXPORT(uncompactCellsSize)(&pentagon, 1, 2);
382+
int64_t childrenSz;
383+
t_assertSuccess(
384+
H3_EXPORT(uncompactCellsSize)(&pentagon, 1, 2, &childrenSz));
384385
H3Index* children = calloc(childrenSz, sizeof(H3Index));
385-
int uncompactCellsResult =
386-
H3_EXPORT(uncompactCells)(&pentagon, 1, children, childrenSz, 2);
387-
t_assert(uncompactCellsResult == 0, "uncompactCells pentagon succeeds");
386+
t_assertSuccess(
387+
H3_EXPORT(uncompactCells)(&pentagon, 1, children, childrenSz, 2));
388388

389389
H3Index* result = calloc(childrenSz, sizeof(H3Index));
390-
int compactResult =
391-
H3_EXPORT(compactCells)(children, result, childrenSz);
392-
t_assert(compactResult == 0, "compact pentagon succeeds");
390+
t_assertSuccess(H3_EXPORT(compactCells)(children, result, childrenSz));
393391

394392
int found = 0;
395393
for (int i = 0; i < childrenSz; i++) {
@@ -410,7 +408,8 @@ SUITE(compactCells) {
410408
int res = 15;
411409

412410
int64_t expected = 4747561509943L; // 7^15
413-
int64_t out = H3_EXPORT(uncompactCellsSize)(cells, 1, res);
411+
int64_t out;
412+
t_assertSuccess(H3_EXPORT(uncompactCellsSize)(cells, 1, res, &out));
414413

415414
t_assert(out == expected, "uncompactCells size needs 64 bit int");
416415
}
@@ -420,7 +419,8 @@ SUITE(compactCells) {
420419
int res = 15;
421420

422421
int64_t expected = 3956301258286L; // 1 + 5*(7^15 - 1)/6
423-
int64_t out = H3_EXPORT(uncompactCellsSize)(cells, 1, res);
422+
int64_t out;
423+
t_assertSuccess(H3_EXPORT(uncompactCellsSize)(cells, 1, res, &out));
424424

425425
t_assert(out == expected, "uncompactCells size needs 64 bit int");
426426
}

src/apps/testapps/testGridRingUnsafe.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ SUITE(gridRingUnsafe) {
121121
for (int i = 0; i < NUM_BASE_CELLS; i++) {
122122
H3Index bc;
123123
setH3Index(&bc, 0, i, 0);
124-
int64_t childrenSz = H3_EXPORT(uncompactCellsSize)(&bc, 1, res);
124+
int64_t childrenSz;
125+
t_assertSuccess(
126+
H3_EXPORT(uncompactCellsSize)(&bc, 1, res, &childrenSz));
125127
H3Index *children = calloc(childrenSz, sizeof(H3Index));
126-
H3_EXPORT(uncompactCells)(&bc, 1, children, childrenSz, res);
128+
t_assertSuccess(H3_EXPORT(uncompactCells)(&bc, 1, children,
129+
childrenSz, res));
127130

128131
for (int j = 0; j < childrenSz; j++) {
129132
if (children[j] == 0) {

0 commit comments

Comments
 (0)