summaryrefslogtreecommitdiff
path: root/src/unittests/resources/resourcemanager/resourcemanager.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/unittests/resources/resourcemanager/resourcemanager.cc')
-rw-r--r--src/unittests/resources/resourcemanager/resourcemanager.cc701
1 files changed, 701 insertions, 0 deletions
diff --git a/src/unittests/resources/resourcemanager/resourcemanager.cc b/src/unittests/resources/resourcemanager/resourcemanager.cc
new file mode 100644
index 000000000..90bc873ab
--- /dev/null
+++ b/src/unittests/resources/resourcemanager/resourcemanager.cc
@@ -0,0 +1,701 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2013-2017 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "unittests/unittests.h"
+
+#include "client.h"
+#include "dirs.h"
+#include "logger.h"
+#include "graphicsmanager.h"
+
+#include "being/actorsprite.h"
+
+#include "fs/virtfs/fs.h"
+
+#include "gui/gui.h"
+
+#include "resources/sdlimagehelper.h"
+
+#include "resources/resourcemanager/resourcemanager.h"
+
+#include "utils/env.h"
+#include "utils/delete2.h"
+
+#include <unistd.h>
+
+PRAGMA48(GCC diagnostic push)
+PRAGMA48(GCC diagnostic ignored "-Wshadow")
+#ifndef USE_SDL2
+#include <SDL.h>
+#endif // USE_SDL2
+PRAGMA48(GCC diagnostic pop)
+
+#include "debug.h"
+
+namespace
+{
+ int testResouceCounter = 0;
+
+ class TestResource : public Resource
+ {
+ public:
+ TestResource() :
+ Resource()
+ {
+ testResouceCounter ++;
+ }
+
+ ~TestResource()
+ {
+ testResouceCounter --;
+ }
+ };
+
+ struct TestLoader final
+ {
+ std::string path;
+ static Resource *load(const void *const v)
+ {
+ BLOCK_START("TestLoader::load")
+ if (v == nullptr)
+ {
+ BLOCK_END("TestLoader::load")
+ return nullptr;
+ }
+
+ Resource *const res = new TestResource();
+ BLOCK_END("TestLoader::load")
+ return res;
+ }
+ };
+
+} // namespace
+
+TEST_CASE("resourcemanager leak test1", "")
+{
+ logger = new Logger();
+ REQUIRE(gui == nullptr);
+ ResourceManager::cleanOrphans(true);
+ ResourceManager::deleteInstance();
+ delete2(logger);
+}
+
+TEST_CASE("resourcemanager", "resourcemanager")
+{
+ setEnv("SDL_VIDEODRIVER", "dummy");
+
+ client = new Client;
+ XML::initXML();
+ SDL_Init(SDL_INIT_VIDEO);
+ logger = new Logger();
+ VirtFs::mountDirSilent("data", Append_false);
+ VirtFs::mountDirSilent("../data", Append_false);
+
+ imageHelper = new SDLImageHelper();
+#ifdef USE_SDL2
+ SDLImageHelper::setRenderer(graphicsManager.createRenderer(
+ graphicsManager.createWindow(640, 480, 0,
+ SDL_WINDOW_SHOWN | SDL_SWSURFACE), SDL_RENDERER_SOFTWARE));
+#else // USE_SDL2
+
+ graphicsManager.createWindow(640, 480, 0, SDL_ANYFORMAT | SDL_SWSURFACE);
+#endif // USE_SDL2
+
+ ActorSprite::load();
+
+ Dirs::initRootDir();
+ Dirs::initHomeDir();
+
+// ConfigManager::initConfiguration();
+// getConfigDefaults2(config.getDefaultValues());
+
+ while (ResourceManager::cleanOrphans(true))
+ continue;
+
+ testResouceCounter = 0;
+ const size_t resSize = ResourceManager::getResources().size();
+
+ SECTION("resourcemanager get 0")
+ {
+ REQUIRE(testResouceCounter == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ }
+
+ SECTION("resourcemanager get 1")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager get 2")
+ {
+ TestLoader rl = { "test1" };
+ Resource *const res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ res->mSource = "source 1";
+ REQUIRE(res->mSource == "source 1");
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ Resource *const res2 = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res2 != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 2);
+ REQUIRE(res2->mRefCount == 2);
+ REQUIRE(res->mIdPath == res2->mIdPath);
+ REQUIRE(res2->mSource == "source 1");
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res2);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ res2->decRef();
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager get 3")
+ {
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->mSource = "source 1";
+ res->decRef();
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager get 4")
+ {
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->mSource = "source 1";
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ Resource *const res2 = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res2 != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res2->mRefCount == 1);
+ REQUIRE(res->mIdPath == res2->mIdPath);
+ REQUIRE(res2->mSource == "source 1");
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res2);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res2->decRef();
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager get 5")
+ {
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(ResourceManager::isInCache("test1") == true);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->mSource = "source 1";
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().size() == 1);
+ REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ sleep(33);
+ ResourceManager::cleanOrphans();
+ REQUIRE(ResourceManager::isInCache("test1") == false);
+ REQUIRE(testResouceCounter == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ Resource *const res2 = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(ResourceManager::isInCache("test1") == true);
+ REQUIRE(res2 != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res2->mRefCount == 1);
+ REQUIRE(res2->mSource.empty());
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res2);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res2->decRef();
+ REQUIRE(res2->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager get 6")
+ {
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ res->mSource = "source 1";
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ Resource *const res2 = ResourceManager::get("test2",
+ TestLoader::load, &rl);
+ REQUIRE(res2 != nullptr);
+ REQUIRE(testResouceCounter == 2);
+ REQUIRE(res2->mRefCount == 1);
+ REQUIRE(res2->mSource.empty());
+ REQUIRE(ResourceManager::getResources().size() == 2 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getResources()["test2"] == res2);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ res2->decRef();
+ REQUIRE(res->mRefCount == 0);
+ REQUIRE(res2->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager getFromCache 1")
+ {
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ Resource *const res2 = ResourceManager::getFromCache("test1");
+ REQUIRE(res2 != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res2->mRefCount == 2);
+ REQUIRE(res == res2);
+ REQUIRE(res2->mSource.empty());
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res2);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager getFromCache 2")
+ {
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ Resource *const res2 = ResourceManager::getFromCache("test1");
+ REQUIRE(res2 != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res2->mRefCount == 1);
+ REQUIRE(res == res2);
+ REQUIRE(res2->mSource.empty());
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res2);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager getFromCache 3")
+ {
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ res->mSource = "source 1";
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ Resource *const res2 = ResourceManager::get("test2",
+ TestLoader::load, &rl);
+ REQUIRE(res2 != nullptr);
+ REQUIRE(testResouceCounter == 2);
+ REQUIRE(res2->mRefCount == 1);
+ REQUIRE(res2->mSource.empty());
+ REQUIRE(ResourceManager::getResources().size() == 2 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getResources()["test2"] == res2);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ Resource *const resC = ResourceManager::getFromCache("test1");
+ Resource *const res2C = ResourceManager::getFromCache("test2");
+ REQUIRE(resC != nullptr);
+ REQUIRE(res2C != nullptr);
+ REQUIRE(testResouceCounter == 2);
+ REQUIRE(res == resC);
+ REQUIRE(res2 == res2C);
+ REQUIRE(resC->mRefCount == 2);
+ REQUIRE(res2C->mRefCount == 2);
+ REQUIRE(ResourceManager::getResources().size() == 2 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == resC);
+ REQUIRE(ResourceManager::getResources()["test2"] == res2C);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ res->decRef();
+ res->decRef();
+ res2->decRef();
+ res2->decRef();
+ REQUIRE(res->mRefCount == 0);
+ REQUIRE(res2->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager addResource 1")
+ {
+ REQUIRE(testResouceCounter == 0);
+ Resource *res = new TestResource();
+ REQUIRE(testResouceCounter == 1);
+ ResourceManager::addResource("test1", res);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager isInCache 1")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ REQUIRE(ResourceManager::isInCache("test1") == false);
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(ResourceManager::isInCache("test1") == true);
+ REQUIRE(ResourceManager::isInCache("test2") == false);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager getTempResource 1")
+ {
+ TestLoader rl = { "test1" };
+ REQUIRE(ResourceManager::getTempResource("test1") == nullptr);
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(res != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ Resource *const res2 = ResourceManager::getTempResource("test1");
+ REQUIRE(ResourceManager::getTempResource("test2") == nullptr);
+ REQUIRE(res2 != nullptr);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res2->mRefCount == 1);
+ REQUIRE(res == res2);
+ REQUIRE(res2->mSource.empty());
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res2);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ REQUIRE(res->mRefCount == 0);
+ }
+
+ SECTION("resourcemanager moveToDeleted 1")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ ResourceManager::moveToDeleted(res);
+ REQUIRE(testResouceCounter == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ }
+
+ SECTION("resourcemanager moveToDeleted 2")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ res->incRef();
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 2);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ ResourceManager::moveToDeleted(res);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().size() == 1);
+ res->decRef();
+ }
+
+ SECTION("resourcemanager moveToDeleted 3")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ res->decRef();
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().size() == 1);
+ REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ ResourceManager::moveToDeleted(res);
+ REQUIRE(testResouceCounter == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ }
+
+ SECTION("resourcemanager decRefDelete 1")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+
+ ResourceManager::decRefDelete(res);
+ REQUIRE(testResouceCounter == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ }
+
+ SECTION("resourcemanager cleanUp 1")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+
+ ResourceManager::cleanUp(res);
+ REQUIRE(testResouceCounter == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().size() == 1);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ ResourceManager::getOrphanedResources().clear();
+ }
+
+ SECTION("resourcemanager cleanProtected 1")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->mProtected = true;
+ ResourceManager::cleanProtected();
+
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().size() == 1);
+ REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ res->decRef();
+ }
+
+ SECTION("resourcemanager clearDeleted 1")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ res->incRef();
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 2);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ ResourceManager::moveToDeleted(res);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().size() == 1);
+
+ ResourceManager::clearDeleted();
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().size() == 1);
+ res->decRef();
+ }
+
+ SECTION("resourcemanager clearDeleted 2")
+ {
+ REQUIRE(testResouceCounter == 0);
+ TestLoader rl = { "test1" };
+ Resource *res = ResourceManager::get("test1",
+ TestLoader::load, &rl);
+ res->incRef();
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res != nullptr);
+ REQUIRE(res->mRefCount == 2);
+ REQUIRE(ResourceManager::getResources().size() == 1 + resSize);
+ REQUIRE(ResourceManager::getResources()["test1"] == res);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ ResourceManager::moveToDeleted(res);
+ REQUIRE(testResouceCounter == 1);
+ REQUIRE(res->mRefCount == 1);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().size() == 1);
+
+ res->decRef();
+ ResourceManager::clearDeleted();
+ REQUIRE(testResouceCounter == 0);
+ REQUIRE(ResourceManager::getResources().size() == 0 + resSize);
+ REQUIRE(ResourceManager::getOrphanedResources().empty() == true);
+ REQUIRE(ResourceManager::getDeletedResources().empty() == true);
+ }
+
+ delete2(client);
+ VirtFs::unmountDirSilent("data");
+ VirtFs::unmountDirSilent("../data");
+ delete2(logger);
+// VirtFs::deinit();
+}
+
+TEST_CASE("resourcemanager leak test2", "")
+{
+ logger = new Logger();
+ REQUIRE(gui == nullptr);
+ ResourceManager::cleanOrphans(true);
+ ResourceManager::deleteInstance();
+ delete2(logger);
+}