Skip to content

Commit 05032d4

Browse files
authored
Add isValidIndex function (#1056)
* Add isValidIndex function Implement a general validation function that checks if an H3 index is valid for any mode (cell, directed edge, or vertex). Changes: - Add isValidIndex() function declaration to h3api.h.in - Implement isValidIndex() in h3Index.c - Add includes for directedEdge.h and vertex.h in h3Index.c - Add comprehensive test coverage in testH3Index.c The function returns true if the index is valid as a cell, directed edge, or vertex, providing a single validation function for any H3 index type. Fixes #1043 * Fix neighbor retrieval in isValidIndex test Replace incorrect neighbor++ with proper gridRingUnsafe call to get an actual neighboring cell for directed edge testing. Simply incrementing an H3Index will almost never produce a valid neighboring cell. This addresses the review feedback from ajfriend on lines 224-227. * Fix clang-format formatting in isValidIndex function Apply clang-format style to match project formatting conventions: - Put first two conditions on same line - Wrap long comment to 80 characters * Fix clang-format formatting in h3api.h.in header Apply clang-format style to isValidIndex documentation comment: - Wrap comment across 3 lines for proper formatting
1 parent 5af8ee5 commit 05032d4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/apps/testapps/testH3Index.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,39 @@ SUITE(h3Index) {
212212
"matches existing definition");
213213
}
214214
}
215+
216+
TEST(isValidIndex) {
217+
// Test with valid cell
218+
LatLng coord = {0, 0};
219+
H3Index cell;
220+
t_assertSuccess(H3_EXPORT(latLngToCell)(&coord, 5, &cell));
221+
t_assert(H3_EXPORT(isValidIndex)(cell),
222+
"isValidIndex returns true for valid cell");
223+
224+
// Test with valid directed edge
225+
H3Index ring[7] = {0};
226+
t_assertSuccess(H3_EXPORT(gridRingUnsafe)(cell, 1, ring));
227+
H3Index neighbor = ring[0];
228+
H3Index edge;
229+
t_assertSuccess(H3_EXPORT(cellsToDirectedEdge)(cell, neighbor, &edge));
230+
t_assert(H3_EXPORT(isValidIndex)(edge),
231+
"isValidIndex returns true for valid directed edge");
232+
233+
// Test with valid vertex
234+
H3Index vertex;
235+
t_assertSuccess(H3_EXPORT(cellToVertex)(cell, 0, &vertex));
236+
t_assert(H3_EXPORT(isValidIndex)(vertex),
237+
"isValidIndex returns true for valid vertex");
238+
239+
// Test with invalid index
240+
H3Index invalid = 0;
241+
t_assert(!H3_EXPORT(isValidIndex)(invalid),
242+
"isValidIndex returns false for invalid index");
243+
244+
// Test with corrupted index
245+
H3Index corrupted = cell;
246+
H3_SET_HIGH_BIT(corrupted, 1);
247+
t_assert(!H3_EXPORT(isValidIndex)(corrupted),
248+
"isValidIndex returns false for corrupted index");
249+
}
215250
}

src/h3lib/include/h3api.h.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,17 @@ DECLSPEC H3Error H3_EXPORT(h3ToString)(H3Index h, char *str, size_t sz);
552552
DECLSPEC int H3_EXPORT(isValidCell)(H3Index h);
553553
/** @} */
554554

555+
/** @defgroup isValidIndex isValidIndex
556+
* Functions for isValidIndex
557+
* @{
558+
*/
559+
/** @brief confirms if an H3Index is valid for any mode (cell, directed edge, or
560+
* vertex) Returns 1 if the H3 index is valid for any supported type, 0
561+
* otherwise
562+
*/
563+
DECLSPEC int H3_EXPORT(isValidIndex)(H3Index h);
564+
/** @} */
565+
555566
/** @defgroup cellToParent cellToParent
556567
* Functions for cellToParent
557568
* @{

src/h3lib/lib/h3Index.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626

2727
#include "alloc.h"
2828
#include "baseCells.h"
29+
#include "directedEdge.h"
2930
#include "faceijk.h"
3031
#include "h3Assert.h"
3132
#include "iterators.h"
3233
#include "mathExtensions.h"
34+
#include "vertex.h"
3335

3436
/** @var H3ErrorDescriptions
3537
* @brief An array of strings describing each of the H3ErrorCodes enum values
@@ -325,6 +327,17 @@ int H3_EXPORT(isValidCell)(H3Index h) {
325327
return true;
326328
}
327329

330+
/**
331+
* Returns whether or not an H3 index is valid for any mode (cell, directed
332+
* edge, or vertex).
333+
* @param h The H3 index to validate.
334+
* @return 1 if the H3 index is valid for any supported type, 0 otherwise.
335+
*/
336+
int H3_EXPORT(isValidIndex)(H3Index h) {
337+
return H3_EXPORT(isValidCell)(h) || H3_EXPORT(isValidDirectedEdge)(h) ||
338+
H3_EXPORT(isValidVertex)(h);
339+
}
340+
328341
/**
329342
* Initializes an H3 index.
330343
* @param hp The H3 index to initialize.

0 commit comments

Comments
 (0)