userver: /data/code/userver/libraries/multi-index-lru/src/container_test.cpp Source File
Loading...
Searching...
No Matches
container_test.cpp
1#include <userver/multi-index-lru/container.hpp>
2#include <userver/utest/utest.hpp>
3
4#include <string>
5
6#include <boost/multi_index/hashed_index.hpp>
7#include <boost/multi_index/identity.hpp>
8#include <boost/multi_index/member.hpp>
9#include <boost/multi_index/ordered_index.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace {
14
15class LRUUsersTest : public ::testing::Test {
16protected:
17 void SetUp() override {}
18
19 struct IdTag {};
20 struct EmailTag {};
21 struct NameTag {};
22
23 struct User {
24 int id;
25 std::string email;
26 std::string name;
27
28 bool operator==(const User& other) const {
29 return id == other.id && email == other.email && name == other.name;
30 }
31 };
32
33 using UserCache = multi_index_lru::Container<
34 User,
35 boost::multi_index::indexed_by<
36 boost::multi_index::ordered_unique<
37 boost::multi_index::tag<IdTag>,
38 boost::multi_index::member<User, int, &User::id>>,
39 boost::multi_index::ordered_unique<
40 boost::multi_index::tag<EmailTag>,
41 boost::multi_index::member<User, std::string, &User::email>>,
42 boost::multi_index::ordered_non_unique<
43 boost::multi_index::tag<NameTag>,
44 boost::multi_index::member<User, std::string, &User::name>>>>;
45};
46
47UTEST_F(LRUUsersTest, EraseByKeyReportsPresence) {
48 UserCache cache(2);
49 cache.insert({.id = 1, .email = "a", .name = "A"});
50 cache.insert({.id = 2, .email = "b", .name = "B"});
51
52 EXPECT_TRUE(cache.erase<IdTag>(2));
53 EXPECT_EQ(cache.size(), 1);
54 EXPECT_FALSE(cache.erase<IdTag>(2));
55 EXPECT_TRUE(cache.erase<IdTag>(1));
56 EXPECT_TRUE(cache.empty());
57 EXPECT_FALSE(cache.erase<IdTag>(1));
58}
59
60UTEST_F(LRUUsersTest, BasicOperations) {
61 UserCache cache(3); // capacity == 3
62
63 // Test insertion
64 cache.emplace(User{.id = 1, .email = "alice@test.com", .name = "Alice"});
65 cache.emplace(User{.id = 2, .email = "bob@test.com", .name = "Bob"});
66 cache.emplace(User{.id = 3, .email = "charlie@test.com", .name = "Charlie"});
67
68 EXPECT_EQ(cache.size(), 3);
69
70 // Test find by id
71 auto by_id = cache.find<IdTag, int>(1);
72 EXPECT_NE(by_id, cache.end<IdTag>());
73 EXPECT_EQ(by_id->name, "Alice");
74
75 // Test find by email
76 auto by_email = cache.find<EmailTag, std::string>("bob@test.com");
77 EXPECT_NE(by_email, cache.end<EmailTag>());
78 EXPECT_EQ(by_email->id, 2);
79
80 // Test find by name
81 auto by_name = cache.find<NameTag, std::string>("Charlie");
82 EXPECT_NE(by_name, cache.end<NameTag>());
83 EXPECT_EQ(by_name->email, "charlie@test.com");
84
85 auto it = cache.find<EmailTag, std::string>("alice@test.com");
86 EXPECT_NE(it, cache.end<EmailTag>());
87}
88
89UTEST_F(LRUUsersTest, LRUEviction) {
90 UserCache cache(3);
91
92 cache.emplace(User{.id = 1, .email = "alice@test.com", .name = "Alice"});
93 cache.emplace(User{.id = 2, .email = "bob@test.com", .name = "Bob"});
94 cache.emplace(User{.id = 3, .email = "charlie@test.com", .name = "Charlie"});
95
96 // Access Alice and Charlie to make them recently used
97 cache.find<IdTag>(1);
98 cache.find<IdTag>(3);
99
100 // Add fourth element - Bob should be evicted
101 cache.emplace(User{.id = 4, .email = "david@test.com", .name = "David"});
102
103 EXPECT_FALSE((cache.contains<IdTag>(2))); // Bob evicted
104 EXPECT_TRUE((cache.contains<IdTag>(1))); // Alice remains
105 EXPECT_TRUE((cache.contains<IdTag>(3))); // Charlie remains
106 EXPECT_TRUE((cache.contains<IdTag>(4))); // David added
107}
108
109UTEST_F(LRUUsersTest, GetNoUpdateDoesNotChangeLru) {
110 UserCache cache(3);
111
112 cache.emplace(User{.id = 1, .email = "alice@test.com", .name = "Alice"});
113 cache.emplace(User{.id = 2, .email = "bob@test.com", .name = "Bob"});
114 cache.emplace(User{.id = 3, .email = "charlie@test.com", .name = "Charlie"});
115
116 auto it = cache.find_no_update<IdTag>(1); // without updating
117 EXPECT_NE(it, cache.end<IdTag>());
118 EXPECT_EQ(it->name, "Alice");
119
120 cache.emplace(User{.id = 4, .email = "david@test.com", .name = "David"});
121
122 EXPECT_FALSE((cache.contains<IdTag>(1))); // evicted
123 EXPECT_TRUE((cache.contains<IdTag>(2))); // remains
124 EXPECT_TRUE((cache.contains<IdTag>(3))); // remains
125 EXPECT_TRUE((cache.contains<IdTag>(4))); // added
126}
127
128UTEST_F(LRUUsersTest, EqualRangeUpdatesLruForAllMatches) {
129 UserCache cache(4);
130
131 cache.emplace(User{.id = 1, .email = "john1@test.com", .name = "John"});
132 cache.emplace(User{.id = 2, .email = "john2@test.com", .name = "John"});
133 cache.emplace(User{.id = 3, .email = "alice@test.com", .name = "Alice"});
134 cache.emplace(User{.id = 4, .email = "bob@test.com", .name = "Bob"});
135
136 auto [begin, end] = cache.equal_range<NameTag, std::string>("John");
137 int count = 0;
138 for (auto it = begin; it != end; ++it) {
139 ++count;
140 }
141 EXPECT_EQ(count, 2);
142
143 cache.emplace(User{.id = 5, .email = "eve@test.com", .name = "Eve"});
144
145 EXPECT_TRUE((cache.contains<IdTag>(1))); // remains
146 EXPECT_TRUE((cache.contains<IdTag>(2))); // remains
147 EXPECT_FALSE((cache.contains<IdTag>(3))); // evicted
148 EXPECT_TRUE((cache.contains<IdTag>(4))); // remains
149 EXPECT_TRUE((cache.contains<IdTag>(5))); // added
150}
151
152UTEST_F(LRUUsersTest, EqualRangeNoUpdateDoesNotChangeLru) {
153 UserCache cache(4);
154
155 cache.emplace(User{.id = 1, .email = "john1@test.com", .name = "John"});
156 cache.emplace(User{.id = 2, .email = "john2@test.com", .name = "John"});
157 cache.emplace(User{.id = 3, .email = "alice@test.com", .name = "Alice"});
158 cache.emplace(User{.id = 4, .email = "bob@test.com", .name = "Bob"});
159
160 auto [begin, end] = cache.equal_range_no_update<NameTag, std::string>("John");
161 int count = 0;
162 for (auto it = begin; it != end; ++it) {
163 ++count;
164 }
165 EXPECT_EQ(count, 2);
166
167 cache.emplace(User{.id = 5, .email = "eve@test.com", .name = "Eve"});
168
169 EXPECT_FALSE((cache.contains<IdTag>(1))); // evicted
170 EXPECT_TRUE((cache.contains<IdTag>(2))); // remains
171 EXPECT_TRUE((cache.contains<IdTag>(3))); // remains
172 EXPECT_TRUE((cache.contains<IdTag>(4))); // remains
173 EXPECT_TRUE((cache.contains<IdTag>(5))); // added
174}
175
176UTEST_F(LRUUsersTest, EqualRangeWorksWithEmptyRange) {
177 UserCache cache(3);
178 cache.emplace(User{.id = 1, .email = "alice@test.com", .name = "Alice"});
179
180 auto [begin, end] = cache.equal_range<NameTag, std::string>("Nonexistent");
181 EXPECT_EQ(begin, end);
182}
183
184UTEST_F(LRUUsersTest, EqualRangeNoUpdateWorksWithEmptyRange) {
185 UserCache cache(3);
186 cache.emplace(User{.id = 1, .email = "alice@test.com", .name = "Alice"});
187
188 auto [begin, end] = cache.equal_range_no_update<NameTag, std::string>("Nonexistent");
189 EXPECT_EQ(begin, end);
190}
191
192class ProductsTest : public ::testing::Test {
193protected:
194 struct SkuTag {};
195 struct NameTag {};
196
197 struct Product {
198 std::string sku;
199 std::string name;
200 double price;
201
202 bool operator==(const Product& other) const {
203 return sku == other.sku && name == other.name && price == other.price;
204 }
205 };
206
207 using ProductCache = multi_index_lru::Container<
208 Product,
209 boost::multi_index::indexed_by<
210 boost::multi_index::ordered_unique<
211 boost::multi_index::tag<SkuTag>,
212 boost::multi_index::member<Product, std::string, &Product::sku>>,
213 boost::multi_index::ordered_unique<
214 boost::multi_index::tag<NameTag>,
215 boost::multi_index::member<Product, std::string, &Product::name>>>>;
216};
217
218UTEST_F(ProductsTest, BasicProductOperations) {
219 ProductCache cache(2);
220
221 cache.emplace(Product{.sku = "A1", .name = "Laptop", .price = 999.99});
222 cache.emplace(Product{.sku = "A2", .name = "Mouse", .price = 29.99});
223
224 auto laptop = cache.find<SkuTag, std::string>("A1");
225 EXPECT_NE(laptop, cache.end<SkuTag>());
226 EXPECT_EQ(laptop->name, "Laptop");
227}
228
229UTEST_F(ProductsTest, ProductEviction) {
230 ProductCache cache(2);
231
232 cache.emplace(Product{.sku = "A1", .name = "Laptop", .price = 999.99});
233 cache.emplace(Product{.sku = "A2", .name = "Mouse", .price = 29.99});
234
235 // A1 was used, so A2 should be ousted when adding A3
236 cache.find<SkuTag>("A1");
237 cache.emplace(Product{.sku = "A3", .name = "Keyboard", .price = 79.99});
238
239 EXPECT_TRUE((cache.contains<SkuTag, std::string>("A1"))); // used
240 EXPECT_TRUE((cache.contains<SkuTag, std::string>("A3"))); // new
241 EXPECT_FALSE((cache.contains<SkuTag, std::string>("A2"))); // ousted
242
243 EXPECT_NE(cache.find<NameTag>("Keyboard"), cache.end<NameTag>());
244 EXPECT_EQ(cache.find<NameTag>("Mouse"), cache.end<NameTag>());
245}
246
247class ProductsTestWithAllocator : public ProductsTest {
248protected:
249 class Counter {
250 public:
251 static std::atomic<size_t> count;
252 static void increment() { count++; }
253 static size_t get() { return count.load(); }
254 static void reset() { count = 0; }
255 };
256
257 template <typename T>
258 class CountingAllocator : public std::allocator<T> {
259 public:
260 CountingAllocator() = default;
261 template <typename U>
262 CountingAllocator(const CountingAllocator<U>&) {}
263
264 T* allocate(size_t n) {
265 Counter::increment();
266 return std::allocator<T>::allocate(n);
267 }
268
269 static size_t get_count() { return Counter::get(); }
270 static void reset_count() { Counter::reset(); }
271
272 template <typename U>
273 struct rebind { // NOLINT(readability-identifier-naming)
274 using other = CountingAllocator<U>;
275 };
276 };
277
278 using ProductCache = multi_index_lru::Container<
279 Product,
280 boost::multi_index::indexed_by<
281 boost::multi_index::ordered_unique<
282 boost::multi_index::tag<SkuTag>,
283 boost::multi_index::member<Product, std::string, &Product::sku>>,
284 boost::multi_index::ordered_unique<
285 boost::multi_index::tag<NameTag>,
286 boost::multi_index::member<Product, std::string, &Product::name>>>,
287 CountingAllocator<Product>>;
288};
289
290std::atomic<size_t> ProductsTestWithAllocator::Counter::count{0};
291
292UTEST_F(ProductsTestWithAllocator, AllocationsCheck) {
293 ProductCache cache(20);
294
295 for (int i = 0; i < 1000; ++i) {
296 cache.insert(Product{.sku = "A" + std::to_string(i), .name = "Laptop_" + std::to_string(i), .price = 999.99});
297 }
298 const auto first_allocations_count = ProductsTestWithAllocator::CountingAllocator<int>::get_count();
299
300 cache.clear();
301 for (int i = 0; i < 1000; ++i) {
302 cache.insert(Product{.sku = "A" + std::to_string(i), .name = "Laptop_" + std::to_string(i), .price = 999.99});
303 }
304
305 // no extra allocations since nodes are being reused
306 EXPECT_EQ(first_allocations_count, ProductsTestWithAllocator::CountingAllocator<int>::get_count());
307
308 cache.insert(Product{.sku = "B_0", .name = "C_0", .price = 999.99});
309 cache.erase(cache.find<NameTag>("C_0"));
310 cache.insert(Product{.sku = "B_1", .name = "C_1", .price = 999.99});
311 cache.erase(cache.find<SkuTag>("B_1"));
312 cache.insert(Product{.sku = "B_2", .name = "C_2", .price = 999.99});
313
314 // no extra allocations since nodes are being reused
315 EXPECT_EQ(first_allocations_count, ProductsTestWithAllocator::CountingAllocator<int>::get_count());
316}
317
318UTEST(Snippet, SimpleUsage) {
319 struct MyValueT {
320 std::string key;
321 int val;
322 };
323
324 struct MyTag {};
325
326 MyValueT my_value{.key = "some_key", .val = 1};
327 /// [Usage]
328 using MyLruCache = multi_index_lru::Container<
329 MyValueT,
330 boost::multi_index::indexed_by<boost::multi_index::hashed_unique<
331 boost::multi_index::tag<MyTag>,
332 boost::multi_index::member<MyValueT, std::string, &MyValueT::key>>>>;
333
334 MyLruCache cache(1000); // Capacity of 1000 items
335 cache.insert(my_value);
336 auto it = cache.find<MyTag>("some_key");
337 EXPECT_NE(it, cache.end<MyTag>());
338 /// [Usage]
339}
340
341} // namespace
342
343USERVER_NAMESPACE_END