Skip to content

Commit 71d0e98

Browse files
committed
Adds new tests and asserts
1 parent 133400e commit 71d0e98

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

21_LRUCacheUnitTest/main.cpp

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,19 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
8383

8484
cache.print(m_logger);
8585

86-
// Previous gets left the cahce with LRU order 13 11 10 12
87-
8886
//non const, const
8987
int i = 0;
90-
// This insert makes LRU order 1 13 11 10 12
9188
cache.insert(++i, '1');
92-
// This insert evicts 12 -> 2 1 13 11 10
89+
assert(cache.getState() == "{12, e}, {10, c}, {11, d}, {13, f}, {1, 1}");
9390
cache.insert(++i, '2');
94-
// This insert evicts 10 -> 3 2 1 13 11
91+
assert(cache.getState() == "{10, c}, {11, d}, {13, f}, {1, 1}, {2, 2}");
9592
cache.insert(++i, '3');
93+
assert(cache.getState() == "{11, d}, {13, f}, {1, 1}, {2, 2}, {3, 3}");
9694
returned = *(cache.get(1));
9795
assert(returned == '1');
9896

9997
//const, non const
10098
char ch = 'N';
101-
// This insert evicts 11 -> 4 3 2 1 13
10299
cache.insert(4, ch);
103100

104101
returned = *(cache.peek(4));
@@ -107,7 +104,6 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
107104
//non const, non const
108105
i = 6;
109106
ch = 'Y';
110-
// This insert evicts 13 -> 6 4 3 2 1
111107
cache.insert(i, ch);
112108

113109
returned = *(cache.get(6));
@@ -128,10 +124,11 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
128124
m_logger->log("Clearing test");
129125
m_logger->log("Print contents before clearing");
130126
cache.print(m_logger);
131-
// Clearing is done in LRU order, so for current state 6 4 3 2 1 (6 being last recently used) you should see evictions in that order
127+
assert(cache.getState() == "{2, 2}, {3, 3}, {1, 1}, {4, N}, {6, Y}");
132128
cache.clear();
133129
m_logger->log("Print contents after clearing");
134130
cache.print(m_logger);
131+
assert(cache.getState() == "");
135132

136133
ResizableLRUCache<int, std::string> cache2(5u);
137134

@@ -146,6 +143,45 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
146143
cache2.print(m_logger);
147144
cache2.insert(++i, "key is 112");
148145

146+
// Grow test - try growing the cache
147+
m_logger->log("Growing test");
148+
auto previousState = cache2.getState();
149+
// Grow cache
150+
assert(cache2.grow(10));
151+
// Cache state should be the same
152+
assert(cache2.getState() == previousState);
153+
cache2.print(m_logger);
154+
cache2.insert(++i, "key is 113");
155+
cache2.insert(++i, "key is 114");
156+
cache2.insert(++i, "key is 115");
157+
cache2.insert(++i, "key is 116");
158+
cache2.insert(++i, "key is 117");
159+
cache2.print(m_logger);
160+
// Should evict key 52
161+
cache2.insert(++i, "key is 118");
162+
cache2.print(m_logger);
163+
const auto latestState = cache2.getState();
164+
assert(latestState == "{21, key is 21}, {22, key is 22}, {23, key is 23}, {112, key is 112}, {113, key is 113}, {114, key is 114}, {115, key is 115}, {116, key is 116}, {117, key is 117}, {118, key is 118}");
165+
// Invalid grow should fail
166+
assert(!cache2.grow(5));
167+
assert(!cache2.grow(10));
168+
// Call a bunch of grows that shouldn't fail and some others that should
169+
for (auto i = 1u; i < 50; i++)
170+
{
171+
assert(cache2.grow(50 * i));
172+
assert(!cache2.grow(25 * i));
173+
assert(!cache2.grow(50 * i));
174+
assert(cache2.getState() == latestState);
175+
}
176+
177+
// Single element cache test - checking for edge cases
178+
ResizableLRUCache<int, std::string> cache3(1u);
179+
cache3.insert(0, "foo");
180+
cache3.insert(1, "bar");
181+
cache3.clear();
182+
183+
m_logger->log("all good");
184+
149185
m_textureLRUCache = std::unique_ptr<TextureLRUCache>(new TextureLRUCache(1024u));
150186
{
151187
SIntendedSubmitInfo intendedNextSubmit = {};
@@ -164,26 +200,6 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL
164200
TextureReference* inserted = m_textureLRUCache->insert(69420, nextSemaSignal.value, evictionCallback);
165201
}
166202

167-
#ifdef _NBL_DEBUG
168-
cache2.print(m_logger);
169-
#endif
170-
171-
// Grow test - try growing the cache
172-
m_logger->log("Growing test");
173-
cache2.grow(10);
174-
cache2.print(m_logger);
175-
cache2.insert(++i, "key is 113");
176-
cache2.insert(++i, "key is 114");
177-
cache2.insert(++i, "key is 115");
178-
cache2.insert(++i, "key is 116");
179-
cache2.insert(++i, "key is 117");
180-
cache2.print(m_logger);
181-
// Should evict key 52
182-
cache2.insert(++i, "key is 118");
183-
m_logger->log("Key 52 should have been evicted:");
184-
cache2.print(m_logger);
185-
m_logger->log("all good");
186-
187203
constexpr uint32_t InvalidIdx = ~0u;
188204
struct TextureReference
189205
{

0 commit comments

Comments
 (0)