c++: fix faulty roundtrip of z>15 tile IDs

This commit is contained in:
Brandon Liu
2022-12-22 07:53:34 +08:00
parent 910c48e994
commit f9cc6a20ac
2 changed files with 25 additions and 2 deletions

View File

@@ -387,7 +387,7 @@ inline zxy tileid_to_zxy(uint64_t tileid) {
uint64_t acc = 0; uint64_t acc = 0;
uint8_t t_z = 0; uint8_t t_z = 0;
while (true) { 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) { if (acc + num_tiles > tileid) {
return t_on_level(t_z, tileid - acc); 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) { inline uint64_t zxy_to_tileid(uint8_t z, uint32_t x, uint32_t y) {
uint64_t acc = 0; 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 n = 1 << z;
int64_t rx, ry, s, d = 0; int64_t rx, ry, s, d = 0;
int64_t tx = x; int64_t tx = x;

View File

@@ -45,6 +45,28 @@ MU_TEST(test_zxy_to_tileid) {
mu_check(zxy_to_tileid(2, 0, 0) == 5); 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) { MU_TEST(test_serialize_directory) {
vector<entryv3> entries; vector<entryv3> entries;
entries.push_back(entryv3(0, 0, 0, 0)); entries.push_back(entryv3(0, 0, 0, 0));
@@ -197,6 +219,7 @@ MU_TEST(test_build_dirs) {
MU_TEST_SUITE(test_suite) { MU_TEST_SUITE(test_suite) {
MU_RUN_TEST(test_tileid_to_zxy); MU_RUN_TEST(test_tileid_to_zxy);
MU_RUN_TEST(test_zxy_to_tileid); MU_RUN_TEST(test_zxy_to_tileid);
MU_RUN_TEST(test_roundtrip);
MU_RUN_TEST(test_serialize_directory); MU_RUN_TEST(test_serialize_directory);
MU_RUN_TEST(test_serialize_header); MU_RUN_TEST(test_serialize_header);
MU_RUN_TEST(test_deserialize_header); MU_RUN_TEST(test_deserialize_header);