From 50130c101e07c531c6e1a0d6048b07b2808ec00b Mon Sep 17 00:00:00 2001 From: Yury Melnichek Date: Fri, 23 Sep 2011 13:57:43 +0200 Subject: [PATCH] Fix CellId.ToInt64(depth) when depth <= m_Level. --- geometry/cellid.hpp | 26 +++++++++++++++---------- geometry/geometry_tests/cellid_test.cpp | 18 +++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/geometry/cellid.hpp b/geometry/cellid.hpp index f37054c328..ea1df53b5f 100644 --- a/geometry/cellid.hpp +++ b/geometry/cellid.hpp @@ -238,18 +238,24 @@ public: { ASSERT(0 < depth && depth <= DEPTH_LEVELS, (m_Bits, m_Level, depth)); ASSERT(IsValid(), (m_Bits, m_Level)); - uint64_t bits = m_Bits, res = 0; - for (int i = 0; i <= m_Level; ++i, bits >>= 2) - res += bits + 1; - bits = m_Bits; - for (int i = m_Level + 1; i < depth; ++i) + + if (m_Level >= depth) + return AncestorAtLevel(depth - 1).ToInt64(depth); + else { - bits <<= 2; - res += bits; + uint64_t bits = m_Bits, res = 0; + for (int i = 0; i <= m_Level; ++i, bits >>= 2) + res += bits + 1; + bits = m_Bits; + for (int i = m_Level + 1; i < depth; ++i) + { + bits <<= 2; + res += bits; + } + ASSERT_GREATER(res, 0, (m_Bits, m_Level)); + ASSERT_LESS_OR_EQUAL(res, TreeSizeForDepth(depth), (m_Bits, m_Level)); + return static_cast(res); } - ASSERT_GREATER(res, 0, (m_Bits, m_Level)); - ASSERT_LESS_OR_EQUAL(res, TreeSizeForDepth(depth), (m_Bits, m_Level)); - return static_cast(res); } // Level order, numbering by Z-curve. diff --git a/geometry/geometry_tests/cellid_test.cpp b/geometry/geometry_tests/cellid_test.cpp index 07e7ed2ec7..a664f33e9e 100644 --- a/geometry/geometry_tests/cellid_test.cpp +++ b/geometry/geometry_tests/cellid_test.cpp @@ -49,6 +49,24 @@ UNIT_TEST(CellId_ToInt64) TEST_EQUAL(m2::CellId<3>("33").ToInt64(3), 21, ()); } +UNIT_TEST(CellId_ToInt64_LevelLessThanDepth) +{ + TEST_EQUAL(m2::CellId<3>("").ToInt64(2), 1, ()); + TEST_EQUAL(m2::CellId<3>("0").ToInt64(2), 2, ()); + TEST_EQUAL(m2::CellId<3>("1").ToInt64(2), 3, ()); + TEST_EQUAL(m2::CellId<3>("2").ToInt64(2), 4, ()); + TEST_EQUAL(m2::CellId<3>("3").ToInt64(2), 5, ()); + TEST_EQUAL(m2::CellId<3>("00").ToInt64(2), 2, ()); + TEST_EQUAL(m2::CellId<3>("01").ToInt64(2), 2, ()); + TEST_EQUAL(m2::CellId<3>("03").ToInt64(2), 2, ()); + TEST_EQUAL(m2::CellId<3>("10").ToInt64(2), 3, ()); + TEST_EQUAL(m2::CellId<3>("20").ToInt64(2), 4, ()); + TEST_EQUAL(m2::CellId<3>("23").ToInt64(2), 4, ()); + TEST_EQUAL(m2::CellId<3>("30").ToInt64(2), 5, ()); + TEST_EQUAL(m2::CellId<3>("31").ToInt64(2), 5, ()); + TEST_EQUAL(m2::CellId<3>("33").ToInt64(2), 5, ()); +} + UNIT_TEST(CellId_FromInt64) { TEST_EQUAL(m2::CellId<3>(""), m2::CellId<3>::FromInt64(1, 3), ());