ext/h3/src/src/apps/testapps/testCompact.c in h3-3.6.2 vs ext/h3/src/src/apps/testapps/testCompact.c in h3-3.7.1

- old
+ new

@@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ #include <stdlib.h> + #include "constants.h" #include "h3Index.h" #include "test.h" H3Index sunnyvale = 0x89283470c27ffff; @@ -148,9 +149,83 @@ } H3Index compressed[10]; t_assert(H3_EXPORT(compact)(someHexagons, compressed, numHex) != 0, "compact fails on duplicate input"); + } + + TEST(compact_duplicateMinimum) { + // Test that the minimum number of duplicate hexagons causes failure + H3Index h3; + int res = 10; + // Arbitrary index + setH3Index(&h3, res, 0, 2); + + int arrSize = H3_EXPORT(maxH3ToChildrenSize)(h3, res + 1) + 1; + H3Index* children = calloc(arrSize, sizeof(H3Index)); + + H3_EXPORT(h3ToChildren)(h3, res + 1, children); + // duplicate one index + children[arrSize - 1] = children[0]; + + H3Index* output = calloc(arrSize, sizeof(H3Index)); + + int compactResult = H3_EXPORT(compact)(children, output, arrSize); + t_assert(compactResult == COMPACT_DUPLICATE, + "compact fails on duplicate input (single duplicate)"); + + free(output); + free(children); + } + + TEST(compact_duplicatePentagonLimit) { + // Test that the minimum number of duplicate hexagons causes failure + H3Index h3; + int res = 10; + // Arbitrary pentagon parent cell + setH3Index(&h3, res, 4, 0); + + int arrSize = H3_EXPORT(maxH3ToChildrenSize)(h3, res + 1) + 1; + H3Index* children = calloc(arrSize, sizeof(H3Index)); + + H3_EXPORT(h3ToChildren)(h3, res + 1, children); + // duplicate one index + children[arrSize - 1] = H3_EXPORT(h3ToCenterChild)(h3, res + 1); + + H3Index* output = calloc(arrSize, sizeof(H3Index)); + + int compactResult = H3_EXPORT(compact)(children, output, arrSize); + t_assert(compactResult == COMPACT_DUPLICATE, + "compact fails on duplicate input (pentagon parent)"); + + free(output); + free(children); + } + + TEST(compact_duplicateIgnored) { + // Test that duplicated cells are not rejected by compact. + // This is not necessarily desired - just asserting the + // existing behavior. + H3Index h3; + int res = 10; + // Arbitrary index + setH3Index(&h3, res, 0, 2); + + int arrSize = H3_EXPORT(maxH3ToChildrenSize)(h3, res + 1); + H3Index* children = calloc(arrSize, sizeof(H3Index)); + + H3_EXPORT(h3ToChildren)(h3, res + 1, children); + // duplicate one index + children[arrSize - 1] = children[0]; + + H3Index* output = calloc(arrSize, sizeof(H3Index)); + + int compactResult = H3_EXPORT(compact)(children, output, arrSize); + t_assert(compactResult == COMPACT_SUCCESS, + "compact succeeds on duplicate input (correct count)"); + + free(output); + free(children); } TEST(compact_empty) { t_assert(H3_EXPORT(compact)(NULL, NULL, 0) == 0, "compact succeeds on empty input");