Skip to content

Commit d38f5fa

Browse files
committed
MORE LRUCacheUnitTests
1 parent ccfc41a commit d38f5fa

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

21_LRUCacheUnitTest/main.cpp

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,21 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
4141
cache.insert(11, 'd');
4242
cache.insert(12, 'e');
4343
cache.insert(13, 'f');
44-
44+
cache.print(m_logger);
45+
m_logger->log("We're Referencing `10:c`");
46+
char returned = *(cache.get(10));
47+
cache.print(m_logger);
48+
m_logger->log("We're erasing `11:d`");
49+
cache.erase(11);
50+
assert(cache.get(11) == nullptr);
51+
cache.print(m_logger);
52+
m_logger->log("We're adding `11:d` again");
53+
cache.insert(11, 'd');
4554
cache.print(m_logger);
4655

47-
char returned = *(cache.get(11));
48-
assert(returned == 'd');
49-
returned = *(cache.get(10));
5056
assert(returned == 'c');
57+
returned = *(cache.get(11));
58+
assert(returned == 'd');
5159
returned = *(cache.get(13));
5260
assert(returned == 'f');
5361

@@ -105,6 +113,44 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
105113
#endif
106114
m_logger->log("all good");
107115

116+
constexpr uint32_t InvalidIdx = ~0u;
117+
struct TextureReference
118+
{
119+
uint32_t alloc_idx;
120+
uint64_t lastUsedSemaphoreValue;
121+
122+
// copy ctor
123+
TextureReference(const TextureReference& tref)
124+
{
125+
assert(false); // based on the code in this test, copy constuctor shouldn't be called
126+
}
127+
TextureReference(TextureReference&& tref) = default;
128+
inline TextureReference& operator=(TextureReference&& tref) = default;
129+
130+
TextureReference(uint32_t alloc_idx, uint64_t semaphoreVal) : alloc_idx(alloc_idx), lastUsedSemaphoreValue(semaphoreVal) {}
131+
TextureReference(uint64_t semaphoreVal) : TextureReference(InvalidIdx, semaphoreVal) {}
132+
TextureReference() : TextureReference(InvalidIdx, ~0ull) {}
133+
134+
// In LRU Cache `insert` function, in case of cache hit, we need to assign semaphore value to TextureReference without changing `alloc_idx`
135+
inline TextureReference& operator=(uint64_t semamphoreVal) { lastUsedSemaphoreValue = semamphoreVal; return *this; }
136+
};
137+
using TextureLRUCache = LRUCache<uint32_t, TextureReference>;
138+
139+
TextureLRUCache textureCache = TextureLRUCache(3u);
140+
141+
static_assert(std::is_assignable_v<TextureReference, uint64_t>);
142+
static_assert(std::is_constructible_v<TextureReference, uint64_t>);
143+
144+
textureCache.insert(91u, TextureReference{ ~0u, 69u });
145+
textureCache.insert(92u, TextureReference{ 20u, 70u });
146+
textureCache.insert(93u, TextureReference{ 10u, 71u });
147+
auto t = textureCache.get(91u);
148+
assert(t->lastUsedSemaphoreValue == 69u); // make 91 jump to front, now 92 is the LRU
149+
// next insertion will evict because capacity is 3
150+
auto insertion = textureCache.insert(99u, 6999ull, [](const TextureReference& evictedTextureRef) -> void { assert(evictedTextureRef.alloc_idx == 20u); });
151+
assert(insertion->alloc_idx == InvalidIdx);
152+
assert(insertion->lastUsedSemaphoreValue == 6999ull);
153+
108154
return true;
109155
}
110156

0 commit comments

Comments
 (0)