| 1 | // |
| 2 | // LRUCacheTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "LRUCacheTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/Exception.h" |
| 15 | #include "Poco/LRUCache.h" |
| 16 | #include "Poco/Bugcheck.h" |
| 17 | #include "Poco/Delegate.h" |
| 18 | |
| 19 | |
| 20 | using namespace Poco; |
| 21 | |
| 22 | |
| 23 | LRUCacheTest::LRUCacheTest(const std::string& rName): CppUnit::TestCase(rName) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | |
| 28 | LRUCacheTest::~LRUCacheTest() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | |
| 33 | void LRUCacheTest::testClear() |
| 34 | { |
| 35 | LRUCache<int, int> aCache(3); |
| 36 | assertTrue (aCache.size() == 0); |
| 37 | assertTrue (aCache.getAllKeys().size() == 0); |
| 38 | aCache.add(1, 2); |
| 39 | aCache.add(3, 4); |
| 40 | aCache.add(5, 6); |
| 41 | assertTrue (aCache.size() == 3); |
| 42 | assertTrue (aCache.getAllKeys().size() == 3); |
| 43 | assertTrue (aCache.has(1)); |
| 44 | assertTrue (aCache.has(3)); |
| 45 | assertTrue (aCache.has(5)); |
| 46 | assertTrue (*aCache.get(1) == 2); |
| 47 | assertTrue (*aCache.get(3) == 4); |
| 48 | assertTrue (*aCache.get(5) == 6); |
| 49 | aCache.clear(); |
| 50 | assertTrue (!aCache.has(1)); |
| 51 | assertTrue (!aCache.has(3)); |
| 52 | assertTrue (!aCache.has(5)); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void LRUCacheTest::testCacheSize0() |
| 57 | { |
| 58 | // cache size 0 is illegal |
| 59 | try |
| 60 | { |
| 61 | LRUCache<int, int> aCache(0); |
| 62 | failmsg ("cache size of 0 is illegal, test should fail" ); |
| 63 | } |
| 64 | catch (Poco::InvalidArgumentException&) |
| 65 | { |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | |
| 70 | void LRUCacheTest::testCacheSize1() |
| 71 | { |
| 72 | LRUCache<int, int> aCache(1); |
| 73 | aCache.add(1, 2); |
| 74 | assertTrue (aCache.has(1)); |
| 75 | assertTrue (*aCache.get(1) == 2); |
| 76 | |
| 77 | aCache.add(3, 4); // replaces 1 |
| 78 | assertTrue (!aCache.has(1)); |
| 79 | assertTrue (aCache.has(3)); |
| 80 | assertTrue (*aCache.get(3) == 4); |
| 81 | |
| 82 | aCache.add(5, 6); |
| 83 | assertTrue (!aCache.has(1)); |
| 84 | assertTrue (!aCache.has(3)); |
| 85 | assertTrue (aCache.has(5)); |
| 86 | assertTrue (*aCache.get(5) == 6); |
| 87 | |
| 88 | aCache.remove(5); |
| 89 | assertTrue (!aCache.has(5)); |
| 90 | |
| 91 | // removing illegal entries should work too |
| 92 | aCache.remove(666); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void LRUCacheTest::testCacheSize2() |
| 97 | { |
| 98 | // 3-1 represents the cache sorted by pos, elements get replaced at the end of the list |
| 99 | // 3-1|5 -> 5 gets removed |
| 100 | LRUCache<int, int> aCache(2); |
| 101 | aCache.add(1, 2); // 1 |
| 102 | assertTrue (aCache.has(1)); |
| 103 | assertTrue (*aCache.get(1) == 2); |
| 104 | |
| 105 | aCache.add(3, 4); // 3-1 |
| 106 | assertTrue (aCache.has(1)); |
| 107 | assertTrue (aCache.has(3)); |
| 108 | assertTrue (*aCache.get(1) == 2); // 1-3 |
| 109 | assertTrue (*aCache.get(3) == 4); // 3-1 |
| 110 | |
| 111 | aCache.add(5, 6); // 5-3|1 |
| 112 | assertTrue (!aCache.has(1)); |
| 113 | assertTrue (aCache.has(3)); |
| 114 | assertTrue (aCache.has(5)); |
| 115 | assertTrue (*aCache.get(5) == 6); // 5-3 |
| 116 | assertTrue (*aCache.get(3) == 4); // 3-5 |
| 117 | |
| 118 | // test remove from the end and the beginning of the list |
| 119 | aCache.remove(5); // 3 |
| 120 | assertTrue (!aCache.has(5)); |
| 121 | assertTrue (*aCache.get(3) == 4); // 3 |
| 122 | aCache.add(5, 6); // 5-3 |
| 123 | assertTrue (*aCache.get(3) == 4); // 3-5 |
| 124 | aCache.remove(3); // 5 |
| 125 | assertTrue (!aCache.has(3)); |
| 126 | assertTrue (*aCache.get(5) == 6); // 5 |
| 127 | |
| 128 | // removing illegal entries should work too |
| 129 | aCache.remove(666); |
| 130 | |
| 131 | aCache.clear(); |
| 132 | assertTrue (!aCache.has(5)); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void LRUCacheTest::testCacheSizeN() |
| 137 | { |
| 138 | // 3-1 represents the cache sorted by pos, elements get replaced at the end of the list |
| 139 | // 3-1|5 -> 5 gets removed |
| 140 | LRUCache<int, int> aCache(3); |
| 141 | aCache.add(1, 2); // 1 |
| 142 | assertTrue (aCache.has(1)); |
| 143 | assertTrue (*aCache.get(1) == 2); |
| 144 | |
| 145 | aCache.add(3, 4); // 3-1 |
| 146 | assertTrue (aCache.has(1)); |
| 147 | assertTrue (aCache.has(3)); |
| 148 | assertTrue (*aCache.get(1) == 2); // 1-3 |
| 149 | assertTrue (*aCache.get(3) == 4); // 3-1 |
| 150 | |
| 151 | aCache.add(5, 6); // 5-3-1 |
| 152 | assertTrue (aCache.has(1)); |
| 153 | assertTrue (aCache.has(3)); |
| 154 | assertTrue (aCache.has(5)); |
| 155 | assertTrue (*aCache.get(5) == 6); // 5-3-1 |
| 156 | assertTrue (*aCache.get(3) == 4); // 3-5-1 |
| 157 | |
| 158 | aCache.add(7, 8); // 7-5-3|1 |
| 159 | assertTrue (!aCache.has(1)); |
| 160 | assertTrue (aCache.has(7)); |
| 161 | assertTrue (aCache.has(3)); |
| 162 | assertTrue (aCache.has(5)); |
| 163 | assertTrue (*aCache.get(5) == 6); // 5-7-3 |
| 164 | assertTrue (*aCache.get(3) == 4); // 3-5-7 |
| 165 | assertTrue (*aCache.get(7) == 8); // 7-3-5 |
| 166 | |
| 167 | // test remove from the end and the beginning of the list |
| 168 | aCache.remove(5); // 7-3 |
| 169 | assertTrue (!aCache.has(5)); |
| 170 | assertTrue (*aCache.get(3) == 4); // 3-7 |
| 171 | aCache.add(5, 6); // 5-3-7 |
| 172 | assertTrue (*aCache.get(7) == 8); // 7-5-3 |
| 173 | aCache.remove(7); // 5-3 |
| 174 | assertTrue (!aCache.has(7)); |
| 175 | assertTrue (aCache.has(3)); |
| 176 | assertTrue (*aCache.get(5) == 6); // 5-3 |
| 177 | |
| 178 | // removing illegal entries should work too |
| 179 | aCache.remove(666); |
| 180 | |
| 181 | aCache.clear(); |
| 182 | assertTrue (!aCache.has(5)); |
| 183 | assertTrue (!aCache.has(3)); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void LRUCacheTest::testDuplicateAdd() |
| 188 | { |
| 189 | LRUCache<int, int> aCache(3); |
| 190 | aCache.add(1, 2); // 1 |
| 191 | assertTrue (aCache.has(1)); |
| 192 | assertTrue (*aCache.get(1) == 2); |
| 193 | aCache.add(1, 3); |
| 194 | assertTrue (aCache.has(1)); |
| 195 | assertTrue (*aCache.get(1) == 3); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | void LRUCacheTest::testUpdate() |
| 200 | { |
| 201 | addCnt = 0; |
| 202 | updateCnt = 0; |
| 203 | removeCnt = 0; |
| 204 | LRUCache<int, int> aCache(3); |
| 205 | aCache.Add += delegate(this, &LRUCacheTest::onAdd); |
| 206 | aCache.Remove += delegate(this, &LRUCacheTest::onRemove); |
| 207 | aCache.Update += delegate(this, &LRUCacheTest::onUpdate); |
| 208 | aCache.add(1, 2); // 1 ,one add event |
| 209 | assertTrue (addCnt == 1); |
| 210 | assertTrue (updateCnt == 0); |
| 211 | assertTrue (removeCnt == 0); |
| 212 | |
| 213 | assertTrue (aCache.has(1)); |
| 214 | assertTrue (*aCache.get(1) == 2); |
| 215 | aCache.update(1, 3); // one update event only! |
| 216 | assertTrue (addCnt == 1); |
| 217 | assertTrue (updateCnt == 1); |
| 218 | assertTrue (removeCnt == 0); |
| 219 | assertTrue (aCache.has(1)); |
| 220 | assertTrue (*aCache.get(1) == 3); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | void LRUCacheTest::onUpdate(const void* pSender, const Poco::KeyValueArgs<int, int>& args) |
| 225 | { |
| 226 | ++updateCnt; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | void LRUCacheTest::onAdd(const void* pSender, const Poco::KeyValueArgs<int, int>& args) |
| 231 | { |
| 232 | ++addCnt; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | void LRUCacheTest::onRemove(const void* pSender, const int& args) |
| 237 | { |
| 238 | ++removeCnt; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | void LRUCacheTest::setUp() |
| 243 | { |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void LRUCacheTest::tearDown() |
| 248 | { |
| 249 | } |
| 250 | |
| 251 | |
| 252 | CppUnit::Test* LRUCacheTest::suite() |
| 253 | { |
| 254 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("LRUCacheTest" ); |
| 255 | |
| 256 | CppUnit_addTest(pSuite, LRUCacheTest, testClear); |
| 257 | CppUnit_addTest(pSuite, LRUCacheTest, testCacheSize0); |
| 258 | CppUnit_addTest(pSuite, LRUCacheTest, testCacheSize1); |
| 259 | CppUnit_addTest(pSuite, LRUCacheTest, testCacheSize2); |
| 260 | CppUnit_addTest(pSuite, LRUCacheTest, testCacheSizeN); |
| 261 | CppUnit_addTest(pSuite, LRUCacheTest, testDuplicateAdd); |
| 262 | CppUnit_addTest(pSuite, LRUCacheTest, testUpdate); |
| 263 | |
| 264 | return pSuite; |
| 265 | } |
| 266 | |