Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 18 | |
| 19 | #include <android/log.h> |
| 20 | #include <gtest/gtest.h> |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 21 | #include <utils/JenkinsHash.h> |
| 22 | #include <utils/LruCache.h> |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 23 | |
Dan Albert | 27d166c | 2014-10-16 20:47:51 -0700 | [diff] [blame] | 24 | namespace { |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 25 | |
| 26 | typedef int SimpleKey; |
| 27 | typedef const char* StringValue; |
| 28 | |
| 29 | struct ComplexKey { |
| 30 | int k; |
| 31 | |
Fabien Sanglard | 2aba0a2 | 2023-06-14 23:08:02 +0000 | [diff] [blame] | 32 | explicit ComplexKey() : k(0) { instanceCount += 1; } |
| 33 | |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 34 | explicit ComplexKey(int k) : k(k) { |
| 35 | instanceCount += 1; |
| 36 | } |
| 37 | |
| 38 | ComplexKey(const ComplexKey& other) : k(other.k) { |
| 39 | instanceCount += 1; |
| 40 | } |
| 41 | |
| 42 | ~ComplexKey() { |
| 43 | instanceCount -= 1; |
| 44 | } |
| 45 | |
| 46 | bool operator ==(const ComplexKey& other) const { |
| 47 | return k == other.k; |
| 48 | } |
| 49 | |
| 50 | bool operator !=(const ComplexKey& other) const { |
| 51 | return k != other.k; |
| 52 | } |
| 53 | |
| 54 | static ssize_t instanceCount; |
| 55 | }; |
| 56 | |
| 57 | ssize_t ComplexKey::instanceCount = 0; |
| 58 | |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 59 | struct ComplexValue { |
| 60 | int v; |
| 61 | |
Fabien Sanglard | 2aba0a2 | 2023-06-14 23:08:02 +0000 | [diff] [blame] | 62 | explicit ComplexValue() : v(0) { instanceCount += 1; } |
| 63 | |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 64 | explicit ComplexValue(int v) : v(v) { |
| 65 | instanceCount += 1; |
| 66 | } |
| 67 | |
| 68 | ComplexValue(const ComplexValue& other) : v(other.v) { |
| 69 | instanceCount += 1; |
| 70 | } |
| 71 | |
| 72 | ~ComplexValue() { |
| 73 | instanceCount -= 1; |
| 74 | } |
| 75 | |
| 76 | static ssize_t instanceCount; |
| 77 | }; |
| 78 | |
| 79 | ssize_t ComplexValue::instanceCount = 0; |
| 80 | |
Sergio Giro | b7170fe | 2015-11-17 11:52:05 +0000 | [diff] [blame] | 81 | struct KeyWithPointer { |
| 82 | int *ptr; |
| 83 | bool operator ==(const KeyWithPointer& other) const { |
| 84 | return *ptr == *other.ptr; |
| 85 | } |
| 86 | }; |
| 87 | |
Sergio Giro | 4c56e0a | 2016-06-23 17:19:13 +0100 | [diff] [blame] | 88 | struct KeyFailsOnCopy : public ComplexKey { |
| 89 | public: |
Fabien Sanglard | 2aba0a2 | 2023-06-14 23:08:02 +0000 | [diff] [blame] | 90 | KeyFailsOnCopy() : ComplexKey() {} |
| 91 | KeyFailsOnCopy(const KeyFailsOnCopy& key) : ComplexKey(key) { ADD_FAILURE(); } |
| 92 | KeyFailsOnCopy(int key) : ComplexKey(key) {} |
Sergio Giro | 4c56e0a | 2016-06-23 17:19:13 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
Dan Albert | 27d166c | 2014-10-16 20:47:51 -0700 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| 97 | |
| 98 | namespace android { |
| 99 | |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 100 | typedef LruCache<ComplexKey, ComplexValue> ComplexCache; |
| 101 | |
Dan Albert | 27d166c | 2014-10-16 20:47:51 -0700 | [diff] [blame] | 102 | template<> inline android::hash_t hash_type(const ComplexKey& value) { |
| 103 | return hash_type(value.k); |
| 104 | } |
| 105 | |
Sergio Giro | b7170fe | 2015-11-17 11:52:05 +0000 | [diff] [blame] | 106 | template<> inline android::hash_t hash_type(const KeyWithPointer& value) { |
| 107 | return hash_type(*value.ptr); |
| 108 | } |
| 109 | |
Sergio Giro | 4c56e0a | 2016-06-23 17:19:13 +0100 | [diff] [blame] | 110 | template<> inline android::hash_t hash_type(const KeyFailsOnCopy& value) { |
| 111 | return hash_type<ComplexKey>(value); |
| 112 | } |
| 113 | |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 114 | class EntryRemovedCallback : public OnEntryRemoved<SimpleKey, StringValue> { |
| 115 | public: |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 116 | EntryRemovedCallback() : callbackCount(0), lastKey(-1), lastValue(nullptr) { } |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 117 | ~EntryRemovedCallback() {} |
| 118 | void operator()(SimpleKey& k, StringValue& v) { |
| 119 | callbackCount += 1; |
| 120 | lastKey = k; |
| 121 | lastValue = v; |
| 122 | } |
| 123 | ssize_t callbackCount; |
| 124 | SimpleKey lastKey; |
| 125 | StringValue lastValue; |
| 126 | }; |
| 127 | |
Sergio Giro | b7170fe | 2015-11-17 11:52:05 +0000 | [diff] [blame] | 128 | class InvalidateKeyCallback : public OnEntryRemoved<KeyWithPointer, StringValue> { |
| 129 | public: |
| 130 | void operator()(KeyWithPointer& k, StringValue&) { |
| 131 | delete k.ptr; |
| 132 | k.ptr = nullptr; |
| 133 | } |
| 134 | }; |
| 135 | |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 136 | class LruCacheTest : public testing::Test { |
| 137 | protected: |
| 138 | virtual void SetUp() { |
| 139 | ComplexKey::instanceCount = 0; |
| 140 | ComplexValue::instanceCount = 0; |
| 141 | } |
| 142 | |
| 143 | virtual void TearDown() { |
| 144 | ASSERT_NO_FATAL_FAILURE(assertInstanceCount(0, 0)); |
| 145 | } |
| 146 | |
| 147 | void assertInstanceCount(ssize_t keys, ssize_t values) { |
| 148 | if (keys != ComplexKey::instanceCount || values != ComplexValue::instanceCount) { |
| 149 | FAIL() << "Expected " << keys << " keys and " << values << " values " |
| 150 | "but there were actually " << ComplexKey::instanceCount << " keys and " |
| 151 | << ComplexValue::instanceCount << " values"; |
| 152 | } |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | TEST_F(LruCacheTest, Empty) { |
| 157 | LruCache<SimpleKey, StringValue> cache(100); |
| 158 | |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 159 | EXPECT_EQ(nullptr, cache.get(0)); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 160 | EXPECT_EQ(0u, cache.size()); |
| 161 | } |
| 162 | |
| 163 | TEST_F(LruCacheTest, Simple) { |
| 164 | LruCache<SimpleKey, StringValue> cache(100); |
| 165 | |
| 166 | cache.put(1, "one"); |
| 167 | cache.put(2, "two"); |
| 168 | cache.put(3, "three"); |
| 169 | EXPECT_STREQ("one", cache.get(1)); |
| 170 | EXPECT_STREQ("two", cache.get(2)); |
| 171 | EXPECT_STREQ("three", cache.get(3)); |
| 172 | EXPECT_EQ(3u, cache.size()); |
| 173 | } |
| 174 | |
| 175 | TEST_F(LruCacheTest, MaxCapacity) { |
| 176 | LruCache<SimpleKey, StringValue> cache(2); |
| 177 | |
| 178 | cache.put(1, "one"); |
| 179 | cache.put(2, "two"); |
| 180 | cache.put(3, "three"); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 181 | EXPECT_EQ(nullptr, cache.get(1)); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 182 | EXPECT_STREQ("two", cache.get(2)); |
| 183 | EXPECT_STREQ("three", cache.get(3)); |
| 184 | EXPECT_EQ(2u, cache.size()); |
| 185 | } |
| 186 | |
| 187 | TEST_F(LruCacheTest, RemoveLru) { |
| 188 | LruCache<SimpleKey, StringValue> cache(100); |
| 189 | |
| 190 | cache.put(1, "one"); |
| 191 | cache.put(2, "two"); |
| 192 | cache.put(3, "three"); |
| 193 | cache.removeOldest(); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 194 | EXPECT_EQ(nullptr, cache.get(1)); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 195 | EXPECT_STREQ("two", cache.get(2)); |
| 196 | EXPECT_STREQ("three", cache.get(3)); |
| 197 | EXPECT_EQ(2u, cache.size()); |
| 198 | } |
| 199 | |
| 200 | TEST_F(LruCacheTest, GetUpdatesLru) { |
| 201 | LruCache<SimpleKey, StringValue> cache(100); |
| 202 | |
| 203 | cache.put(1, "one"); |
| 204 | cache.put(2, "two"); |
| 205 | cache.put(3, "three"); |
| 206 | EXPECT_STREQ("one", cache.get(1)); |
| 207 | cache.removeOldest(); |
| 208 | EXPECT_STREQ("one", cache.get(1)); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 209 | EXPECT_EQ(nullptr, cache.get(2)); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 210 | EXPECT_STREQ("three", cache.get(3)); |
| 211 | EXPECT_EQ(2u, cache.size()); |
| 212 | } |
| 213 | |
| 214 | uint32_t hash_int(int x) { |
| 215 | return JenkinsHashWhiten(JenkinsHashMix(0, x)); |
| 216 | } |
| 217 | |
| 218 | TEST_F(LruCacheTest, StressTest) { |
| 219 | const size_t kCacheSize = 512; |
| 220 | LruCache<SimpleKey, StringValue> cache(512); |
| 221 | const size_t kNumKeys = 16 * 1024; |
| 222 | const size_t kNumIters = 100000; |
| 223 | char* strings[kNumKeys]; |
| 224 | |
| 225 | for (size_t i = 0; i < kNumKeys; i++) { |
| 226 | strings[i] = (char *)malloc(16); |
Sasha Levitskiy | cdc1cfb | 2014-04-10 17:10:21 -0700 | [diff] [blame] | 227 | sprintf(strings[i], "%zu", i); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | srandom(12345); |
| 231 | int hitCount = 0; |
| 232 | for (size_t i = 0; i < kNumIters; i++) { |
| 233 | int index = random() % kNumKeys; |
| 234 | uint32_t key = hash_int(index); |
| 235 | const char *val = cache.get(key); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 236 | if (val != nullptr) { |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 237 | EXPECT_EQ(strings[index], val); |
| 238 | hitCount++; |
| 239 | } else { |
| 240 | cache.put(key, strings[index]); |
| 241 | } |
| 242 | } |
| 243 | size_t expectedHitCount = kNumIters * kCacheSize / kNumKeys; |
| 244 | EXPECT_LT(int(expectedHitCount * 0.9), hitCount); |
| 245 | EXPECT_GT(int(expectedHitCount * 1.1), hitCount); |
| 246 | EXPECT_EQ(kCacheSize, cache.size()); |
| 247 | |
| 248 | for (size_t i = 0; i < kNumKeys; i++) { |
| 249 | free((void *)strings[i]); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | TEST_F(LruCacheTest, NoLeak) { |
| 254 | ComplexCache cache(100); |
| 255 | |
| 256 | cache.put(ComplexKey(0), ComplexValue(0)); |
| 257 | cache.put(ComplexKey(1), ComplexValue(1)); |
Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 258 | EXPECT_EQ(2U, cache.size()); |
Sergio Giro | bb58cde | 2015-09-25 18:03:18 +0100 | [diff] [blame] | 259 | assertInstanceCount(2, 3); // the member mNullValue counts as an instance |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | TEST_F(LruCacheTest, Clear) { |
| 263 | ComplexCache cache(100); |
| 264 | |
| 265 | cache.put(ComplexKey(0), ComplexValue(0)); |
| 266 | cache.put(ComplexKey(1), ComplexValue(1)); |
Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 267 | EXPECT_EQ(2U, cache.size()); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 268 | assertInstanceCount(2, 3); |
| 269 | cache.clear(); |
| 270 | assertInstanceCount(0, 1); |
| 271 | } |
| 272 | |
| 273 | TEST_F(LruCacheTest, ClearNoDoubleFree) { |
| 274 | { |
| 275 | ComplexCache cache(100); |
| 276 | |
| 277 | cache.put(ComplexKey(0), ComplexValue(0)); |
| 278 | cache.put(ComplexKey(1), ComplexValue(1)); |
Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 279 | EXPECT_EQ(2U, cache.size()); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 280 | assertInstanceCount(2, 3); |
| 281 | cache.removeOldest(); |
| 282 | cache.clear(); |
| 283 | assertInstanceCount(0, 1); |
| 284 | } |
| 285 | assertInstanceCount(0, 0); |
| 286 | } |
| 287 | |
| 288 | TEST_F(LruCacheTest, ClearReuseOk) { |
| 289 | ComplexCache cache(100); |
| 290 | |
| 291 | cache.put(ComplexKey(0), ComplexValue(0)); |
| 292 | cache.put(ComplexKey(1), ComplexValue(1)); |
Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 293 | EXPECT_EQ(2U, cache.size()); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 294 | assertInstanceCount(2, 3); |
| 295 | cache.clear(); |
| 296 | assertInstanceCount(0, 1); |
| 297 | cache.put(ComplexKey(0), ComplexValue(0)); |
| 298 | cache.put(ComplexKey(1), ComplexValue(1)); |
Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 299 | EXPECT_EQ(2U, cache.size()); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 300 | assertInstanceCount(2, 3); |
| 301 | } |
| 302 | |
| 303 | TEST_F(LruCacheTest, Callback) { |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 304 | EntryRemovedCallback callback; |
Florian Mayer | e0240d3 | 2022-03-31 20:21:12 +0000 | [diff] [blame] | 305 | LruCache<SimpleKey, StringValue> cache(100); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 306 | cache.setOnEntryRemovedListener(&callback); |
| 307 | |
| 308 | cache.put(1, "one"); |
| 309 | cache.put(2, "two"); |
| 310 | cache.put(3, "three"); |
Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 311 | EXPECT_EQ(3U, cache.size()); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 312 | cache.removeOldest(); |
| 313 | EXPECT_EQ(1, callback.callbackCount); |
| 314 | EXPECT_EQ(1, callback.lastKey); |
| 315 | EXPECT_STREQ("one", callback.lastValue); |
| 316 | } |
| 317 | |
| 318 | TEST_F(LruCacheTest, CallbackOnClear) { |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 319 | EntryRemovedCallback callback; |
Florian Mayer | e0240d3 | 2022-03-31 20:21:12 +0000 | [diff] [blame] | 320 | LruCache<SimpleKey, StringValue> cache(100); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 321 | cache.setOnEntryRemovedListener(&callback); |
| 322 | |
| 323 | cache.put(1, "one"); |
| 324 | cache.put(2, "two"); |
| 325 | cache.put(3, "three"); |
Nick Kralevich | 3e6c451 | 2015-08-17 18:26:13 -0700 | [diff] [blame] | 326 | EXPECT_EQ(3U, cache.size()); |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 327 | cache.clear(); |
| 328 | EXPECT_EQ(3, callback.callbackCount); |
| 329 | } |
| 330 | |
Sergio Giro | b7170fe | 2015-11-17 11:52:05 +0000 | [diff] [blame] | 331 | TEST_F(LruCacheTest, CallbackRemovesKeyWorksOK) { |
Sergio Giro | b7170fe | 2015-11-17 11:52:05 +0000 | [diff] [blame] | 332 | InvalidateKeyCallback callback; |
Florian Mayer | e0240d3 | 2022-03-31 20:21:12 +0000 | [diff] [blame] | 333 | LruCache<KeyWithPointer, StringValue> cache(1); |
Sergio Giro | b7170fe | 2015-11-17 11:52:05 +0000 | [diff] [blame] | 334 | cache.setOnEntryRemovedListener(&callback); |
| 335 | KeyWithPointer key1; |
| 336 | key1.ptr = new int(1); |
| 337 | KeyWithPointer key2; |
| 338 | key2.ptr = new int(2); |
| 339 | |
| 340 | cache.put(key1, "one"); |
| 341 | // As the size of the cache is 1, the put will call the callback. |
| 342 | // Make sure everything goes smoothly even if the callback invalidates |
| 343 | // the key (b/24785286) |
| 344 | cache.put(key2, "two"); |
| 345 | EXPECT_EQ(1U, cache.size()); |
| 346 | EXPECT_STREQ("two", cache.get(key2)); |
| 347 | cache.clear(); |
| 348 | } |
| 349 | |
Sergio Giro | 0cb59c0 | 2015-10-02 17:31:34 +0100 | [diff] [blame] | 350 | TEST_F(LruCacheTest, IteratorCheck) { |
| 351 | LruCache<int, int> cache(100); |
| 352 | |
| 353 | cache.put(1, 4); |
| 354 | cache.put(2, 5); |
| 355 | cache.put(3, 6); |
| 356 | EXPECT_EQ(3U, cache.size()); |
| 357 | |
| 358 | LruCache<int, int>::Iterator it(cache); |
| 359 | std::unordered_set<int> returnedValues; |
| 360 | while (it.next()) { |
| 361 | int v = it.value(); |
| 362 | // Check we haven't seen the value before. |
| 363 | EXPECT_TRUE(returnedValues.find(v) == returnedValues.end()); |
| 364 | returnedValues.insert(v); |
| 365 | } |
| 366 | EXPECT_EQ(std::unordered_set<int>({4, 5, 6}), returnedValues); |
| 367 | } |
| 368 | |
| 369 | TEST_F(LruCacheTest, EmptyCacheIterator) { |
| 370 | // Check that nothing crashes... |
| 371 | LruCache<int, int> cache(100); |
| 372 | |
| 373 | LruCache<int, int>::Iterator it(cache); |
| 374 | std::unordered_set<int> returnedValues; |
| 375 | while (it.next()) { |
| 376 | returnedValues.insert(it.value()); |
| 377 | } |
| 378 | EXPECT_EQ(std::unordered_set<int>(), returnedValues); |
| 379 | } |
| 380 | |
| 381 | TEST_F(LruCacheTest, OneElementCacheIterator) { |
| 382 | // Check that nothing crashes... |
| 383 | LruCache<int, int> cache(100); |
| 384 | cache.put(1, 2); |
| 385 | |
| 386 | LruCache<int, int>::Iterator it(cache); |
| 387 | std::unordered_set<int> returnedValues; |
| 388 | while (it.next()) { |
| 389 | returnedValues.insert(it.value()); |
| 390 | } |
| 391 | EXPECT_EQ(std::unordered_set<int>({ 2 }), returnedValues); |
| 392 | } |
| 393 | |
| 394 | TEST_F(LruCacheTest, OneElementCacheRemove) { |
| 395 | LruCache<int, int> cache(100); |
| 396 | cache.put(1, 2); |
| 397 | |
| 398 | cache.remove(1); |
| 399 | |
| 400 | LruCache<int, int>::Iterator it(cache); |
| 401 | std::unordered_set<int> returnedValues; |
| 402 | while (it.next()) { |
| 403 | returnedValues.insert(it.value()); |
| 404 | } |
| 405 | EXPECT_EQ(std::unordered_set<int>({ }), returnedValues); |
| 406 | } |
| 407 | |
| 408 | TEST_F(LruCacheTest, Remove) { |
| 409 | LruCache<int, int> cache(100); |
| 410 | cache.put(1, 4); |
| 411 | cache.put(2, 5); |
| 412 | cache.put(3, 6); |
| 413 | |
| 414 | cache.remove(2); |
| 415 | |
| 416 | LruCache<int, int>::Iterator it(cache); |
| 417 | std::unordered_set<int> returnedValues; |
| 418 | while (it.next()) { |
| 419 | returnedValues.insert(it.value()); |
| 420 | } |
| 421 | EXPECT_EQ(std::unordered_set<int>({ 4, 6 }), returnedValues); |
| 422 | } |
| 423 | |
| 424 | TEST_F(LruCacheTest, RemoveYoungest) { |
| 425 | LruCache<int, int> cache(100); |
| 426 | cache.put(1, 4); |
| 427 | cache.put(2, 5); |
| 428 | cache.put(3, 6); |
| 429 | |
| 430 | cache.remove(3); |
| 431 | |
| 432 | LruCache<int, int>::Iterator it(cache); |
| 433 | std::unordered_set<int> returnedValues; |
| 434 | while (it.next()) { |
| 435 | returnedValues.insert(it.value()); |
| 436 | } |
| 437 | EXPECT_EQ(std::unordered_set<int>({ 4, 5 }), returnedValues); |
| 438 | } |
| 439 | |
| 440 | TEST_F(LruCacheTest, RemoveNonMember) { |
| 441 | LruCache<int, int> cache(100); |
| 442 | cache.put(1, 4); |
| 443 | cache.put(2, 5); |
| 444 | cache.put(3, 6); |
| 445 | |
| 446 | cache.remove(7); |
| 447 | |
| 448 | LruCache<int, int>::Iterator it(cache); |
| 449 | std::unordered_set<int> returnedValues; |
| 450 | while (it.next()) { |
| 451 | returnedValues.insert(it.value()); |
| 452 | } |
| 453 | EXPECT_EQ(std::unordered_set<int>({ 4, 5, 6 }), returnedValues); |
| 454 | } |
| 455 | |
Sergio Giro | 4c56e0a | 2016-06-23 17:19:13 +0100 | [diff] [blame] | 456 | TEST_F(LruCacheTest, DontCopyKeyInGet) { |
| 457 | LruCache<KeyFailsOnCopy, KeyFailsOnCopy> cache(1); |
| 458 | // Check that get doesn't copy the key |
| 459 | cache.get(KeyFailsOnCopy(0)); |
| 460 | } |
| 461 | |
Raph Levien | b6ea175 | 2012-10-25 23:11:13 -0700 | [diff] [blame] | 462 | } |