diff options
author | Vladimir Byko-Ianko <v.bykoianko@corp.mail.ru> | 2018-03-30 18:09:49 +0300 |
---|---|---|
committer | Tatiana Yan <tatiana.kondakova@gmail.com> | 2018-04-05 12:12:34 +0300 |
commit | 4470291861a4e32a5fdb0262c63e1ca0a79f2aee (patch) | |
tree | 78ef3e709921b2358a49bbc4ec157be13719568a /base | |
parent | fe036cbb137d187b12c3d00213facab0f4bc4078 (diff) |
Tests on fifo cache.
Diffstat (limited to 'base')
-rw-r--r-- | base/base_tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | base/base_tests/fifo_cache_test.cpp | 105 |
2 files changed, 106 insertions, 0 deletions
diff --git a/base/base_tests/CMakeLists.txt b/base/base_tests/CMakeLists.txt index cc622c2f55..253173f2ee 100644 --- a/base/base_tests/CMakeLists.txt +++ b/base/base_tests/CMakeLists.txt @@ -14,6 +14,7 @@ set( condition_test.cpp containers_test.cpp control_flow_tests.cpp + fifo_cache_test.cpp levenshtein_dfa_test.cpp logging_test.cpp math_test.cpp diff --git a/base/base_tests/fifo_cache_test.cpp b/base/base_tests/fifo_cache_test.cpp new file mode 100644 index 0000000000..b80190ad46 --- /dev/null +++ b/base/base_tests/fifo_cache_test.cpp @@ -0,0 +1,105 @@ +#include "testing/testing.hpp" + +#include "base/fifo_cache.hpp" + +#include <list> +#include <set> +#include <unordered_map> + +using namespace std; + +template <typename Key, typename Value> +class FifoCacheTest +{ +public: + FifoCacheTest(size_t capacity, typename FifoCache<Key, Value>::Loader const & loader) + : m_cache(capacity, loader) + { + } + + Value const & GetValue(Key const & key) { return m_cache.GetValue(key); } + unordered_map<Key, Value> const & GetMap() const { return m_cache.m_map; } + list<Key> const & GetList() const { return m_cache.m_list; } + + bool IsValid() const + { + std::set<Key> listKeys(m_cache.m_list.cbegin(), m_cache.m_list.cend()); + std::set<Key> mapKeys; + + for (auto const & kv :m_cache. m_map) + mapKeys.insert(kv.first); + + return listKeys == mapKeys; + } + +private: + FifoCache<Key, Value> m_cache; +}; + +UNIT_TEST(FifoCacheSmokeTest) +{ + using Key = int; + using Value = int; + FifoCacheTest<Key, Value> cache(3 /* capacity */, [](Key k, Value & v) { v = k; } /* loader */); + + TEST_EQUAL(cache.GetValue(1), 1, ()); + TEST_EQUAL(cache.GetValue(2), 2, ()); + TEST_EQUAL(cache.GetValue(3), 3, ()); + TEST_EQUAL(cache.GetValue(4), 4, ()); + TEST_EQUAL(cache.GetValue(1), 1, ()); + TEST(cache.IsValid(), ()); +} + +UNIT_TEST(FifoCacheTest) +{ + using Key = int; + using Value = int; + FifoCacheTest<Key, Value> cache(3 /* capacity */, [](Key k, Value & v) { v = k; } /* loader */); + + TEST_EQUAL(cache.GetValue(1), 1, ()); + TEST_EQUAL(cache.GetValue(3), 3, ()); + TEST_EQUAL(cache.GetValue(2), 2, ()); + TEST(cache.IsValid(), ()); + { + unordered_map<Key, Value> expectedMap({{1 /* key */, 1 /* value */}, {2, 2}, {3, 3}}); + TEST_EQUAL(cache.GetMap(), expectedMap, ()); + list<Key> expectedList({2, 3, 1}); + TEST_EQUAL(cache.GetList(), expectedList, ()); + } + + TEST_EQUAL(cache.GetValue(7), 7, ()); + TEST(cache.IsValid(), ()); + { + unordered_map<Key, Value> expectedMap({{7 /* key */, 7 /* value */}, {2, 2}, {3, 3}}); + TEST_EQUAL(cache.GetMap(), expectedMap, ()); + list<Key> expectedList({7, 2, 3}); + TEST_EQUAL(cache.GetList(), expectedList, ()); + } +} + +UNIT_TEST(FifoCacheLoaderCallsTest) +{ + using Key = int; + using Value = int; + bool shouldLoadBeCalled = true; + auto loader = [&shouldLoadBeCalled](Key k, Value & v) { + TEST(shouldLoadBeCalled, ()); + v = k; + }; + + FifoCacheTest<Key, Value> cache(3 /* capacity */, loader); + TEST(cache.IsValid(), ()); + cache.GetValue(1); + cache.GetValue(2); + cache.GetValue(3); + TEST(cache.IsValid(), ()); + shouldLoadBeCalled = false; + cache.GetValue(3); + cache.GetValue(2); + cache.GetValue(1); + TEST(cache.IsValid(), ()); + shouldLoadBeCalled = true; + cache.GetValue(4); + cache.GetValue(1); + TEST(cache.IsValid(), ()); +} |