Skip to content

Commit 8755d79

Browse files
committed
Add test to check proper cache destruction
1 parent b1cb0d8 commit 8755d79

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

21_LRUCacheUnitTest/main.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,42 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
180180
cache3.insert(1, "bar");
181181
cache3.clear();
182182

183+
// Besides the disposal function that gets called when evicting, we need to check that the Cache properly destroys all resident `Key,Value` pairs when destroyed
184+
struct Foo
185+
{
186+
int* destroyCounter;
187+
188+
Foo(int* _destroyCounter) : destroyCounter(_destroyCounter) {}
189+
190+
void operator=(Foo&& other)
191+
{
192+
destroyCounter = other.destroyCounter;
193+
other.destroyCounter = nullptr;
194+
}
195+
196+
Foo(Foo&& other)
197+
{
198+
operator=(std::move(other));
199+
}
200+
201+
~Foo()
202+
{
203+
if (destroyCounter)
204+
(*destroyCounter)++;
205+
}
206+
};
207+
208+
int destroyCounter = 0;
209+
{
210+
LRUCache<int, Foo> cache4(10u);
211+
for (int i = 0; i < 10; i++)
212+
cache4.insert(i, Foo(&destroyCounter));
213+
int x = 0;
214+
}
215+
216+
assert(destroyCounter == 10);
217+
218+
183219
m_logger->log("all good");
184220

185221
m_textureLRUCache = std::unique_ptr<TextureLRUCache>(new TextureLRUCache(1024u));

0 commit comments

Comments
 (0)