2021-12-29 07:25:33 +00:00
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <gtest/gtest.h>
|
2022-04-28 05:56:26 +00:00
|
|
|
#include <Common/CacheBase.h>
|
2021-12-29 07:25:33 +00:00
|
|
|
|
|
|
|
TEST(LRUCache, set)
|
|
|
|
{
|
2022-04-28 05:56:26 +00:00
|
|
|
using SimpleCacheBase = DB::CacheBase<int, int>;
|
2023-03-16 16:08:24 +00:00
|
|
|
auto lru_cache = SimpleCacheBase("LRU", /*max_size_in_bytes*/ 10, /*max_count*/ 10);
|
2021-12-29 07:25:33 +00:00
|
|
|
lru_cache.set(1, std::make_shared<int>(2));
|
2021-12-29 08:00:57 +00:00
|
|
|
lru_cache.set(2, std::make_shared<int>(3));
|
|
|
|
|
2021-12-29 07:25:33 +00:00
|
|
|
auto w = lru_cache.weight();
|
|
|
|
auto n = lru_cache.count();
|
|
|
|
ASSERT_EQ(w, 2);
|
|
|
|
ASSERT_EQ(n, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LRUCache, update)
|
|
|
|
{
|
2022-04-28 05:56:26 +00:00
|
|
|
using SimpleCacheBase = DB::CacheBase<int, int>;
|
2023-03-16 16:08:24 +00:00
|
|
|
auto lru_cache = SimpleCacheBase("LRU", /*max_size_in_bytes*/ 10, /*max_count*/ 10);
|
2021-12-29 07:25:33 +00:00
|
|
|
lru_cache.set(1, std::make_shared<int>(2));
|
2021-12-29 08:00:57 +00:00
|
|
|
lru_cache.set(1, std::make_shared<int>(3));
|
2021-12-29 07:25:33 +00:00
|
|
|
auto val = lru_cache.get(1);
|
|
|
|
ASSERT_TRUE(val != nullptr);
|
|
|
|
ASSERT_TRUE(*val == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LRUCache, get)
|
|
|
|
{
|
2022-04-28 05:56:26 +00:00
|
|
|
using SimpleCacheBase = DB::CacheBase<int, int>;
|
2023-03-16 16:08:24 +00:00
|
|
|
auto lru_cache = SimpleCacheBase("LRU", /*max_size_in_bytes*/ 10, /*max_count*/ 10);
|
2021-12-29 07:25:33 +00:00
|
|
|
lru_cache.set(1, std::make_shared<int>(2));
|
|
|
|
lru_cache.set(2, std::make_shared<int>(3));
|
2022-04-28 05:56:26 +00:00
|
|
|
SimpleCacheBase::MappedPtr value = lru_cache.get(1);
|
2021-12-29 07:25:33 +00:00
|
|
|
ASSERT_TRUE(value != nullptr);
|
|
|
|
ASSERT_EQ(*value, 2);
|
|
|
|
|
|
|
|
value = lru_cache.get(2);
|
|
|
|
ASSERT_TRUE(value != nullptr);
|
|
|
|
ASSERT_EQ(*value, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ValueWeight
|
|
|
|
{
|
2021-12-29 08:00:57 +00:00
|
|
|
size_t operator()(const size_t & x) const { return x; }
|
2021-12-29 07:25:33 +00:00
|
|
|
};
|
|
|
|
|
2021-12-30 06:34:08 +00:00
|
|
|
TEST(LRUCache, evictOnSize)
|
2021-12-29 07:25:33 +00:00
|
|
|
{
|
2022-04-28 05:56:26 +00:00
|
|
|
using SimpleCacheBase = DB::CacheBase<int, size_t>;
|
2023-03-16 16:08:24 +00:00
|
|
|
auto lru_cache = SimpleCacheBase("LRU", /*max_size_in_bytes*/ 20, /*max_count*/ 3);
|
2021-12-29 07:25:33 +00:00
|
|
|
lru_cache.set(1, std::make_shared<size_t>(2));
|
|
|
|
lru_cache.set(2, std::make_shared<size_t>(3));
|
|
|
|
lru_cache.set(3, std::make_shared<size_t>(4));
|
|
|
|
lru_cache.set(4, std::make_shared<size_t>(5));
|
|
|
|
|
|
|
|
auto n = lru_cache.count();
|
|
|
|
ASSERT_EQ(n, 3);
|
|
|
|
|
|
|
|
auto value = lru_cache.get(1);
|
|
|
|
ASSERT_TRUE(value == nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-30 06:34:08 +00:00
|
|
|
TEST(LRUCache, evictOnWeight)
|
2021-12-29 07:25:33 +00:00
|
|
|
{
|
2022-04-28 05:56:26 +00:00
|
|
|
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
|
2023-03-16 16:08:24 +00:00
|
|
|
auto lru_cache = SimpleCacheBase("LRU", /*max_size_in_bytes*/ 10, /*max_count*/ 10);
|
2021-12-29 07:25:33 +00:00
|
|
|
lru_cache.set(1, std::make_shared<size_t>(2));
|
|
|
|
lru_cache.set(2, std::make_shared<size_t>(3));
|
|
|
|
lru_cache.set(3, std::make_shared<size_t>(4));
|
|
|
|
lru_cache.set(4, std::make_shared<size_t>(5));
|
|
|
|
|
|
|
|
auto n = lru_cache.count();
|
|
|
|
ASSERT_EQ(n, 2);
|
|
|
|
|
|
|
|
auto w = lru_cache.weight();
|
|
|
|
ASSERT_EQ(w, 9);
|
|
|
|
|
|
|
|
auto value = lru_cache.get(1);
|
|
|
|
ASSERT_TRUE(value == nullptr);
|
|
|
|
value = lru_cache.get(2);
|
|
|
|
ASSERT_TRUE(value == nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LRUCache, getOrSet)
|
|
|
|
{
|
2022-04-28 05:56:26 +00:00
|
|
|
using SimpleCacheBase = DB::CacheBase<int, size_t, std::hash<int>, ValueWeight>;
|
2023-03-16 16:08:24 +00:00
|
|
|
auto lru_cache = SimpleCacheBase("LRU", /*max_size_in_bytes*/ 10, /*max_count*/ 10);
|
2021-12-29 07:25:33 +00:00
|
|
|
size_t x = 10;
|
2021-12-29 08:00:57 +00:00
|
|
|
auto load_func = [&] { return std::make_shared<size_t>(x); };
|
2021-12-29 07:25:33 +00:00
|
|
|
auto [value, loaded] = lru_cache.getOrSet(1, load_func);
|
|
|
|
ASSERT_TRUE(value != nullptr);
|
|
|
|
ASSERT_TRUE(*value == 10);
|
|
|
|
}
|
|
|
|
|