diff --git a/next/test/CMakeLists.txt b/next/test/CMakeLists.txt index 4995fa4e56b..12a6d8108cc 100644 --- a/next/test/CMakeLists.txt +++ b/next/test/CMakeLists.txt @@ -77,6 +77,7 @@ add_library( ${MBGL_ROOT}/test/tile/geometry_tile_data.test.cpp ${MBGL_ROOT}/test/tile/raster_dem_tile.test.cpp ${MBGL_ROOT}/test/tile/raster_tile.test.cpp + ${MBGL_ROOT}/test/tile/tile_cache.test.cpp ${MBGL_ROOT}/test/tile/tile_coordinate.test.cpp ${MBGL_ROOT}/test/tile/tile_id.test.cpp ${MBGL_ROOT}/test/tile/vector_tile.test.cpp diff --git a/src/mbgl/tile/tile_cache.cpp b/src/mbgl/tile/tile_cache.cpp index d9363ead8fc..272ecdfee58 100644 --- a/src/mbgl/tile/tile_cache.cpp +++ b/src/mbgl/tile/tile_cache.cpp @@ -8,7 +8,7 @@ void TileCache::setSize(size_t size_) { while (orderedKeys.size() > size) { auto key = orderedKeys.front(); - orderedKeys.pop_front(); + orderedKeys.remove(key); tiles.erase(key); } @@ -21,7 +21,7 @@ void TileCache::add(const OverscaledTileID& key, std::unique_ptr tile) { } // insert new or query existing tile - if (tiles.emplace(key, std::move(tile)).second) { + if (!tiles.emplace(key, std::move(tile)).second) { // remove existing tile key orderedKeys.remove(key); } diff --git a/test/test-files.json b/test/test-files.json index a98e896e7e7..1d220f579d1 100644 --- a/test/test-files.json +++ b/test/test-files.json @@ -79,6 +79,7 @@ "test/tile/geometry_tile_data.test.cpp", "test/tile/raster_dem_tile.test.cpp", "test/tile/raster_tile.test.cpp", + "test/tile/tile_cache.test.cpp", "test/tile/tile_coordinate.test.cpp", "test/tile/tile_id.test.cpp", "test/tile/vector_tile.test.cpp", diff --git a/test/tile/tile_cache.test.cpp b/test/tile/tile_cache.test.cpp new file mode 100644 index 00000000000..e64ad0bb79c --- /dev/null +++ b/test/tile/tile_cache.test.cpp @@ -0,0 +1,87 @@ +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace mbgl; + +class VectorTileTest { +public: + std::shared_ptr fileSource = std::make_shared(); + TransformState transformState; + util::RunLoop loop; + style::Style style{*fileSource, 1}; + AnnotationManager annotationManager{style}; + ImageManager imageManager; + GlyphManager glyphManager; + Tileset tileset{{"https://example.com"}, {0, 22}, "none"}; + + TileParameters tileParameters{1.0, + MapDebugOptions(), + transformState, + fileSource, + MapMode::Continuous, + annotationManager, + imageManager, + glyphManager, + 0}; +}; + +class VectorTileMock : public VectorTile { +public: + VectorTileMock(const OverscaledTileID& id, + std::string sourceID, + const TileParameters& parameters, + const Tileset& tileset) + : VectorTile(id, sourceID, parameters, tileset) { + renderable = true; + } +}; + +TEST(TileCache, Smoke) { + VectorTileTest test; + TileCache cache(1); + OverscaledTileID id(0, 0, 0); + std::unique_ptr tile = std::make_unique(id, "source", test.tileParameters, test.tileset); + + cache.add(id, std::move(tile)); + EXPECT_TRUE(cache.has(id)); + cache.clear(); + EXPECT_FALSE(cache.has(id)); +} + +TEST(TileCache, Issue15926) { + VectorTileTest test; + TileCache cache(2); + OverscaledTileID id0(0, 0, 0); + OverscaledTileID id1(1, 0, 0); + std::unique_ptr tile1 = std::make_unique(id0, "source", test.tileParameters, test.tileset); + std::unique_ptr tile2 = std::make_unique(id0, "source", test.tileParameters, test.tileset); + std::unique_ptr tile3 = std::make_unique(id1, "source", test.tileParameters, test.tileset); + + cache.add(id0, std::move(tile1)); + EXPECT_TRUE(cache.has(id0)); + cache.add(id0, std::move(tile2)); + cache.setSize(1); + cache.add(id1, std::move(tile3)); + EXPECT_FALSE(cache.has(id0)); + EXPECT_TRUE(cache.has(id1)); +}