mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-03 02:23:44 +00:00
cache system test
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
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::shared_ptr<T>>());
|
||||
std::vector<std::shared_ptr<T>> &matchingDataVector = this->_cache.at(path);
|
||||
|
||||
for (std::shared_ptr<T> &i: matchingDataVector) {
|
||||
if (!lonely)
|
||||
return i;
|
||||
if (lonely && i.use_count() == 1)
|
||||
return i;
|
||||
if (matchingDataVector.size()) {
|
||||
for (std::shared_ptr<T> &i: matchingDataVector) {
|
||||
if (!lonely)
|
||||
return i;
|
||||
if (lonely && i.use_count() == 1)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
matchingDataVector.emplace_back(std::shared_ptr<T>(
|
||||
matchingDataVector.push_back(std::shared_ptr<T>(
|
||||
new T(this->_dataLoader(path.c_str())), [this](T *p) {
|
||||
this->_dataUnloader(*p);
|
||||
delete p;
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#define private public
|
||||
#include <Drawables/Image.hpp>
|
||||
#include <Audio/Sound.hpp>
|
||||
#include <Audio/Music.hpp>
|
||||
#include <TraceLog.hpp>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
TEST_CASE("Cache test - New Load - Not lonely", "[Cache]")
|
||||
{
|
||||
RAY::TraceLog::setLevel(RAY::TraceLog::Level::LOG_NONE);
|
||||
RAY::Image myImage("assets/icon.png", false);
|
||||
|
||||
REQUIRE(RAY::Image::_imagesCache._cache.size() == 1);
|
||||
REQUIRE(RAY::Image::_imagesCache._cache.contains("assets/icon.png"));
|
||||
REQUIRE(RAY::Image::_imagesCache._cache["assets/icon.png"].size() == 1);
|
||||
REQUIRE(myImage._image.use_count() == 2);
|
||||
RAY::Image::_imagesCache._cache.clear();
|
||||
}
|
||||
|
||||
TEST_CASE("Cache test - New Load - Lonely", "[Cache][segf]")
|
||||
{
|
||||
RAY::TraceLog::setLevel(RAY::TraceLog::Level::LOG_NONE);
|
||||
RAY::Image myImage("assets/icon.png", false);
|
||||
|
||||
REQUIRE(RAY::Image::_imagesCache._cache.size() == 1);
|
||||
REQUIRE(RAY::Image::_imagesCache._cache.contains("assets/icon.png"));
|
||||
REQUIRE(RAY::Image::_imagesCache._cache["assets/icon.png"].size() == 1);
|
||||
REQUIRE(myImage._image.use_count() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("Cache test - Already loaded - Lonely", "[Cache]")
|
||||
{
|
||||
RAY::Audio::Sound mySound("assets/sounds/crate_break.ogg", false);
|
||||
RAY::Audio::Sound mySound2("assets/sounds/crate_break.ogg", true);
|
||||
|
||||
REQUIRE(RAY::Audio::Sound::_soundsCache._cache.size() == 1);
|
||||
REQUIRE(RAY::Audio::Sound::_soundsCache._cache.contains("assets/sounds/crate_break.ogg"));
|
||||
REQUIRE(RAY::Audio::Sound::_soundsCache._cache["assets/sounds/crate_break.ogg"].size() == 2);
|
||||
REQUIRE(mySound._sound.use_count() == 2);
|
||||
REQUIRE(mySound2._sound.use_count() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("Cache test - Already loaded - Not lonely", "[Cache]")
|
||||
{
|
||||
RAY::Audio::Music myMusic("assets/musics/music_result.ogg", false);
|
||||
RAY::Audio::Music myMusic2("assets/musics/music_result.ogg", false);
|
||||
|
||||
REQUIRE(RAY::Audio::Music::_musicsCache._cache.size() == 1);
|
||||
REQUIRE(RAY::Audio::Music::_musicsCache._cache.contains("assets/musics/music_result.ogg"));
|
||||
REQUIRE(RAY::Audio::Music::_musicsCache._cache["assets/musics/music_result.ogg"].size() == 1);
|
||||
REQUIRE(myMusic._music.use_count() == 3);
|
||||
REQUIRE(myMusic2._music.use_count() == 3);
|
||||
}
|
||||
Reference in New Issue
Block a user