]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_intrusive_lru.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 #include "gtest/gtest.h"
6 #include "common/intrusive_lru.h"
8 template <typename TestLRUItem
>
9 struct item_to_unsigned
{
10 using type
= unsigned;
11 const type
&operator()(const TestLRUItem
&item
) {
16 struct TestLRUItem
: public ceph::common::intrusive_lru_base
<
17 ceph::common::intrusive_lru_config
<
18 unsigned, TestLRUItem
, item_to_unsigned
<TestLRUItem
>>> {
23 TestLRUItem(unsigned key
) : key(key
) {}
26 class LRUTest
: public TestLRUItem::lru_t
{
28 auto add(unsigned int key
, int value
) {
29 auto [ref
, key_existed
] = get_or_create(key
);
33 return std::pair(ref
, key_existed
);
37 TEST(LRU
, add_immediate_evict
) {
43 auto [ref
, existed
] = cache
.add(key
, value1
);
45 ASSERT_EQ(value1
, ref
->value
);
46 ASSERT_FALSE(existed
);
49 auto [ref2
, existed
] = cache
.add(key
, value2
);
50 ASSERT_EQ(value2
, ref2
->value
);
51 ASSERT_FALSE(existed
);
55 TEST(LRU
, lookup_lru_size
) {
59 cache
.set_target_size(1);
61 auto [ref
, existed
] = cache
.add(key
, value
);
63 ASSERT_FALSE(existed
);
66 auto [ref
, existed
] = cache
.add(key
, value
);
70 cache
.set_target_size(0);
71 auto [ref2
, existed2
] = cache
.add(key
, value
);
73 ASSERT_FALSE(existed2
);
75 auto [ref
, existed
] = cache
.add(key
, value
);
82 const unsigned SIZE
= 3;
84 cache
.set_target_size(SIZE
);
86 for (unsigned i
= 0; i
< SIZE
; ++i
) {
87 auto [ref
, existed
] = cache
.add(i
, i
);
88 ASSERT_TRUE(ref
&& !existed
);
92 auto [ref
, existed
] = cache
.add(0, 0);
93 ASSERT_TRUE(ref
&& existed
);
96 for (unsigned i
= SIZE
; i
< (2*SIZE
) - 1; ++i
) {
97 auto [ref
, existed
] = cache
.add(i
, i
);
98 ASSERT_TRUE(ref
&& !existed
);
102 auto [ref
, existed
] = cache
.add(0, 0);
103 ASSERT_TRUE(ref
&& existed
);
106 for (unsigned i
= 1; i
< SIZE
; ++i
) {
107 auto [ref
, existed
] = cache
.add(i
, i
);
108 ASSERT_TRUE(ref
&& !existed
);
112 TEST(LRU
, eviction_live_ref
) {
113 const unsigned SIZE
= 3;
115 cache
.set_target_size(SIZE
);
117 auto [live_ref
, existed2
] = cache
.add(1, 1);
118 ASSERT_TRUE(live_ref
&& !existed2
);
120 for (unsigned i
= 0; i
< SIZE
; ++i
) {
121 auto [ref
, existed
] = cache
.add(i
, i
);
124 ASSERT_TRUE(existed
);
126 ASSERT_FALSE(existed
);
131 auto [ref
, existed
] = cache
.add(0, 0);
132 ASSERT_TRUE(ref
&& existed
);
135 for (unsigned i
= SIZE
; i
< (2*SIZE
) - 1; ++i
) {
136 auto [ref
, existed
] = cache
.add(i
, i
);
137 ASSERT_TRUE(ref
&& !existed
);
140 for (unsigned i
= 0; i
< SIZE
; ++i
) {
141 auto [ref
, existed
] = cache
.add(i
, i
);
144 ASSERT_TRUE(existed
);
146 ASSERT_FALSE(existed
);