|
5 | 5 |
|
6 | 6 | // I've moved out a tiny part of this example into a shared header for reuse, please open and read it. |
7 | 7 | #include "nbl/application_templates/MonoSystemMonoLoggerApplication.hpp" |
| 8 | +#include <ranges> |
8 | 9 |
|
9 | 10 | using namespace nbl; |
10 | 11 | using namespace core; |
@@ -180,6 +181,38 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL |
180 | 181 | cache3.insert(1, "bar"); |
181 | 182 | cache3.clear(); |
182 | 183 |
|
| 184 | + // Cache iterator test |
| 185 | + constexpr uint32_t cache4Size = 10; |
| 186 | + ResizableLRUCache<uint32_t, uint32_t> cache4(cache4Size); |
| 187 | + for (auto i = 0u; i < cache4Size; i++) |
| 188 | + { |
| 189 | + cache4.insert(i, i); |
| 190 | + } |
| 191 | + // Default iterator is MRU -> LRU |
| 192 | + uint32_t counter = cache4Size - 1; |
| 193 | + for (auto& pair : cache4) |
| 194 | + { |
| 195 | + assert(pair.first == counter && pair.second == counter); |
| 196 | + counter--; |
| 197 | + } |
| 198 | + // Reverse LRU -> MRU traversal |
| 199 | + counter = 0u; |
| 200 | + for (auto it = cache4.crbegin(); it != cache4.crend(); it++) |
| 201 | + { |
| 202 | + assert(it->first == counter && it->second == counter); |
| 203 | + counter++; |
| 204 | + } |
| 205 | + |
| 206 | + // Cache copy test |
| 207 | + ResizableLRUCache<uint32_t, uint32_t> cache4Copy(cache4); |
| 208 | + for (auto it = cache4.cbegin(), itCopy = cache4Copy.cbegin(); it != cache4.cend(); it++, itCopy++) |
| 209 | + { |
| 210 | + assert(*it == *itCopy); |
| 211 | + // Assert deep copy |
| 212 | + assert(it.operator->() != itCopy.operator->()); |
| 213 | + |
| 214 | + } |
| 215 | + |
183 | 216 | // 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 | 217 | struct Foo |
185 | 218 | { |
@@ -208,15 +241,13 @@ class LRUCacheTestApp final : public nbl::application_templates::MonoSystemMonoL |
208 | 241 |
|
209 | 242 | int destroyCounter = 0; |
210 | 243 | { |
211 | | - ResizableLRUCache<int, Foo> cache4(10u); |
| 244 | + ResizableLRUCache<int, Foo> cache5(10u); |
212 | 245 | for (int i = 0; i < 10; i++) |
213 | | - cache4.insert(i, Foo(&destroyCounter)); |
| 246 | + cache5.insert(i, Foo(&destroyCounter)); |
214 | 247 | int x = 0; |
215 | 248 | } |
216 | | - |
217 | 249 | assert(destroyCounter == 10); |
218 | 250 |
|
219 | | - |
220 | 251 | m_logger->log("all good"); |
221 | 252 |
|
222 | 253 | m_textureLRUCache = std::unique_ptr<TextureLRUCache>(new TextureLRUCache(1024u)); |
|
0 commit comments