diff --git a/cpp/pmtiles.hpp b/cpp/pmtiles.hpp index 0dec4b7..0b3de55 100644 --- a/cpp/pmtiles.hpp +++ b/cpp/pmtiles.hpp @@ -387,7 +387,7 @@ inline zxy tileid_to_zxy(uint64_t tileid) { uint64_t acc = 0; uint8_t t_z = 0; while (true) { - uint64_t num_tiles = (1 << t_z) * (1 << t_z); + uint64_t num_tiles = (1LL << t_z) * (1LL << t_z); if (acc + num_tiles > tileid) { return t_on_level(t_z, tileid - acc); } @@ -398,7 +398,7 @@ inline zxy tileid_to_zxy(uint64_t tileid) { inline uint64_t zxy_to_tileid(uint8_t z, uint32_t x, uint32_t y) { uint64_t acc = 0; - for (uint8_t t_z = 0; t_z < z; t_z++) acc += (0x1 << t_z) * (0x1 << t_z); + for (uint8_t t_z = 0; t_z < z; t_z++) acc += (1LL << t_z) * (1LL << t_z); int64_t n = 1 << z; int64_t rx, ry, s, d = 0; int64_t tx = x; diff --git a/cpp/test.cpp b/cpp/test.cpp index 8d47c74..90e8217 100644 --- a/cpp/test.cpp +++ b/cpp/test.cpp @@ -45,6 +45,28 @@ MU_TEST(test_zxy_to_tileid) { mu_check(zxy_to_tileid(2, 0, 0) == 5); } +MU_TEST(test_roundtrip) { + for (int z = 0; z < 25; z++) { + uint64_t dim = (1 << z) - 1; + auto tl = tileid_to_zxy(zxy_to_tileid(z, 0, 0)); + mu_check(tl.z == z); + mu_check(tl.x == 0); + mu_check(tl.y == 0); + auto tr = tileid_to_zxy(zxy_to_tileid(z, dim, 0)); + mu_check(tr.z == z); + mu_check(tr.x == dim); + mu_check(tr.y == 0); + auto bl = tileid_to_zxy(zxy_to_tileid(z, 0, dim)); + mu_check(bl.z == z); + mu_check(bl.x == 0); + mu_check(bl.y == dim); + auto br = tileid_to_zxy(zxy_to_tileid(z, dim, dim)); + mu_check(br.z == z); + mu_check(br.x == dim); + mu_check(br.y == dim); + } +} + MU_TEST(test_serialize_directory) { vector entries; entries.push_back(entryv3(0, 0, 0, 0)); @@ -197,6 +219,7 @@ MU_TEST(test_build_dirs) { MU_TEST_SUITE(test_suite) { MU_RUN_TEST(test_tileid_to_zxy); MU_RUN_TEST(test_zxy_to_tileid); + MU_RUN_TEST(test_roundtrip); MU_RUN_TEST(test_serialize_directory); MU_RUN_TEST(test_serialize_header); MU_RUN_TEST(test_deserialize_header);