diff options
-rw-r--r-- | src/mbgl/geometry/binpack.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/util/rect.hpp | 7 | ||||
-rw-r--r-- | test/miscellaneous/binpack.cpp | 51 | ||||
-rw-r--r-- | test/test.gypi | 1 |
4 files changed, 62 insertions, 1 deletions
diff --git a/src/mbgl/geometry/binpack.hpp b/src/mbgl/geometry/binpack.hpp index 9aadaa202c..504417f452 100644 --- a/src/mbgl/geometry/binpack.hpp +++ b/src/mbgl/geometry/binpack.hpp @@ -23,7 +23,11 @@ public: if (width <= ref.w && height <= ref.h) { if (smallest == free.end() || (ref.y <= rect.y && ref.x <= rect.x)) { smallest = it; + } else { + // Our current "smallest" rect is already closer to 0/0. } + } else { + // The rect in the free list is not big enough. } } diff --git a/src/mbgl/util/rect.hpp b/src/mbgl/util/rect.hpp index b5f07eb23d..f4eb0679c4 100644 --- a/src/mbgl/util/rect.hpp +++ b/src/mbgl/util/rect.hpp @@ -12,10 +12,15 @@ struct Rect { T originalW = 0, originalH = 0; template <typename Number> - Rect operator *(Number value) const { + inline Rect operator *(Number value) const { return Rect(x * value, y * value, w * value, h * value); } + template <typename R> + inline bool operator==(const R& r) const { + return x == r.x && y == r.y && w == r.w && h == r.h; + } + inline bool hasArea() const { return w != 0 && h != 0; } }; } diff --git a/test/miscellaneous/binpack.cpp b/test/miscellaneous/binpack.cpp new file mode 100644 index 0000000000..a597f0a299 --- /dev/null +++ b/test/miscellaneous/binpack.cpp @@ -0,0 +1,51 @@ +#include "../fixtures/util.hpp" + +#include <mbgl/geometry/binpack.hpp> + +#include <iosfwd> +#include <array> + +namespace mbgl { +template <typename T> ::std::ostream& operator<<(::std::ostream& os, const Rect<T>& t) { + return os << "Rect { " << t.x << ", " << t.y << ", " << t.w << ", " << t.h << " }"; +} +} + +TEST(BinPack, Allocating) { + mbgl::BinPack<uint16_t> bin(128, 128); + std::array<mbgl::Rect<uint16_t>, 4> rects; + + rects[0] = bin.allocate(32, 48); + ASSERT_EQ(mbgl::Rect<uint16_t>(0, 0, 32, 48), rects[0]); + rects[1] = bin.allocate(8, 17); + ASSERT_EQ(mbgl::Rect<uint16_t>(32, 0, 8, 17), rects[1]); + rects[2] = bin.allocate(8, 17); + ASSERT_EQ(mbgl::Rect<uint16_t>(0, 48, 8, 17), rects[2]); + + bin.release(rects[0]); + rects[0] = bin.allocate(32, 24); + ASSERT_EQ(mbgl::Rect<uint16_t>(0, 0, 32, 24), rects[0]); + rects[3] = bin.allocate(32, 24); + ASSERT_EQ(mbgl::Rect<uint16_t>(32, 17, 32, 24), rects[3]); +} + + +TEST(BinPack, Full) { + mbgl::BinPack<uint16_t> bin(128, 128); + std::vector<mbgl::Rect<uint16_t>> rects; + + for (int j = 0; j < 3; j++) { + for (int i = 0; i < 256; i++) { + auto rect = bin.allocate(8, 8); + ASSERT_TRUE(rect.hasArea()); + rects.push_back(rect); + } + + ASSERT_FALSE(bin.allocate(8, 8).hasArea()); + + for (auto& rect: rects) { + bin.release(rect); + } + rects.clear(); + } +} diff --git a/test/test.gypi b/test/test.gypi index f86878b2f7..03bceaae4b 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -41,6 +41,7 @@ 'headless/headless.cpp', 'miscellaneous/clip_ids.cpp', + 'miscellaneous/binpack.cpp', 'miscellaneous/bilinear.cpp', 'miscellaneous/comparisons.cpp', 'miscellaneous/enums.cpp', |