From 0174d121f1f7e4383033883953fadb09db5b3923 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Wed, 9 Jun 2021 12:25:21 +0200 Subject: [PATCH] cache system test --- CMakeLists.txt | 1 + lib/Ray/sources/Utils/Cache.hpp | 15 +++++---- tests/CacheTest.cpp | 57 +++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 tests/CacheTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 48dcf444..93c17008 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,7 @@ target_link_libraries(bomberman PUBLIC wal ray) add_executable(unit_tests EXCLUDE_FROM_ALL ${SOURCES} + tests/CacheTest.cpp tests/EntityTests.cpp tests/MainTest.cpp tests/EngineTests.cpp diff --git a/lib/Ray/sources/Utils/Cache.hpp b/lib/Ray/sources/Utils/Cache.hpp index c0e370d1..cea5dd71 100644 --- a/lib/Ray/sources/Utils/Cache.hpp +++ b/lib/Ray/sources/Utils/Cache.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace RAY { //! @brief A templated class used to cache ressources, indexed with a string @@ -42,13 +43,15 @@ namespace RAY { this->_cache.emplace(path, std::vector>()); std::vector> &matchingDataVector = this->_cache.at(path); - for (std::shared_ptr &i: matchingDataVector) { - if (!lonely) - return i; - if (lonely && i.use_count() == 1) - return i; + if (matchingDataVector.size()) { + for (std::shared_ptr &i: matchingDataVector) { + if (!lonely) + return i; + if (lonely && i.use_count() == 1) + return i; + } } - matchingDataVector.emplace_back(std::shared_ptr( + matchingDataVector.push_back(std::shared_ptr( new T(this->_dataLoader(path.c_str())), [this](T *p) { this->_dataUnloader(*p); delete p; diff --git a/tests/CacheTest.cpp b/tests/CacheTest.cpp new file mode 100644 index 00000000..7e30cd99 --- /dev/null +++ b/tests/CacheTest.cpp @@ -0,0 +1,57 @@ + +#include + +#define private public +#include +#include