1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include <stdio.h>
#include "gtest/gtest.h"
#include "common/intrusive_lru.h"
template <typename TestLRUItem>
struct item_to_unsigned {
using type = unsigned;
const type &operator()(const TestLRUItem &item) {
return item.key;
}
};
struct TestLRUItem : public ceph::common::intrusive_lru_base<
ceph::common::intrusive_lru_config<
unsigned, TestLRUItem, item_to_unsigned<TestLRUItem>>> {
unsigned key = 0;
int value = 0;
TestLRUItem(unsigned key) : key(key) {}
};
class LRUTest : public TestLRUItem::lru_t {
public:
auto add(unsigned int key, int value) {
auto [ref, key_existed] = get_or_create(key);
if (!key_existed) {
ref->value = value;
}
return std::pair(ref, key_existed);
}
};
TEST(LRU, add_immediate_evict) {
LRUTest cache;
unsigned int key = 1;
int value1 = 2;
int value2 = 3;
{
auto [ref, existed] = cache.add(key, value1);
ASSERT_TRUE(ref);
ASSERT_EQ(value1, ref->value);
ASSERT_FALSE(existed);
}
{
auto [ref2, existed] = cache.add(key, value2);
ASSERT_EQ(value2, ref2->value);
ASSERT_FALSE(existed);
}
}
TEST(LRU, lookup_lru_size) {
LRUTest cache;
int key = 1;
int value = 1;
cache.set_target_size(1);
{
auto [ref, existed] = cache.add(key, value);
ASSERT_TRUE(ref);
ASSERT_FALSE(existed);
}
{
auto [ref, existed] = cache.add(key, value);
ASSERT_TRUE(ref);
ASSERT_TRUE(existed);
}
cache.set_target_size(0);
auto [ref2, existed2] = cache.add(key, value);
ASSERT_TRUE(ref2);
ASSERT_FALSE(existed2);
{
auto [ref, existed] = cache.add(key, value);
ASSERT_TRUE(ref);
ASSERT_TRUE(existed);
}
}
TEST(LRU, eviction) {
const unsigned SIZE = 3;
LRUTest cache;
cache.set_target_size(SIZE);
for (unsigned i = 0; i < SIZE; ++i) {
auto [ref, existed] = cache.add(i, i);
ASSERT_TRUE(ref && !existed);
}
{
auto [ref, existed] = cache.add(0, 0);
ASSERT_TRUE(ref && existed);
}
for (unsigned i = SIZE; i < (2*SIZE) - 1; ++i) {
auto [ref, existed] = cache.add(i, i);
ASSERT_TRUE(ref && !existed);
}
{
auto [ref, existed] = cache.add(0, 0);
ASSERT_TRUE(ref && existed);
}
for (unsigned i = 1; i < SIZE; ++i) {
auto [ref, existed] = cache.add(i, i);
ASSERT_TRUE(ref && !existed);
}
}
TEST(LRU, eviction_live_ref) {
const unsigned SIZE = 3;
LRUTest cache;
cache.set_target_size(SIZE);
auto [live_ref, existed2] = cache.add(1, 1);
ASSERT_TRUE(live_ref && !existed2);
for (unsigned i = 0; i < SIZE; ++i) {
auto [ref, existed] = cache.add(i, i);
ASSERT_TRUE(ref);
if (i == 1) {
ASSERT_TRUE(existed);
} else {
ASSERT_FALSE(existed);
}
}
{
auto [ref, existed] = cache.add(0, 0);
ASSERT_TRUE(ref && existed);
}
for (unsigned i = SIZE; i < (2*SIZE) - 1; ++i) {
auto [ref, existed] = cache.add(i, i);
ASSERT_TRUE(ref && !existed);
}
for (unsigned i = 0; i < SIZE; ++i) {
auto [ref, existed] = cache.add(i, i);
ASSERT_TRUE(ref);
if (i == 1) {
ASSERT_TRUE(existed);
} else {
ASSERT_FALSE(existed);
}
}
}
TEST(LRU, clear_range) {
LRUTest cache;
const unsigned SIZE = 10;
cache.set_target_size(SIZE);
{
auto [ref, existed] = cache.add(1, 4);
ASSERT_FALSE(existed);
}
{
auto [ref, existed] = cache.add(2, 4);
ASSERT_FALSE(existed);
}
{
auto [ref, existed] = cache.add(3, 4);
ASSERT_FALSE(existed);
}
// Unlike above, the reference is not being destroyed
auto [live_ref1, existed1] = cache.add(4, 4);
ASSERT_FALSE(existed1);
auto [live_ref2, existed2] = cache.add(5, 4);
ASSERT_FALSE(existed2);
cache.clear_range(0,4);
// Should not exists (Unreferenced):
{
auto [ref, existed] = cache.add(1, 4);
ASSERT_FALSE(existed);
}
{
auto [ref, existed] = cache.add(2, 4);
ASSERT_FALSE(existed);
}
{
auto [ref, existed] = cache.add(3, 4);
ASSERT_FALSE(existed);
}
// Should exist (Still being referenced):
{
auto [ref, existed] = cache.add(4, 4);
ASSERT_TRUE(existed);
}
// Should exists (Still being referenced and wasn't removed)
{
auto [ref, existed] = cache.add(5, 4);
ASSERT_TRUE(existed);
}
// Test out of bound deletion:
{
cache.clear_range(3,8);
auto [ref, existed] = cache.add(4, 4);
ASSERT_TRUE(existed);
}
{
auto [ref, existed] = cache.add(3, 4);
ASSERT_FALSE(existed);
}
}
|