diff options
Diffstat (limited to 'test/util')
-rw-r--r-- | test/util/geo.test.cpp | 125 | ||||
-rw-r--r-- | test/util/grid_index.test.cpp | 53 | ||||
-rw-r--r-- | test/util/mapbox.test.cpp | 1 | ||||
-rw-r--r-- | test/util/memory.test.cpp | 6 | ||||
-rw-r--r-- | test/util/unique_any.test.cpp | 186 |
5 files changed, 368 insertions, 3 deletions
diff --git a/test/util/geo.test.cpp b/test/util/geo.test.cpp index d0d01b6f88..6832ba3486 100644 --- a/test/util/geo.test.cpp +++ b/test/util/geo.test.cpp @@ -220,3 +220,128 @@ TEST(LatLngBounds, FromTileID) { ASSERT_DOUBLE_EQ(util::LATITUDE_MAX, bounds.north()); } } + +TEST(LatLngBounds, ContainsPoint) { + auto bounds = LatLngBounds::hull({50.0, -100.0},{-50.0, 100.0}); + + EXPECT_FALSE(bounds.contains(LatLng{0.0, 170.0})); + EXPECT_FALSE(bounds.contains(LatLng{0.0, -170.0})); + EXPECT_TRUE(bounds.contains(LatLng{0.0, -100.0})); + EXPECT_TRUE(bounds.contains(LatLng{-50.0, 100.0})); + EXPECT_FALSE(bounds.contains(LatLng{0.0, 365.0})); +} + +TEST(LatLngBounds, ContainsPoint_Wrapped) { + auto bounds = LatLngBounds::hull({50.0, -160.0}, {-50.0, 160.0}); + EXPECT_FALSE(bounds.contains(LatLng{0.0, 170.0})); + EXPECT_FALSE(bounds.contains(LatLng{0.0, -170.0})); + + bounds = LatLngBounds::hull({50.0, -200}, {-50.0, -160.0}); + EXPECT_FALSE(bounds.contains(LatLng{0.0, 170.0})); + EXPECT_TRUE(bounds.contains(LatLng{0.0, 170.0}, LatLng::Wrapped)); + EXPECT_TRUE(bounds.contains(LatLng{0.0, -170.0})); + EXPECT_TRUE(bounds.contains(LatLng{0.0, -170.0}, LatLng::Wrapped)); + EXPECT_FALSE(bounds.contains(LatLng{0.0, 190.0})); + EXPECT_TRUE(bounds.contains(LatLng{0.0, 190.0}, LatLng::Wrapped)); + EXPECT_FALSE(bounds.contains(LatLng{0.0, 541.0})); + EXPECT_TRUE(bounds.contains(LatLng{0.0, 541.0}, LatLng::Wrapped)); +} + +TEST(LatLngBounds, ContainsBounds) { + auto bounds = LatLngBounds::hull({ 50.0, -160.0 }, {-50.0, 160.0}); + EXPECT_TRUE(bounds.contains(bounds)); + + auto innerBounds = LatLngBounds::hull({10.0, -180.0}, {-10.0, -170.0}); + EXPECT_FALSE(bounds.contains(innerBounds)); + EXPECT_FALSE(innerBounds.contains(bounds)); + + innerBounds = LatLngBounds::hull({10, 120.0}, {-60, 125.0}); + EXPECT_FALSE(bounds.contains(innerBounds)); + EXPECT_FALSE(innerBounds.contains(bounds)); + + innerBounds = LatLngBounds::hull({10, 120.0}, {-10, 125.0}); + EXPECT_TRUE(bounds.contains(innerBounds)); + EXPECT_FALSE(innerBounds.contains(bounds)); + +} + +TEST(LatLngBounds, ContainsBounds_Wrapped) { + auto bounds = LatLngBounds::hull({50.0, -200}, {-50.0, -160.0}); + + auto inner = LatLngBounds::hull({10.0, -180.0}, {-10.0, -170.0}); + EXPECT_TRUE(bounds.contains(inner)); + EXPECT_TRUE(bounds.contains(inner, LatLng::Wrapped)); + + inner = LatLngBounds::hull({10.0, 180.0}, {-10.0, 190.0}); + EXPECT_FALSE(bounds.contains(inner)); + EXPECT_TRUE(bounds.contains(inner, LatLng::Wrapped)); + + inner = LatLngBounds::hull({10.0, 190.0}, {-10.0, 220.0}); + EXPECT_FALSE(bounds.contains(inner)); + EXPECT_FALSE(bounds.contains(inner, LatLng::Wrapped)); + + auto unwrapped = LatLngBounds::hull({10.0, 170.0}, { -10.0, -175.0}); + EXPECT_FALSE(bounds.contains(unwrapped)); + EXPECT_FALSE(bounds.contains(unwrapped, LatLng::Wrapped)); + + unwrapped = LatLngBounds::hull({10.0, 0.0} , {-10.0, -10.0}); + EXPECT_FALSE(bounds.contains(unwrapped)); + EXPECT_FALSE(bounds.contains(unwrapped, LatLng::Wrapped)); + + unwrapped = LatLngBounds::hull({10.0, -165.0}, {-10.0, -180.0}); + EXPECT_TRUE(bounds.contains(unwrapped)); + EXPECT_TRUE(bounds.contains(unwrapped, LatLng::Wrapped)); + + unwrapped = LatLngBounds::hull({10.0, 180.0}, {-10.0, 160.0}); + EXPECT_FALSE(bounds.contains(unwrapped)); + EXPECT_TRUE(bounds.contains(unwrapped, LatLng::Wrapped)); + + unwrapped = LatLngBounds::hull({10.0, 540.0}, {-10.0, 560.0}); + EXPECT_FALSE(bounds.contains(unwrapped)); + EXPECT_TRUE(bounds.contains(unwrapped, LatLng::Wrapped)); +} + +TEST(LatLngBounds, ContainsTileIDs) { + LatLngBounds bounds(CanonicalTileID(4,2,6)); + LatLngBounds innerBounds(CanonicalTileID(9,82,197)); + EXPECT_TRUE(bounds.contains(innerBounds)); + EXPECT_FALSE(bounds.contains(LatLngBounds{ CanonicalTileID(3, 1, 0) })); +} + +TEST(LatLngBounds, Intersects) { + auto bounds = LatLngBounds::hull({ 50.0, -160.0 }, { -50.0, 160.0 }); + EXPECT_TRUE(bounds.intersects(bounds)); + + auto other = LatLngBounds::hull({50.0, -160.0}, {10, 160.0}); + EXPECT_TRUE(bounds.intersects(other)); + EXPECT_TRUE(other.intersects(bounds)); +} + +TEST(LatLngBounds, Intersects_Wrapped) { + auto bounds = LatLngBounds::hull({50.0, -200.0}, {-50.0, -160.0}); + EXPECT_TRUE(bounds.intersects(bounds)); + + auto other = LatLngBounds::hull({50.0, -150.0}, {10, 160.0}); + EXPECT_FALSE(bounds.intersects(other)); + EXPECT_FALSE(other.intersects(bounds)); + EXPECT_FALSE(bounds.intersects(other, LatLng::Wrapped)); + EXPECT_FALSE(other.intersects(bounds, LatLng::Wrapped)); + + other = LatLngBounds::hull({10.0, -150.0}, {-10.0, -210.0}); + EXPECT_TRUE(bounds.intersects(other)); + EXPECT_TRUE(bounds.intersects(other, LatLng::Wrapped)); + EXPECT_TRUE(other.intersects(bounds)); + EXPECT_TRUE(other.intersects(bounds, LatLng::Wrapped)); + + other = LatLngBounds::hull({10.0, 150.0}, {-10.0, 210.0}); + EXPECT_FALSE(bounds.intersects(other)); + EXPECT_FALSE(other.intersects(bounds)); + EXPECT_TRUE(bounds.intersects(other, LatLng::Wrapped)); + EXPECT_TRUE(other.intersects(bounds, LatLng::Wrapped)); + + other = LatLngBounds::hull({10.0, 195.0}, {-10.0, 300.0}); + EXPECT_FALSE(bounds.intersects(other)); + EXPECT_FALSE(other.intersects(bounds)); + EXPECT_TRUE(bounds.intersects(other, LatLng::Wrapped)); + EXPECT_TRUE(other.intersects(bounds, LatLng::Wrapped)); +} diff --git a/test/util/grid_index.test.cpp b/test/util/grid_index.test.cpp new file mode 100644 index 0000000000..b0a4e581a3 --- /dev/null +++ b/test/util/grid_index.test.cpp @@ -0,0 +1,53 @@ +#include <mbgl/util/grid_index.hpp> +#include <mbgl/util/grid_index.cpp> + +#include <mbgl/test/util.hpp> + +using namespace mbgl; + +TEST(GridIndex, IndexesFeatures) { + GridIndex<int16_t> grid(100, 100, 10); + grid.insert(0, {{4, 10}, {6, 30}}); + grid.insert(1, {{4, 10}, {30, 12}}); + grid.insert(2, {{-10, 30}, {5, 35}}); + + EXPECT_EQ(grid.query({{4, 10}, {5, 11}}), (std::vector<int16_t>{0, 1})); + EXPECT_EQ(grid.query({{24, 10}, {25, 11}}), (std::vector<int16_t>{1})); + EXPECT_EQ(grid.query({{40, 40}, {100, 100}}), (std::vector<int16_t>{})); + EXPECT_EQ(grid.query({{-6, 0}, {3, 100}}), (std::vector<int16_t>{2})); + EXPECT_EQ(grid.query({{-1000, -1000}, {1000, 1000}}), (std::vector<int16_t>{0, 1, 2})); +} + +TEST(GridIndex, DuplicateKeys) { + GridIndex<int16_t> grid(100, 100, 10); + #define KEY 123 + grid.insert(KEY, {{3, 4}, {4, 4}}); + grid.insert(KEY, {{13, 13}, {14, 14}}); + grid.insert(KEY, {{23, 23}, {24, 24}}); + + EXPECT_EQ(grid.query({{0, 0}, {30, 30}}), (std::vector<int16_t>{KEY, KEY, KEY})); +} + +TEST(GridIndex, CircleCircle) { + GridIndex<int16_t> grid(100, 100, 10); + grid.insert(0, {{50, 50}, 10}); + grid.insert(1, {{60, 60}, 15}); + grid.insert(2, {{-10, 110}, 20}); + + EXPECT_TRUE(grid.hitTest({{55, 55}, 2})); + EXPECT_FALSE(grid.hitTest({{10, 10}, 10})); + EXPECT_TRUE(grid.hitTest({{0, 100}, 10})); + EXPECT_TRUE(grid.hitTest({{80, 60}, 10})); +} + +TEST(GridIndex, CircleBox) { + GridIndex<int16_t> grid(100, 100, 10); + grid.insert(0, {{50, 50}, 10}); + grid.insert(1, {{60, 60}, 15}); + grid.insert(2, {{-10, 110}, 20}); + + EXPECT_EQ(grid.query({{45, 45}, {55, 55}}), (std::vector<int16_t>{0, 1})); + EXPECT_EQ(grid.query({{0, 0}, {30, 30}}), (std::vector<int16_t>{})); + EXPECT_EQ(grid.query({{0, 80}, {20, 100}}), (std::vector<int16_t>{2})); +} + diff --git a/test/util/mapbox.test.cpp b/test/util/mapbox.test.cpp index cdbd85118f..301475dae4 100644 --- a/test/util/mapbox.test.cpp +++ b/test/util/mapbox.test.cpp @@ -7,6 +7,7 @@ #include <stdexcept> using namespace mbgl; +using SourceType = mbgl::style::SourceType; TEST(Mapbox, SourceURL) { EXPECT_EQ( diff --git a/test/util/memory.test.cpp b/test/util/memory.test.cpp index 54763cd9db..6befb521f0 100644 --- a/test/util/memory.test.cpp +++ b/test/util/memory.test.cpp @@ -72,7 +72,7 @@ TEST(Memory, Vector) { HeadlessFrontend frontend { { 256, 256 }, ratio, test.fileSource, test.threadPool }; Map map(frontend, MapObserver::nullObserver(), frontend.getSize(), ratio, test.fileSource, - test.threadPool, MapMode::Still); + test.threadPool, MapMode::Static); map.setZoom(16); // more map features map.getStyle().loadURL("mapbox://streets"); @@ -85,7 +85,7 @@ TEST(Memory, Raster) { HeadlessFrontend frontend { { 256, 256 }, ratio, test.fileSource, test.threadPool }; Map map(frontend, MapObserver::nullObserver(), frontend.getSize(), ratio, test.fileSource, - test.threadPool, MapMode::Still); + test.threadPool, MapMode::Static); map.getStyle().loadURL("mapbox://satellite"); frontend.render(map); @@ -122,7 +122,7 @@ TEST(Memory, Footprint) { public: FrontendAndMap(MemoryTest& test_, const char* style) : frontend(Size{ 256, 256 }, 2, test_.fileSource, test_.threadPool) - , map(frontend, MapObserver::nullObserver(), frontend.getSize(), 2, test_.fileSource, test_.threadPool, MapMode::Still) { + , map(frontend, MapObserver::nullObserver(), frontend.getSize(), 2, test_.fileSource, test_.threadPool, MapMode::Static) { map.setZoom(16); map.getStyle().loadURL(style); frontend.render(map); diff --git a/test/util/unique_any.test.cpp b/test/util/unique_any.test.cpp new file mode 100644 index 0000000000..9357b9c0ec --- /dev/null +++ b/test/util/unique_any.test.cpp @@ -0,0 +1,186 @@ +#include <mbgl/test/util.hpp> + +#include <mbgl/util/unique_any.hpp> + +using namespace mbgl::util; + +class TestType { +public: + TestType() : i1(0), i2(1) { + str[0] = 'a'; + } + + TestType(unique_any& p) : TestType() { + p = std::unique_ptr<TestType>(this); + } + + //Detect moves + TestType(TestType&& t): i1(t.i1+1), i2(t.i2+2) { + str[0] = t.str[0]+1; + } + + int i1; + int i2; + char str[256]; +}; + +bool IsStackAllocated (const unique_any& a, const void* obj1) { + uintptr_t a_ptr = (uintptr_t)(&a); + uintptr_t obj = (uintptr_t)(obj1); + return (obj >= a_ptr && obj < a_ptr + sizeof(unique_any)); +}; + +TEST(UniqueAny, Empty) { + EXPECT_FALSE(unique_any().has_value()); + EXPECT_TRUE(unique_any().type() == typeid(void)); + EXPECT_THROW(any_cast<int>(unique_any()), bad_any_cast); +} + +TEST(UniqueAny, BasicTypes) { + unique_any i = 3; + EXPECT_TRUE(i.has_value()); + EXPECT_TRUE(i.type() == typeid(int)); + EXPECT_TRUE(IsStackAllocated(i, any_cast<int>(&i))); + + auto iValue = any_cast<int>(i); + EXPECT_TRUE(iValue == 3); + + EXPECT_TRUE(unique_any(4).has_value()); + EXPECT_TRUE(unique_any(4).type() == typeid(int)); + + unique_any f = 6.2f; + EXPECT_TRUE(f.has_value()); + EXPECT_TRUE(f.type() == typeid(float)); + EXPECT_TRUE(IsStackAllocated(f, any_cast<float>(&f))); + + const float fValue = any_cast<const float>(f); + EXPECT_TRUE(fValue == 6.2f); + + EXPECT_TRUE(unique_any(1.0f).has_value()); + EXPECT_TRUE(unique_any(1.0f).type() == typeid(float)); + + unique_any c = 'z'; + EXPECT_TRUE(c.has_value()); + EXPECT_TRUE(c.type() == typeid(char)); + EXPECT_TRUE(IsStackAllocated(c, any_cast<char>(&c))); + + EXPECT_THROW(any_cast<float>(c), bad_any_cast); + + EXPECT_TRUE(unique_any('4').has_value()); + EXPECT_TRUE(unique_any('4').type() == typeid(char)); +} + +TEST(UniqueAny, BasicTypes_Move) { + unique_any i = 3; + EXPECT_TRUE(i.has_value()); + EXPECT_TRUE(i.type() == typeid(int)); + + unique_any f = 6.2f; + EXPECT_TRUE(f.has_value()); + EXPECT_TRUE(f.type() == typeid(float)); + + f = std::move(i); + EXPECT_FALSE(i.has_value()); + EXPECT_TRUE(i.type() == typeid(void)); + + EXPECT_TRUE(f.has_value()); + EXPECT_TRUE(f.type() == typeid(int)); + +} + +TEST(UniqueAny, LargeType) { + TestType t1; + unique_any u1 = unique_any(std::move(t1)); + EXPECT_TRUE(u1.has_value()); + EXPECT_TRUE(u1.type() == typeid(TestType)); + EXPECT_FALSE(IsStackAllocated(u1, any_cast<TestType>(&u1))); + + //TestType should be moved into owning unique_any + EXPECT_EQ(any_cast<TestType>(&u1)->i1, 1); + + auto u2(std::move(u1)); + EXPECT_TRUE(u2.type() == typeid(TestType)); + EXPECT_TRUE(u1.type() == typeid(void)); + + //TestType should not be moved when owning unique_any is moved; + EXPECT_EQ(any_cast<TestType>(&u2)->i1, 1); + + //TestType should be moved out of owning unique_any + // Note: two moves are involved in returning the moved value + // First out of the unique_any, and then in the return statement + auto t2 = any_cast<TestType>(std::move(u2)); + EXPECT_EQ(t2.i1, 3); + EXPECT_TRUE(u2.type() == typeid(void)); +} + +TEST(UniqueAny, Pointer) { + auto t1 = new TestType(); + + auto u1 = unique_any(std::move(t1)); + EXPECT_TRUE(u1.has_value()); + EXPECT_TRUE(u1.type() == typeid(TestType *)); + EXPECT_TRUE(IsStackAllocated(u1, any_cast<TestType *>(&u1))); + + //Only the pointer should be moved + TestType * t2 = *any_cast<TestType *>(&u1); + EXPECT_EQ(t2->i1, 0); + + unique_any u2(4); + std::swap(u2, u1); + + EXPECT_TRUE(u1.has_value()); + EXPECT_TRUE(u1.type() == typeid(int)); + + EXPECT_TRUE(u2.has_value()); + EXPECT_TRUE(u2.type() == typeid(TestType *)); + + t2 = *any_cast<TestType *>(&u2); + EXPECT_EQ(t2->i1, 0); + delete t2; +} + + +TEST(UniqueAny, UniquePtr) { + auto t1 = std::make_unique<TestType>(); + auto u1 = unique_any(std::move(t1)); + + EXPECT_EQ(t1.get(), nullptr); + EXPECT_TRUE(u1.has_value()); + EXPECT_TRUE(u1.type() == typeid(std::unique_ptr<TestType>)); + + EXPECT_TRUE(IsStackAllocated(u1, any_cast<std::unique_ptr<TestType>>(&u1))); + + auto t2 = any_cast<std::unique_ptr<TestType> >(std::move(u1)); + EXPECT_FALSE(u1.has_value()); + + unique_any u2; + TestType * t3 = new TestType(); + u2 = std::unique_ptr<TestType>(t3); + EXPECT_TRUE(u2.has_value()); + EXPECT_TRUE(any_cast<std::unique_ptr<TestType>>(&u2)->get() == t3); +} + +TEST(UniqueAny, SharedPtr) { + + std::shared_ptr<int> shared(new int(3)); + std::weak_ptr<int> weak = shared; + unique_any u1 = 0; + + EXPECT_THROW(any_cast<float>(u1), bad_any_cast); + + EXPECT_EQ(weak.use_count(), 1); + unique_any u2 = shared; + EXPECT_EQ(weak.use_count(), 2); + + EXPECT_EQ(any_cast<std::unique_ptr<int>>(&u1), nullptr); + EXPECT_FALSE(IsStackAllocated(u1, any_cast<std::shared_ptr<TestType>>(&u1))); + + u1 = std::move(u2); + EXPECT_EQ(weak.use_count(), 2); + u2.swap(u1); + EXPECT_EQ(weak.use_count(), 2); + u2 = 0; + EXPECT_EQ(weak.use_count(), 1); + shared = nullptr; + EXPECT_EQ(weak.use_count(), 0); +} |