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 +#include +#include + +using namespace std; + +template +class FifoCacheTest +{ +public: + FifoCacheTest(size_t capacity, typename FifoCache::Loader const & loader) + : m_cache(capacity, loader) + { + } + + Value const & GetValue(Key const & key) { return m_cache.GetValue(key); } + unordered_map const & GetMap() const { return m_cache.m_map; } + list const & GetList() const { return m_cache.m_list; } + + bool IsValid() const + { + std::set listKeys(m_cache.m_list.cbegin(), m_cache.m_list.cend()); + std::set mapKeys; + + for (auto const & kv :m_cache. m_map) + mapKeys.insert(kv.first); + + return listKeys == mapKeys; + } + +private: + FifoCache m_cache; +}; + +UNIT_TEST(FifoCacheSmokeTest) +{ + using Key = int; + using Value = int; + FifoCacheTest 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 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 expectedMap({{1 /* key */, 1 /* value */}, {2, 2}, {3, 3}}); + TEST_EQUAL(cache.GetMap(), expectedMap, ()); + list expectedList({2, 3, 1}); + TEST_EQUAL(cache.GetList(), expectedList, ()); + } + + TEST_EQUAL(cache.GetValue(7), 7, ()); + TEST(cache.IsValid(), ()); + { + unordered_map expectedMap({{7 /* key */, 7 /* value */}, {2, 2}, {3, 3}}); + TEST_EQUAL(cache.GetMap(), expectedMap, ()); + list 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 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(), ()); +} diff --git a/xcode/base/base.xcodeproj/project.pbxproj b/xcode/base/base.xcodeproj/project.pbxproj index ab21dc7d78..079e358bde 100644 --- a/xcode/base/base.xcodeproj/project.pbxproj +++ b/xcode/base/base.xcodeproj/project.pbxproj @@ -57,6 +57,7 @@ 3D7815731F3A145F0068B6AC /* task_loop.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D7815711F3A145F0068B6AC /* task_loop.hpp */; }; 3D78157B1F3D89EC0068B6AC /* waiter.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 3D78157A1F3D89EC0068B6AC /* waiter.hpp */; }; 564BB445206E89ED00BDD211 /* fifo_cache.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 564BB444206E89ED00BDD211 /* fifo_cache.hpp */; }; + 564BB447206E8A4D00BDD211 /* fifo_cache_test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 564BB446206E8A4D00BDD211 /* fifo_cache_test.cpp */; }; 56B1A0741E69DE4D00395022 /* random.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 56B1A0711E69DE4D00395022 /* random.cpp */; }; 56B1A0751E69DE4D00395022 /* random.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56B1A0721E69DE4D00395022 /* random.hpp */; }; 56B1A0761E69DE4D00395022 /* small_set.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 56B1A0731E69DE4D00395022 /* small_set.hpp */; }; @@ -190,6 +191,7 @@ 3D7815711F3A145F0068B6AC /* task_loop.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = task_loop.hpp; sourceTree = ""; }; 3D78157A1F3D89EC0068B6AC /* waiter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = waiter.hpp; sourceTree = ""; }; 564BB444206E89ED00BDD211 /* fifo_cache.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = fifo_cache.hpp; sourceTree = ""; }; + 564BB446206E8A4D00BDD211 /* fifo_cache_test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fifo_cache_test.cpp; sourceTree = ""; }; 56B1A0711E69DE4D00395022 /* random.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = random.cpp; sourceTree = ""; }; 56B1A0721E69DE4D00395022 /* random.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = random.hpp; sourceTree = ""; }; 56B1A0731E69DE4D00395022 /* small_set.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = small_set.hpp; sourceTree = ""; }; @@ -290,6 +292,7 @@ 39FD26C71CC659D200AFF551 /* base_tests */ = { isa = PBXGroup; children = ( + 564BB446206E8A4D00BDD211 /* fifo_cache_test.cpp */, 39B89C391FD1898A001104AF /* control_flow_tests.cpp */, 67E40EC71E4DC0D500A6D200 /* small_set_test.cpp */, 3446C67E1DDCAA6E00146687 /* newtype_test.cpp */, @@ -682,6 +685,7 @@ 675341F91A3F57E400A0A8C3 /* src_point.cpp in Sources */, 675342031A3F57E400A0A8C3 /* strings_bundle.cpp in Sources */, 39B89C3A1FD1898A001104AF /* control_flow_tests.cpp in Sources */, + 564BB447206E8A4D00BDD211 /* fifo_cache_test.cpp in Sources */, 3D74EF141F8B902C0081202C /* bwt.cpp in Sources */, 3D74EF131F8B902C0081202C /* move_to_front.cpp in Sources */, 675341CD1A3F57E400A0A8C3 /* base.cpp in Sources */,