|
| 1 | +// Aleth: Ethereum C++ client, tools and libraries. |
| 2 | +// Copyright 2019 Aleth Authors. |
| 3 | +// Licensed under the GNU General Public License, Version 3. |
| 4 | + |
| 5 | +#include <libdevcore/LruCache.h> |
| 6 | +#include <test/tools/libtestutils/Common.h> |
| 7 | +#include <gtest/gtest.h> |
| 8 | + |
| 9 | +using namespace std; |
| 10 | +using namespace dev; |
| 11 | +using namespace dev::test; |
| 12 | + |
| 13 | +namespace |
| 14 | +{ |
| 15 | +using LRU = LruCache<int, int>; |
| 16 | +using PAIR = pair<int, int>; |
| 17 | +using VEC = vector<PAIR>; |
| 18 | + |
| 19 | +constexpr size_t c_capacity = 10; |
| 20 | + |
| 21 | +mt19937_64 g_randomGenerator(random_device{}()); |
| 22 | + |
| 23 | +int randomNumber(int _min = INT_MIN, int _max = INT_MAX) |
| 24 | +{ |
| 25 | + return std::uniform_int_distribution<int>{_min, _max}(g_randomGenerator); |
| 26 | +} |
| 27 | + |
| 28 | +VEC Populate(LRU& _lruCache, size_t _count) |
| 29 | +{ |
| 30 | + VEC ret; |
| 31 | + for (size_t i = 0; i < _count; i++) |
| 32 | + { |
| 33 | + auto const item = PAIR{randomNumber(), randomNumber()}; |
| 34 | + ret.push_back(item); |
| 35 | + _lruCache.insert(item.first, item.second); |
| 36 | + } |
| 37 | + reverse(ret.begin(), ret.end()); |
| 38 | + |
| 39 | + return ret; |
| 40 | +} |
| 41 | + |
| 42 | +void VerifyEquals(LRU& _lruCache, VEC& _data) |
| 43 | +{ |
| 44 | + EXPECT_EQ(_lruCache.size(), _data.size()); |
| 45 | + size_t i = 0; |
| 46 | + auto iter = _lruCache.begin(); |
| 47 | + while (iter != _lruCache.cend() && i < _data.size()) |
| 48 | + { |
| 49 | + EXPECT_EQ(*iter, _data[i]); |
| 50 | + iter++; |
| 51 | + i++; |
| 52 | + } |
| 53 | +} |
| 54 | +} // namespace |
| 55 | + |
| 56 | +TEST(LruCache, BasicOperations) |
| 57 | +{ |
| 58 | + LRU lruCache{c_capacity}; |
| 59 | + EXPECT_EQ(lruCache.capacity(), c_capacity); |
| 60 | + EXPECT_TRUE(lruCache.empty()); |
| 61 | + |
| 62 | + // Populate and verify |
| 63 | + VEC testData = Populate(lruCache, lruCache.capacity()); |
| 64 | + VerifyEquals(lruCache, testData); |
| 65 | + |
| 66 | + // Reverse order and verify |
| 67 | + for (size_t i = 0; i < testData.size(); i++) |
| 68 | + lruCache.touch(testData[i].first); |
| 69 | + reverse(testData.begin(), testData.end()); |
| 70 | + VerifyEquals(lruCache, testData); |
| 71 | + |
| 72 | + // Remove elements and verify |
| 73 | + auto size = lruCache.size(); |
| 74 | + for (PAIR item : testData) |
| 75 | + { |
| 76 | + lruCache.remove(item.first); |
| 77 | + EXPECT_FALSE(lruCache.contains(item.first)); |
| 78 | + EXPECT_EQ(lruCache.size(), --size); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +TEST(LruCache, AdvancedOperations) |
| 83 | +{ |
| 84 | + LRU lruCache{c_capacity}; |
| 85 | + VEC testData = Populate(lruCache, lruCache.capacity()); |
| 86 | + VerifyEquals(lruCache, testData); |
| 87 | + testData = Populate(lruCache, lruCache.capacity()); |
| 88 | + VerifyEquals(lruCache, testData); |
| 89 | + lruCache.clear(); |
| 90 | + EXPECT_TRUE(lruCache.empty()); |
| 91 | + EXPECT_EQ(lruCache.capacity(), c_capacity); |
| 92 | +} |
| 93 | + |
| 94 | +TEST(LruCache, Constructors) |
| 95 | +{ |
| 96 | + LRU lruCache{c_capacity}; |
| 97 | + VEC testData = Populate(lruCache, lruCache.capacity()); |
| 98 | + VerifyEquals(lruCache, testData); |
| 99 | + |
| 100 | + LRU lruCacheCopy{lruCache}; |
| 101 | + VerifyEquals(lruCacheCopy, testData); |
| 102 | + EXPECT_EQ(lruCache.capacity(), lruCacheCopy.capacity()); |
| 103 | + |
| 104 | + LRU lruCacheMove{move(lruCache)}; |
| 105 | + VerifyEquals(lruCacheMove, testData); |
| 106 | + EXPECT_TRUE(lruCache.empty()); |
| 107 | + EXPECT_EQ(lruCache.capacity(), c_capacity); |
| 108 | +} |
0 commit comments