/* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Vladimír Vondruš Copyright © 2019 Daniel Guzman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include "Magnum/AbstractResourceLoader.h" #include "Magnum/ResourceManager.h" namespace Magnum { namespace Test { namespace { struct ResourceManagerTest: TestSuite::Tester { explicit ResourceManagerTest(); void constructResource(); void constructResourceEmpty(); void constructResourceCopy(); void constructResourceMove(); void compare(); void state(); void stateFallback(); void stateDisallowed(); void basic(); void residentPolicy(); void referenceCountedPolicy(); void manualPolicy(); void defaults(); void clear(); void clearWhileReferenced(); void loader(); void loaderSetNullptr(); void debugResourceState(); void debugResourceKey(); }; struct Data { static std::size_t count; explicit Data() { ++count; } ~Data() { --count; } }; typedef Magnum::ResourceManager ResourceManager; size_t Data::count = 0; ResourceManagerTest::ResourceManagerTest() { addTests({&ResourceManagerTest::constructResource, &ResourceManagerTest::constructResourceEmpty, &ResourceManagerTest::constructResourceCopy, &ResourceManagerTest::constructResourceMove, &ResourceManagerTest::compare, &ResourceManagerTest::state, &ResourceManagerTest::stateFallback, &ResourceManagerTest::stateDisallowed, &ResourceManagerTest::basic, &ResourceManagerTest::residentPolicy, &ResourceManagerTest::referenceCountedPolicy, &ResourceManagerTest::manualPolicy, &ResourceManagerTest::defaults, &ResourceManagerTest::clear, &ResourceManagerTest::clearWhileReferenced, &ResourceManagerTest::loader, &ResourceManagerTest::loaderSetNullptr, &ResourceManagerTest::debugResourceState, &ResourceManagerTest::debugResourceKey}); } void ResourceManagerTest::constructResource() { ResourceManager rm; rm.set("thing", 6432); Resource a = rm.get("thing"); CORRADE_COMPARE(a.key(), ResourceKey("thing")); CORRADE_COMPARE(a.state(), ResourceState::Final); CORRADE_COMPARE(*a, 6432); CORRADE_COMPARE(rm.referenceCount("thing"), 1); } void ResourceManagerTest::constructResourceEmpty() { Resource a; CORRADE_COMPARE(a.key(), ResourceKey{}); CORRADE_COMPARE(a.state(), ResourceState::Final); CORRADE_VERIFY(!a); } void ResourceManagerTest::constructResourceCopy() { ResourceManager rm; rm.set("thing", 6432); Resource a = rm.get("thing"); CORRADE_COMPARE(rm.referenceCount("thing"), 1); Resource b = a; CORRADE_COMPARE(a.key(), ResourceKey("thing")); CORRADE_COMPARE(b.key(), ResourceKey("thing")); CORRADE_COMPARE(a.state(), ResourceState::Final); CORRADE_COMPARE(b.state(), ResourceState::Final); CORRADE_COMPARE(*a, 6432); CORRADE_COMPARE(*b, 6432); CORRADE_COMPARE(rm.referenceCount("thing"), 2); Resource c; c = b; CORRADE_COMPARE(b.key(), ResourceKey("thing")); CORRADE_COMPARE(c.key(), ResourceKey("thing")); CORRADE_COMPARE(b.state(), ResourceState::Final); CORRADE_COMPARE(c.state(), ResourceState::Final); CORRADE_COMPARE(*b, 6432); CORRADE_COMPARE(*c, 6432); CORRADE_COMPARE(rm.referenceCount("thing"), 3); } void ResourceManagerTest::constructResourceMove() { ResourceManager rm; rm.set("thing", 6432); Resource a = rm.get("thing"); CORRADE_COMPARE(rm.referenceCount("thing"), 1); Resource b = std::move(a); CORRADE_COMPARE(b.key(), ResourceKey("thing")); CORRADE_COMPARE(b.state(), ResourceState::Final); CORRADE_COMPARE(*b, 6432); CORRADE_COMPARE(rm.referenceCount("thing"), 1); Resource c; c = std::move(b); CORRADE_COMPARE(c.key(), ResourceKey("thing")); CORRADE_COMPARE(c.state(), ResourceState::Final); CORRADE_COMPARE(*c, 6432); CORRADE_COMPARE(rm.referenceCount("thing"), 1); CORRADE_VERIFY(std::is_nothrow_move_constructible>::value); CORRADE_VERIFY(std::is_nothrow_move_assignable>::value); } void ResourceManagerTest::compare() { ResourceManager rm1; ResourceKey resKeyA("keyA"); ResourceKey resKeyB("keyB"); rm1.set(resKeyA, 1); rm1.set(resKeyB, 0); Resource resA1 = rm1.get(resKeyA); Resource resA2 = rm1.get(resKeyA); Resource resB = rm1.get(resKeyB); CORRADE_VERIFY(resA1 == resA1); CORRADE_VERIFY(resA1 == resA2); CORRADE_VERIFY(resA1 != resB); Magnum::ResourceManager rm2; rm2.set(resKeyA, 1); rm2.set(resKeyA, 1.0f); Resource resAOther = rm2.get(resKeyA); Resource resADifferentType = rm2.get(resKeyA); /* Verify it checks for manager equality as well */ CORRADE_VERIFY(resA1 != resAOther); /* If the comparison operator wouldn't be deleted, the implicit conversion would kick in and then 1.0f == 1.0f, which is wrong. With the deleted operator this doesn't compile. */ //CORRADE_VERIFY(resA1 != resADifferentType); //CORRADE_VERIFY(!(resA1 == resADifferentType)); } void ResourceManagerTest::state() { ResourceManager rm; Resource data = rm.get("data"); CORRADE_VERIFY(!data); CORRADE_COMPARE(data.state(), ResourceState::NotLoaded); CORRADE_COMPARE(rm.state("data"), ResourceState::NotLoaded); rm.set("data", nullptr, ResourceDataState::Loading, ResourcePolicy::Resident); CORRADE_VERIFY(!data); CORRADE_COMPARE(data.state(), ResourceState::Loading); CORRADE_COMPARE(rm.state("data"), ResourceState::Loading); rm.set("data", nullptr, ResourceDataState::NotFound, ResourcePolicy::Resident); CORRADE_VERIFY(!data); CORRADE_COMPARE(data.state(), ResourceState::NotFound); CORRADE_COMPARE(rm.state("data"), ResourceState::NotFound); /* Nothing happened at all */ CORRADE_COMPARE(Data::count, 0); } void ResourceManagerTest::stateFallback() { { ResourceManager rm; rm.setFallback(Containers::pointer()); Resource data = rm.get("data"); CORRADE_VERIFY(data); CORRADE_COMPARE(data.state(), ResourceState::NotLoadedFallback); CORRADE_COMPARE(rm.state("data"), ResourceState::NotLoadedFallback); rm.set("data", nullptr, ResourceDataState::Loading, ResourcePolicy::Resident); CORRADE_VERIFY(data); CORRADE_COMPARE(data.state(), ResourceState::LoadingFallback); CORRADE_COMPARE(rm.state("data"), ResourceState::LoadingFallback); rm.set("data", nullptr, ResourceDataState::NotFound, ResourcePolicy::Resident); CORRADE_VERIFY(data); CORRADE_COMPARE(data.state(), ResourceState::NotFoundFallback); CORRADE_COMPARE(rm.state("data"), ResourceState::NotFoundFallback); /* Only fallback is here */ CORRADE_COMPARE(Data::count, 1); } /* Fallback gets destroyed */ CORRADE_COMPARE(Data::count, 0); } void ResourceManagerTest::stateDisallowed() { ResourceManager rm; std::ostringstream out; Error redirectError{&out}; Data d; /* Done this way to prevent memory leak on assertion (yes, the code is bad) */ rm.set("data", &d, ResourceDataState::Loading, ResourcePolicy::Resident); CORRADE_COMPARE(out.str(), "ResourceManager::set(): data should be null if and only if state is NotFound or Loading\n"); out.str({}); rm.set("data", nullptr, ResourceDataState::Final, ResourcePolicy::Resident); CORRADE_COMPARE(out.str(), "ResourceManager::set(): data should be null if and only if state is NotFound or Loading\n"); } void ResourceManagerTest::basic() { ResourceManager rm; /* One mutable, one final */ ResourceKey questionKey("the-question"); ResourceKey answerKey("the-answer"); rm.set(questionKey, 10, ResourceDataState::Mutable, ResourcePolicy::Resident); rm.set(answerKey, 42, ResourceDataState::Final, ResourcePolicy::Resident); Resource theQuestion = rm.get(questionKey); Resource theAnswer = rm.get(answerKey); /* Check basic functionality */ CORRADE_COMPARE(theQuestion.state(), ResourceState::Mutable); CORRADE_COMPARE(theAnswer.state(), ResourceState::Final); CORRADE_COMPARE(*theQuestion, 10); CORRADE_COMPARE(*theAnswer, 42); CORRADE_COMPARE(rm.count(), 2); /* Cannot change already final resource */ std::ostringstream out; Error redirectError{&out}; int a = 43; /* Done this way to prevent a memory leak on assert (yes, the code is bad) */ rm.set(answerKey, &a, ResourceDataState::Mutable, ResourcePolicy::Resident); CORRADE_COMPARE(*theAnswer, 42); CORRADE_COMPARE(out.str(), Utility::formatString("ResourceManager::set(): cannot change already final resource ResourceKey(0x{})\n", answerKey.hexString())); /* But non-final can be changed */ rm.set(questionKey, 20, ResourceDataState::Final, ResourcePolicy::Resident); CORRADE_COMPARE(theQuestion.state(), ResourceState::Final); CORRADE_COMPARE(*theQuestion, 20); } void ResourceManagerTest::residentPolicy() { { ResourceManager rm; rm.set("blah", Containers::pointer(), ResourceDataState::Mutable, ResourcePolicy::Resident); CORRADE_COMPARE(Data::count, 1); rm.free(); CORRADE_COMPARE(Data::count, 1); } CORRADE_COMPARE(Data::count, 0); } void ResourceManagerTest::referenceCountedPolicy() { ResourceManager rm; ResourceKey dataRefCountKey("dataRefCount"); /* Resource is deleted after all references are removed */ rm.set(dataRefCountKey, Containers::pointer(), ResourceDataState::Final, ResourcePolicy::ReferenceCounted); CORRADE_COMPARE(rm.count(), 1); { Resource data = rm.get(dataRefCountKey); CORRADE_COMPARE(data.state(), ResourceState::Final); CORRADE_COMPARE(Data::count, 1); } CORRADE_COMPARE(rm.count(), 0); CORRADE_COMPARE(Data::count, 0); /* Reference counted resources which were not used once will stay loaded until free() is called */ rm.set(dataRefCountKey, Containers::pointer(), ResourceDataState::Final, ResourcePolicy::ReferenceCounted); CORRADE_COMPARE(rm.count(), 1); CORRADE_COMPARE(rm.state(dataRefCountKey), ResourceState::Final); CORRADE_COMPARE(rm.referenceCount(dataRefCountKey), 0); rm.free(); CORRADE_COMPARE(rm.count(), 0); CORRADE_COMPARE(rm.state(dataRefCountKey), ResourceState::NotLoaded); CORRADE_COMPARE(rm.referenceCount(dataRefCountKey), 0); } void ResourceManagerTest::manualPolicy() { ResourceManager rm; ResourceKey dataKey("data"); /* Manual free */ { rm.set(dataKey, Containers::pointer(), ResourceDataState::Mutable, ResourcePolicy::Manual); Resource data = rm.get(dataKey); rm.free(); } CORRADE_COMPARE(rm.count(), 1); CORRADE_COMPARE(Data::count, 1); rm.free(); CORRADE_COMPARE(rm.count(), 0); CORRADE_COMPARE(Data::count, 0); rm.set(dataKey, Containers::pointer(), ResourceDataState::Mutable, ResourcePolicy::Manual); CORRADE_COMPARE(rm.count(), 1); CORRADE_COMPARE(Data::count, 1); } void ResourceManagerTest::defaults() { ResourceManager rm; rm.set("data", Containers::pointer()); CORRADE_COMPARE(rm.state("data"), ResourceState::Final); } void ResourceManagerTest::clear() { ResourceManager rm; rm.set("blah", Containers::pointer()); CORRADE_COMPARE(Data::count, 1); rm.free(); CORRADE_COMPARE(Data::count, 1); rm.clear(); CORRADE_COMPARE(Data::count, 0); } void ResourceManagerTest::clearWhileReferenced() { /* Should cover also the destruction case */ std::ostringstream out; Error redirectError{&out}; ResourceManager rm; int a{}; /* Done this way to prevent leak on assertion (yes, the code is bad) */ rm.set("blah", &a); /** @todo this will leak, is there any better solution without hitting assertion in decrementReferenceCount()? */ /** @todo remove the suppression from package/ci/leaksanitizer.conf then */ new Resource(rm.get("blah")); rm.clear(); CORRADE_COMPARE(out.str(), "ResourceManager: cleared/destroyed while data are still referenced\n"); } void ResourceManagerTest::loader() { class IntResourceLoader: public AbstractResourceLoader { public: IntResourceLoader(ResourceManager& instance): resource{instance.get("data")} {} void load() { set("hello", Containers::pointer(773), ResourceDataState::Final, ResourcePolicy::Resident); setNotFound("world"); } private: void doLoad(ResourceKey) override {} std::string doName(ResourceKey key) const override { if(key == ResourceKey("hello")) return "hello"; return ""; } /* To verify that the loader is destroyed before unloading _all types of_ resources */ Resource resource; }; { ResourceManager rm; Containers::Pointer loaderPtr{Containers::InPlaceInit, rm}; IntResourceLoader& loader = *loaderPtr; rm.setLoader(std::move(loaderPtr)); Resource data = rm.get("data"); Resource hello = rm.get("hello"); Resource world = rm.get("world"); CORRADE_COMPARE(data.state(), ResourceState::NotLoaded); CORRADE_COMPARE(hello.state(), ResourceState::Loading); CORRADE_COMPARE(world.state(), ResourceState::Loading); CORRADE_COMPARE(loader.requestedCount(), 2); CORRADE_COMPARE(loader.loadedCount(), 0); CORRADE_COMPARE(loader.notFoundCount(), 0); CORRADE_COMPARE(loader.name(ResourceKey("hello")), "hello"); loader.load(); CORRADE_COMPARE(hello.state(), ResourceState::Final); CORRADE_COMPARE(*hello, 773); CORRADE_COMPARE(world.state(), ResourceState::NotFound); CORRADE_COMPARE(loader.requestedCount(), 2); CORRADE_COMPARE(loader.loadedCount(), 1); CORRADE_COMPARE(loader.notFoundCount(), 1); /* Verify that the loader is deleted at proper time */ rm.set("data", Containers::pointer()); CORRADE_COMPARE(Data::count, 1); } CORRADE_COMPARE(Data::count, 0); } void ResourceManagerTest::loaderSetNullptr() { class IntResourceLoader: public AbstractResourceLoader { public: void load() { set("hello", 1337, ResourceDataState::Final, ResourcePolicy::Resident); set("world", 42, ResourceDataState::Final, ResourcePolicy::Resident); } private: void doLoad(ResourceKey key) override { /* Verify that calling load() with nullptr + Loading works */ set(key, nullptr, ResourceDataState::Loading, ResourcePolicy::Resident); set("world", nullptr, ResourceDataState::Loading, ResourcePolicy::Resident); } }; ResourceManager rm; Containers::Pointer loaderPtr{Containers::InPlaceInit}; IntResourceLoader& loader = *loaderPtr; rm.setLoader(std::move(loaderPtr)); CORRADE_COMPARE(rm.state("hello"), ResourceState::NotLoaded); CORRADE_COMPARE(rm.state("world"), ResourceState::NotLoaded); /* Loading "hello" triggers a load of "world" as well */ Resource hello = rm.get("hello"); CORRADE_COMPARE(hello.state(), ResourceState::Loading); CORRADE_COMPARE(rm.state("world"), ResourceState::Loading); CORRADE_COMPARE(loader.requestedCount(), 1); CORRADE_COMPARE(loader.loadedCount(), 0); CORRADE_COMPARE(loader.notFoundCount(), 0); /* Load the things */ loader.load(); CORRADE_COMPARE(hello.state(), ResourceState::Final); CORRADE_COMPARE(*hello, 1337); CORRADE_COMPARE(loader.requestedCount(), 1); CORRADE_COMPARE(loader.loadedCount(), 2); CORRADE_COMPARE(loader.notFoundCount(), 0); /* World is now loaded as well as a side-effect */ Resource world = rm.get("world"); CORRADE_COMPARE(world.state(), ResourceState::Final); CORRADE_COMPARE(*world, 42); } void ResourceManagerTest::debugResourceState() { std::ostringstream out; Debug{&out} << ResourceState::Loading << ResourceState(0xbe); CORRADE_COMPARE(out.str(), "ResourceState::Loading ResourceState(0xbe)\n"); } void ResourceManagerTest::debugResourceKey() { std::ostringstream out; ResourceKey hello = "hello"; Debug{&out} << hello; CORRADE_COMPARE(out.str(), Utility::formatString("ResourceKey(0x{})\n", hello.hexString())); } }}} CORRADE_TEST_MAIN(Magnum::Test::ResourceManagerTest)