|
|
|
|
@ -26,94 +26,115 @@ CORRADE_TEST_MAIN(Magnum::Test::ResourceManagerTest)
|
|
|
|
|
|
|
|
|
|
namespace Magnum { namespace Test { |
|
|
|
|
|
|
|
|
|
class Data { |
|
|
|
|
public: |
|
|
|
|
static std::size_t count; |
|
|
|
|
|
|
|
|
|
inline Data() { ++count; } |
|
|
|
|
inline ~Data() { --count; } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
typedef Magnum::ResourceManager<int32_t, Data> ResourceManager; |
|
|
|
|
|
|
|
|
|
size_t Data::count = 0; |
|
|
|
|
|
|
|
|
|
ResourceManagerTest::ResourceManagerTest() { |
|
|
|
|
rm = new ResourceManager; |
|
|
|
|
|
|
|
|
|
addTests(&ResourceManagerTest::state, |
|
|
|
|
&ResourceManagerTest::residentPolicy, |
|
|
|
|
&ResourceManagerTest::referenceCountedPolicy, |
|
|
|
|
&ResourceManagerTest::manualPolicy, |
|
|
|
|
&ResourceManagerTest::destroy); |
|
|
|
|
&ResourceManagerTest::manualPolicy); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ResourceManagerTest::state() { |
|
|
|
|
ResourceManager rm; |
|
|
|
|
|
|
|
|
|
ResourceKey questionKey("the-question"); |
|
|
|
|
rm->set(questionKey, new int32_t(10), ResourceDataState::Mutable, ResourcePolicy::Resident); |
|
|
|
|
Resource<int32_t> theQuestion = rm->get<int32_t>(questionKey); |
|
|
|
|
rm.set(questionKey, new int32_t(10), ResourceDataState::Mutable, ResourcePolicy::Resident); |
|
|
|
|
Resource<int32_t> theQuestion = rm.get<int32_t>(questionKey); |
|
|
|
|
CORRADE_VERIFY(theQuestion.state() == ResourceState::Mutable); |
|
|
|
|
CORRADE_COMPARE(*theQuestion, 10); |
|
|
|
|
|
|
|
|
|
/* Check that hash function is working properly */ |
|
|
|
|
ResourceKey answerKey("the-answer"); |
|
|
|
|
rm->set(answerKey, new int32_t(42), ResourceDataState::Final, ResourcePolicy::Resident); |
|
|
|
|
Resource<int32_t> theAnswer = rm->get<int32_t>(answerKey); |
|
|
|
|
rm.set(answerKey, new int32_t(42), ResourceDataState::Final, ResourcePolicy::Resident); |
|
|
|
|
Resource<int32_t> theAnswer = rm.get<int32_t>(answerKey); |
|
|
|
|
CORRADE_VERIFY(theAnswer.state() == ResourceState::Final); |
|
|
|
|
CORRADE_COMPARE(*theAnswer, 42); |
|
|
|
|
|
|
|
|
|
CORRADE_COMPARE(rm->count<int32_t>(), 2); |
|
|
|
|
CORRADE_COMPARE(rm.count<int32_t>(), 2); |
|
|
|
|
|
|
|
|
|
/* Cannot change already final resource */ |
|
|
|
|
stringstream out; |
|
|
|
|
Error::setOutput(&out); |
|
|
|
|
rm->set(answerKey, new int32_t(43), ResourceDataState::Mutable, ResourcePolicy::Resident); |
|
|
|
|
rm.set(answerKey, new int32_t(43), ResourceDataState::Mutable, ResourcePolicy::Resident); |
|
|
|
|
CORRADE_COMPARE(*theAnswer, 42); |
|
|
|
|
CORRADE_COMPARE(out.str(), "ResourceManager: cannot change already final resource\n"); |
|
|
|
|
|
|
|
|
|
/* Check non-final resource changes */ |
|
|
|
|
rm->set(questionKey, new int32_t(20), ResourceDataState::Final, ResourcePolicy::Resident); |
|
|
|
|
rm.set(questionKey, new int32_t(20), ResourceDataState::Final, ResourcePolicy::Resident); |
|
|
|
|
CORRADE_VERIFY(theQuestion.state() == ResourceState::Final); |
|
|
|
|
CORRADE_COMPARE(*theQuestion, 20); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ResourceManagerTest::residentPolicy() { |
|
|
|
|
ResourceManager* rm = new ResourceManager; |
|
|
|
|
|
|
|
|
|
rm->set("blah", new Data(), ResourceDataState::Mutable, ResourcePolicy::Resident); |
|
|
|
|
CORRADE_COMPARE(Data::count, 1); |
|
|
|
|
|
|
|
|
|
rm->free(); |
|
|
|
|
CORRADE_COMPARE(Data::count, 1); |
|
|
|
|
|
|
|
|
|
delete rm; |
|
|
|
|
CORRADE_COMPARE(Data::count, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ResourceManagerTest::referenceCountedPolicy() { |
|
|
|
|
ResourceManager rm; |
|
|
|
|
|
|
|
|
|
ResourceKey dataRefCountKey("dataRefCount"); |
|
|
|
|
|
|
|
|
|
/* Reference counted resources must be requested first */ |
|
|
|
|
{ |
|
|
|
|
rm->set(dataRefCountKey, new Data(), ResourceDataState::Final, ResourcePolicy::ReferenceCounted); |
|
|
|
|
CORRADE_COMPARE(rm->count<Data>(), 0); |
|
|
|
|
Resource<Data> data = rm->get<Data>(dataRefCountKey); |
|
|
|
|
rm.set(dataRefCountKey, new Data(), ResourceDataState::Final, ResourcePolicy::ReferenceCounted); |
|
|
|
|
CORRADE_COMPARE(rm.count<Data>(), 0); |
|
|
|
|
Resource<Data> data = rm.get<Data>(dataRefCountKey); |
|
|
|
|
CORRADE_VERIFY(data.state() == ResourceState::NotLoaded); |
|
|
|
|
CORRADE_COMPARE(Data::count, 0); |
|
|
|
|
} { |
|
|
|
|
Resource<Data> data = rm->get<Data>(dataRefCountKey); |
|
|
|
|
CORRADE_COMPARE(rm->count<Data>(), 1); |
|
|
|
|
Resource<Data> data = rm.get<Data>(dataRefCountKey); |
|
|
|
|
CORRADE_COMPARE(rm.count<Data>(), 1); |
|
|
|
|
CORRADE_VERIFY(data.state() == ResourceState::NotLoaded); |
|
|
|
|
rm->set(dataRefCountKey, new Data(), ResourceDataState::Final, ResourcePolicy::ReferenceCounted); |
|
|
|
|
rm.set(dataRefCountKey, new Data(), ResourceDataState::Final, ResourcePolicy::ReferenceCounted); |
|
|
|
|
CORRADE_VERIFY(data.state() == ResourceState::Final); |
|
|
|
|
CORRADE_COMPARE(Data::count, 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CORRADE_COMPARE(rm->count<Data>(), 0); |
|
|
|
|
CORRADE_COMPARE(rm.count<Data>(), 0); |
|
|
|
|
CORRADE_COMPARE(Data::count, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ResourceManagerTest::manualPolicy() { |
|
|
|
|
ResourceManager rm; |
|
|
|
|
|
|
|
|
|
ResourceKey dataKey("data"); |
|
|
|
|
|
|
|
|
|
/* Manual free */ |
|
|
|
|
{ |
|
|
|
|
rm->set(dataKey, new Data(), ResourceDataState::Mutable, ResourcePolicy::Manual); |
|
|
|
|
Resource<Data> data = rm->get<Data>(dataKey); |
|
|
|
|
rm->free(); |
|
|
|
|
rm.set(dataKey, new Data(), ResourceDataState::Mutable, ResourcePolicy::Manual); |
|
|
|
|
Resource<Data> data = rm.get<Data>(dataKey); |
|
|
|
|
rm.free(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CORRADE_COMPARE(rm->count<Data>(), 1); |
|
|
|
|
CORRADE_COMPARE(rm.count<Data>(), 1); |
|
|
|
|
CORRADE_COMPARE(Data::count, 1); |
|
|
|
|
rm->free(); |
|
|
|
|
CORRADE_COMPARE(rm->count<Data>(), 0); |
|
|
|
|
rm.free(); |
|
|
|
|
CORRADE_COMPARE(rm.count<Data>(), 0); |
|
|
|
|
CORRADE_COMPARE(Data::count, 0); |
|
|
|
|
|
|
|
|
|
rm->set(dataKey, new Data(), ResourceDataState::Mutable, ResourcePolicy::Manual); |
|
|
|
|
CORRADE_COMPARE(rm->count<Data>(), 1); |
|
|
|
|
rm.set(dataKey, new Data(), ResourceDataState::Mutable, ResourcePolicy::Manual); |
|
|
|
|
CORRADE_COMPARE(rm.count<Data>(), 1); |
|
|
|
|
CORRADE_COMPARE(Data::count, 1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void ResourceManagerTest::destroy() { |
|
|
|
|
delete rm; |
|
|
|
|
rm = nullptr; |
|
|
|
|
CORRADE_COMPARE(Data::count, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
}} |
|
|
|
|
|