diff options
author | heios <heios@yandex-team.ru> | 2022-02-10 16:50:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:15 +0300 |
commit | 58861ee1428f6e29c13edd982e662c38dd44c105 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/cache/ut | |
parent | e0ff5d1ed8ec4db4bba3153fc3c3acb85052ada9 (diff) | |
download | ydb-58861ee1428f6e29c13edd982e662c38dd44c105.tar.gz |
Restoring authorship annotation for <heios@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/cache/ut')
-rw-r--r-- | library/cpp/cache/ut/cache_ut.cpp | 356 |
1 files changed, 178 insertions, 178 deletions
diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index e1e37eddd7..329872cfde 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -438,181 +438,181 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheUnsafeTest) { } } } - -Y_UNIT_TEST_SUITE(TThreadSafeLRUCacheTest) { - typedef TThreadSafeLRUCache<size_t, TString, size_t> TCache; - - TVector<TString> Values = {"zero", "one", "two", "three", "four"}; - - class TCallbacks: public TCache::ICallbacks { - public: - TKey GetKey(size_t i) const override { - return i; - } - TValue* CreateObject(size_t i) const override { - UNIT_ASSERT(i < Values.size()); - Creations++; - return new TString(Values[i]); - } - - mutable size_t Creations = 0; - }; - - Y_UNIT_TEST(SimpleTest) { - for (size_t i = 0; i < Values.size(); ++i) { - const TString data = *TCache::Get<TCallbacks>(i); - UNIT_ASSERT(data == Values[i]); - } - } - - Y_UNIT_TEST(InsertUpdateTest) { - TCallbacks callbacks; - TCache cache(callbacks, 10); - - cache.Insert(2, MakeAtomicShared<TString>("hj")); - TAtomicSharedPtr<TString> item = cache.Get(2); - - UNIT_ASSERT(callbacks.Creations == 0); - UNIT_ASSERT(*item == "hj"); - - cache.Insert(2, MakeAtomicShared<TString>("hjk")); - item = cache.Get(2); - - UNIT_ASSERT(callbacks.Creations == 0); - UNIT_ASSERT(*item == "hj"); - - cache.Update(2, MakeAtomicShared<TString>("hjk")); - item = cache.Get(2); - - UNIT_ASSERT(callbacks.Creations == 0); - UNIT_ASSERT(*item == "hjk"); - } - - Y_UNIT_TEST(LRUTest) { - TCallbacks callbacks; - TCache cache(callbacks, 3); - - UNIT_ASSERT_EQUAL(cache.GetMaxSize(), 3); - - for (size_t i = 0; i < Values.size(); ++i) { - TAtomicSharedPtr<TString> item = cache.Get(i); - UNIT_ASSERT(*item == Values[i]); - } - UNIT_ASSERT(callbacks.Creations == Values.size()); - - size_t expectedCreations = Values.size(); - TAtomicSharedPtr<TString> item; - - - item = cache.Get(4); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "four"); - - item = cache.Get(2); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "two"); - - item = cache.Get(0); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "zero"); - - UNIT_ASSERT(cache.Contains(1) == false); - UNIT_ASSERT(cache.Contains(3) == false); - UNIT_ASSERT(cache.Contains(4)); - UNIT_ASSERT(cache.Contains(2)); - UNIT_ASSERT(cache.Contains(0)); - - item = cache.Get(3); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "three"); - - item = cache.Get(2); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "two"); - - item = cache.Get(0); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "zero"); - - item = cache.Get(1); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "one"); - - item = cache.Get(2); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "two"); - - item = cache.Get(4); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "four"); - } - - Y_UNIT_TEST(ChangeMaxSizeTest) { - TCallbacks callbacks; - TCache cache(callbacks, 3); - - UNIT_ASSERT_EQUAL(cache.GetMaxSize(), 3); - - for (size_t i = 0; i < Values.size(); ++i) { - TAtomicSharedPtr<TString> item = cache.Get(i); - UNIT_ASSERT(*item == Values[i]); - } - - size_t expectedCreations = Values.size(); - TAtomicSharedPtr<TString> item; - - item = cache.Get(4); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "four"); - - item = cache.Get(3); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "three"); - - item = cache.Get(2); - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "two"); - - item = cache.Get(1); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "one"); - - cache.SetMaxSize(4); - - item = cache.Get(0); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "zero"); - - item = cache.Get(4); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "four"); - - item = cache.Get(3); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "three"); - UNIT_ASSERT(cache.Contains(2) == false); - - cache.SetMaxSize(2); - UNIT_ASSERT(cache.Contains(3)); - UNIT_ASSERT(cache.Contains(4)); - UNIT_ASSERT(cache.Contains(2) == false); - UNIT_ASSERT(cache.Contains(1) == false); - UNIT_ASSERT(cache.Contains(0) == false); - - item = cache.Get(0); - expectedCreations++; - UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); - UNIT_ASSERT(*item == "zero"); - UNIT_ASSERT(cache.Contains(4) == false); - UNIT_ASSERT(cache.Contains(3)); - UNIT_ASSERT(cache.Contains(0)); - } -} + +Y_UNIT_TEST_SUITE(TThreadSafeLRUCacheTest) { + typedef TThreadSafeLRUCache<size_t, TString, size_t> TCache; + + TVector<TString> Values = {"zero", "one", "two", "three", "four"}; + + class TCallbacks: public TCache::ICallbacks { + public: + TKey GetKey(size_t i) const override { + return i; + } + TValue* CreateObject(size_t i) const override { + UNIT_ASSERT(i < Values.size()); + Creations++; + return new TString(Values[i]); + } + + mutable size_t Creations = 0; + }; + + Y_UNIT_TEST(SimpleTest) { + for (size_t i = 0; i < Values.size(); ++i) { + const TString data = *TCache::Get<TCallbacks>(i); + UNIT_ASSERT(data == Values[i]); + } + } + + Y_UNIT_TEST(InsertUpdateTest) { + TCallbacks callbacks; + TCache cache(callbacks, 10); + + cache.Insert(2, MakeAtomicShared<TString>("hj")); + TAtomicSharedPtr<TString> item = cache.Get(2); + + UNIT_ASSERT(callbacks.Creations == 0); + UNIT_ASSERT(*item == "hj"); + + cache.Insert(2, MakeAtomicShared<TString>("hjk")); + item = cache.Get(2); + + UNIT_ASSERT(callbacks.Creations == 0); + UNIT_ASSERT(*item == "hj"); + + cache.Update(2, MakeAtomicShared<TString>("hjk")); + item = cache.Get(2); + + UNIT_ASSERT(callbacks.Creations == 0); + UNIT_ASSERT(*item == "hjk"); + } + + Y_UNIT_TEST(LRUTest) { + TCallbacks callbacks; + TCache cache(callbacks, 3); + + UNIT_ASSERT_EQUAL(cache.GetMaxSize(), 3); + + for (size_t i = 0; i < Values.size(); ++i) { + TAtomicSharedPtr<TString> item = cache.Get(i); + UNIT_ASSERT(*item == Values[i]); + } + UNIT_ASSERT(callbacks.Creations == Values.size()); + + size_t expectedCreations = Values.size(); + TAtomicSharedPtr<TString> item; + + + item = cache.Get(4); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "four"); + + item = cache.Get(2); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "two"); + + item = cache.Get(0); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "zero"); + + UNIT_ASSERT(cache.Contains(1) == false); + UNIT_ASSERT(cache.Contains(3) == false); + UNIT_ASSERT(cache.Contains(4)); + UNIT_ASSERT(cache.Contains(2)); + UNIT_ASSERT(cache.Contains(0)); + + item = cache.Get(3); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "three"); + + item = cache.Get(2); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "two"); + + item = cache.Get(0); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "zero"); + + item = cache.Get(1); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "one"); + + item = cache.Get(2); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "two"); + + item = cache.Get(4); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "four"); + } + + Y_UNIT_TEST(ChangeMaxSizeTest) { + TCallbacks callbacks; + TCache cache(callbacks, 3); + + UNIT_ASSERT_EQUAL(cache.GetMaxSize(), 3); + + for (size_t i = 0; i < Values.size(); ++i) { + TAtomicSharedPtr<TString> item = cache.Get(i); + UNIT_ASSERT(*item == Values[i]); + } + + size_t expectedCreations = Values.size(); + TAtomicSharedPtr<TString> item; + + item = cache.Get(4); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "four"); + + item = cache.Get(3); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "three"); + + item = cache.Get(2); + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "two"); + + item = cache.Get(1); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "one"); + + cache.SetMaxSize(4); + + item = cache.Get(0); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "zero"); + + item = cache.Get(4); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "four"); + + item = cache.Get(3); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "three"); + UNIT_ASSERT(cache.Contains(2) == false); + + cache.SetMaxSize(2); + UNIT_ASSERT(cache.Contains(3)); + UNIT_ASSERT(cache.Contains(4)); + UNIT_ASSERT(cache.Contains(2) == false); + UNIT_ASSERT(cache.Contains(1) == false); + UNIT_ASSERT(cache.Contains(0) == false); + + item = cache.Get(0); + expectedCreations++; + UNIT_ASSERT_EQUAL(callbacks.Creations, expectedCreations); + UNIT_ASSERT(*item == "zero"); + UNIT_ASSERT(cache.Contains(4) == false); + UNIT_ASSERT(cache.Contains(3)); + UNIT_ASSERT(cache.Contains(0)); + } +} |