| 48 | } // namespace |
| 49 | |
| 50 | TEST(SimpleLRUCache, basicCaching) { |
| 51 | SimpleLRUCache<int, int> cache(1000); |
| 52 | |
| 53 | EXPECT_FALSE(cache.get(1).has_value()); |
| 54 | EXPECT_FALSE(cache.get(2).has_value()); |
| 55 | |
| 56 | verifyCacheStats(cache.getStats(), 1000, 0, 0, 2); |
| 57 | |
| 58 | int firstValue = 11; |
| 59 | ASSERT_TRUE(cache.add(1, firstValue)); |
| 60 | auto value = cache.get(1); |
| 61 | ASSERT_EQ(value, std::make_optional(11)); |
| 62 | |
| 63 | int secondValue = 22; |
| 64 | ASSERT_TRUE(cache.add(2, secondValue)); |
| 65 | |
| 66 | verifyCacheStats(cache.getStats(), 1000, 2, 1, 3); |
| 67 | |
| 68 | value = cache.get(1); |
| 69 | ASSERT_EQ(value, std::make_optional(11)); |
| 70 | |
| 71 | value = cache.get(2); |
| 72 | ASSERT_EQ(value, std::make_optional(22)); |
| 73 | |
| 74 | value = cache.get(1); |
| 75 | ASSERT_EQ(value, std::make_optional(11)); |
| 76 | |
| 77 | value = cache.get(2); |
| 78 | ASSERT_EQ(value, std::make_optional(22)); |
| 79 | verifyCacheStats(cache.getStats(), 1000, 2, 5, 7); |
| 80 | |
| 81 | cache.clear(); |
| 82 | verifyCacheStats(cache.getStats(), 1000, 0, 5, 7); |
| 83 | EXPECT_FALSE(cache.get(1).has_value()); |
| 84 | EXPECT_FALSE(cache.get(2).has_value()); |
| 85 | } |
| 86 | |
| 87 | TEST(SimpleLRUCache, eviction) { |
| 88 | SimpleLRUCache<int, int> cache(1000); |